PyTypeWrapper.h

Go to the documentation of this file.
00001 
00003 //    Copyright 2004, SenseGraphics AB
00004 //
00005 //    This file is part of H3D API.
00006 //
00007 //    H3D API is free software; you can redistribute it and/or modify
00008 //    it under the terms of the GNU General Public License as published by
00009 //    the Free Software Foundation; either version 2 of the License, or
00010 //    (at your option) any later version.
00011 //
00012 //    H3D API is distributed in the hope that it will be useful,
00013 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 //    GNU General Public License for more details.
00016 //
00017 //    You should have received a copy of the GNU General Public License
00018 //    along with H3D API; if not, write to the Free Software
00019 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 //
00021 //    A commercial license is also available. Please contact us at 
00022 //    www.sensegraphics.com for more information.
00023 //
00024 //
00029 //
00031 
00032 #ifdef HAVE_PYTHON
00033 
00034 #if defined(_MSC_VER)
00035 // undefine _DEBUG since we want to always link to the release version of
00036 // python and pyconfig.h automatically links debug version if _DEBUG is
00037 // defined.
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 // redefine _DEBUG if it was undefed
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; // THROW ERROR!?
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           // int type
00192         } else if( PyInt_Check( vecb ) ) {
00193           Type c = a * PyInt_AsLong( vecb );
00194           return NewFunc( c );
00195           // long type
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         // float type
00203         if( PyFloat_Check( veca ) ) {
00204           Type c = PyFloat_AsDouble( veca ) * b;
00205           return NewFunc( c );
00206           // int type
00207         } else if( PyInt_Check( veca ) ) {
00208           Type c = PyInt_AsLong( veca ) * b;
00209           return NewFunc( c );
00210           // long type
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         // float type
00224         if( PyFloat_Check( floatb ) ) {
00225           Type c = a / PyFloat_AsDouble( floatb );
00226           return NewFunc( c );
00227           // int type
00228         } else if( PyInt_Check( floatb ) ) {
00229           Type c = a / PyInt_AsLong( floatb );
00230           return NewFunc( c );
00231           // long type
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 // HAVE_PYTHON

Generated on Thu Aug 24 12:38:34 2006 for H3D API by  doxygen 1.4.5