game engine python api
[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_Return { Py_INCREF(Py_None); return Py_None;}
48
49 static inline void Py_Fatal(char *M) {
50         //cout << M << endl; 
51         exit(-1);
52 };
53
54                                                                 // This must be the first line of each 
55                                                                 // PyC++ class
56 #define Py_Header \
57  public: \
58   static PyTypeObject   Type; \
59   static PyMethodDef    Methods[]; \
60   static PyParentObject Parents[]; \
61   virtual PyTypeObject *GetType(void) {return &Type;}; \
62   virtual PyParentObject *GetParents(void) {return Parents;}
63
64
65                                                                 // This defines the _getattr_up macro
66                                                                 // which allows attribute and method calls
67                                                                 // to be properly passed up the hierarchy.
68 #define _getattr_up(Parent) \
69   PyObject *rvalue = NULL; \
70   if (attr=="__methods__") { \
71     PyObject *_attr_string = NULL; \
72     PyMethodDef *meth = Methods; \
73     rvalue = Parent::_getattr(attr); \
74     if (rvalue==NULL) { \
75         PyErr_Clear(); \
76         rvalue = PyList_New(0); \
77     } \
78     if (meth) { \
79       for (; meth->ml_name != NULL; meth++) { \
80         _attr_string = PyString_FromString(meth->ml_name); \
81                 PyList_Append(rvalue, _attr_string); \
82                 Py_DECREF(_attr_string); \
83           } \
84         } \
85   } else { \
86     rvalue = Py_FindMethod(Methods, this, const_cast<char*>(attr.ReadPtr())); \
87     if (rvalue == NULL) { \
88       PyErr_Clear(); \
89       rvalue = Parent::_getattr(attr); \
90     } \
91   } \
92   return rvalue; \
93
94
95 /**
96  * These macros are helpfull when embedding Python routines. The second
97  * macro is one that also requires a documentation string
98  */
99 #define KX_PYMETHOD(class_name, method_name)                    \
100         PyObject* Py##method_name(PyObject* self, PyObject* args, PyObject* kwds); \
101         static PyObject* sPy##method_name( PyObject* self, PyObject* args, PyObject* kwds) { \
102                 return ((class_name*) self)->Py##method_name(self, args, kwds);         \
103         }; \
104
105 #define KX_PYMETHOD_NOARGS(class_name, method_name)                     \
106         PyObject* Py##method_name(PyObject* self); \
107         static PyObject* sPy##method_name( PyObject* self) { \
108                 return ((class_name*) self)->Py##method_name(self);             \
109         }; \
110         
111 #define KX_PYMETHOD_O(class_name, method_name)                  \
112         PyObject* Py##method_name(PyObject* self, PyObject* value); \
113         static PyObject* sPy##method_name( PyObject* self, PyObject* value) { \
114                 return ((class_name*) self)->Py##method_name(self, value);              \
115         }; \
116
117 #define KX_PYMETHOD_DOC(class_name, method_name)                        \
118         PyObject* Py##method_name(PyObject* self, PyObject* args, PyObject* kwds); \
119         static PyObject* sPy##method_name( PyObject* self, PyObject* args, PyObject* kwds) { \
120                 return ((class_name*) self)->Py##method_name(self, args, kwds);         \
121         }; \
122     static char method_name##_doc[]; \
123
124 #define KX_PYMETHOD_DOC_O(class_name, method_name)                      \
125         PyObject* Py##method_name(PyObject* self, PyObject* value); \
126         static PyObject* sPy##method_name( PyObject* self, PyObject* value) { \
127                 return ((class_name*) self)->Py##method_name(self, value);              \
128         }; \
129     static char method_name##_doc[]; \
130
131 #define KX_PYMETHOD_DOC_NOARGS(class_name, method_name)                 \
132         PyObject* Py##method_name(PyObject* self); \
133         static PyObject* sPy##method_name( PyObject* self) { \
134                 return ((class_name*) self)->Py##method_name(self);             \
135         }; \
136     static char method_name##_doc[]; \
137
138
139 /* The line above should remain empty */
140 /**
141  * Method table macro (with doc)
142  */
143 #define KX_PYMETHODTABLE(class_name, method_name) \
144         {#method_name , (PyCFunction) class_name::sPy##method_name, METH_VARARGS, class_name::method_name##_doc}
145
146 /**
147  * Function implementation macro
148  */
149 #define KX_PYMETHODDEF_DOC(class_name, method_name, doc_string) \
150 char class_name::method_name##_doc[] = doc_string; \
151 PyObject* class_name::Py##method_name(PyObject*, PyObject* args, PyObject*)
152
153
154 /*------------------------------
155  * PyObjectPlus
156 ------------------------------*/
157 typedef PyTypeObject * PyParentObject;                          // Define the PyParent Object
158
159 class PyObjectPlus : public PyObject 
160 {                               // The PyObjectPlus abstract class
161         Py_Header;                                                      // Always start with Py_Header
162         
163 public:
164         PyObjectPlus(PyTypeObject *T);
165         
166         virtual ~PyObjectPlus();                                        // destructor
167         static void PyDestructor(PyObject *P)                           // python wrapper
168         {  
169                 delete ((PyObjectPlus *) P);  
170         };
171         
172 //      void INCREF(void) {
173 //                Py_INCREF(this);
174 //        };                            // incref method
175 //      void DECREF(void) {
176 //                Py_DECREF(this);
177 //        };                            // decref method
178         
179         virtual PyObject *_getattr(const STR_String& attr);                     // _getattr method
180         static  PyObject *__getattr(PyObject * PyObj, char *attr)       // This should be the entry in Type. 
181         {
182                 return ((PyObjectPlus*) PyObj)->_getattr(STR_String(attr)); 
183         }
184         
185         virtual int _delattr(const STR_String& attr);
186         virtual int _setattr(const STR_String& attr, PyObject *value);          // _setattr method
187         static  int __setattr(PyObject *PyObj,                  // This should be the entry in Type. 
188                                 char *attr, 
189                                 PyObject *value)
190         { 
191                 if (!value)
192                         return ((PyObjectPlus*) PyObj)->_delattr(attr);
193                 return ((PyObjectPlus*) PyObj)->_setattr(STR_String(attr), value);  
194         }
195         
196         virtual PyObject *_repr(void);                          // _repr method
197         static  PyObject *__repr(PyObject *PyObj)                       // This should be the entry in Type.
198         {
199                 return ((PyObjectPlus*) PyObj)->_repr();  
200         }
201         
202                                                                         // isA methods
203         bool isA(PyTypeObject *T);
204         bool isA(const char *mytypename);
205         PyObject *Py_isA(PyObject *args);
206         static PyObject *sPy_isA(PyObject *self, PyObject *args, PyObject *kwd)
207         {
208                 return ((PyObjectPlus*)self)->Py_isA(args);
209         }
210 };
211
212 #endif //  _adr_py_lib_h_
213
214 #endif //NO_EXP_PYTHON_EMBEDDING
215