38 #if defined _DEBUG && ! defined HAVE_PYTHON_DEBUG_LIBRARY
39 #define _DEBUG_UNDEFED
53 #if defined(__APPLE__) && defined(__MACH__) && defined( HAVE_PYTHON_OSX_FRAMEWORK )
54 #include <Python/Python.h>
87 PyTypeObject *TypeObject,
88 string ( *NameFunc ) (),
89 bool ( *CheckFunc )( PyObject * ),
90 Type (*ValueFunc)( PyObject *),
91 PyObject *(*NewFunc)(
const Type &) >
97 if (PyType_Ready( TypeObject ) < 0 )
100 Py_INCREF( TypeObject );
101 PyModule_AddObject( _H3D_module,
102 (
char *)NameFunc().c_str(),
103 (PyObject *)TypeObject );
110 return PyType_GenericAlloc( TypeObject, 1 );
115 self->ob_type->tp_free(
self );
119 static PyObject*
repr( PyObject *myself, PyObject * ) {
120 if( CheckFunc( myself ) ) {
122 s << ValueFunc( myself );
123 return PyString_FromString( s.str().c_str() );
126 s <<
"PyObject * is not a " << NameFunc() <<
"*";
134 static int compare( PyObject *veca, PyObject *vecb ) {
135 if( CheckFunc( veca ) && CheckFunc( vecb ) ){
136 Type a = ValueFunc( veca );
137 Type b = ValueFunc( vecb );
141 s <<
"PyObject * is not a " << NameFunc() <<
"*";
146 #if PY_MAJOR_VERSION >= 3
147 static PyObject* richCompare( PyObject *lhs, PyObject *rhs,
int operation ) {
148 switch( operation ) {
151 if( !compare( lhs, rhs ) ) {
152 if( operation == Py_EQ ) {
158 if( operation == Py_EQ ) {
166 Py_RETURN_NOTIMPLEMENTED;
187 template<
class Type,
188 PyTypeObject *TypeObject,
189 string ( *NameFunc ) (),
190 bool ( *CheckFunc )( PyObject * ),
191 Type (*ValueFunc)( PyObject *),
192 PyObject *(*NewFunc)(
const Type &) >
202 static PyObject*
add( PyObject *veca, PyObject *vecb ) {
203 if( CheckFunc( veca ) && CheckFunc( vecb ) ){
204 Type c = ValueFunc( veca ) + ValueFunc( vecb );
207 Py_INCREF( Py_NotImplemented );
208 return Py_NotImplemented;
212 static PyObject*
sub( PyObject *veca, PyObject *vecb ) {
213 if( CheckFunc( veca ) && CheckFunc( vecb ) ){
214 Type c = ValueFunc( veca ) - ValueFunc( vecb );
217 Py_INCREF( Py_NotImplemented );
218 return Py_NotImplemented;
222 static PyObject*
mul( PyObject *veca, PyObject *vecb ) {
223 if( CheckFunc( veca ) ) {
224 Type a = ValueFunc( veca );
225 if( PyFloat_Check( vecb ) ) {
226 Type c = a * PyFloat_AsDouble( vecb );
229 }
else if( PyInt_Check( vecb ) ) {
230 Type c = a * PyInt_AsLong( vecb );
233 }
else if( PyLong_Check( vecb ) ) {
234 Type c = a * PyLong_AsLong( vecb );
237 }
else if( CheckFunc( vecb ) ) {
238 Type b = ValueFunc( vecb );
240 if( PyFloat_Check( veca ) ) {
241 Type c = PyFloat_AsDouble( veca ) * b;
244 }
else if( PyInt_Check( veca ) ) {
245 Type c = PyInt_AsLong( veca ) * b;
248 }
else if( PyLong_Check( veca ) ) {
249 Type c = PyLong_AsLong( veca ) * b;
253 Py_INCREF( Py_NotImplemented );
254 return Py_NotImplemented;
258 static PyObject*
div( PyObject *veca, PyObject *floatb ) {
259 if( CheckFunc( veca ) ) {
260 Type a = ValueFunc( veca );
262 if( PyFloat_Check( floatb ) ) {
263 Type c = a / PyFloat_AsDouble( floatb );
266 }
else if( PyInt_Check( floatb ) ) {
267 Type c = a / PyInt_AsLong( floatb );
270 }
else if( PyLong_Check( floatb ) ) {
271 Type c = a / PyLong_AsLong( floatb );
275 Py_INCREF( Py_NotImplemented );
276 return Py_NotImplemented;
280 static PyObject*
neg( PyObject *vec ) {
281 if( CheckFunc( vec ) ){
282 Type c = -ValueFunc( vec );
285 Py_INCREF( Py_NotImplemented );
286 return Py_NotImplemented;
303 template<
class Type,
304 PyTypeObject *TypeObject,
305 string ( *NameFunc ) (),
306 bool ( *CheckFunc )( PyObject * ),
307 Type (*ValueFunc)( PyObject *),
308 PyObject *(*NewFunc)(
const Type &) >
317 static PyObject*
mul( PyObject *veca, PyObject *vecb ) {
318 if( CheckFunc( veca ) && CheckFunc( vecb ) ) {
319 Type a = ValueFunc( veca );
320 Type b = ValueFunc( vecb );
321 return PyFloat_FromDouble( a * b );
328 NewFunc >::mul( veca, vecb );
345 template<
class Type,
346 PyTypeObject *TypeObject,
347 string ( *NameFunc ) (),
348 bool ( *CheckFunc )( PyObject * ),
349 Type (*ValueFunc)( PyObject *),
350 PyObject *(*NewFunc)(
const Type &) >
359 static PyObject*
mul( PyObject *veca, PyObject *vecb ) {
360 if( CheckFunc( veca ) && CheckFunc( vecb ) ) {
361 Type a = ValueFunc( veca );
362 Type b = ValueFunc( vecb );
363 return NewFunc( a * b );
370 NewFunc >::mul( veca, vecb );
#define H3D_FULL_LOCATION
H3D API namespace.
Definition: Anchor.h:38
PyNumberTypeWrapperBase is a template wrapper class to create new Python types from types already exi...
Definition: PyTypeWrapper.h:199
static PyObject * sub(PyObject *veca, PyObject *vecb)
Performs subtraction between two instances.
Definition: PyTypeWrapper.h:212
static PyObject * neg(PyObject *vec)
Negation.
Definition: PyTypeWrapper.h:280
static PyObject * div(PyObject *veca, PyObject *floatb)
Performs division with a float, long or int instance.
Definition: PyTypeWrapper.h:258
static PyObject * mul(PyObject *veca, PyObject *vecb)
Performs multiplication between a instance of the type and a float, long or int.
Definition: PyTypeWrapper.h:222
static PyObject * add(PyObject *veca, PyObject *vecb)
Performs addition between two instances.
Definition: PyTypeWrapper.h:202
PyNumberTypeWrapper is the same as PyNumberTypeWrapperBase with the difference that its mul function ...
Definition: PyTypeWrapper.h:357
static PyObject * mul(PyObject *veca, PyObject *vecb)
Performs multiplacation.
Definition: PyTypeWrapper.h:359
PyTypeWrapper is a template wrapper class to create new Python types from types already existing in t...
Definition: PyTypeWrapper.h:92
static PyObject * repr(PyObject *myself, PyObject *)
Converts to a char* string.
Definition: PyTypeWrapper.h:119
static void dealloc(PyObject *self)
Python type deallocation.
Definition: PyTypeWrapper.h:114
static PyObject * create()
create() a new instance of Type using Python to allocate the memory and initialise the PyObject heade...
Definition: PyTypeWrapper.h:109
static int compare(PyObject *veca, PyObject *vecb)
Test if two PyTypes are equal or not.
Definition: PyTypeWrapper.h:134
static void installType(PyObject *_H3D_module)
Install type in the given python module.
Definition: PyTypeWrapper.h:96
Base class for all Python C type wrappers.
Definition: PyTypeWrapper.h:71
PyVecTypeWrapper is the same as PyNumberTypeWrapperBase with the difference that its mul function als...
Definition: PyTypeWrapper.h:315
static PyObject * mul(PyObject *veca, PyObject *vecb)
Performs vector dot product of two vector instances.
Definition: PyTypeWrapper.h:317