get/set Angular velocity for KX_GameObjects python api and for the AddObject actuator.
[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_VARARGS(class_name, method_name)                    \
106         PyObject* Py##method_name(PyObject* self, PyObject* args); \
107         static PyObject* sPy##method_name( PyObject* self, PyObject* args) { \
108                 return ((class_name*) self)->Py##method_name(self, args);               \
109         }; \
110
111 #define KX_PYMETHOD_NOARGS(class_name, method_name)                     \
112         PyObject* Py##method_name(PyObject* self); \
113         static PyObject* sPy##method_name( PyObject* self) { \
114                 return ((class_name*) self)->Py##method_name(self);             \
115         }; \
116         
117 #define KX_PYMETHOD_O(class_name, method_name)                  \
118         PyObject* Py##method_name(PyObject* self, PyObject* value); \
119         static PyObject* sPy##method_name( PyObject* self, PyObject* value) { \
120                 return ((class_name*) self)->Py##method_name(self, value);              \
121         }; \
122
123 #define KX_PYMETHOD_DOC(class_name, method_name)                        \
124         PyObject* Py##method_name(PyObject* self, PyObject* args, PyObject* kwds); \
125         static PyObject* sPy##method_name( PyObject* self, PyObject* args, PyObject* kwds) { \
126                 return ((class_name*) self)->Py##method_name(self, args, kwds);         \
127         }; \
128     static char method_name##_doc[]; \
129
130 #define KX_PYMETHOD_DOC_VARARGS(class_name, method_name)                        \
131         PyObject* Py##method_name(PyObject* self, PyObject* args); \
132         static PyObject* sPy##method_name( PyObject* self, PyObject* args) { \
133                 return ((class_name*) self)->Py##method_name(self, args);               \
134         }; \
135     static char method_name##_doc[]; \
136
137 #define KX_PYMETHOD_DOC_O(class_name, method_name)                      \
138         PyObject* Py##method_name(PyObject* self, PyObject* value); \
139         static PyObject* sPy##method_name( PyObject* self, PyObject* value) { \
140                 return ((class_name*) self)->Py##method_name(self, value);              \
141         }; \
142     static char method_name##_doc[]; \
143
144 #define KX_PYMETHOD_DOC_NOARGS(class_name, method_name)                 \
145         PyObject* Py##method_name(PyObject* self); \
146         static PyObject* sPy##method_name( PyObject* self) { \
147                 return ((class_name*) self)->Py##method_name(self);             \
148         }; \
149     static char method_name##_doc[]; \
150
151
152 /* The line above should remain empty */
153 /**
154  * Method table macro (with doc)
155  */
156 #define KX_PYMETHODTABLE(class_name, method_name) \
157         {#method_name , (PyCFunction) class_name::sPy##method_name, METH_VARARGS, class_name::method_name##_doc}
158
159 /**
160  * Function implementation macro
161  */
162 #define KX_PYMETHODDEF_DOC(class_name, method_name, doc_string) \
163 char class_name::method_name##_doc[] = doc_string; \
164 PyObject* class_name::Py##method_name(PyObject*, PyObject* args, PyObject*)
165
166
167 /*------------------------------
168  * PyObjectPlus
169 ------------------------------*/
170 typedef PyTypeObject * PyParentObject;                          // Define the PyParent Object
171
172 class PyObjectPlus : public PyObject 
173 {                               // The PyObjectPlus abstract class
174         Py_Header;                                                      // Always start with Py_Header
175         
176 public:
177         PyObjectPlus(PyTypeObject *T);
178         
179         virtual ~PyObjectPlus();                                        // destructor
180         static void PyDestructor(PyObject *P)                           // python wrapper
181         {  
182                 delete ((PyObjectPlus *) P);  
183         };
184         
185 //      void INCREF(void) {
186 //                Py_INCREF(this);
187 //        };                            // incref method
188 //      void DECREF(void) {
189 //                Py_DECREF(this);
190 //        };                            // decref method
191         
192         virtual PyObject *_getattr(const STR_String& attr);                     // _getattr method
193         static  PyObject *__getattr(PyObject * PyObj, char *attr)       // This should be the entry in Type. 
194         {
195                 return ((PyObjectPlus*) PyObj)->_getattr(STR_String(attr)); 
196         }
197         
198         virtual int _delattr(const STR_String& attr);
199         virtual int _setattr(const STR_String& attr, PyObject *value);          // _setattr method
200         static  int __setattr(PyObject *PyObj,                  // This should be the entry in Type. 
201                                 char *attr, 
202                                 PyObject *value)
203         { 
204                 if (!value)
205                         return ((PyObjectPlus*) PyObj)->_delattr(attr);
206                 return ((PyObjectPlus*) PyObj)->_setattr(STR_String(attr), value);  
207         }
208         
209         virtual PyObject *_repr(void);                          // _repr method
210         static  PyObject *__repr(PyObject *PyObj)                       // This should be the entry in Type.
211         {
212                 return ((PyObjectPlus*) PyObj)->_repr();  
213         }
214         
215                                                                         // isA methods
216         bool isA(PyTypeObject *T);
217         bool isA(const char *mytypename);
218         PyObject *Py_isA(PyObject *args);
219         static PyObject *sPy_isA(PyObject *self, PyObject *args, PyObject *kwd)
220         {
221                 return ((PyObjectPlus*)self)->Py_isA(args);
222         }
223 };
224
225 #endif //  _adr_py_lib_h_
226
227 #endif //NO_EXP_PYTHON_EMBEDDING
228