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