4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
30 /** \file PyObjectPlus.h
31 * \ingroup expressions
34 /* for now keep weakrefs optional */
37 #ifndef _adr_py_lib_h_ // only process once,
38 #define _adr_py_lib_h_ // even if multiply included
40 #ifndef __cplusplus // c++ only
41 #error Must be compiled with C++
44 #include "KX_Python.h"
45 #include "STR_String.h"
46 #include "MT_Vector3.h"
50 /*------------------------------
52 ------------------------------*/
57 #include "../../blender/python/mathutils/mathutils.h" /* so we can have mathutils callbacks */
58 #include "../../blender/python/generic/py_capi_utils.h" /* for PyC_LineSpit only */
62 static inline void Py_Fatal(const char *M) {
63 fprintf(stderr, "%s\n", M);
68 /* Use with ShowDeprecationWarning macro */
74 #define ShowDeprecationWarning(old_way, new_way) \
76 static WarnLink wlink = {false, NULL}; \
77 if ((m_ignore_deprecation_warnings || wlink.warn_done)==0) \
79 ShowDeprecationWarning_func(old_way, new_way); \
81 WarnLink *wlink_last= GetDeprecationWarningLinkLast(); \
82 wlink.warn_done = true; \
86 wlink_last->link= (void *)&(wlink); \
87 SetDeprecationWarningLinkLast(&(wlink)); \
89 SetDeprecationWarningFirst(&(wlink)); \
90 SetDeprecationWarningLinkLast(&(wlink)); \
97 typedef struct PyObjectPlus_Proxy {
98 PyObject_HEAD /* required python macro */
99 class PyObjectPlus *ref; // pointer to GE object, it holds a reference to this proxy
100 void *ptr; // optional pointer to generic structure, the structure holds no reference to this proxy
101 bool py_owns; // true if the object pointed by ref should be deleted when the proxy is deleted
102 bool py_ref; // true if proxy is connected to a GE object (ref is used)
104 PyObject *in_weakreflist; // weak reference enabler
106 } PyObjectPlus_Proxy;
108 #define BGE_PROXY_ERROR_MSG "Blender Game Engine data has been freed, cannot use this python variable"
109 #define BGE_PROXY_REF(_self) (((PyObjectPlus_Proxy *)_self)->ref)
110 #define BGE_PROXY_PTR(_self) (((PyObjectPlus_Proxy *)_self)->ptr)
111 #define BGE_PROXY_PYOWNS(_self) (((PyObjectPlus_Proxy *)_self)->py_owns)
112 #define BGE_PROXY_PYREF(_self) (((PyObjectPlus_Proxy *)_self)->py_ref)
114 #define BGE_PROXY_WKREF(_self) (((PyObjectPlus_Proxy *)_self)->in_weakreflist)
117 /* Note, sometimes we dont care what BGE type this is as long as its a proxy */
118 #define BGE_PROXY_CHECK_TYPE(_type) ((_type)->tp_dealloc == PyObjectPlus::py_base_dealloc)
120 /* Opposite of BGE_PROXY_REF */
121 #define BGE_PROXY_FROM_REF(_self) (((PyObjectPlus *)_self)->GetProxy())
124 // This must be the first line of each
126 // AttributesPtr correspond to attributes of proxy generic pointer
127 // each PyC++ class must be registered in KX_PythonInitTypes.cpp
128 #define __Py_Header \
130 static PyTypeObject Type; \
131 static PyMethodDef Methods[]; \
132 static PyAttributeDef Attributes[]; \
133 virtual PyTypeObject *GetType(void) {return &Type;}; \
134 virtual PyObject *GetProxy() {return GetProxyPlus_Ext(this, &Type, NULL);}; \
135 virtual PyObject *NewProxy(bool py_owns) {return NewProxyPlus_Ext(this, &Type, NULL, py_owns);}; \
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 \
142 static PyTypeObject Type; \
143 static PyMethodDef Methods[]; \
144 static PyAttributeDef Attributes[]; \
145 static PyAttributeDef AttributesPtr[]; \
146 virtual PyTypeObject *GetType(void) {return &Type;}; \
147 virtual PyObject *GetProxy(); \
148 virtual PyObject *NewProxy(bool py_owns); \
150 // leave above line empty (macro)!
151 #ifdef WITH_CXX_GUARDEDALLOC
152 #define Py_Header __Py_Header \
153 void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, Type.tp_name); } \
154 void operator delete(void *mem) { MEM_freeN(mem); } \
157 #define Py_Header __Py_Header
160 #ifdef WITH_CXX_GUARDEDALLOC
161 #define Py_HeaderPtr __Py_HeaderPtr \
162 void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, Type.tp_name); } \
163 void operator delete( void *mem ) { MEM_freeN(mem); } \
166 #define Py_HeaderPtr __Py_HeaderPtr
170 * nonzero values are an error for setattr
171 * however because of the nested lookups we need to know if the errors
172 * was because the attribute didnt exits of if there was some problem setting the value
175 #define PY_SET_ATTR_COERCE_FAIL 2
176 #define PY_SET_ATTR_FAIL 1
177 #define PY_SET_ATTR_MISSING -1
178 #define PY_SET_ATTR_SUCCESS 0
181 * These macros are helpfull when embedding Python routines. The second
182 * macro is one that also requires a documentation string
184 #define KX_PYMETHOD(class_name, method_name) \
185 PyObject* Py##method_name(PyObject* args, PyObject* kwds); \
186 static PyObject* sPy##method_name( PyObject* self, PyObject* args, PyObject* kwds) { \
187 if(BGE_PROXY_REF(self)==NULL) { PyErr_SetString(PyExc_RuntimeError, #class_name "." #method_name "() - " BGE_PROXY_ERROR_MSG); return NULL; } \
188 return ((class_name*)BGE_PROXY_REF(self))->Py##method_name(args, kwds); \
191 #define KX_PYMETHOD_VARARGS(class_name, method_name) \
192 PyObject* Py##method_name(PyObject* args); \
193 static PyObject* sPy##method_name( PyObject* self, PyObject* args) { \
194 if(BGE_PROXY_REF(self)==NULL) { PyErr_SetString(PyExc_RuntimeError, #class_name "." #method_name "() - " BGE_PROXY_ERROR_MSG); return NULL; } \
195 return ((class_name*)BGE_PROXY_REF(self))->Py##method_name(args); \
198 #define KX_PYMETHOD_NOARGS(class_name, method_name) \
199 PyObject* Py##method_name(); \
200 static PyObject* sPy##method_name( PyObject* self) { \
201 if(BGE_PROXY_REF(self)==NULL) { PyErr_SetString(PyExc_RuntimeError, #class_name "." #method_name "() - " BGE_PROXY_ERROR_MSG); return NULL; } \
202 return ((class_name*)BGE_PROXY_REF(self))->Py##method_name(); \
205 #define KX_PYMETHOD_O(class_name, method_name) \
206 PyObject* Py##method_name(PyObject* value); \
207 static PyObject* sPy##method_name( PyObject* self, PyObject* value) { \
208 if(BGE_PROXY_REF(self)==NULL) { PyErr_SetString(PyExc_RuntimeError, #class_name "." #method_name "(value) - " BGE_PROXY_ERROR_MSG); return NULL; } \
209 return ((class_name*)BGE_PROXY_REF(self))->Py##method_name(value); \
212 #define KX_PYMETHOD_DOC(class_name, method_name) \
213 PyObject* Py##method_name(PyObject* args, PyObject* kwds); \
214 static PyObject* sPy##method_name( PyObject* self, PyObject* args, PyObject* kwds) { \
215 if(BGE_PROXY_REF(self)==NULL) { PyErr_SetString(PyExc_RuntimeError, #class_name "." #method_name "(...) - " BGE_PROXY_ERROR_MSG); return NULL; } \
216 return ((class_name*)BGE_PROXY_REF(self))->Py##method_name(args, kwds); \
218 static const char method_name##_doc[]; \
220 #define KX_PYMETHOD_DOC_VARARGS(class_name, method_name) \
221 PyObject* Py##method_name(PyObject* args); \
222 static PyObject* sPy##method_name( PyObject* self, PyObject* args) { \
223 if(BGE_PROXY_REF(self)==NULL) { PyErr_SetString(PyExc_RuntimeError, #class_name "." #method_name "(...) - " BGE_PROXY_ERROR_MSG); return NULL; } \
224 return ((class_name*)BGE_PROXY_REF(self))->Py##method_name(args); \
226 static const char method_name##_doc[]; \
228 #define KX_PYMETHOD_DOC_O(class_name, method_name) \
229 PyObject* Py##method_name(PyObject* value); \
230 static PyObject* sPy##method_name( PyObject* self, PyObject* value) { \
231 if(BGE_PROXY_REF(self)==NULL) { PyErr_SetString(PyExc_RuntimeError, #class_name "." #method_name "(value) - " BGE_PROXY_ERROR_MSG); return NULL; } \
232 return ((class_name*)BGE_PROXY_REF(self))->Py##method_name(value); \
234 static const char method_name##_doc[]; \
236 #define KX_PYMETHOD_DOC_NOARGS(class_name, method_name) \
237 PyObject* Py##method_name(); \
238 static PyObject* sPy##method_name( PyObject* self) { \
239 if(BGE_PROXY_REF(self)==NULL) { PyErr_SetString(PyExc_RuntimeError, #class_name "." #method_name "() - " BGE_PROXY_ERROR_MSG); return NULL; } \
240 return ((class_name*)BGE_PROXY_REF(self))->Py##method_name(); \
242 static const char method_name##_doc[]; \
245 /* The line above should remain empty */
247 * Method table macro (with doc)
249 #define KX_PYMETHODTABLE(class_name, method_name) \
250 {#method_name , (PyCFunction) class_name::sPy##method_name, METH_VARARGS, (const char *)class_name::method_name##_doc}
252 #define KX_PYMETHODTABLE_O(class_name, method_name) \
253 {#method_name , (PyCFunction) class_name::sPy##method_name, METH_O, (const char *)class_name::method_name##_doc}
255 #define KX_PYMETHODTABLE_NOARGS(class_name, method_name) \
256 {#method_name , (PyCFunction) class_name::sPy##method_name, METH_NOARGS, (const char *)class_name::method_name##_doc}
258 #define KX_PYMETHODTABLE_KEYWORDS(class_name, method_name) \
259 {#method_name , (PyCFunction) class_name::sPy##method_name, METH_VARARGS|METH_KEYWORDS, (const char *)class_name::method_name##_doc}
262 * Function implementation macro
264 #define KX_PYMETHODDEF_DOC(class_name, method_name, doc_string) \
265 const char class_name::method_name##_doc[] = doc_string; \
266 PyObject* class_name::Py##method_name(PyObject* args, PyObject* kwds)
268 #define KX_PYMETHODDEF_DOC_VARARGS(class_name, method_name, doc_string) \
269 const char class_name::method_name##_doc[] = doc_string; \
270 PyObject* class_name::Py##method_name(PyObject* args)
272 #define KX_PYMETHODDEF_DOC_O(class_name, method_name, doc_string) \
273 const char class_name::method_name##_doc[] = doc_string; \
274 PyObject* class_name::Py##method_name(PyObject* value)
276 #define KX_PYMETHODDEF_DOC_NOARGS(class_name, method_name, doc_string) \
277 const char class_name::method_name##_doc[] = doc_string; \
278 PyObject* class_name::Py##method_name()
281 * Attribute management
283 enum KX_PYATTRIBUTE_TYPE {
284 KX_PYATTRIBUTE_TYPE_BOOL,
285 KX_PYATTRIBUTE_TYPE_ENUM,
286 KX_PYATTRIBUTE_TYPE_SHORT,
287 KX_PYATTRIBUTE_TYPE_INT,
288 KX_PYATTRIBUTE_TYPE_FLOAT,
289 KX_PYATTRIBUTE_TYPE_STRING,
290 KX_PYATTRIBUTE_TYPE_DUMMY,
291 KX_PYATTRIBUTE_TYPE_FUNCTION,
292 KX_PYATTRIBUTE_TYPE_VECTOR,
293 KX_PYATTRIBUTE_TYPE_FLAG,
294 KX_PYATTRIBUTE_TYPE_CHAR,
297 enum KX_PYATTRIBUTE_ACCESS {
302 struct KX_PYATTRIBUTE_DEF;
303 typedef int (*KX_PYATTRIBUTE_CHECK_FUNCTION)(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef);
304 typedef int (*KX_PYATTRIBUTE_SET_FUNCTION)(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
305 typedef PyObject* (*KX_PYATTRIBUTE_GET_FUNCTION)(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef);
307 typedef struct KX_PYATTRIBUTE_DEF {
308 const char *m_name; // name of the python attribute
309 KX_PYATTRIBUTE_TYPE m_type; // type of value
310 KX_PYATTRIBUTE_ACCESS m_access; // read/write access or read-only
311 int m_imin; // minimum value in case of integer attributes
312 // (for string: minimum string length, for flag: mask value, for float: matrix row size)
313 int m_imax; // maximum value in case of integer attributes
314 // (for string: maximum string length, for flag: 1 if flag is negative, float: vector/matrix col size)
315 float m_fmin; // minimum value in case of float attributes
316 float m_fmax; // maximum value in case of float attributes
317 bool m_clamp; // enforce min/max value by clamping
318 bool m_usePtr; // the attribute uses the proxy generic pointer, set at runtime
319 size_t m_offset; // position of field in structure
320 size_t m_size; // size of field for runtime verification (enum only)
321 size_t m_length; // length of array, 1=simple attribute
322 KX_PYATTRIBUTE_CHECK_FUNCTION m_checkFunction; // static function to check the assignment, returns 0 if no error
323 KX_PYATTRIBUTE_SET_FUNCTION m_setFunction; // static function to check the assignment, returns 0 if no error
324 KX_PYATTRIBUTE_GET_FUNCTION m_getFunction; // static function to check the assignment, returns 0 if no error
326 // The following pointers are just used to have compile time check for attribute type.
327 // It would have been good to use a union but that would require C99 compatibility
328 // to initialize specific union fields through designated initializers.
331 short int *m_shortPtr;
334 STR_String *m_stringPtr;
335 MT_Vector3 *m_vectorPtr;
340 #define KX_PYATTRIBUTE_BOOL_RW(name,object,field) \
341 { 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} }
342 #define KX_PYATTRIBUTE_BOOL_RW_CHECK(name,object,field,function) \
343 { 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} }
344 #define KX_PYATTRIBUTE_BOOL_RO(name,object,field) \
345 { 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} }
347 /* attribute points to a single bit of an integer field, attribute=true if bit is set */
348 #define KX_PYATTRIBUTE_FLAG_RW(name,object,field,bit) \
349 { 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} }
350 #define KX_PYATTRIBUTE_FLAG_RW_CHECK(name,object,field,bit,function) \
351 { 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} }
352 #define KX_PYATTRIBUTE_FLAG_RO(name,object,field,bit) \
353 { 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} }
355 /* attribute points to a single bit of an integer field, attribute=true if bit is set*/
356 #define KX_PYATTRIBUTE_FLAG_NEGATIVE_RW(name,object,field,bit) \
357 { 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} }
358 #define KX_PYATTRIBUTE_FLAG_NEGATIVE_RW_CHECK(name,object,field,bit,function) \
359 { 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} }
360 #define KX_PYATTRIBUTE_FLAG_NEGATIVE_RO(name,object,field,bit) \
361 { 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} }
363 // enum field cannot be mapped to pointer (because we would need a pointer for each enum)
364 // use field size to verify mapping at runtime only, assuming enum size is equal to int size.
365 #define KX_PYATTRIBUTE_ENUM_RW(name,min,max,clamp,object,field) \
366 { 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} }
367 #define KX_PYATTRIBUTE_ENUM_RW_CHECK(name,min,max,clamp,object,field,function) \
368 { 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} }
369 #define KX_PYATTRIBUTE_ENUM_RO(name,object,field) \
370 { 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} }
372 #define KX_PYATTRIBUTE_SHORT_RW(name,min,max,clamp,object,field) \
373 { 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} }
374 #define KX_PYATTRIBUTE_SHORT_RW_CHECK(name,min,max,clamp,object,field,function) \
375 { 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} }
376 #define KX_PYATTRIBUTE_SHORT_RO(name,object,field) \
377 { 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} }
378 #define KX_PYATTRIBUTE_SHORT_ARRAY_RW(name,min,max,clamp,object,field,length) \
379 { 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} }
380 #define KX_PYATTRIBUTE_SHORT_ARRAY_RW_CHECK(name,min,max,clamp,object,field,length,function) \
381 { 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} }
382 #define KX_PYATTRIBUTE_SHORT_ARRAY_RO(name,object,field,length) \
383 { 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} }
385 #define KX_PYATTRIBUTE_SHORT_LIST_RW(name,min,max,clamp,object,field,length) \
386 { 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} }
387 #define KX_PYATTRIBUTE_SHORT_LIST_RW_CHECK(name,min,max,clamp,object,field,length,function) \
388 { 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} }
389 #define KX_PYATTRIBUTE_SHORT_LIST_RO(name,object,field,length) \
390 { 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} }
392 #define KX_PYATTRIBUTE_INT_RW(name,min,max,clamp,object,field) \
393 { 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} }
394 #define KX_PYATTRIBUTE_INT_RW_CHECK(name,min,max,clamp,object,field,function) \
395 { 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} }
396 #define KX_PYATTRIBUTE_INT_RO(name,object,field) \
397 { 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} }
398 #define KX_PYATTRIBUTE_INT_ARRAY_RW(name,min,max,clamp,object,field,length) \
399 { 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} }
400 #define KX_PYATTRIBUTE_INT_ARRAY_RW_CHECK(name,min,max,clamp,object,field,length,function) \
401 { 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} }
402 #define KX_PYATTRIBUTE_INT_ARRAY_RO(name,object,field,length) \
403 { 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} }
405 #define KX_PYATTRIBUTE_INT_LIST_RW(name,min,max,clamp,object,field,length) \
406 { 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} }
407 #define KX_PYATTRIBUTE_INT_LIST_RW_CHECK(name,min,max,clamp,object,field,length,function) \
408 { 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} }
409 #define KX_PYATTRIBUTE_INT_LIST_RO(name,object,field,length) \
410 { 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} }
412 // always clamp for float
413 #define KX_PYATTRIBUTE_FLOAT_RW(name,min,max,object,field) \
414 { 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} }
415 #define KX_PYATTRIBUTE_FLOAT_RW_CHECK(name,min,max,object,field,function) \
416 { 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} }
417 #define KX_PYATTRIBUTE_FLOAT_RO(name,object,field) \
418 { 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} }
419 // field must be float[n], returns a sequence
420 #define KX_PYATTRIBUTE_FLOAT_ARRAY_RW(name,min,max,object,field,length) \
421 { 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} }
422 #define KX_PYATTRIBUTE_FLOAT_ARRAY_RW_CHECK(name,min,max,object,field,length,function) \
423 { 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} }
424 #define KX_PYATTRIBUTE_FLOAT_ARRAY_RO(name,object,field,length) \
425 { 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} }
426 // field must be float[n], returns a vector
427 #define KX_PYATTRIBUTE_FLOAT_VECTOR_RW(name,min,max,object,field,length) \
428 { 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} }
429 #define KX_PYATTRIBUTE_FLOAT_VECTOR_RW_CHECK(name,min,max,object,field,length,function) \
430 { 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} }
431 #define KX_PYATTRIBUTE_FLOAT_VECTOR_RO(name,object,field,length) \
432 { 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} }
433 // field must be float[n][n], returns a matrix
434 #define KX_PYATTRIBUTE_FLOAT_MATRIX_RW(name,min,max,object,field,length) \
435 { 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} }
436 #define KX_PYATTRIBUTE_FLOAT_MATRIX_RW_CHECK(name,min,max,object,field,length,function) \
437 { 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} }
438 #define KX_PYATTRIBUTE_FLOAT_MATRIX_RO(name,object,field,length) \
439 { 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} }
441 // only for STR_String member
442 #define KX_PYATTRIBUTE_STRING_RW(name,min,max,clamp,object,field) \
443 { 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} }
444 #define KX_PYATTRIBUTE_STRING_RW_CHECK(name,min,max,clamp,object,field,function) \
445 { 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} }
446 #define KX_PYATTRIBUTE_STRING_RO(name,object,field) \
447 { 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} }
449 // only for char [] array
450 #define KX_PYATTRIBUTE_CHAR_RW(name,object,field) \
451 { 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} }
452 #define KX_PYATTRIBUTE_CHAR_RW_CHECK(name,object,field,function) \
453 { 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} }
454 #define KX_PYATTRIBUTE_CHAR_RO(name,object,field) \
455 { 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} }
457 // for MT_Vector3 member
458 #define KX_PYATTRIBUTE_VECTOR_RW(name,min,max,object,field) \
459 { 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} }
460 #define KX_PYATTRIBUTE_VECTOR_RW_CHECK(name,min,max,clamp,object,field,function) \
461 { 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} }
462 #define KX_PYATTRIBUTE_VECTOR_RO(name,object,field) \
463 { 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} }
465 #define KX_PYATTRIBUTE_RW_FUNCTION(name,object,getfunction,setfunction) \
466 { 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} }
467 #define KX_PYATTRIBUTE_RO_FUNCTION(name,object,getfunction) \
468 { 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} }
469 #define KX_PYATTRIBUTE_ARRAY_RW_FUNCTION(name,object,length,getfunction,setfunction) \
470 { 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} }
471 #define KX_PYATTRIBUTE_ARRAY_RO_FUNCTION(name,object,length,getfunction) \
472 { 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} }
475 /*------------------------------
477 ------------------------------*/
478 typedef PyTypeObject * PyParentObject; // Define the PyParent Object
482 #ifdef WITH_CXX_GUARDEDALLOC
485 void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, "GE:PyObjectPlus"); } \
486 void operator delete( void *mem ) { MEM_freeN(mem); } \
488 #define Py_HeaderPtr \
490 void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, "GE:PyObjectPlusPtr"); } \
491 void operator delete( void *mem ) { MEM_freeN(mem); } \
493 #else // WITH_CXX_GUARDEDALLOC
498 #define Py_HeaderPtr \
501 #endif // WITH_CXX_GUARDEDALLOC
506 // By making SG_QList the ultimate parent for PyObjectPlus objects, it
507 // allows to put them in 2 different dynamic lists at the same time
508 // The use of these links is interesting because they free of memory allocation
509 // but it's very important not to mess up with them. If you decide that
510 // the SG_QList or SG_DList component is used for something for a certain class,
511 // they cannot can be used for anything else at a parent level!
512 // What these lists are and what they are used for must be carefully documented
513 // at the level where they are used.
514 // DON'T MAKE ANY USE OF THESE LIST AT THIS LEVEL, they are already used
515 // at SCA_IActuator, SCA_ISensor, SCA_IController level which rules out the
516 // possibility to use them at SCA_ILogicBrick, CValue and PyObjectPlus level.
517 class PyObjectPlus : public SG_QList
518 { // The PyObjectPlus abstract class
519 Py_Header; // Always start with Py_Header
524 virtual ~PyObjectPlus(); // destructor
527 PyObject *m_proxy; /* actually a PyObjectPlus_Proxy */
529 /* These static functions are referenced by ALL PyObjectPlus_Proxy types
530 * they take the C++ reference from the PyObjectPlus_Proxy and call
531 * its own virtual py_repr, py_base_dealloc ,etc. functions.
534 static PyObject* py_base_new(PyTypeObject *type, PyObject *args, PyObject *kwds); /* allows subclassing */
535 static void py_base_dealloc(PyObject *self);
536 static PyObject* py_base_repr(PyObject *self);
538 /* These are all virtual python methods that are defined in each class
539 * Our own fake subclassing calls these on each class, then calls the parent */
540 virtual PyObject* py_repr(void);
541 /* subclass may overwrite this function to implement more sophisticated method of validating a proxy */
542 virtual bool py_is_valid(void) { return true; }
544 static PyObject* py_get_attrdef(PyObject *self_py, const PyAttributeDef *attrdef);
545 static int py_set_attrdef(PyObject *self_py, PyObject *value, const PyAttributeDef *attrdef);
547 /* Kindof dumb, always returns True, the false case is checked for, before this function gets accessed */
548 static PyObject* pyattr_get_invalid(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
550 static PyObject *GetProxyPlus_Ext(PyObjectPlus *self, PyTypeObject *tp, void *ptr);
551 /* self=NULL => proxy to generic pointer detached from GE object
552 if py_owns is true, the memory pointed by ptr will be deleted automatially with MEM_freeN
553 self!=NULL=> proxy attached to GE object, ptr is optional and point to a struct from which attributes can be defined
554 if py_owns is true, the object will be deleted automatically, ptr will NOT be deleted
555 (assume object destructor takes care of it) */
556 static PyObject *NewProxyPlus_Ext(PyObjectPlus *self, PyTypeObject *tp, void *ptr, bool py_owns);
558 static WarnLink* GetDeprecationWarningLinkFirst(void);
559 static WarnLink* GetDeprecationWarningLinkLast(void);
560 static void SetDeprecationWarningFirst(WarnLink* wlink);
561 static void SetDeprecationWarningLinkLast(WarnLink* wlink);
562 static void NullDeprecationWarning();
564 /** enable/disable display of deprecation warnings */
565 static void SetDeprecationWarnings(bool ignoreDeprecationWarnings);
566 /** Shows a deprecation warning */
567 static void ShowDeprecationWarning_func(const char* method,const char* prop);
568 static void ClearDeprecationWarning();
572 void InvalidateProxy();
575 * Makes sure any internal data owned by this class is deep copied.
577 virtual void ProcessReplica();
579 static bool m_ignore_deprecation_warnings;
583 PyObject *py_getattr_dict(PyObject *pydict, PyObject *tp_dict);
586 #endif // _adr_py_lib_h_