svn merge -r 15392:15551 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender.git] / source / gameengine / Expressions / PyObjectPlus.h
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
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.
10  *
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.
15  *
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #ifndef NO_EXP_PYTHON_EMBEDDING
31
32 #ifndef _adr_py_lib_h_                          // only process once,
33 #define _adr_py_lib_h_                          // even if multiply included
34
35 #ifndef __cplusplus                             // c++ only
36 #error Must be compiled with C++
37 #endif
38
39 #include "KX_Python.h"
40 #include "STR_String.h"
41
42 /*------------------------------
43  * Python defines
44 ------------------------------*/
45
46                                                                 // some basic python macros
47 #define Py_NEWARGS 1                    
48 #define Py_Return { Py_INCREF(Py_None); return Py_None;}
49 static inline PyObject* Py_Success(bool truth)
50 {
51         if (truth)
52         {
53                 Py_INCREF(Py_True);
54                 return Py_True;
55         }
56         Py_INCREF(Py_False);
57         return Py_False;
58 }
59
60 #define Py_Error(E, M)   {PyErr_SetString(E, M); return NULL;}
61 #define Py_Try(F) {if (!(F)) return NULL;}
62 #define Py_Assert(A,E,M) {if (!(A)) {PyErr_SetString(E, M); return NULL;}}
63
64 static inline void Py_Fatal(char *M) {
65         //cout << M << endl; 
66         exit(-1);
67 };
68
69                                                                 // This must be the first line of each 
70                                                                 // PyC++ class
71 #define Py_Header \
72  public: \
73   static PyTypeObject   Type; \
74   static PyMethodDef    Methods[]; \
75   static PyParentObject Parents[]; \
76   virtual PyTypeObject *GetType(void) {return &Type;}; \
77   virtual PyParentObject *GetParents(void) {return Parents;}
78
79
80                                                                 // This defines the _getattr_up macro
81                                                                 // which allows attribute and method calls
82                                                                 // to be properly passed up the hierarchy.
83 #define _getattr_up(Parent) \
84   PyObject *rvalue = NULL; \
85   if (attr=="__methods__") { \
86     PyObject *_attr_string = NULL; \
87     PyMethodDef *meth = Methods; \
88     rvalue = Parent::_getattr(attr); \
89     if (rvalue==NULL) { \
90         PyErr_Clear(); \
91         rvalue = PyList_New(0); \
92     } \
93     if (meth) { \
94       for (; meth->ml_name != NULL; meth++) { \
95         _attr_string = PyString_FromString(meth->ml_name); \
96                 PyList_Append(rvalue, _attr_string); \
97                 Py_DECREF(_attr_string); \
98           } \
99         } \
100   } else { \
101     rvalue = Py_FindMethod(Methods, this, const_cast<char*>(attr.ReadPtr())); \
102     if (rvalue == NULL) { \
103       PyErr_Clear(); \
104       rvalue = Parent::_getattr(attr); \
105     } \
106   } \
107   return rvalue; \
108
109
110 /**
111  * These macros are helpfull when embedding Python routines. The second
112  * macro is one that also requires a documentation string
113  */
114 #define KX_PYMETHOD(class_name, method_name)                    \
115         PyObject* Py##method_name(PyObject* self, PyObject* args, PyObject* kwds); \
116         static PyObject* sPy##method_name( PyObject* self, PyObject* args, PyObject* kwds) { \
117                 return ((class_name*) self)->Py##method_name(self, args, kwds);         \
118         }; \
119
120 #define KX_PYMETHOD_NOARGS(class_name, method_name)                     \
121         PyObject* Py##method_name(PyObject* self); \
122         static PyObject* sPy##method_name( PyObject* self) { \
123                 return ((class_name*) self)->Py##method_name(self);             \
124         }; \
125         
126 #define KX_PYMETHOD_O(class_name, method_name)                  \
127         PyObject* Py##method_name(PyObject* self, PyObject* value); \
128         static PyObject* sPy##method_name( PyObject* self, PyObject* value) { \
129                 return ((class_name*) self)->Py##method_name(self, value);              \
130         }; \
131
132 #define KX_PYMETHOD_DOC(class_name, method_name)                        \
133         PyObject* Py##method_name(PyObject* self, PyObject* args, PyObject* kwds); \
134         static PyObject* sPy##method_name( PyObject* self, PyObject* args, PyObject* kwds) { \
135                 return ((class_name*) self)->Py##method_name(self, args, kwds);         \
136         }; \
137     static char method_name##_doc[]; \
138
139 #define KX_PYMETHOD_DOC_O(class_name, method_name)                      \
140         PyObject* Py##method_name(PyObject* self, PyObject* value); \
141         static PyObject* sPy##method_name( PyObject* self, PyObject* value) { \
142                 return ((class_name*) self)->Py##method_name(self, value);              \
143         }; \
144     static char method_name##_doc[]; \
145
146 #define KX_PYMETHOD_DOC_NOARGS(class_name, method_name)                 \
147         PyObject* Py##method_name(PyObject* self); \
148         static PyObject* sPy##method_name( PyObject* self) { \
149                 return ((class_name*) self)->Py##method_name(self);             \
150         }; \
151     static char method_name##_doc[]; \
152
153
154 /* The line above should remain empty */
155 /**
156  * Method table macro (with doc)
157  */
158 #define KX_PYMETHODTABLE(class_name, method_name) \
159         {#method_name , (PyCFunction) class_name::sPy##method_name, METH_VARARGS, class_name::method_name##_doc}
160
161 /**
162  * Function implementation macro
163  */
164 #define KX_PYMETHODDEF_DOC(class_name, method_name, doc_string) \
165 char class_name::method_name##_doc[] = doc_string; \
166 PyObject* class_name::Py##method_name(PyObject*, PyObject* args, PyObject*)
167
168
169 /*------------------------------
170  * PyObjectPlus
171 ------------------------------*/
172 typedef PyTypeObject * PyParentObject;                          // Define the PyParent Object
173
174 class PyObjectPlus : public PyObject 
175 {                               // The PyObjectPlus abstract class
176         Py_Header;                                                      // Always start with Py_Header
177         
178 public:
179         PyObjectPlus(PyTypeObject *T);
180         
181         virtual ~PyObjectPlus();                                        // destructor
182         static void PyDestructor(PyObject *P)                           // python wrapper
183         {  
184                 delete ((PyObjectPlus *) P);  
185         };
186         
187 //      void INCREF(void) {
188 //                Py_INCREF(this);
189 //        };                            // incref method
190 //      void DECREF(void) {
191 //                Py_DECREF(this);
192 //        };                            // decref method
193         
194         virtual PyObject *_getattr(const STR_String& attr);                     // _getattr method
195         static  PyObject *__getattr(PyObject * PyObj, char *attr)       // This should be the entry in Type. 
196         {
197                 return ((PyObjectPlus*) PyObj)->_getattr(STR_String(attr)); 
198         }
199         
200         virtual int _delattr(const STR_String& attr);
201         virtual int _setattr(const STR_String& attr, PyObject *value);          // _setattr method
202         static  int __setattr(PyObject *PyObj,                  // This should be the entry in Type. 
203                                 char *attr, 
204                                 PyObject *value)
205         { 
206                 if (!value)
207                         return ((PyObjectPlus*) PyObj)->_delattr(attr);
208                 return ((PyObjectPlus*) PyObj)->_setattr(STR_String(attr), value);  
209         }
210         
211         virtual PyObject *_repr(void);                          // _repr method
212         static  PyObject *__repr(PyObject *PyObj)                       // This should be the entry in Type.
213         {
214                 return ((PyObjectPlus*) PyObj)->_repr();  
215         }
216         
217                                                                         // isA methods
218         bool isA(PyTypeObject *T);
219         bool isA(const char *mytypename);
220         PyObject *Py_isA(PyObject *args);
221         static PyObject *sPy_isA(PyObject *self, PyObject *args, PyObject *kwd)
222         {
223                 return ((PyObjectPlus*)self)->Py_isA(args);
224         }
225 };
226
227 #endif //  _adr_py_lib_h_
228
229 #endif //NO_EXP_PYTHON_EMBEDDING
230