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