504d79f7cbbdeaece8e3ba90f54646af1c78dc3b
[blender.git] / source / gameengine / Expressions / EXP_PyObjectPlus.h
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file EXP_PyObjectPlus.h
29  *  \ingroup expressions
30  */
31
32 #ifndef __EXP_PYOBJECTPLUS_H__
33 #define __EXP_PYOBJECTPLUS_H__
34
35 /* for now keep weakrefs optional */
36 #define USE_WEAKREFS
37
38
39 #ifndef __cplusplus                             // c++ only
40 #error Must be compiled with C++
41 #endif
42
43 #include "EXP_Python.h"
44 #include "STR_String.h"
45 #include "MT_Vector3.h"
46 #include "SG_QList.h"
47 #include <cstddef>
48 #include <cstdint>
49
50 #ifdef WITH_PYTHON
51 #ifdef USE_MATHUTILS
52 extern "C" {
53 #include "../../blender/python/mathutils/mathutils.h" /* so we can have mathutils callbacks */
54 #include "../../blender/python/generic/py_capi_utils.h" /* for PyC_LineSpit only */
55 }
56 #endif
57
58 #define MAX_PROP_NAME 64
59
60 /* Use with ShowDeprecationWarning macro */
61 typedef struct {
62         bool warn_done;
63         void *link;
64 } WarnLink;
65
66 #define ShowDeprecationWarning(old_way, new_way)                              \
67 {                                                                             \
68         static WarnLink wlink = {false, NULL};                                    \
69         if ((PyObjectPlus::m_ignore_deprecation_warnings || wlink.warn_done)==0)  \
70         {                                                                         \
71                 PyObjectPlus::ShowDeprecationWarning_func(old_way, new_way);          \
72                                                                                       \
73                 WarnLink *wlink_last= PyObjectPlus::GetDeprecationWarningLinkLast();  \
74                 wlink.warn_done = true;                                               \
75                 wlink.link = NULL;                                                    \
76                                                                                       \
77                 if (wlink_last) {                                                     \
78                         wlink_last->link= (void *)&(wlink);                               \
79                         PyObjectPlus::SetDeprecationWarningLinkLast(&(wlink));            \
80                 }                                                                     \
81                 else {                                                                \
82                         PyObjectPlus::SetDeprecationWarningFirst(&(wlink));               \
83                         PyObjectPlus::SetDeprecationWarningLinkLast(&(wlink));            \
84                 }                                                                     \
85         }                                                                         \
86 }                                                                             \
87
88
89
90 typedef struct PyObjectPlus_Proxy {
91         PyObject_HEAD           /* required python macro   */
92         class PyObjectPlus *ref;        // pointer to GE object, it holds a reference to this proxy
93         void *ptr;                                      // optional pointer to generic structure, the structure holds no reference to this proxy
94         bool py_owns;           // true if the object pointed by ref should be deleted when the proxy is deleted
95         bool py_ref;            // true if proxy is connected to a GE object (ref is used)
96 #ifdef USE_WEAKREFS
97         PyObject *in_weakreflist; // weak reference enabler
98 #endif
99 } PyObjectPlus_Proxy;
100
101 #define BGE_PROXY_ERROR_MSG "Blender Game Engine data has been freed, cannot use this python variable"
102 #define BGE_PROXY_REF(_self) (((PyObjectPlus_Proxy *)_self)->ref)
103 #define BGE_PROXY_PTR(_self) (((PyObjectPlus_Proxy *)_self)->ptr)
104 #define BGE_PROXY_PYOWNS(_self) (((PyObjectPlus_Proxy *)_self)->py_owns)
105 #define BGE_PROXY_PYREF(_self) (((PyObjectPlus_Proxy *)_self)->py_ref)
106 #ifdef USE_WEAKREFS
107 #  define BGE_PROXY_WKREF(_self) (((PyObjectPlus_Proxy *)_self)->in_weakreflist)
108 #endif
109
110 /* Note, sometimes we don't care what BGE type this is as long as its a proxy */
111 #define BGE_PROXY_CHECK_TYPE(_type) ((_type)->tp_dealloc == PyObjectPlus::py_base_dealloc)
112
113 /* Opposite of BGE_PROXY_REF */
114 #define BGE_PROXY_FROM_REF(_self) (((PyObjectPlus *)_self)->GetProxy())
115 /* Same as 'BGE_PROXY_REF' but doesn't incref. */
116 #define BGE_PROXY_FROM_REF_BORROW(_self) _bge_proxy_from_ref_borrow((void *)_self)
117
118
119 // This must be the first line of each 
120 // PyC++ class
121 // AttributesPtr correspond to attributes of proxy generic pointer 
122 // each PyC++ class must be registered in KX_PythonInitTypes.cpp
123 #define __Py_Header                                                           \
124 public:                                                                       \
125         static PyTypeObject   Type;                                               \
126         static PyMethodDef    Methods[];                                          \
127         static PyAttributeDef Attributes[];                                       \
128         virtual PyTypeObject *GetType(void) {                                     \
129                 return &Type;                                                         \
130         }                                                                         \
131         virtual PyObject *GetProxy() {                                            \
132                 return GetProxyPlus_Ext(this, &Type, NULL);                           \
133         }                                                                         \
134         virtual PyObject *NewProxy(bool py_owns) {                                \
135                 return NewProxyPlus_Ext(this, &Type, NULL, py_owns);                  \
136         }                                                                         \
137
138 // leave above line empty (macro)!
139 // use this macro for class that use generic pointer in proxy
140 // GetProxy() and NewProxy() must be defined to set the correct pointer in the proxy
141 #define __Py_HeaderPtr                                                        \
142 public:                                                                       \
143         static PyTypeObject   Type;                                               \
144         static PyMethodDef    Methods[];                                          \
145         static PyAttributeDef Attributes[];                                       \
146         static PyAttributeDef AttributesPtr[];                                    \
147         virtual PyTypeObject *GetType(void) {                                     \
148                 return &Type;                                                         \
149         }                                                                         \
150         virtual PyObject *GetProxy();                                             \
151         virtual PyObject *NewProxy(bool py_owns);                                 \
152
153 // leave above line empty (macro)!
154 #ifdef WITH_CXX_GUARDEDALLOC
155 #define Py_Header __Py_Header                                                 \
156         void *operator new(size_t num_bytes) {                                    \
157                 return MEM_mallocN(num_bytes, Type.tp_name);                          \
158         }                                                                         \
159         void operator delete(void *mem) {                                         \
160                 MEM_freeN(mem);                                                       \
161         }                                                                         \
162
163 #else
164 #  define Py_Header __Py_Header
165 #endif
166
167 #ifdef WITH_CXX_GUARDEDALLOC
168 #define Py_HeaderPtr __Py_HeaderPtr                                           \
169         void *operator new(size_t num_bytes) {                                    \
170                 return MEM_mallocN(num_bytes, Type.tp_name);                          \
171         }                                                                         \
172         void operator delete(void *mem) {                                         \
173                 MEM_freeN(mem);                                                       \
174         }                                                                         \
175
176 #else
177 #  define Py_HeaderPtr __Py_HeaderPtr
178 #endif
179
180 /*
181  * nonzero values are an error for setattr
182  * however because of the nested lookups we need to know if the errors
183  * was because the attribute didnt exits of if there was some problem setting the value
184  */
185
186 #define PY_SET_ATTR_COERCE_FAIL  2
187 #define PY_SET_ATTR_FAIL                 1
188 #define PY_SET_ATTR_MISSING             -1
189 #define PY_SET_ATTR_SUCCESS              0
190
191 /**
192  * These macros are helpful when embedding Python routines. The second
193  * macro is one that also requires a documentation string
194  */
195 #define KX_PYMETHOD(class_name, method_name)                                   \
196         PyObject *Py##method_name(PyObject *args, PyObject *kwds);                 \
197         static PyObject *                                                          \
198         sPy##method_name(PyObject *self, PyObject *args, PyObject *kwds) {         \
199                 if (BGE_PROXY_REF(self)==NULL) {                                       \
200                         PyErr_SetString(PyExc_RuntimeError,                                \
201                                         #class_name "." #method_name "() - "               \
202                                         BGE_PROXY_ERROR_MSG);                              \
203                         return NULL;                                                       \
204                 }                                                                      \
205                 return((class_name*)BGE_PROXY_REF(self))->Py##method_name(args, kwds); \
206         }                                                                          \
207
208 #define KX_PYMETHOD_VARARGS(class_name, method_name)                           \
209         PyObject *Py##method_name(PyObject *args);                                 \
210         static PyObject*                                                           \
211         sPy##method_name(PyObject *self, PyObject *args) {                         \
212                 if (BGE_PROXY_REF(self)==NULL) {                                       \
213                         PyErr_SetString(PyExc_RuntimeError,                                \
214                                         #class_name "." #method_name "() - "               \
215                                         BGE_PROXY_ERROR_MSG); return NULL;                 \
216                 }                                                                      \
217                 return((class_name*)BGE_PROXY_REF(self))->Py##method_name(args);       \
218         }                                                                          \
219
220 #define KX_PYMETHOD_NOARGS(class_name, method_name)                            \
221         PyObject *Py##method_name();                                               \
222         static PyObject*                                                           \
223         sPy##method_name(PyObject *self) {                                         \
224                 if (BGE_PROXY_REF(self)==NULL) {                                       \
225                         PyErr_SetString(PyExc_RuntimeError,                                \
226                                         #class_name "." #method_name "() - "               \
227                                         BGE_PROXY_ERROR_MSG); return NULL;                 \
228                 }                                                                      \
229                 return((class_name*)BGE_PROXY_REF(self))->Py##method_name();           \
230         }                                                                          \
231
232 #define KX_PYMETHOD_O(class_name, method_name)                                 \
233         PyObject *Py##method_name(PyObject *value);                                \
234         static PyObject*                                                           \
235         sPy##method_name(PyObject *self, PyObject *value) {                        \
236                 if (BGE_PROXY_REF(self)==NULL) {                                       \
237                         PyErr_SetString(PyExc_RuntimeError,                                \
238                                         #class_name "." #method_name "(value) - "          \
239                                         BGE_PROXY_ERROR_MSG); return NULL;                 \
240                 }                                                                      \
241                 return((class_name*)BGE_PROXY_REF(self))->Py##method_name(value);      \
242         }                                                                          \
243
244 #define KX_PYMETHOD_DOC(class_name, method_name)                               \
245         PyObject *Py##method_name(PyObject *args, PyObject *kwds);                 \
246         static PyObject*                                                           \
247         sPy##method_name(PyObject *self, PyObject *args, PyObject *kwds) {         \
248                 if (BGE_PROXY_REF(self)==NULL) {                                       \
249                         PyErr_SetString(PyExc_RuntimeError,                                \
250                                         #class_name "." #method_name "(...) - "            \
251                                         BGE_PROXY_ERROR_MSG); return NULL;                 \
252                 }                                                                      \
253                 return((class_name*)BGE_PROXY_REF(self))->Py##method_name(args, kwds); \
254         }                                                                          \
255         static const char method_name##_doc[];                                     \
256
257 #define KX_PYMETHOD_DOC_VARARGS(class_name, method_name)                       \
258         PyObject *Py##method_name(PyObject *args);                                 \
259         static PyObject*                                                           \
260         sPy##method_name(PyObject *self, PyObject *args) {                         \
261                 if (BGE_PROXY_REF(self)==NULL) {                                       \
262                         PyErr_SetString(PyExc_RuntimeError,                                \
263                                         #class_name "." #method_name "(...) - "            \
264                                         BGE_PROXY_ERROR_MSG);                              \
265                         return NULL;                                                       \
266                 }                                                                      \
267                 return((class_name*)BGE_PROXY_REF(self))->Py##method_name(args);       \
268         }                                                                          \
269         static const char method_name##_doc[];                                     \
270
271 #define KX_PYMETHOD_DOC_O(class_name, method_name)                             \
272         PyObject *Py##method_name(PyObject *value);                                \
273         static PyObject *                                                          \
274         sPy##method_name(PyObject *self, PyObject *value) {                        \
275                 if (BGE_PROXY_REF(self)==NULL) {                                       \
276                         PyErr_SetString(PyExc_RuntimeError,                                \
277                                         #class_name "." #method_name "(value) - "          \
278                                         BGE_PROXY_ERROR_MSG);                              \
279                         return NULL;                                                       \
280                 }                                                                      \
281                 return ((class_name*)BGE_PROXY_REF(self))->Py##method_name(value);     \
282         }                                                                          \
283         static const char method_name##_doc[];                                     \
284
285 #define KX_PYMETHOD_DOC_NOARGS(class_name, method_name)                        \
286         PyObject *Py##method_name();                                               \
287         static PyObject *                                                          \
288         sPy##method_name(PyObject *self) {                                         \
289                 if (BGE_PROXY_REF(self)==NULL) {                                       \
290                         PyErr_SetString(PyExc_RuntimeError,                                \
291                                         #class_name "." #method_name "() - "               \
292                                         BGE_PROXY_ERROR_MSG);                              \
293                         return NULL;                                                       \
294                 }                                                                      \
295                 return ((class_name*)BGE_PROXY_REF(self))->Py##method_name();          \
296         }                                                                          \
297         static const char method_name##_doc[];                                     \
298
299
300 /* The line above should remain empty */
301 /**
302  * Method table macro (with doc)
303  */
304 #define KX_PYMETHODTABLE(class_name, method_name) \
305         {#method_name, (PyCFunction) class_name::sPy##method_name, METH_VARARGS, (const char *)class_name::method_name##_doc}
306
307 #define KX_PYMETHODTABLE_O(class_name, method_name) \
308         {#method_name, (PyCFunction) class_name::sPy##method_name, METH_O, (const char *)class_name::method_name##_doc}
309
310 #define KX_PYMETHODTABLE_NOARGS(class_name, method_name) \
311         {#method_name, (PyCFunction) class_name::sPy##method_name, METH_NOARGS, (const char *)class_name::method_name##_doc}
312
313 #define KX_PYMETHODTABLE_KEYWORDS(class_name, method_name) \
314         {#method_name, (PyCFunction) class_name::sPy##method_name, METH_VARARGS|METH_KEYWORDS, (const char *)class_name::method_name##_doc}
315
316 /**
317  * Function implementation macro
318  */
319 #define KX_PYMETHODDEF_DOC(class_name, method_name, doc_string) \
320 const char class_name::method_name##_doc[] = doc_string; \
321 PyObject *class_name::Py##method_name(PyObject *args, PyObject *kwds)
322
323 #define KX_PYMETHODDEF_DOC_VARARGS(class_name, method_name, doc_string) \
324 const char class_name::method_name##_doc[] = doc_string; \
325 PyObject *class_name::Py##method_name(PyObject *args)
326
327 #define KX_PYMETHODDEF_DOC_O(class_name, method_name, doc_string) \
328 const char class_name::method_name##_doc[] = doc_string; \
329 PyObject *class_name::Py##method_name(PyObject *value)
330
331 #define KX_PYMETHODDEF_DOC_NOARGS(class_name, method_name, doc_string) \
332 const char class_name::method_name##_doc[] = doc_string; \
333 PyObject *class_name::Py##method_name()
334
335 /**
336  * Attribute management
337  */
338 enum KX_PYATTRIBUTE_TYPE {
339         KX_PYATTRIBUTE_TYPE_BOOL,
340         KX_PYATTRIBUTE_TYPE_ENUM,
341         KX_PYATTRIBUTE_TYPE_SHORT,
342         KX_PYATTRIBUTE_TYPE_INT,
343         KX_PYATTRIBUTE_TYPE_FLOAT,
344         KX_PYATTRIBUTE_TYPE_STRING,
345         KX_PYATTRIBUTE_TYPE_DUMMY,
346         KX_PYATTRIBUTE_TYPE_FUNCTION,
347         KX_PYATTRIBUTE_TYPE_VECTOR,
348         KX_PYATTRIBUTE_TYPE_FLAG,
349         KX_PYATTRIBUTE_TYPE_CHAR
350 };
351
352 enum KX_PYATTRIBUTE_ACCESS {
353         KX_PYATTRIBUTE_RW,
354         KX_PYATTRIBUTE_RO
355 };
356
357 struct KX_PYATTRIBUTE_DEF;
358 typedef int (*KX_PYATTRIBUTE_CHECK_FUNCTION)(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef);
359 typedef int (*KX_PYATTRIBUTE_SET_FUNCTION)(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
360 typedef PyObject *(*KX_PYATTRIBUTE_GET_FUNCTION)(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef);
361
362 typedef struct KX_PYATTRIBUTE_DEF {
363         const char *m_name;                             // name of the python attribute
364         KX_PYATTRIBUTE_TYPE m_type;             // type of value
365         KX_PYATTRIBUTE_ACCESS m_access; // read/write access or read-only
366         int m_imin;                                             // minimum value in case of integer attributes 
367                                                                         // (for string: minimum string length, for flag: mask value, for float: matrix row size)
368         int m_imax;                                             // maximum value in case of integer attributes 
369                                                                         // (for string: maximum string length, for flag: 1 if flag is negative, float: vector/matrix col size)
370         float m_fmin;                                   // minimum value in case of float attributes
371         float m_fmax;                                   // maximum value in case of float attributes
372         bool   m_clamp;                                 // enforce min/max value by clamping
373         bool   m_usePtr;                                // the attribute uses the proxy generic pointer, set at runtime
374         size_t m_offset;                                // position of field in structure
375         size_t m_size;                                  // size of field for runtime verification (enum only)
376         size_t m_length;                                // length of array, 1=simple attribute
377         KX_PYATTRIBUTE_CHECK_FUNCTION m_checkFunction;  // static function to check the assignment, returns 0 if no error
378         KX_PYATTRIBUTE_SET_FUNCTION m_setFunction;      // static function to check the assignment, returns 0 if no error
379         KX_PYATTRIBUTE_GET_FUNCTION m_getFunction;      // static function to check the assignment, returns 0 if no error
380
381         // The following pointers are just used to have compile time check for attribute type.
382         // It would have been good to use a union but that would require C99 compatibility
383         // to initialize specific union fields through designated initializers.
384         struct {
385                 bool *m_boolPtr;
386                 short int *m_shortPtr;
387                 int *m_intPtr;
388                 float *m_floatPtr;
389                 STR_String *m_stringPtr;
390                 MT_Vector3 *m_vectorPtr;
391                 char *m_charPtr;
392         } m_typeCheck;
393 } PyAttributeDef;
394
395 #define KX_PYATTRIBUTE_BOOL_RW(name, object, field) \
396         { name, KX_PYATTRIBUTE_TYPE_BOOL, KX_PYATTRIBUTE_RW, 0, 1, 0.f, 0.f, false, false, offsetof(object, field), 0, 1, NULL, NULL, NULL, {&((object *)0)->field, NULL, NULL, NULL, NULL, NULL, NULL} }
397 #define KX_PYATTRIBUTE_BOOL_RW_CHECK(name, object, field, function) \
398         { name, KX_PYATTRIBUTE_TYPE_BOOL, KX_PYATTRIBUTE_RW, 0, 1, 0.f, 0.f, false, false, offsetof(object, field), 0, 1, &object::function, NULL, NULL, {&((object *)0)->field, NULL, NULL, NULL, NULL, NULL, NULL} }
399 #define KX_PYATTRIBUTE_BOOL_RO(name, object, field) \
400         { name, KX_PYATTRIBUTE_TYPE_BOOL, KX_PYATTRIBUTE_RO, 0, 1, 0.f, 0.f, false, false, offsetof(object, field), 0, 1, NULL, NULL, NULL, {&((object *)0)->field, NULL, NULL, NULL, NULL, NULL, NULL} }
401
402 /* attribute points to a single bit of an integer field, attribute=true if bit is set */
403 #define KX_PYATTRIBUTE_FLAG_RW(name, object, field, bit) \
404         { name, KX_PYATTRIBUTE_TYPE_FLAG, KX_PYATTRIBUTE_RW, bit, 0, 0.f, 0.f, false, false, offsetof(object, field), sizeof(((object *)0)->field), 1, NULL, NULL, NULL, {NULL, NULL, NULL, NULL, NULL, NULL, NULL} }
405 #define KX_PYATTRIBUTE_FLAG_RW_CHECK(name, object, field, bit, function) \
406         { name, KX_PYATTRIBUTE_TYPE_FLAG, KX_PYATTRIBUTE_RW, bit, 0, 0.f, 0.f, false, false, offsetof(object, field), sizeof(((object *)0)->field), 1, &object::function, NULL, NULL, {NULL, NULL, NULL, NULL, NULL, NULL, NULL} }
407 #define KX_PYATTRIBUTE_FLAG_RO(name, object, field, bit) \
408         { name, KX_PYATTRIBUTE_TYPE_FLAG, KX_PYATTRIBUTE_RO, bit, 0, 0.f, 0.f, false, false, offsetof(object, field), sizeof(((object *)0)->field), 1, NULL, NULL, NULL, {NULL, NULL, NULL, NULL, NULL, NULL, NULL} }
409
410 /* attribute points to a single bit of an integer field, attribute=true if bit is set*/
411 #define KX_PYATTRIBUTE_FLAG_NEGATIVE_RW(name, object, field, bit) \
412         { name, KX_PYATTRIBUTE_TYPE_FLAG, KX_PYATTRIBUTE_RW, bit, 1, 0.f, 0.f, false, false, offsetof(object, field), sizeof(((object *)0)->field), 1, NULL, NULL, NULL, {NULL, NULL, NULL, NULL, NULL, NULL, NULL} }
413 #define KX_PYATTRIBUTE_FLAG_NEGATIVE_RW_CHECK(name, object, field, bit, function) \
414         { name, KX_PYATTRIBUTE_TYPE_FLAG, KX_PYATTRIBUTE_RW, bit, 1, 0.f, 0.f, false, false, offsetof(object, field), sizeof(((object *)0)->field), 1, &object::function, NULL, NULL, {NULL, NULL, NULL, NULL, NULL, NULL, NULL} }
415 #define KX_PYATTRIBUTE_FLAG_NEGATIVE_RO(name, object, field, bit) \
416         { name, KX_PYATTRIBUTE_TYPE_FLAG, KX_PYATTRIBUTE_RO, bit, 1, 0.f, 0.f, false, false, offsetof(object, field), sizeof(((object *)0)->field), 1, NULL, NULL, NULL, {NULL, NULL, NULL, NULL, NULL, NULL, NULL} }
417
418 // enum field cannot be mapped to pointer (because we would need a pointer for each enum)
419 // use field size to verify mapping at runtime only, assuming enum size is equal to int size.
420 #define KX_PYATTRIBUTE_ENUM_RW(name, min, max, clamp, object, field) \
421         { name, KX_PYATTRIBUTE_TYPE_ENUM, KX_PYATTRIBUTE_RW, min, max, 0.f, 0.f, clamp, false, offsetof(object, field), sizeof(((object *)0)->field), 1, NULL, NULL, NULL, {NULL, NULL, NULL, NULL, NULL, NULL, NULL} }
422 #define KX_PYATTRIBUTE_ENUM_RW_CHECK(name, min, max, clamp, object, field, function) \
423         { name, KX_PYATTRIBUTE_TYPE_ENUM, KX_PYATTRIBUTE_RW, min, max, 0.f, 0.f, clamp, false, offsetof(object, field), sizeof(((object *)0)->field), 1, &object::function, NULL, NULL, {NULL, NULL, NULL, NULL, NULL, NULL, NULL} }
424 #define KX_PYATTRIBUTE_ENUM_RO(name, object, field) \
425         { name, KX_PYATTRIBUTE_TYPE_ENUM, KX_PYATTRIBUTE_RO, 0, 0, 0.f, 0.f, false, false, offsetof(object, field), sizeof(((object *)0)->field), 1, NULL, NULL, NULL, {NULL, NULL, NULL, NULL, NULL, NULL, NULL} }
426
427 #define KX_PYATTRIBUTE_SHORT_RW(name, min, max, clamp, object, field) \
428         { name, KX_PYATTRIBUTE_TYPE_SHORT, KX_PYATTRIBUTE_RW, min, max, 0.f, 0.f, clamp, false, offsetof(object, field), 0, 1, NULL, NULL, NULL, {NULL, &((object *)0)->field, NULL, NULL, NULL, NULL, NULL} }
429 #define KX_PYATTRIBUTE_SHORT_RW_CHECK(name, min, max, clamp, object, field, function) \
430         { name, KX_PYATTRIBUTE_TYPE_SHORT, KX_PYATTRIBUTE_RW, min, max, 0.f, 0.f, clamp, false, offsetof(object, field), 0, 1, &object::function, NULL, NULL, {NULL, &((object *)0)->field, NULL, NULL, NULL, NULL, NULL} }
431 #define KX_PYATTRIBUTE_SHORT_RO(name, object, field) \
432         { name, KX_PYATTRIBUTE_TYPE_SHORT, KX_PYATTRIBUTE_RO, 0, 0, 0.f, 0.f, false, false, offsetof(object, field), 0, 1, NULL, NULL, NULL, {NULL, &((object *)0)->field, NULL, NULL, NULL, NULL, NULL} }
433 #define KX_PYATTRIBUTE_SHORT_ARRAY_RW(name, min, max, clamp, object, field, length) \
434         { name, KX_PYATTRIBUTE_TYPE_SHORT, KX_PYATTRIBUTE_RW, min, max, 0.f, 0.f, clamp, false, offsetof(object, field), 0, length, NULL, NULL, NULL, {NULL, ((object *)0)->field, NULL, NULL, NULL, NULL, NULL} }
435 #define KX_PYATTRIBUTE_SHORT_ARRAY_RW_CHECK(name, min, max, clamp, object, field, length, function) \
436         { name, KX_PYATTRIBUTE_TYPE_SHORT, KX_PYATTRIBUTE_RW, min, max, 0.f, 0.f, clamp, false, offsetof(object, field), 0, length, &object::function, NULL, NULL, {NULL, ((object *)0)->field, NULL, NULL, NULL, NULL, NULL} }
437 #define KX_PYATTRIBUTE_SHORT_ARRAY_RO(name, object, field, length) \
438         { name, KX_PYATTRIBUTE_TYPE_SHORT, KX_PYATTRIBUTE_RO, 0, 0, 0.f, 0.f, false, false, offsetof(object, field), 0, length, NULL, NULL, NULL, {NULL, ((object *)0)->field, NULL, NULL, NULL, NULL, NULL} }
439 // SHORT_LIST
440 #define KX_PYATTRIBUTE_SHORT_LIST_RW(name, min, max, clamp, object, field, length) \
441         { name, KX_PYATTRIBUTE_TYPE_SHORT, KX_PYATTRIBUTE_RW, min, max, 0.f, 0.f, clamp, false, offsetof(object, field), 0, length, NULL, NULL, NULL, {NULL, &((object *)0)->field, NULL, NULL, NULL, NULL, NULL} }
442 #define KX_PYATTRIBUTE_SHORT_LIST_RW_CHECK(name, min, max, clamp, object, field, length, function) \
443         { name, KX_PYATTRIBUTE_TYPE_SHORT, KX_PYATTRIBUTE_RW, min, max, 0.f, 0.f, clamp, false, offsetof(object, field), 0, length, &object::function, NULL, NULL, {NULL, &((object *)0)->field, NULL, NULL, NULL, NULL, NULL} }
444 #define KX_PYATTRIBUTE_SHORT_LIST_RO(name, object, field, length) \
445         { name, KX_PYATTRIBUTE_TYPE_SHORT, KX_PYATTRIBUTE_RO, 0, 0, 0.f, 0.f, false, false, offsetof(object, field), 0, length, NULL, NULL, NULL, {NULL, &((object *)0)->field, NULL, NULL, NULL, NULL, NULL} }
446
447 #define KX_PYATTRIBUTE_INT_RW(name, min, max, clamp, object, field) \
448         { name, KX_PYATTRIBUTE_TYPE_INT, KX_PYATTRIBUTE_RW, min, max, 0.f, 0.f, clamp, false, offsetof(object, field), 0, 1, NULL, NULL, NULL, {NULL, NULL, &((object *)0)->field, NULL, NULL, NULL, NULL} }
449 #define KX_PYATTRIBUTE_INT_RW_CHECK(name, min, max, clamp, object, field, function) \
450         { name, KX_PYATTRIBUTE_TYPE_INT, KX_PYATTRIBUTE_RW, min, max, 0.f, 0.f, clamp, false, offsetof(object, field), 0, 1, &object::function, NULL, NULL, {NULL, NULL, &((object *)0)->field, NULL, NULL, NULL, NULL} }
451 #define KX_PYATTRIBUTE_INT_RO(name, object, field) \
452         { name, KX_PYATTRIBUTE_TYPE_INT, KX_PYATTRIBUTE_RO, 0, 0, 0.f, 0.f, false, false, offsetof(object, field), 0, 1, NULL, NULL, NULL, {NULL, NULL, &((object *)0)->field, NULL, NULL, NULL, NULL} }
453 #define KX_PYATTRIBUTE_INT_ARRAY_RW(name, min, max, clamp, object, field, length) \
454         { name, KX_PYATTRIBUTE_TYPE_INT, KX_PYATTRIBUTE_RW, min, max, 0.f, 0.f, clamp, false, offsetof(object, field), 0, length, NULL, NULL, NULL, {NULL, NULL, ((object *)0)->field, NULL, NULL, NULL, NULL} }
455 #define KX_PYATTRIBUTE_INT_ARRAY_RW_CHECK(name, min, max, clamp, object, field, length, function) \
456         { name, KX_PYATTRIBUTE_TYPE_INT, KX_PYATTRIBUTE_RW, min, max, 0.f, 0.f, clamp, false, offsetof(object, field), 0, length, &object::function, NULL, NULL, {NULL, NULL, ((object *)0)->field, NULL, NULL, NULL, NULL} }
457 #define KX_PYATTRIBUTE_INT_ARRAY_RO(name, object, field, length) \
458         { name, KX_PYATTRIBUTE_TYPE_INT, KX_PYATTRIBUTE_RO, 0, 0, 0.f, 0.f, false, false, offsetof(object, field), 0, length, NULL, NULL, NULL, {NULL, NULL, ((object *)0)->field, NULL, NULL, NULL, NULL} }
459 // INT_LIST
460 #define KX_PYATTRIBUTE_INT_LIST_RW(name, min, max, clamp, object, field, length) \
461         { name, KX_PYATTRIBUTE_TYPE_INT, KX_PYATTRIBUTE_RW, min, max, 0.f, 0.f, clamp, false, offsetof(object, field), 0, length, NULL, NULL, NULL, {NULL, NULL, &((object *)0)->field, NULL, NULL, NULL, NULL} }
462 #define KX_PYATTRIBUTE_INT_LIST_RW_CHECK(name, min, max, clamp, object, field, length, function) \
463         { name, KX_PYATTRIBUTE_TYPE_INT, KX_PYATTRIBUTE_RW, min, max, 0.f, 0.f, clamp, false, offsetof(object, field), 0, length, &object::function, NULL, NULL, {NULL, NULL, &((object *)0)->field, NULL, NULL, NULL, NULL} }
464 #define KX_PYATTRIBUTE_INT_LIST_RO(name, object, field, length) \
465         { name, KX_PYATTRIBUTE_TYPE_INT, KX_PYATTRIBUTE_RO, 0, 0, 0.f, 0.f, false, false, offsetof(object, field), 0, length, NULL, NULL, NULL, {NULL, NULL, &((object *)0)->field, NULL, NULL, NULL, NULL} }
466
467 // always clamp for float
468 #define KX_PYATTRIBUTE_FLOAT_RW(name, min, max, object, field) \
469         { name, KX_PYATTRIBUTE_TYPE_FLOAT, KX_PYATTRIBUTE_RW, 0, 0, min, max, true, false, offsetof(object, field), 0, 1, NULL, NULL, NULL, {NULL, NULL, NULL, &((object *)0)->field, NULL, NULL, NULL} }
470 #define KX_PYATTRIBUTE_FLOAT_RW_CHECK(name, min, max, object, field, function) \
471         { name, KX_PYATTRIBUTE_TYPE_FLOAT, KX_PYATTRIBUTE_RW, 0, 0, min, max, true, false, offsetof(object, field), 0, 1, &object::function, NULL, NULL, {NULL, NULL, NULL, &((object *)0)->field, NULL, NULL, NULL} }
472 #define KX_PYATTRIBUTE_FLOAT_RO(name, object, field) \
473         { name, KX_PYATTRIBUTE_TYPE_FLOAT, KX_PYATTRIBUTE_RO, 0, 0, 0.f, 0.f, false, false, offsetof(object, field), 0, 1, NULL, NULL, NULL, {NULL, NULL, NULL, &((object *)0)->field, NULL, NULL, NULL} }
474 // field must be float[n], returns a sequence
475 #define KX_PYATTRIBUTE_FLOAT_ARRAY_RW(name, min, max, object, field, length) \
476         { name, KX_PYATTRIBUTE_TYPE_FLOAT, KX_PYATTRIBUTE_RW, 0, 0, min, max, true, false, offsetof(object, field), 0, length, NULL, NULL, NULL, {NULL, NULL, NULL, ((object *)0)->field, NULL, NULL, NULL} }
477 #define KX_PYATTRIBUTE_FLOAT_ARRAY_RW_CHECK(name, min, max, object, field, length, function) \
478         { name, KX_PYATTRIBUTE_TYPE_FLOAT, KX_PYATTRIBUTE_RW, 0, 0, min, max, true, false, offsetof(object, field), 0, length, &object::function, NULL, NULL, {NULL, NULL, NULL, ((object *)0)->field, NULL, NULL, NULL} }
479 #define KX_PYATTRIBUTE_FLOAT_ARRAY_RO(name, object, field, length) \
480         { name, KX_PYATTRIBUTE_TYPE_FLOAT, KX_PYATTRIBUTE_RO, 0, 0, 0.f, 0.f, false, false, offsetof(object, field), 0, length, NULL, NULL, NULL, {NULL, NULL, NULL, ((object *)0)->field, NULL, NULL, NULL} }
481 // field must be float[n], returns a vector
482 #define KX_PYATTRIBUTE_FLOAT_VECTOR_RW(name, min, max, object, field, length) \
483         { name, KX_PYATTRIBUTE_TYPE_FLOAT, KX_PYATTRIBUTE_RW, 0, length, min, max, true, false, offsetof(object, field), sizeof(((object *)0)->field), 1, NULL, NULL, NULL, {NULL, NULL, NULL, ((object *)0)->field, NULL, NULL, NULL} }
484 #define KX_PYATTRIBUTE_FLOAT_VECTOR_RW_CHECK(name, min, max, object, field, length, function) \
485         { name, KX_PYATTRIBUTE_TYPE_FLOAT, KX_PYATTRIBUTE_RW, 0, length, min, max, true, false, offsetof(object, field), sizeof(((object *)0)->field), 1, &object::function, NULL, NULL, {NULL, NULL, NULL, ((object *)0)->field, NULL, NULL, NULL} }
486 #define KX_PYATTRIBUTE_FLOAT_VECTOR_RO(name, object, field, length) \
487         { name, KX_PYATTRIBUTE_TYPE_FLOAT, KX_PYATTRIBUTE_RO, 0, length, 0.f, 0.f, false, false, offsetof(object, field), sizeof(((object *)0)->field), 1, NULL, NULL, NULL, {NULL, NULL, NULL, ((object *)0)->field, NULL, NULL, NULL} }
488 // field must be float[n][n], returns a matrix
489 #define KX_PYATTRIBUTE_FLOAT_MATRIX_RW(name, min, max, object, field, length) \
490         { name, KX_PYATTRIBUTE_TYPE_FLOAT, KX_PYATTRIBUTE_RW, length, length, min, max, true, false, offsetof(object, field), sizeof(((object *)0)->field), 1, NULL, NULL, NULL, {NULL, NULL, NULL, ((object *)0)->field[0], NULL, NULL, NULL} }
491 #define KX_PYATTRIBUTE_FLOAT_MATRIX_RW_CHECK(name, min, max, object, field, length, function) \
492         { name, KX_PYATTRIBUTE_TYPE_FLOAT, KX_PYATTRIBUTE_RW, length, length, min, max, true, false, offsetof(object, field), sizeof(((object *)0)->field), 1, &object::function, NULL, NULL, {NULL, NULL, NULL, ((object *)0)->field[0], NULL, NULL, NULL} }
493 #define KX_PYATTRIBUTE_FLOAT_MATRIX_RO(name, object, field, length) \
494         { name, KX_PYATTRIBUTE_TYPE_FLOAT, KX_PYATTRIBUTE_RO, length, length, 0.f, 0.f, false, false, offsetof(object, field), sizeof(((object *)0)->field), 1, NULL, NULL, NULL, {NULL, NULL, NULL, ((object *)0)->field[0], NULL, NULL, NULL} }
495
496 // only for STR_String member
497 #define KX_PYATTRIBUTE_STRING_RW(name, min, max, clamp, object, field) \
498         { name, KX_PYATTRIBUTE_TYPE_STRING, KX_PYATTRIBUTE_RW, min, max, 0.f, 0.f, clamp, false, offsetof(object, field), 0, 1, NULL, NULL, NULL, {NULL, NULL, NULL, NULL, &((object *)0)->field, NULL, NULL} }
499 #define KX_PYATTRIBUTE_STRING_RW_CHECK(name, min, max, clamp, object, field, function) \
500         { name, KX_PYATTRIBUTE_TYPE_STRING, KX_PYATTRIBUTE_RW, min, max, 0.f, 0.f, clamp, false, offsetof(object, field), 0, 1, &object::function, NULL, NULL, {NULL, NULL, NULL, NULL, &((object *)0)->field, NULL, NULL} }
501 #define KX_PYATTRIBUTE_STRING_RO(name, object, field) \
502         { name, KX_PYATTRIBUTE_TYPE_STRING, KX_PYATTRIBUTE_RO, 0, 0, 0.f, 0.f, false, false, offsetof(object, field), 0, 1 , NULL, NULL, NULL, {NULL, NULL, NULL, NULL, &((object *)0)->field, NULL, NULL} }
503
504 // only for char [] array 
505 #define KX_PYATTRIBUTE_CHAR_RW(name, object, field) \
506         { name, KX_PYATTRIBUTE_TYPE_CHAR, KX_PYATTRIBUTE_RW, 0, 0, 0.f, 0.f, true, false, offsetof(object, field), sizeof(((object *)0)->field), 1, NULL, NULL, NULL, {NULL, NULL, NULL, NULL, NULL, NULL, ((object *)0)->field} }
507 #define KX_PYATTRIBUTE_CHAR_RW_CHECK(name, object, field, function) \
508         { name, KX_PYATTRIBUTE_TYPE_CHAR, KX_PYATTRIBUTE_RW, 0, 0, 0.f, 0.f, true, false, offsetof(object, field), sizeof(((object *)0)->field), 1, &object::function, NULL, NULL, {NULL, NULL, NULL, NULL, NULL, NULL, ((object *)0)->field} }
509 #define KX_PYATTRIBUTE_CHAR_RO(name, object, field) \
510         { name, KX_PYATTRIBUTE_TYPE_CHAR, KX_PYATTRIBUTE_RO, 0, 0, 0.f, 0.f, false, false, offsetof(object, field), sizeof(((object *)0)->field), 1 , NULL, NULL, NULL, {NULL, NULL, NULL, NULL, NULL, NULL, ((object *)0)->field} }
511
512 // for MT_Vector3 member
513 #define KX_PYATTRIBUTE_VECTOR_RW(name, min, max, object, field) \
514         { name, KX_PYATTRIBUTE_TYPE_VECTOR, KX_PYATTRIBUTE_RW, 0, 0, min, max, true, false, offsetof(object, field), 0, 1, NULL, NULL, NULL, {NULL, NULL, NULL, NULL, NULL, &((object *)0)->field, NULL} }
515 #define KX_PYATTRIBUTE_VECTOR_RW_CHECK(name, min, max, clamp, object, field, function) \
516         { name, KX_PYATTRIBUTE_TYPE_VECTOR, KX_PYATTRIBUTE_RW, 0, 0, min, max, true, false, offsetof(object, field), 0, 1, &object::function, NULL, NULL, {NULL, NULL, NULL, NULL, NULL, &((object *)0)->field, NULL} }
517 #define KX_PYATTRIBUTE_VECTOR_RO(name, object, field) \
518         { name, KX_PYATTRIBUTE_TYPE_VECTOR, KX_PYATTRIBUTE_RO, 0, 0, 0.f, 0.f, false, false, offsetof(object, field), 0, 1 , NULL, NULL, NULL, {NULL, NULL, NULL, NULL, NULL, &((object *)0)->field, NULL} }
519
520 #define KX_PYATTRIBUTE_RW_FUNCTION(name, object, getfunction, setfunction) \
521         { name, KX_PYATTRIBUTE_TYPE_FUNCTION, KX_PYATTRIBUTE_RW, 0, 0, 0.f, 0.f, false, false, 0, 0, 1, NULL, &object::setfunction, &object::getfunction, {NULL, NULL, NULL, NULL, NULL, NULL, NULL} }
522 #define KX_PYATTRIBUTE_RO_FUNCTION(name, object, getfunction) \
523         { name, KX_PYATTRIBUTE_TYPE_FUNCTION, KX_PYATTRIBUTE_RO, 0, 0, 0.f, 0.f, false, false, 0, 0, 1, NULL, NULL, &object::getfunction, {NULL, NULL, NULL, NULL, NULL, NULL, NULL} }
524 #define KX_PYATTRIBUTE_ARRAY_RW_FUNCTION(name, object, length, getfunction, setfunction) \
525         { name, KX_PYATTRIBUTE_TYPE_FUNCTION, KX_PYATTRIBUTE_RW, 0, 0, 0.f, 0, f, false, false, 0, 0, length, NULL, &object::setfunction, &object::getfunction, {NULL, NULL, NULL, NULL, NULL, NULL, NULL} }
526 #define KX_PYATTRIBUTE_ARRAY_RO_FUNCTION(name, object, length, getfunction) \
527         { name, KX_PYATTRIBUTE_TYPE_FUNCTION, KX_PYATTRIBUTE_RO, 0, 0, 0.f, 0, f, false, false, 0, 0, length, NULL, NULL, &object::getfunction, {NULL, NULL, NULL, NULL, NULL, NULL, NULL} }
528
529
530 /*------------------------------
531  * PyObjectPlus
532  *------------------------------ */
533 typedef PyTypeObject *PyParentObject;  /* Define the PyParent Object */
534
535 #else // WITH_PYTHON
536
537 #ifdef WITH_CXX_GUARDEDALLOC
538 #define Py_Header                                                             \
539 public:                                                                       \
540         MEM_CXX_CLASS_ALLOC_FUNCS("GE:PyObjectPlus")                              \
541
542
543 #define Py_HeaderPtr                                                          \
544         MEM_CXX_CLASS_ALLOC_FUNCS("GE:PyObjectPlusPtr")                           \
545
546 #else // WITH_CXX_GUARDEDALLOC
547
548 #define Py_Header \
549 public: \
550
551 #define Py_HeaderPtr \
552 public: \
553
554 #endif  /* WITH_CXX_GUARDEDALLOC */
555
556 #endif
557
558
559 // By making SG_QList the ultimate parent for PyObjectPlus objects, it
560 // allows to put them in 2 different dynamic lists at the same time
561 // The use of these links is interesting because they free of memory allocation
562 // but it's very important not to mess up with them. If you decide that 
563 // the SG_QList or SG_DList component is used for something for a certain class,
564 // they cannot can be used for anything else at a parent level!
565 // What these lists are and what they are used for must be carefully documented
566 // at the level where they are used.
567 // DON'T MAKE ANY USE OF THESE LIST AT THIS LEVEL, they are already used
568 // at SCA_IActuator, SCA_ISensor, SCA_IController level which rules out the
569 // possibility to use them at SCA_ILogicBrick, CValue and PyObjectPlus level.
570 class PyObjectPlus : public SG_QList
571 {                               // The PyObjectPlus abstract class
572         Py_Header                                                       // Always start with Py_Header
573         
574 public:
575         PyObjectPlus();
576         
577         virtual ~PyObjectPlus();                                        // destructor
578         
579 #ifdef WITH_PYTHON
580         PyObject *m_proxy; /* actually a PyObjectPlus_Proxy */
581
582         /* These static functions are referenced by ALL PyObjectPlus_Proxy types
583          * they take the C++ reference from the PyObjectPlus_Proxy and call
584          * its own virtual py_repr, py_base_dealloc, etc. functions.
585          */
586
587         static PyObject*                py_base_new(PyTypeObject *type, PyObject *args, PyObject *kwds); /* allows subclassing */
588         static void                     py_base_dealloc(PyObject *self);
589         static PyObject*                py_base_repr(PyObject *self);
590
591         /* These are all virtual python methods that are defined in each class
592          * Our own fake subclassing calls these on each class, then calls the parent */
593         virtual PyObject*               py_repr(void);
594         /* subclass may overwrite this function to implement more sophisticated method of validating a proxy */
595         virtual bool                    py_is_valid(void) { return true; }
596
597         static PyObject*                py_get_attrdef(PyObject *self_py, const PyAttributeDef *attrdef);
598         static int                              py_set_attrdef(PyObject *self_py, PyObject *value, const PyAttributeDef *attrdef);
599         
600         /* Kindof dumb, always returns True, the false case is checked for, before this function gets accessed */
601         static PyObject*        pyattr_get_invalid(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
602
603         static PyObject *GetProxyPlus_Ext(PyObjectPlus *self, PyTypeObject *tp, void *ptr);
604         /* self=NULL => proxy to generic pointer detached from GE object
605          *              if py_owns is true, the memory pointed by ptr will be deleted automatically with MEM_freeN
606          * self!=NULL=> proxy attached to GE object, ptr is optional and point to a struct from which attributes can be defined
607          *              if py_owns is true, the object will be deleted automatically, ptr will NOT be deleted
608          *              (assume object destructor takes care of it) */
609         static PyObject *NewProxyPlus_Ext(PyObjectPlus *self, PyTypeObject *tp, void *ptr, bool py_owns);
610
611         static  WarnLink*               GetDeprecationWarningLinkFirst(void);
612         static  WarnLink*               GetDeprecationWarningLinkLast(void);
613         static  void                    SetDeprecationWarningFirst(WarnLink* wlink);
614         static  void                    SetDeprecationWarningLinkLast(WarnLink* wlink);
615         static void                     NullDeprecationWarning();
616         
617         /** enable/disable display of deprecation warnings */
618         static void                     SetDeprecationWarnings(bool ignoreDeprecationWarnings);
619         /** Shows a deprecation warning */
620         static void                     ShowDeprecationWarning_func(const char *method, const char *prop);
621         static void                     ClearDeprecationWarning();
622         
623 #endif
624
625         void    InvalidateProxy();
626
627         /**
628          * Makes sure any internal data owned by this class is deep copied.
629          */
630         virtual void ProcessReplica();
631
632         static bool                     m_ignore_deprecation_warnings;
633 };
634
635 #ifdef WITH_PYTHON
636 PyObject *PyUnicode_From_STR_String(const STR_String& str);
637
638 inline PyObject *_bge_proxy_from_ref_borrow(void *self_v)
639 {
640         PyObject *self_proxy = BGE_PROXY_FROM_REF(self_v);
641         /* this is typically _very_ bad practice,
642          * however we know the proxy is owned by 'self_v' */
643         self_proxy->ob_refcnt--;
644         return self_proxy;
645 }
646
647 #endif
648
649 #endif  /* __EXP_PYOBJECTPLUS_H__ */