00001
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00029
00031
00032 #ifdef HAVE_PYTHON
00033
00034 #if defined(_MSC_VER)
00035
00036
00037
00038 #ifdef _DEBUG
00039 #define _DEBUG_UNDEFED
00040 #undef _DEBUG
00041 #endif
00042 #endif
00043 #if defined(__APPLE__) && defined(__MACH__)
00044 #include <Python/Python.h>
00045 #else
00046 #include <Python.h>
00047 #endif
00048 #if defined(_MSC_VER)
00049
00050 #ifdef _DEBUG_UNDEFED
00051 #define _DEBUG
00052 #endif
00053 #endif
00054
00055 namespace H3D {
00056
00061 struct PyType {
00062 PyObject_HEAD
00063 };
00064
00076 template< class Type,
00077 PyTypeObject *TypeObject,
00078 string ( *NameFunc ) (),
00079 bool ( *CheckFunc )( PyObject * ),
00080 Type (*ValueFunc)( PyObject *),
00081 PyObject *(*NewFunc)( const Type &) >
00082 struct PyTypeWrapper: public PyType, Type {
00083
00086 static void installType( PyObject* H3D_module ) {
00087 if (PyType_Ready( TypeObject ) < 0 )
00088 return;
00089
00090 Py_INCREF( TypeObject );
00091 PyModule_AddObject( H3D_module,
00092 (char *)NameFunc().c_str(),
00093 (PyObject *)TypeObject );
00094 }
00095
00099 static PyObject* create() {
00100 return PyType_GenericAlloc( TypeObject, 1 );
00101 }
00102
00104 static void dealloc( PyObject *self ) {
00105 self->ob_type->tp_free( self );
00106 }
00107
00109 static PyObject* repr( PyObject *myself, PyObject *args ) {
00110 if( CheckFunc( myself ) ) {
00111 ostringstream s;
00112 s << ValueFunc( myself );
00113 return PyString_FromString( s.str().c_str() );
00114 } else {
00115 ostringstream s;
00116 s << "PyObject * is not a " << NameFunc() << "*";
00117 throw Exception::H3DAPIException( s.str(),
00118 H3D_FULL_LOCATION );
00119 }
00120 }
00121
00122
00124 static int compare( PyObject *veca, PyObject *vecb ) {
00125 if( CheckFunc( veca ) && CheckFunc( vecb ) ){
00126 Type a = ValueFunc( veca );
00127 Type b = ValueFunc( vecb );
00128 return a != b;
00129 } else {
00130 ostringstream s;
00131 s << "PyObject * is not a " << NameFunc() << "*";
00132 throw Exception::H3DAPIException( s.str(),
00133 H3D_FULL_LOCATION );
00134 }
00135 }
00136 };
00137
00138
00152 template< class Type,
00153 PyTypeObject *TypeObject,
00154 string ( *NameFunc ) (),
00155 bool ( *CheckFunc )( PyObject * ),
00156 Type (*ValueFunc)( PyObject *),
00157 PyObject *(*NewFunc)( const Type &) >
00158 struct PyNumberTypeWrapperBase:
00159 public PyTypeWrapper< Type,
00160 TypeObject,
00161 NameFunc,
00162 CheckFunc,
00163 ValueFunc,
00164 NewFunc > {
00165
00167 static PyObject* add( PyObject *veca, PyObject *vecb ) {
00168 if( CheckFunc( veca ) && CheckFunc( vecb ) ){
00169 Type c = ValueFunc( veca ) + ValueFunc( vecb );
00170 return NewFunc( c );
00171 }
00172 return Py_NotImplemented;
00173 }
00174
00176 static PyObject* sub( PyObject *veca, PyObject *vecb ) {
00177 if( CheckFunc( veca ) && CheckFunc( vecb ) ){
00178 Type c = ValueFunc( veca ) - ValueFunc( vecb );
00179 return NewFunc( c );
00180 }
00181 return Py_NotImplemented;
00182 }
00185 static PyObject* mul( PyObject *veca, PyObject *vecb ) {
00186 if( CheckFunc( veca ) ) {
00187 Type a = ValueFunc( veca );
00188 if( PyFloat_Check( vecb ) ) {
00189 Type c = a * PyFloat_AsDouble( vecb );
00190 return NewFunc( c );
00191
00192 } else if( PyInt_Check( vecb ) ) {
00193 Type c = a * PyInt_AsLong( vecb );
00194 return NewFunc( c );
00195
00196 } else if( PyLong_Check( vecb ) ) {
00197 Type c = a * PyLong_AsLong( vecb );
00198 return NewFunc( c );
00199 }
00200 } else if( CheckFunc( vecb ) ) {
00201 Type b = ValueFunc( vecb );
00202
00203 if( PyFloat_Check( veca ) ) {
00204 Type c = PyFloat_AsDouble( veca ) * b;
00205 return NewFunc( c );
00206
00207 } else if( PyInt_Check( veca ) ) {
00208 Type c = PyInt_AsLong( veca ) * b;
00209 return NewFunc( c );
00210
00211 } else if( PyLong_Check( veca ) ) {
00212 Type c = PyLong_AsLong( veca ) * b;
00213 return NewFunc( c );
00214 }
00215 }
00216 return Py_NotImplemented;
00217 }
00218
00220 static PyObject* div( PyObject *veca, PyObject *floatb ) {
00221 if( CheckFunc( veca ) ) {
00222 Type a = ValueFunc( veca );
00223
00224 if( PyFloat_Check( floatb ) ) {
00225 Type c = a / PyFloat_AsDouble( floatb );
00226 return NewFunc( c );
00227
00228 } else if( PyInt_Check( floatb ) ) {
00229 Type c = a / PyInt_AsLong( floatb );
00230 return NewFunc( c );
00231
00232 } else if( PyLong_Check( floatb ) ) {
00233 Type c = a / PyLong_AsLong( floatb );
00234 return NewFunc( c );
00235 }
00236 }
00237 return Py_NotImplemented;
00238 }
00239
00241 static PyObject* neg( PyObject *vec ) {
00242 if( CheckFunc( vec ) ){
00243 Type c = -ValueFunc( vec );
00244 return NewFunc( c );
00245 }
00246 return Py_NotImplemented;
00247 }
00248
00249 };
00250
00263 template< class Type,
00264 PyTypeObject *TypeObject,
00265 string ( *NameFunc ) (),
00266 bool ( *CheckFunc )( PyObject * ),
00267 Type (*ValueFunc)( PyObject *),
00268 PyObject *(*NewFunc)( const Type &) >
00269 struct PyVecTypeWrapper:
00270 public PyNumberTypeWrapperBase< Type,
00271 TypeObject,
00272 NameFunc,
00273 CheckFunc,
00274 ValueFunc,
00275 NewFunc > {
00277 static PyObject* mul( PyObject *veca, PyObject *vecb ) {
00278 if( CheckFunc( veca ) && CheckFunc( vecb ) ) {
00279 Type a = ValueFunc( veca );
00280 Type b = ValueFunc( vecb );
00281 return PyFloat_FromDouble( a * b );
00282 } else {
00283 return PyNumberTypeWrapperBase< Type,
00284 TypeObject,
00285 NameFunc,
00286 CheckFunc,
00287 ValueFunc,
00288 NewFunc >::mul( veca, vecb );
00289 }
00290 }
00291 };
00292
00305 template< class Type,
00306 PyTypeObject *TypeObject,
00307 string ( *NameFunc ) (),
00308 bool ( *CheckFunc )( PyObject * ),
00309 Type (*ValueFunc)( PyObject *),
00310 PyObject *(*NewFunc)( const Type &) >
00311 struct PyNumberTypeWrapper:
00312 public PyNumberTypeWrapperBase< Type,
00313 TypeObject,
00314 NameFunc,
00315 CheckFunc,
00316 ValueFunc,
00317 NewFunc > {
00319 static PyObject* mul( PyObject *veca, PyObject *vecb ) {
00320 if( CheckFunc( veca ) && CheckFunc( vecb ) ) {
00321 Type a = ValueFunc( veca );
00322 Type b = ValueFunc( vecb );
00323 return NewFunc( a * b );
00324 } else {
00325 return PyNumberTypeWrapperBase< Type,
00326 TypeObject,
00327 NameFunc,
00328 CheckFunc,
00329 ValueFunc,
00330 NewFunc >::mul( veca, vecb );
00331 }
00332 }
00333 };
00334
00335 }
00336
00337 #endif