8#define _IS_FINAL(T) (__is_sealed(T))
10#define _IS_FINAL(T) (__is_final(T))
12#define _IS_FINAL(T) (0)
19template<
class T> INLINE
bool
21 if (DtoolInstance_Check(self)) {
22 Dtool_PyTypedObject *target_class = (Dtool_PyTypedObject *)get_type_handle(T).get_python_type();
23 if (target_class !=
nullptr) {
25 if (DtoolInstance_TYPE(self) == target_class) {
26 into = (T *)DtoolInstance_VOID_PTR(self);
31 into = (T *)DtoolInstance_UPCAST(self, *target_class);
33 return (into !=
nullptr);
44template<
class T> INLINE
bool
46 if (DtoolInstance_Check(self)) {
48 if (DtoolInstance_TYPE(self) == &target_class) {
49 into = (T *)DtoolInstance_VOID_PTR(self);
54 into = (T *)DtoolInstance_UPCAST(self, target_class);
56 return (into !=
nullptr);
66 if (self !=
nullptr && DtoolInstance_Check(self)) {
67 return (Py_hash_t)(intptr_t)DtoolInstance_VOID_PTR(self);
76 void *v1_this = DtoolInstance_Check(v1) ? DtoolInstance_VOID_PTR(v1) :
nullptr;
77 void *v2_this = DtoolInstance_Check(v2) ? DtoolInstance_VOID_PTR(v2) :
nullptr;
78 if (v1_this !=
nullptr && v2_this !=
nullptr) {
79 return (v1_this > v2_this) - (v1_this < v2_this);
81 return (v1 > v2) - (v1 < v2);
90 Py_RETURN_RICHCOMPARE(cmpval, 0, op);
98 PyObject *prev_value = ptr;
99 if (prev_value != value) {
102 Py_XDECREF(prev_value);
110 PyObject *val = PyObject_GetAttrString(value,
"value");
111 if (val !=
nullptr) {
112 long as_long = PyLongOrInt_AS_LONG(val);
123template<
class T> INLINE PyObject *
125 Dtool_PyTypedObject *known_class = (Dtool_PyTypedObject *)get_type_handle(T).get_python_type();
126 nassertr(known_class !=
nullptr,
nullptr);
130template<
class T> INLINE PyObject *
132 Dtool_PyTypedObject *known_class = (Dtool_PyTypedObject *)get_type_handle(T).get_python_type();
133 nassertr(known_class !=
nullptr,
nullptr);
137template<
class T> INLINE PyObject *
138DTool_CreatePyInstanceTyped(
const T *obj,
bool memory_rules) {
139 Dtool_PyTypedObject *known_class = (Dtool_PyTypedObject *)get_type_handle(T).get_python_type();
140 nassertr(known_class !=
nullptr,
nullptr);
141 return DTool_CreatePyInstanceTyped((
void*) obj, *known_class, memory_rules,
true, obj->get_type().get_index());
144template<
class T> INLINE PyObject *
145DTool_CreatePyInstanceTyped(T *obj,
bool memory_rules) {
146 Dtool_PyTypedObject *known_class = (Dtool_PyTypedObject *)get_type_handle(T).get_python_type();
147 nassertr(known_class !=
nullptr,
nullptr);
148 return DTool_CreatePyInstanceTyped((
void*) obj, *known_class, memory_rules,
false, obj->get_type().get_index());
155DTool_PyInit_Finalize(PyObject *self,
void *local_this, Dtool_PyTypedObject *type,
bool memory_rules,
bool is_const) {
156 ((Dtool_PyInstDef *)self)->_My_Type = type;
157 ((Dtool_PyInstDef *)self)->_ptr_to_object = local_this;
158 ((Dtool_PyInstDef *)self)->_memory_rules = memory_rules;
159 ((Dtool_PyInstDef *)self)->_is_const = is_const;
168 return PyTuple_GET_SIZE(args) == 0;
176 return PyTuple_GET_SIZE(args) == 0 &&
177 (kwds ==
nullptr || PyDict_GET_SIZE(kwds) == 0);
184#if PY_MAJOR_VERSION >= 3
185 return PyLong_FromLong((
long)value);
187 return PyInt_FromLong((
long)value);
192#if PY_MAJOR_VERSION >= 3 && SIZEOF_INT < SIZEOF_LONG
193 return PyLong_FromLong((
long)value);
194#elif PY_MAJOR_VERSION >= 3
195 return PyLong_FromUnsignedLong((
unsigned long)value);
196#elif SIZEOF_INT < SIZEOF_LONG
197 return PyInt_FromLong((
long)value);
199 return (value > LONG_MAX)
200 ? PyLong_FromUnsignedLong((
unsigned long)value)
201 : PyInt_FromLong((long)value);
206#if PY_MAJOR_VERSION >= 3
207 return PyLong_FromLong(value);
209 return PyInt_FromLong(value);
214#if PY_MAJOR_VERSION >= 3
215 return PyLong_FromUnsignedLong(value);
217 return (value > LONG_MAX)
218 ? PyLong_FromUnsignedLong(value)
219 : PyInt_FromLong((long)value);
224 return PyLong_FromLongLong(value);
230#if PY_MAJOR_VERSION >= 3
231 return PyLong_FromUnsignedLongLong(value);
233 return (value > LONG_MAX)
234 ? PyLong_FromUnsignedLongLong(value)
235 : PyInt_FromLong((long)value);
240 PyObject *result = (value ? Py_True : Py_False);
246 return PyFloat_FromDouble(value);
250 if (value ==
nullptr) {
254#if PY_MAJOR_VERSION >= 3
255 return PyUnicode_FromString(value);
257 return PyString_FromString(value);
263 if (value ==
nullptr) {
267 return PyUnicode_FromWideChar(value, (Py_ssize_t)wcslen(value));
272#if PY_MAJOR_VERSION >= 3
273 return PyUnicode_FromStringAndSize(value.data(), (Py_ssize_t)value.length());
275 return PyString_FromStringAndSize(value.data(), (Py_ssize_t)value.length());
280 return PyUnicode_FromWideChar(value.data(), (Py_ssize_t)value.length());
284 if (value ==
nullptr) {
288#if PY_MAJOR_VERSION >= 3
289 return PyUnicode_FromStringAndSize(value->data(), (Py_ssize_t)value->length());
291 return PyString_FromStringAndSize(value->data(), (Py_ssize_t)value->length());
297 if (value ==
nullptr) {
301 return PyUnicode_FromWideChar(value->data(), (Py_ssize_t)value->length());
306#if PY_MAJOR_VERSION >= 3
307 return PyUnicode_FromStringAndSize(&value, 1);
309 return PyString_FromStringAndSize(&value, 1);
314 return PyUnicode_FromWideChar(&value, 1);
327#if PY_MAJOR_VERSION >= 3
328 return PyBytes_FromStringAndSize((
char *)value.data(), (Py_ssize_t)value.size());
330 return PyString_FromStringAndSize((
char *)value.data(), (Py_ssize_t)value.size());
334#if PY_MAJOR_VERSION >= 0x02060000
336 if (value ==
nullptr) {
339 return PyMemoryView_FromBuffer(value);
344template<
class T1,
class T2>
345ALWAYS_INLINE PyObject *
Dtool_WrapValue(
const std::pair<T1, T2> &value) {
346 PyObject *tuple = PyTuple_New(2);
bool Dtool_CheckNoArgs(PyObject *args)
Checks that the tuple is empty.
int DtoolInstance_ComparePointers(PyObject *v1, PyObject *v2)
Python 2-style comparison function that compares objects by pointer.
PyObject * Dtool_WrapValue(int value)
The following functions wrap an arbitrary C++ value into a PyObject.
int DTool_PyInit_Finalize(PyObject *self, void *local_this, Dtool_PyTypedObject *type, bool memory_rules, bool is_const)
Finishes initializing the Dtool_PyInstDef.
long Dtool_EnumValue_AsLong(PyObject *value)
Converts the enum value to a C long.
void Dtool_Assign_PyObject(PyObject *&ptr, PyObject *value)
Utility function for assigning a PyObject pointer while managing refcounts.
PyObject * DTool_CreatePyInstance(const T *obj, bool memory_rules)
These functions wrap a pointer for a class that defines get_type_handle().
PyObject * DtoolInstance_RichComparePointers(PyObject *v1, PyObject *v2, int op)
Rich comparison function that compares objects by pointer.
bool DtoolInstance_GetPointer(PyObject *self, T *&into)
Template function that can be used to extract any TypedObject pointer from a wrapped Python object.
Py_hash_t DtoolInstance_HashPointer(PyObject *self)
Function to create a hash from a wrapped Python object.