Panda3D
py_panda.I
Go to the documentation of this file.
1 /**
2  * @file py_panda.I
3  * @author rdb
4  * @date 2016-06-06
5  */
6 
7 #ifdef _MSC_VER
8 #define _IS_FINAL(T) (__is_sealed(T))
9 #elif defined(__GNUC__)
10 #define _IS_FINAL(T) (__is_final(T))
11 #else
12 #define _IS_FINAL(T) (0)
13 #endif
14 
15 /**
16  * Template function that can be used to extract any TypedObject pointer from
17  * a wrapped Python object.
18  */
19 template<class T> INLINE bool
20 DtoolInstance_GetPointer(PyObject *self, T *&into) {
21  if (DtoolInstance_Check(self)) {
22  Dtool_PyTypedObject *target_class = (Dtool_PyTypedObject *)get_type_handle(T).get_python_type();
23  if (target_class != nullptr) {
24  if (_IS_FINAL(T)) {
25  if (DtoolInstance_TYPE(self) == target_class) {
26  into = (T *)DtoolInstance_VOID_PTR(self);
27  } else {
28  return false;
29  }
30  } else {
31  into = (T *)DtoolInstance_UPCAST(self, *target_class);
32  }
33  return (into != nullptr);
34  }
35  }
36  into = nullptr;
37  return false;
38 }
39 
40 /**
41  * Template function that can be used to extract any TypedObject pointer from
42  * a wrapped Python object. In this case, the Dtool_PyTypedObject is known.
43  */
44 template<class T> INLINE bool
45 DtoolInstance_GetPointer(PyObject *self, T *&into, Dtool_PyTypedObject &target_class) {
46  if (DtoolInstance_Check(self)) {
47  if (_IS_FINAL(T)) {
48  if (DtoolInstance_TYPE(self) == &target_class) {
49  into = (T *)DtoolInstance_VOID_PTR(self);
50  } else {
51  return false;
52  }
53  } else {
54  into = (T *)DtoolInstance_UPCAST(self, target_class);
55  }
56  return (into != nullptr);
57  }
58  into = nullptr;
59  return false;
60 }
61 
62 /**
63  * Function to create a hash from a wrapped Python object.
64  */
65 INLINE Py_hash_t DtoolInstance_HashPointer(PyObject *self) {
66  if (self != nullptr && DtoolInstance_Check(self)) {
67  return (Py_hash_t)(intptr_t)DtoolInstance_VOID_PTR(self);
68  }
69  return -1;
70 }
71 
72 /**
73  * Python 2-style comparison function that compares objects by pointer.
74  */
75 INLINE int DtoolInstance_ComparePointers(PyObject *v1, PyObject *v2) {
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);
80  } else {
81  return (v1 > v2) - (v1 < v2);
82  }
83 }
84 
85 /**
86  * Rich comparison function that compares objects by pointer.
87  */
88 INLINE PyObject *DtoolInstance_RichComparePointers(PyObject *v1, PyObject *v2, int op) {
89  int cmpval = DtoolInstance_ComparePointers(v1, v2);
90  Py_RETURN_RICHCOMPARE(cmpval, 0, op);
91 }
92 
93 /**
94  * Converts the enum value to a C long.
95  */
96 INLINE long Dtool_EnumValue_AsLong(PyObject *value) {
97  PyObject *val = PyObject_GetAttrString(value, "value");
98  if (val != nullptr) {
99  long as_long = PyLongOrInt_AS_LONG(val);
100  Py_DECREF(val);
101  return as_long;
102  } else {
103  return -1;
104  }
105 }
106 
107 /**
108  * These functions wrap a pointer for a class that defines get_type_handle().
109  */
110 template<class T> INLINE PyObject *
111 DTool_CreatePyInstance(const T *obj, bool memory_rules) {
112  Dtool_PyTypedObject *known_class = (Dtool_PyTypedObject *)get_type_handle(T).get_python_type();
113  nassertr(known_class != nullptr, nullptr);
114  return DTool_CreatePyInstance((void*) obj, *known_class, memory_rules, true);
115 }
116 
117 template<class T> INLINE PyObject *
118 DTool_CreatePyInstance(T *obj, bool memory_rules) {
119  Dtool_PyTypedObject *known_class = (Dtool_PyTypedObject *)get_type_handle(T).get_python_type();
120  nassertr(known_class != nullptr, nullptr);
121  return DTool_CreatePyInstance((void*) obj, *known_class, memory_rules, false);
122 }
123 
124 template<class T> INLINE PyObject *
125 DTool_CreatePyInstanceTyped(const T *obj, bool memory_rules) {
126  Dtool_PyTypedObject *known_class = (Dtool_PyTypedObject *)get_type_handle(T).get_python_type();
127  nassertr(known_class != nullptr, nullptr);
128  return DTool_CreatePyInstanceTyped((void*) obj, *known_class, memory_rules, true, obj->get_type().get_index());
129 }
130 
131 template<class T> INLINE PyObject *
132 DTool_CreatePyInstanceTyped(T *obj, bool memory_rules) {
133  Dtool_PyTypedObject *known_class = (Dtool_PyTypedObject *)get_type_handle(T).get_python_type();
134  nassertr(known_class != nullptr, nullptr);
135  return DTool_CreatePyInstanceTyped((void*) obj, *known_class, memory_rules, false, obj->get_type().get_index());
136 }
137 
138 /**
139  * Finishes initializing the Dtool_PyInstDef.
140  */
141 INLINE int
142 DTool_PyInit_Finalize(PyObject *self, void *local_this, Dtool_PyTypedObject *type, bool memory_rules, bool is_const) {
143  ((Dtool_PyInstDef *)self)->_My_Type = type;
144  ((Dtool_PyInstDef *)self)->_ptr_to_object = local_this;
145  ((Dtool_PyInstDef *)self)->_memory_rules = memory_rules;
146  ((Dtool_PyInstDef *)self)->_is_const = is_const;
147  return 0;
148 }
149 
150 /**
151  * Checks that the tuple is empty.
152  */
153 ALWAYS_INLINE bool
154 Dtool_CheckNoArgs(PyObject *args) {
155  return PyTuple_GET_SIZE(args) == 0;
156 }
157 
158 /**
159  * Checks that the tuple is empty, and that the dict is empty or NULL.
160  */
161 ALWAYS_INLINE bool
162 Dtool_CheckNoArgs(PyObject *args, PyObject *kwds) {
163  return PyTuple_GET_SIZE(args) == 0 &&
164  (kwds == nullptr || PyDict_GET_SIZE(kwds) == 0);
165 }
166 
167 /**
168  * The following functions wrap an arbitrary C++ value into a PyObject.
169  */
170 ALWAYS_INLINE PyObject *Dtool_WrapValue(int value) {
171 #if PY_MAJOR_VERSION >= 3
172  return PyLong_FromLong((long)value);
173 #else
174  return PyInt_FromLong((long)value);
175 #endif
176 }
177 
178 ALWAYS_INLINE PyObject *Dtool_WrapValue(unsigned int value) {
179 #if PY_MAJOR_VERSION >= 3 && SIZEOF_INT < SIZEOF_LONG
180  return PyLong_FromLong((long)value);
181 #elif PY_MAJOR_VERSION >= 3
182  return PyLong_FromUnsignedLong((unsigned long)value);
183 #elif SIZEOF_INT < SIZEOF_LONG
184  return PyInt_FromLong((long)value);
185 #else
186  return (value > LONG_MAX)
187  ? PyLong_FromUnsignedLong((unsigned long)value)
188  : PyInt_FromLong((long)value);
189 #endif
190 }
191 
192 ALWAYS_INLINE PyObject *Dtool_WrapValue(long value) {
193 #if PY_MAJOR_VERSION >= 3
194  return PyLong_FromLong(value);
195 #else
196  return PyInt_FromLong(value);
197 #endif
198 }
199 
200 ALWAYS_INLINE PyObject *Dtool_WrapValue(unsigned long value) {
201 #if PY_MAJOR_VERSION >= 3
202  return PyLong_FromUnsignedLong(value);
203 #else
204  return (value > LONG_MAX)
205  ? PyLong_FromUnsignedLong(value)
206  : PyInt_FromLong((long)value);
207 #endif
208 }
209 
210 ALWAYS_INLINE PyObject *Dtool_WrapValue(long long value) {
211  return PyLong_FromLongLong(value);
212 }
213 
214 ALWAYS_INLINE PyObject *Dtool_WrapValue(unsigned long long value) {
215  // size_t is sometimes defined as unsigned long long, and we want to map
216  // that to int in Python 2 so it can be returned from a __len__.
217 #if PY_MAJOR_VERSION >= 3
218  return PyLong_FromUnsignedLongLong(value);
219 #else
220  return (value > LONG_MAX)
221  ? PyLong_FromUnsignedLongLong(value)
222  : PyInt_FromLong((long)value);
223 #endif
224 }
225 
226 ALWAYS_INLINE PyObject *Dtool_WrapValue(bool value) {
227  PyObject *result = (value ? Py_True : Py_False);
228  Py_INCREF(result);
229  return result;
230 }
231 
232 ALWAYS_INLINE PyObject *Dtool_WrapValue(double value) {
233  return PyFloat_FromDouble(value);
234 }
235 
236 ALWAYS_INLINE PyObject *Dtool_WrapValue(const char *value) {
237  if (value == nullptr) {
238  Py_INCREF(Py_None);
239  return Py_None;
240  } else {
241 #if PY_MAJOR_VERSION >= 3
242  return PyUnicode_FromString(value);
243 #else
244  return PyString_FromString(value);
245 #endif
246  }
247 }
248 
249 ALWAYS_INLINE PyObject *Dtool_WrapValue(const wchar_t *value) {
250  if (value == nullptr) {
251  Py_INCREF(Py_None);
252  return Py_None;
253  } else {
254  return PyUnicode_FromWideChar(value, (Py_ssize_t)wcslen(value));
255  }
256 }
257 
258 ALWAYS_INLINE PyObject *Dtool_WrapValue(const std::string &value) {
259 #if PY_MAJOR_VERSION >= 3
260  return PyUnicode_FromStringAndSize(value.data(), (Py_ssize_t)value.length());
261 #else
262  return PyString_FromStringAndSize(value.data(), (Py_ssize_t)value.length());
263 #endif
264 }
265 
266 ALWAYS_INLINE PyObject *Dtool_WrapValue(const std::wstring &value) {
267  return PyUnicode_FromWideChar(value.data(), (Py_ssize_t)value.length());
268 }
269 
270 ALWAYS_INLINE PyObject *Dtool_WrapValue(const std::string *value) {
271  if (value == nullptr) {
272  Py_INCREF(Py_None);
273  return Py_None;
274  } else {
275 #if PY_MAJOR_VERSION >= 3
276  return PyUnicode_FromStringAndSize(value->data(), (Py_ssize_t)value->length());
277 #else
278  return PyString_FromStringAndSize(value->data(), (Py_ssize_t)value->length());
279 #endif
280  }
281 }
282 
283 ALWAYS_INLINE PyObject *Dtool_WrapValue(const std::wstring *value) {
284  if (value == nullptr) {
285  Py_INCREF(Py_None);
286  return Py_None;
287  } else {
288  return PyUnicode_FromWideChar(value->data(), (Py_ssize_t)value->length());
289  }
290 }
291 
292 ALWAYS_INLINE PyObject *Dtool_WrapValue(char value) {
293 #if PY_MAJOR_VERSION >= 3
294  return PyUnicode_FromStringAndSize(&value, 1);
295 #else
296  return PyString_FromStringAndSize(&value, 1);
297 #endif
298 }
299 
300 ALWAYS_INLINE PyObject *Dtool_WrapValue(wchar_t value) {
301  return PyUnicode_FromWideChar(&value, 1);
302 }
303 
304 ALWAYS_INLINE PyObject *Dtool_WrapValue(std::nullptr_t) {
305  Py_INCREF(Py_None);
306  return Py_None;
307 }
308 
309 ALWAYS_INLINE PyObject *Dtool_WrapValue(PyObject *value) {
310  return value;
311 }
312 
313 ALWAYS_INLINE PyObject *Dtool_WrapValue(const vector_uchar &value) {
314 #if PY_MAJOR_VERSION >= 3
315  return PyBytes_FromStringAndSize((char *)value.data(), (Py_ssize_t)value.size());
316 #else
317  return PyString_FromStringAndSize((char *)value.data(), (Py_ssize_t)value.size());
318 #endif
319 }
320 
321 #if PY_MAJOR_VERSION >= 0x02060000
322 ALWAYS_INLINE PyObject *Dtool_WrapValue(Py_buffer *value) {
323  if (value == nullptr) {
324  return value;
325  } else {
326  return PyMemoryView_FromBuffer(value);
327  }
328 }
329 #endif
330 
331 template<class T1, class T2>
332 ALWAYS_INLINE PyObject *Dtool_WrapValue(const std::pair<T1, T2> &value) {
333  PyObject *tuple = PyTuple_New(2);
334  PyTuple_SET_ITEM(tuple, 0, Dtool_WrapValue(value.first));
335  PyTuple_SET_ITEM(tuple, 1, Dtool_WrapValue(value.second));
336  return tuple;
337 }
Py_hash_t DtoolInstance_HashPointer(PyObject *self)
Function to create a hash from a wrapped Python object.
Definition: py_panda.I:65
bool Dtool_CheckNoArgs(PyObject *args)
Checks that the tuple is empty.
Definition: py_panda.I:154
long Dtool_EnumValue_AsLong(PyObject *value)
Converts the enum value to a C long.
Definition: py_panda.I:96
PyObject * DtoolInstance_RichComparePointers(PyObject *v1, PyObject *v2, int op)
Rich comparison function that compares objects by pointer.
Definition: py_panda.I:88
PyObject * Dtool_WrapValue(int value)
The following functions wrap an arbitrary C++ value into a PyObject.
Definition: py_panda.I:170
PyObject * DTool_CreatePyInstance(const T *obj, bool memory_rules)
These functions wrap a pointer for a class that defines get_type_handle().
Definition: py_panda.I:111
int DtoolInstance_ComparePointers(PyObject *v1, PyObject *v2)
Python 2-style comparison function that compares objects by pointer.
Definition: py_panda.I:75
int DTool_PyInit_Finalize(PyObject *self, void *local_this, Dtool_PyTypedObject *type, bool memory_rules, bool is_const)
Finishes initializing the Dtool_PyInstDef.
Definition: py_panda.I:142
bool DtoolInstance_GetPointer(PyObject *self, T *&into)
Template function that can be used to extract any TypedObject pointer from a wrapped Python object.
Definition: py_panda.I:20