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 #ifndef _adr_py_lib_h_ // only process once,
31 #define _adr_py_lib_h_ // even if multiply included
33 #ifndef __cplusplus // c++ only
34 #error Must be compiled with C++
37 #include "KX_Python.h"
38 #include "STR_String.h"
39 #include "MT_Vector3.h"
42 /*------------------------------
44 ------------------------------*/
49 #include "../../blender/python/generic/mathutils.h" /* so we can have mathutils callbacks */
53 static inline void Py_Fatal(const char *M) {
54 fprintf(stderr, "%s\n", M);
59 /* Use with ShowDeprecationWarning macro */
65 #define ShowDeprecationWarning(old_way, new_way) \
67 static WarnLink wlink = {false, NULL}; \
68 if ((m_ignore_deprecation_warnings || wlink.warn_done)==0) \
70 ShowDeprecationWarning_func(old_way, new_way); \
72 WarnLink *wlink_last= GetDeprecationWarningLinkLast(); \
73 wlink.warn_done = true; \
77 wlink_last->link= (void *)&(wlink); \
78 SetDeprecationWarningLinkLast(&(wlink)); \
80 SetDeprecationWarningFirst(&(wlink)); \
81 SetDeprecationWarningLinkLast(&(wlink)); \
88 typedef struct PyObjectPlus_Proxy {
89 PyObject_HEAD /* required python macro */
90 class PyObjectPlus *ref; // pointer to GE object, it holds a reference to this proxy
91 void *ptr; // optional pointer to generic structure, the structure holds no reference to this proxy
92 bool py_owns; // true if the object pointed by ref should be deleted when the proxy is deleted
93 bool py_ref; // true if proxy is connected to a GE object (ref is used)
96 #define BGE_PROXY_ERROR_MSG "Blender Game Engine data has been freed, cannot use this python variable"
97 #define BGE_PROXY_REF(_self) (((PyObjectPlus_Proxy *)_self)->ref)
98 #define BGE_PROXY_PTR(_self) (((PyObjectPlus_Proxy *)_self)->ptr)
99 #define BGE_PROXY_PYOWNS(_self) (((PyObjectPlus_Proxy *)_self)->py_owns)
100 #define BGE_PROXY_PYREF(_self) (((PyObjectPlus_Proxy *)_self)->py_ref)
102 /* Note, sometimes we dont care what BGE type this is as long as its a proxy */
103 #define BGE_PROXY_CHECK_TYPE(_type) ((_type)->tp_dealloc == PyObjectPlus::py_base_dealloc)
105 /* Opposite of BGE_PROXY_REF */
106 #define BGE_PROXY_FROM_REF(_self) (((PyObjectPlus *)_self)->GetProxy())
109 // This must be the first line of each
111 // AttributesPtr correspond to attributes of proxy generic pointer
112 // each PyC++ class must be registered in KX_PythonInitTypes.cpp
113 #define __Py_Header \
115 static PyTypeObject Type; \
116 static PyMethodDef Methods[]; \
117 static PyAttributeDef Attributes[]; \
118 virtual PyTypeObject *GetType(void) {return &Type;}; \
119 virtual PyObject *GetProxy() {return GetProxyPlus_Ext(this, &Type, NULL);}; \
120 virtual PyObject *NewProxy(bool py_owns) {return NewProxyPlus_Ext(this, &Type, NULL, py_owns);}; \
122 // leave above line empty (macro)!
123 // use this macro for class that use generic pointer in proxy
124 // GetProxy() and NewProxy() must be defined to set the correct pointer in the proxy
125 #define __Py_HeaderPtr \
127 static PyTypeObject Type; \
128 static PyMethodDef Methods[]; \
129 static PyAttributeDef Attributes[]; \
130 static PyAttributeDef AttributesPtr[]; \
131 virtual PyTypeObject *GetType(void) {return &Type;}; \
132 virtual PyObject *GetProxy(); \
133 virtual PyObject *NewProxy(bool py_owns); \
135 // leave above line empty (macro)!
136 #ifdef WITH_CXX_GUARDEDALLOC
137 #define Py_Header __Py_Header \
138 void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, Type.tp_name); } \
139 void operator delete(void *mem) { MEM_freeN(mem); } \
142 #define Py_Header __Py_Header
145 #ifdef WITH_CXX_GUARDEDALLOC
146 #define Py_HeaderPtr __Py_HeaderPtr \
147 void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, Type.tp_name); } \
148 void operator delete( void *mem ) { MEM_freeN(mem); } \
151 #define Py_HeaderPtr __Py_HeaderPtr
155 * nonzero values are an error for setattr
156 * however because of the nested lookups we need to know if the errors
157 * was because the attribute didnt exits of if there was some problem setting the value
160 #define PY_SET_ATTR_COERCE_FAIL 2
161 #define PY_SET_ATTR_FAIL 1
162 #define PY_SET_ATTR_MISSING -1
163 #define PY_SET_ATTR_SUCCESS 0
166 * These macros are helpfull when embedding Python routines. The second
167 * macro is one that also requires a documentation string
169 #define KX_PYMETHOD(class_name, method_name) \
170 PyObject* Py##method_name(PyObject* args, PyObject* kwds); \
171 static PyObject* sPy##method_name( PyObject* self, PyObject* args, PyObject* kwds) { \
172 if(BGE_PROXY_REF(self)==NULL) { PyErr_SetString(PyExc_RuntimeError, #class_name "." #method_name "() - " BGE_PROXY_ERROR_MSG); return NULL; } \
173 return ((class_name*)BGE_PROXY_REF(self))->Py##method_name(args, kwds); \
176 #define KX_PYMETHOD_VARARGS(class_name, method_name) \
177 PyObject* Py##method_name(PyObject* args); \
178 static PyObject* sPy##method_name( PyObject* self, PyObject* args) { \
179 if(BGE_PROXY_REF(self)==NULL) { PyErr_SetString(PyExc_RuntimeError, #class_name "." #method_name "() - " BGE_PROXY_ERROR_MSG); return NULL; } \
180 return ((class_name*)BGE_PROXY_REF(self))->Py##method_name(args); \
183 #define KX_PYMETHOD_NOARGS(class_name, method_name) \
184 PyObject* Py##method_name(); \
185 static PyObject* sPy##method_name( PyObject* self) { \
186 if(BGE_PROXY_REF(self)==NULL) { PyErr_SetString(PyExc_RuntimeError, #class_name "." #method_name "() - " BGE_PROXY_ERROR_MSG); return NULL; } \
187 return ((class_name*)BGE_PROXY_REF(self))->Py##method_name(); \
190 #define KX_PYMETHOD_O(class_name, method_name) \
191 PyObject* Py##method_name(PyObject* value); \
192 static PyObject* sPy##method_name( PyObject* self, PyObject* value) { \
193 if(BGE_PROXY_REF(self)==NULL) { PyErr_SetString(PyExc_RuntimeError, #class_name "." #method_name "(value) - " BGE_PROXY_ERROR_MSG); return NULL; } \
194 return ((class_name*)BGE_PROXY_REF(self))->Py##method_name(value); \
197 #define KX_PYMETHOD_DOC(class_name, method_name) \
198 PyObject* Py##method_name(PyObject* args, PyObject* kwds); \
199 static PyObject* sPy##method_name( PyObject* self, PyObject* args, PyObject* kwds) { \
200 if(BGE_PROXY_REF(self)==NULL) { PyErr_SetString(PyExc_RuntimeError, #class_name "." #method_name "(...) - " BGE_PROXY_ERROR_MSG); return NULL; } \
201 return ((class_name*)BGE_PROXY_REF(self))->Py##method_name(args, kwds); \
203 static const char method_name##_doc[]; \
205 #define KX_PYMETHOD_DOC_VARARGS(class_name, method_name) \
206 PyObject* Py##method_name(PyObject* args); \
207 static PyObject* sPy##method_name( PyObject* self, PyObject* args) { \
208 if(BGE_PROXY_REF(self)==NULL) { PyErr_SetString(PyExc_RuntimeError, #class_name "." #method_name "(...) - " BGE_PROXY_ERROR_MSG); return NULL; } \
209 return ((class_name*)BGE_PROXY_REF(self))->Py##method_name(args); \
211 static const char method_name##_doc[]; \
213 #define KX_PYMETHOD_DOC_O(class_name, method_name) \
214 PyObject* Py##method_name(PyObject* value); \
215 static PyObject* sPy##method_name( PyObject* self, PyObject* value) { \
216 if(BGE_PROXY_REF(self)==NULL) { PyErr_SetString(PyExc_RuntimeError, #class_name "." #method_name "(value) - " BGE_PROXY_ERROR_MSG); return NULL; } \
217 return ((class_name*)BGE_PROXY_REF(self))->Py##method_name(value); \
219 static const char method_name##_doc[]; \
221 #define KX_PYMETHOD_DOC_NOARGS(class_name, method_name) \
222 PyObject* Py##method_name(); \
223 static PyObject* sPy##method_name( PyObject* self) { \
224 if(BGE_PROXY_REF(self)==NULL) { PyErr_SetString(PyExc_RuntimeError, #class_name "." #method_name "() - " BGE_PROXY_ERROR_MSG); return NULL; } \
225 return ((class_name*)BGE_PROXY_REF(self))->Py##method_name(); \
227 static const char method_name##_doc[]; \
230 /* The line above should remain empty */
232 * Method table macro (with doc)
234 #define KX_PYMETHODTABLE(class_name, method_name) \
235 {#method_name , (PyCFunction) class_name::sPy##method_name, METH_VARARGS, (const char *)class_name::method_name##_doc}
237 #define KX_PYMETHODTABLE_O(class_name, method_name) \
238 {#method_name , (PyCFunction) class_name::sPy##method_name, METH_O, (const char *)class_name::method_name##_doc}
240 #define KX_PYMETHODTABLE_NOARGS(class_name, method_name) \
241 {#method_name , (PyCFunction) class_name::sPy##method_name, METH_NOARGS, (const char *)class_name::method_name##_doc}
244 * Function implementation macro
246 #define KX_PYMETHODDEF_DOC(class_name, method_name, doc_string) \
247 const char class_name::method_name##_doc[] = doc_string; \
248 PyObject* class_name::Py##method_name(PyObject* args, PyObject*)
250 #define KX_PYMETHODDEF_DOC_VARARGS(class_name, method_name, doc_string) \
251 const char class_name::method_name##_doc[] = doc_string; \
252 PyObject* class_name::Py##method_name(PyObject* args)
254 #define KX_PYMETHODDEF_DOC_O(class_name, method_name, doc_string) \
255 const char class_name::method_name##_doc[] = doc_string; \
256 PyObject* class_name::Py##method_name(PyObject* value)
258 #define KX_PYMETHODDEF_DOC_NOARGS(class_name, method_name, doc_string) \
259 const char class_name::method_name##_doc[] = doc_string; \
260 PyObject* class_name::Py##method_name()
263 * Attribute management
265 enum KX_PYATTRIBUTE_TYPE {
266 KX_PYATTRIBUTE_TYPE_BOOL,
267 KX_PYATTRIBUTE_TYPE_ENUM,
268 KX_PYATTRIBUTE_TYPE_SHORT,
269 KX_PYATTRIBUTE_TYPE_INT,
270 KX_PYATTRIBUTE_TYPE_FLOAT,
271 KX_PYATTRIBUTE_TYPE_STRING,
272 KX_PYATTRIBUTE_TYPE_DUMMY,
273 KX_PYATTRIBUTE_TYPE_FUNCTION,
274 KX_PYATTRIBUTE_TYPE_VECTOR,
275 KX_PYATTRIBUTE_TYPE_FLAG,
276 KX_PYATTRIBUTE_TYPE_CHAR,
279 enum KX_PYATTRIBUTE_ACCESS {
284 struct KX_PYATTRIBUTE_DEF;
285 typedef int (*KX_PYATTRIBUTE_CHECK_FUNCTION)(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef);
286 typedef int (*KX_PYATTRIBUTE_SET_FUNCTION)(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
287 typedef PyObject* (*KX_PYATTRIBUTE_GET_FUNCTION)(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef);
289 typedef struct KX_PYATTRIBUTE_DEF {
290 const char *m_name; // name of the python attribute
291 KX_PYATTRIBUTE_TYPE m_type; // type of value
292 KX_PYATTRIBUTE_ACCESS m_access; // read/write access or read-only
293 int m_imin; // minimum value in case of integer attributes
294 // (for string: minimum string length, for flag: mask value, for float: matrix row size)
295 int m_imax; // maximum value in case of integer attributes
296 // (for string: maximum string length, for flag: 1 if flag is negative, float: vector/matrix col size)
297 float m_fmin; // minimum value in case of float attributes
298 float m_fmax; // maximum value in case of float attributes
299 bool m_clamp; // enforce min/max value by clamping
300 bool m_usePtr; // the attribute uses the proxy generic pointer, set at runtime
301 size_t m_offset; // position of field in structure
302 size_t m_size; // size of field for runtime verification (enum only)
303 size_t m_length; // length of array, 1=simple attribute
304 KX_PYATTRIBUTE_CHECK_FUNCTION m_checkFunction; // static function to check the assignment, returns 0 if no error
305 KX_PYATTRIBUTE_SET_FUNCTION m_setFunction; // static function to check the assignment, returns 0 if no error
306 KX_PYATTRIBUTE_GET_FUNCTION m_getFunction; // static function to check the assignment, returns 0 if no error
308 // The following pointers are just used to have compile time check for attribute type.
309 // It would have been good to use a union but that would require C99 compatibility
310 // to initialize specific union fields through designated initializers.
313 short int *m_shortPtr;
316 STR_String *m_stringPtr;
317 MT_Vector3 *m_vectorPtr;
322 #define KX_PYATTRIBUTE_BOOL_RW(name,object,field) \
323 { 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} }
324 #define KX_PYATTRIBUTE_BOOL_RW_CHECK(name,object,field,function) \
325 { 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} }
326 #define KX_PYATTRIBUTE_BOOL_RO(name,object,field) \
327 { 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} }
329 /* attribute points to a single bit of an integer field, attribute=true if bit is set */
330 #define KX_PYATTRIBUTE_FLAG_RW(name,object,field,bit) \
331 { 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} }
332 #define KX_PYATTRIBUTE_FLAG_RW_CHECK(name,object,field,bit,function) \
333 { 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} }
334 #define KX_PYATTRIBUTE_FLAG_RO(name,object,field,bit) \
335 { 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} }
337 /* attribute points to a single bit of an integer field, attribute=true if bit is set*/
338 #define KX_PYATTRIBUTE_FLAG_NEGATIVE_RW(name,object,field,bit) \
339 { 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} }
340 #define KX_PYATTRIBUTE_FLAG_NEGATIVE_RW_CHECK(name,object,field,bit,function) \
341 { 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} }
342 #define KX_PYATTRIBUTE_FLAG_NEGATIVE_RO(name,object,field,bit) \
343 { 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} }
345 // enum field cannot be mapped to pointer (because we would need a pointer for each enum)
346 // use field size to verify mapping at runtime only, assuming enum size is equal to int size.
347 #define KX_PYATTRIBUTE_ENUM_RW(name,min,max,clamp,object,field) \
348 { 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} }
349 #define KX_PYATTRIBUTE_ENUM_RW_CHECK(name,min,max,clamp,object,field,function) \
350 { 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} }
351 #define KX_PYATTRIBUTE_ENUM_RO(name,object,field) \
352 { 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} }
354 #define KX_PYATTRIBUTE_SHORT_RW(name,min,max,clamp,object,field) \
355 { 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} }
356 #define KX_PYATTRIBUTE_SHORT_RW_CHECK(name,min,max,clamp,object,field,function) \
357 { 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} }
358 #define KX_PYATTRIBUTE_SHORT_RO(name,object,field) \
359 { 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} }
360 #define KX_PYATTRIBUTE_SHORT_ARRAY_RW(name,min,max,clamp,object,field,length) \
361 { 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} }
362 #define KX_PYATTRIBUTE_SHORT_ARRAY_RW_CHECK(name,min,max,clamp,object,field,length,function) \
363 { 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} }
364 #define KX_PYATTRIBUTE_SHORT_ARRAY_RO(name,object,field,length) \
365 { 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} }
367 #define KX_PYATTRIBUTE_SHORT_LIST_RW(name,min,max,clamp,object,field,length) \
368 { 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} }
369 #define KX_PYATTRIBUTE_SHORT_LIST_RW_CHECK(name,min,max,clamp,object,field,length,function) \
370 { 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} }
371 #define KX_PYATTRIBUTE_SHORT_LIST_RO(name,object,field,length) \
372 { 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} }
374 #define KX_PYATTRIBUTE_INT_RW(name,min,max,clamp,object,field) \
375 { 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} }
376 #define KX_PYATTRIBUTE_INT_RW_CHECK(name,min,max,clamp,object,field,function) \
377 { 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} }
378 #define KX_PYATTRIBUTE_INT_RO(name,object,field) \
379 { 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} }
380 #define KX_PYATTRIBUTE_INT_ARRAY_RW(name,min,max,clamp,object,field,length) \
381 { 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} }
382 #define KX_PYATTRIBUTE_INT_ARRAY_RW_CHECK(name,min,max,clamp,object,field,length,function) \
383 { 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} }
384 #define KX_PYATTRIBUTE_INT_ARRAY_RO(name,object,field,length) \
385 { 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} }
387 #define KX_PYATTRIBUTE_INT_LIST_RW(name,min,max,clamp,object,field,length) \
388 { 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} }
389 #define KX_PYATTRIBUTE_INT_LIST_RW_CHECK(name,min,max,clamp,object,field,length,function) \
390 { 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} }
391 #define KX_PYATTRIBUTE_INT_LIST_RO(name,object,field,length) \
392 { 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} }
394 // always clamp for float
395 #define KX_PYATTRIBUTE_FLOAT_RW(name,min,max,object,field) \
396 { 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} }
397 #define KX_PYATTRIBUTE_FLOAT_RW_CHECK(name,min,max,object,field,function) \
398 { 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} }
399 #define KX_PYATTRIBUTE_FLOAT_RO(name,object,field) \
400 { 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} }
401 // field must be float[n], returns a sequence
402 #define KX_PYATTRIBUTE_FLOAT_ARRAY_RW(name,min,max,object,field,length) \
403 { 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} }
404 #define KX_PYATTRIBUTE_FLOAT_ARRAY_RW_CHECK(name,min,max,object,field,length,function) \
405 { 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} }
406 #define KX_PYATTRIBUTE_FLOAT_ARRAY_RO(name,object,field,length) \
407 { 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} }
408 // field must be float[n], returns a vector
409 #define KX_PYATTRIBUTE_FLOAT_VECTOR_RW(name,min,max,object,field,length) \
410 { 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} }
411 #define KX_PYATTRIBUTE_FLOAT_VECTOR_RW_CHECK(name,min,max,object,field,length,function) \
412 { 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} }
413 #define KX_PYATTRIBUTE_FLOAT_VECTOR_RO(name,object,field,length) \
414 { 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} }
415 // field must be float[n][n], returns a matrix
416 #define KX_PYATTRIBUTE_FLOAT_MATRIX_RW(name,min,max,object,field,length) \
417 { 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} }
418 #define KX_PYATTRIBUTE_FLOAT_MATRIX_RW_CHECK(name,min,max,object,field,length,function) \
419 { 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} }
420 #define KX_PYATTRIBUTE_FLOAT_MATRIX_RO(name,object,field,length) \
421 { 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} }
423 // only for STR_String member
424 #define KX_PYATTRIBUTE_STRING_RW(name,min,max,clamp,object,field) \
425 { 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} }
426 #define KX_PYATTRIBUTE_STRING_RW_CHECK(name,min,max,clamp,object,field,function) \
427 { 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} }
428 #define KX_PYATTRIBUTE_STRING_RO(name,object,field) \
429 { 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} }
431 // only for char [] array
432 #define KX_PYATTRIBUTE_CHAR_RW(name,object,field) \
433 { 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} }
434 #define KX_PYATTRIBUTE_CHAR_RW_CHECK(name,object,field,function) \
435 { 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} }
436 #define KX_PYATTRIBUTE_CHAR_RO(name,object,field) \
437 { 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} }
439 // for MT_Vector3 member
440 #define KX_PYATTRIBUTE_VECTOR_RW(name,min,max,object,field) \
441 { 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} }
442 #define KX_PYATTRIBUTE_VECTOR_RW_CHECK(name,min,max,clamp,object,field,function) \
443 { 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} }
444 #define KX_PYATTRIBUTE_VECTOR_RO(name,object,field) \
445 { 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} }
447 #define KX_PYATTRIBUTE_RW_FUNCTION(name,object,getfunction,setfunction) \
448 { 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} }
449 #define KX_PYATTRIBUTE_RO_FUNCTION(name,object,getfunction) \
450 { 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} }
451 #define KX_PYATTRIBUTE_ARRAY_RW_FUNCTION(name,object,length,getfunction,setfunction) \
452 { 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} }
453 #define KX_PYATTRIBUTE_ARRAY_RO_FUNCTION(name,object,length,getfunction) \
454 { 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} }
457 /*------------------------------
459 ------------------------------*/
460 typedef PyTypeObject * PyParentObject; // Define the PyParent Object
464 #ifdef WITH_CXX_GUARDEDALLOC
467 void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, "GE:PyObjectPlus"); } \
468 void operator delete( void *mem ) { MEM_freeN(mem); } \
470 #define Py_HeaderPtr \
472 void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, "GE:PyObjectPlusPtr"); } \
473 void operator delete( void *mem ) { MEM_freeN(mem); } \
475 #else // WITH_CXX_GUARDEDALLOC
480 #define Py_HeaderPtr \
483 #endif // WITH_CXX_GUARDEDALLOC
488 // By making SG_QList the ultimate parent for PyObjectPlus objects, it
489 // allows to put them in 2 different dynamic lists at the same time
490 // The use of these links is interesting because they free of memory allocation
491 // but it's very important not to mess up with them. If you decide that
492 // the SG_QList or SG_DList component is used for something for a certain class,
493 // they cannot can be used for anything else at a parent level!
494 // What these lists are and what they are used for must be carefully documented
495 // at the level where they are used.
496 // DON'T MAKE ANY USE OF THESE LIST AT THIS LEVEL, they are already used
497 // at SCA_IActuator, SCA_ISensor, SCA_IController level which rules out the
498 // possibility to use them at SCA_ILogicBrick, CValue and PyObjectPlus level.
499 class PyObjectPlus : public SG_QList
500 { // The PyObjectPlus abstract class
501 Py_Header; // Always start with Py_Header
506 virtual ~PyObjectPlus(); // destructor
509 PyObject *m_proxy; /* actually a PyObjectPlus_Proxy */
511 /* These static functions are referenced by ALL PyObjectPlus_Proxy types
512 * they take the C++ reference from the PyObjectPlus_Proxy and call
513 * its own virtual py_repr, py_base_dealloc ,etc. functions.
516 static PyObject* py_base_new(PyTypeObject *type, PyObject *args, PyObject *kwds); /* allows subclassing */
517 static void py_base_dealloc(PyObject *self);
518 static PyObject* py_base_repr(PyObject *self);
520 /* These are all virtual python methods that are defined in each class
521 * Our own fake subclassing calls these on each class, then calls the parent */
522 virtual PyObject* py_repr(void);
523 /* subclass may overwrite this function to implement more sophisticated method of validating a proxy */
524 virtual bool py_is_valid(void) { return true; }
526 static PyObject* py_get_attrdef(PyObject *self_py, const PyAttributeDef *attrdef);
527 static int py_set_attrdef(PyObject *self_py, PyObject *value, const PyAttributeDef *attrdef);
529 /* Kindof dumb, always returns True, the false case is checked for, before this function gets accessed */
530 static PyObject* pyattr_get_invalid(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
532 static PyObject *GetProxyPlus_Ext(PyObjectPlus *self, PyTypeObject *tp, void *ptr);
533 /* self=NULL => proxy to generic pointer detached from GE object
534 if py_owns is true, the memory pointed by ptr will be deleted automatially with MEM_freeN
535 self!=NULL=> proxy attached to GE object, ptr is optional and point to a struct from which attributes can be defined
536 if py_owns is true, the object will be deleted automatically, ptr will NOT be deleted
537 (assume object destructor takes care of it) */
538 static PyObject *NewProxyPlus_Ext(PyObjectPlus *self, PyTypeObject *tp, void *ptr, bool py_owns);
540 static WarnLink* GetDeprecationWarningLinkFirst(void);
541 static WarnLink* GetDeprecationWarningLinkLast(void);
542 static void SetDeprecationWarningFirst(WarnLink* wlink);
543 static void SetDeprecationWarningLinkLast(WarnLink* wlink);
544 static void NullDeprecationWarning();
546 /** enable/disable display of deprecation warnings */
547 static void SetDeprecationWarnings(bool ignoreDeprecationWarnings);
548 /** Shows a deprecation warning */
549 static void ShowDeprecationWarning_func(const char* method,const char* prop);
550 static void ClearDeprecationWarning();
554 void InvalidateProxy();
557 * Makes sure any internal data owned by this class is deep copied.
559 virtual void ProcessReplica();
561 static bool m_ignore_deprecation_warnings;
565 PyObject *py_getattr_dict(PyObject *pydict, PyObject *tp_dict);
568 #endif // _adr_py_lib_h_