6#ifndef __CXX_Extensions__h
7#define __CXX_Extensions__h
13#pragma warning(disable: 4786)
22 extern PyObject py_object_initializer;
30 class ExtensionModuleBase;
33 class ExtensionExceptionType :
public Object
36 ExtensionExceptionType();
37 virtual ~ExtensionExceptionType();
40 void init( ExtensionModuleBase &module,
const std::string& name );
48 virtual ~MethodTable();
50 void add(
const char* method_name, PyCFunction f,
const char* doc=
"",
int flag=1);
54 std::vector<PyMethodDef> t;
57 static PyMethodDef method (
const char* method_name, PyCFunction f,
int flags = 1,
const char* doc=
"");
63 MethodTable(
const MethodTable& m);
64 void operator=(
const MethodTable& m);
70 typedef PyObject *(*method_varargs_call_handler_t)( PyObject *_self, PyObject *_args );
71 typedef PyObject *(*method_keyword_call_handler_t)( PyObject *_self, PyObject *_args, PyObject *_dict );
75 class MethodDefExt :
public PyMethodDef
78 typedef Object (T::*method_varargs_function_t)( const Tuple &args );
79 typedef Object (T::*method_keyword_function_t)( const Tuple &args, const Dict &kws );
84 method_varargs_function_t _function,
85 method_varargs_call_handler_t _handler,
89 ext_meth_def.ml_name =
const_cast<char *
>(_name);
90 ext_meth_def.ml_meth = _handler;
91 ext_meth_def.ml_flags = METH_VARARGS;
92 ext_meth_def.ml_doc =
const_cast<char *
>(_doc);
94 ext_varargs_function = _function;
95 ext_keyword_function = NULL;
101 method_keyword_function_t _function,
102 method_keyword_call_handler_t _handler,
106 ext_meth_def.ml_name =
const_cast<char *
>(_name);
107 ext_meth_def.ml_meth = method_varargs_call_handler_t( _handler );
108 ext_meth_def.ml_flags = METH_VARARGS|METH_KEYWORDS;
109 ext_meth_def.ml_doc =
const_cast<char *
>(_doc);
111 ext_varargs_function = NULL;
112 ext_keyword_function = _function;
118 PyMethodDef ext_meth_def;
119 method_varargs_function_t ext_varargs_function;
120 method_keyword_function_t ext_keyword_function;
123 class ExtensionModuleBase
126 ExtensionModuleBase(
const char *name );
127 virtual ~ExtensionModuleBase();
129 Module module(
void)
const;
130 Dict moduleDictionary(
void)
const;
132 virtual Object invoke_method_keyword(
const std::string &_name,
const Tuple &_args,
const Dict &_keywords ) = 0;
133 virtual Object invoke_method_varargs(
const std::string &_name,
const Tuple &_args ) = 0;
135 const std::string &
name()
const;
136 const std::string &fullName()
const;
140 void initialize(
const char *module_doc );
142 const std::string module_name;
143 const std::string full_module_name;
144 MethodTable method_table;
151 ExtensionModuleBase(
const ExtensionModuleBase & );
152 void operator=(
const ExtensionModuleBase & );
156 extern "C" PyObject *method_keyword_call_handler( PyObject *_self_and_name_tuple, PyObject *_args, PyObject *_keywords );
157 extern "C" PyObject *method_varargs_call_handler( PyObject *_self_and_name_tuple, PyObject *_args );
158 extern "C" void do_not_dealloc(
void * );
162 class ExtensionModule :
public ExtensionModuleBase
165 ExtensionModule(
const char *name )
166 : ExtensionModuleBase(
name )
168 virtual ~ExtensionModule()
172 typedef Object (T::*method_varargs_function_t)( const Tuple &args );
173 typedef Object (T::*method_keyword_function_t)( const Tuple &args, const Dict &kws );
174 typedef std::map<std::string,MethodDefExt<T> *> method_map_t;
176 static void add_varargs_method(
const char *name, method_varargs_function_t function,
const char *doc=
"" )
178 method_map_t &mm = methods();
180 MethodDefExt<T> *method_definition =
new MethodDefExt<T>
184 method_varargs_call_handler,
188 mm[std::string( name )] = method_definition;
191 static void add_keyword_method(
const char *name, method_keyword_function_t function,
const char *doc=
"" )
193 method_map_t &mm = methods();
195 MethodDefExt<T> *method_definition =
new MethodDefExt<T>
199 method_keyword_call_handler,
203 mm[std::string( name )] = method_definition;
206 void initialize(
const char *module_doc=
"" )
208 ExtensionModuleBase::initialize( module_doc );
209 Dict dict( moduleDictionary() );
215 method_map_t &mm = methods();
216 typename method_map_t::iterator i;
218 for( i=mm.begin(); i != mm.end(); ++i )
220 MethodDefExt<T> *method_definition = (*i).second;
222 static PyObject *self = PyCObject_FromVoidPtr(
this, do_not_dealloc );
225 args[0] = Object( self );
226 args[1] =
String( (*i).first );
228 PyObject *func = PyCFunction_New
230 &method_definition->ext_meth_def,
231 new_reference_to( args )
234 dict[ (*i).first ] = Object( func );
240 static method_map_t &methods(
void)
242 static method_map_t *map_of_methods = NULL;
243 if( map_of_methods == NULL )
244 map_of_methods =
new method_map_t;
246 return *map_of_methods;
251 virtual Object invoke_method_keyword(
const std::string &name,
const Tuple &args,
const Dict &keywords )
253 method_map_t &mm = methods();
254 MethodDefExt<T> *meth_def = mm[
name ];
255 if( meth_def == NULL )
257 std::string error_msg(
"CXX - cannot invoke keyword method named " );
259 throw RuntimeError( error_msg );
263 T *self =
static_cast<T *
>(
this);
265 return (self->*meth_def->ext_keyword_function)( args, keywords );
269 virtual Object invoke_method_varargs(
const std::string &name,
const Tuple &args )
271 method_map_t &mm = methods();
272 MethodDefExt<T> *meth_def = mm[
name ];
273 if( meth_def == NULL )
275 std::string error_msg(
"CXX - cannot invoke varargs method named " );
277 throw RuntimeError( error_msg );
281 T *self =
static_cast<T *
>(
this);
283 return (self->*meth_def->ext_varargs_function)( args );
290 ExtensionModule(
const ExtensionModule<T> & );
291 void operator=(
const ExtensionModule<T> & );
301 PythonType (
size_t base_size,
int itemsize,
const char *default_name );
302 virtual ~PythonType ();
304 const char *getName ()
const;
305 const char *getDoc ()
const;
307 PyTypeObject* type_object ()
const;
308 void name (
const char* nam);
309 void doc (
const char* d);
310 void dealloc(
void (*f)(PyObject*));
312 void supportPrint(
void);
313 void supportGetattr(
void);
314 void supportSetattr(
void);
315 void supportGetattro(
void);
316 void supportSetattro(
void);
317 void supportCompare(
void);
318 void supportRepr(
void);
319 void supportStr(
void);
320 void supportHash(
void);
321 void supportCall(
void);
323 void supportSequenceType(
void);
324 void supportMappingType(
void);
325 void supportNumberType(
void);
326 void supportBufferType(
void);
330 PySequenceMethods *sequence_table;
331 PyMappingMethods *mapping_table;
332 PyNumberMethods *number_table;
333 PyBufferProcs *buffer_table;
335 void init_sequence();
344 PythonType (
const PythonType& tb);
345 void operator=(
const PythonType& t);
371 class PythonExtensionBase :
public PyObject
374 PythonExtensionBase();
375 virtual ~PythonExtensionBase();
378 virtual int print( FILE *,
int );
379 virtual Object getattr(
const char * ) = 0;
380 virtual int setattr(
const char *,
const Object & );
381 virtual Object getattro(
const Object & );
382 virtual int setattro(
const Object &,
const Object & );
383 virtual int compare(
const Object & );
384 virtual Object repr();
385 virtual Object str();
387 virtual Object call(
const Object &,
const Object & );
390 virtual int sequence_length();
391 virtual Object sequence_concat(
const Object & );
392 virtual Object sequence_repeat(
int );
393 virtual Object sequence_item(
int );
394 virtual Object sequence_slice(
int,
int );
395 virtual int sequence_ass_item(
int,
const Object & );
396 virtual int sequence_ass_slice(
int,
int,
const Object & );
399 virtual int mapping_length();
400 virtual Object mapping_subscript(
const Object & );
401 virtual int mapping_ass_subscript(
const Object &,
const Object & );
404 virtual int number_nonzero();
405 virtual Object number_negative();
406 virtual Object number_positive();
407 virtual Object number_absolute();
408 virtual Object number_invert();
409 virtual Object number_int();
410 virtual Object number_float();
411 virtual Object number_long();
412 virtual Object number_oct();
413 virtual Object number_hex();
414 virtual Object number_add(
const Object & );
415 virtual Object number_subtract(
const Object & );
416 virtual Object number_multiply(
const Object & );
417 virtual Object number_divide(
const Object & );
418 virtual Object number_remainder(
const Object & );
419 virtual Object number_divmod(
const Object & );
420 virtual Object number_lshift(
const Object & );
421 virtual Object number_rshift(
const Object & );
422 virtual Object number_and(
const Object & );
423 virtual Object number_xor(
const Object & );
424 virtual Object number_or(
const Object & );
425 virtual Object number_power(
const Object &,
const Object & );
428 virtual int buffer_getreadbuffer(
int,
void** );
429 virtual int buffer_getwritebuffer(
int,
void** );
430 virtual int buffer_getsegcount(
int* );
433 void missing_method(
void );
434 static PyObject *method_call_handler( PyObject *self, PyObject *args );
438 class PythonExtension:
public PythonExtensionBase
441 static PyTypeObject* type_object()
443 return behaviors().type_object();
446 static int check( PyObject *p )
449 return p->ob_type == type_object();
452 static int check(
const Object& ob )
454 return check( ob.ptr());
462 virtual Object getattr(
const char *name )
464 return getattr_methods( name );
468 explicit PythonExtension()
469 : PythonExtensionBase()
472 (void)PyObject_INIT(
this, type_object() );
475 ob_type = type_object();
479 behaviors().supportGetattr();
482 virtual ~PythonExtension()
485 static PythonType &behaviors()
487 static PythonType* p;
490#if defined( _CPPRTTI )
491 const char *default_name = (
typeid ( T )).name();
493 const char *default_name =
"unknown";
495 p =
new PythonType(
sizeof( T ), 0, default_name );
496 p->dealloc( extension_object_deallocator );
503 typedef Object (T::*method_varargs_function_t)( const Tuple &args );
504 typedef Object (T::*method_keyword_function_t)( const Tuple &args, const Dict &kws );
505 typedef std::map<std::string,MethodDefExt<T> *> method_map_t;
508 virtual Object getattr_default(
const char *_name )
510 std::string
name( _name );
512 if( name ==
"__name__" && type_object()->tp_name != NULL )
514 return Py::String( type_object()->tp_name );
516 else if( name ==
"__doc__" && type_object()->tp_doc != NULL )
518 return Py::String( type_object()->tp_doc );
536 return getattr_methods( _name );
541 virtual Object getattr_methods(
const char *_name )
543 std::string
name( _name );
545 method_map_t &mm = methods();
547 if( name ==
"__methods__" )
551 for(
typename method_map_t::iterator i = mm.begin(); i != mm.end(); ++i )
552 methods.append(
String( (*i).first ) );
558 if( mm.find( name ) == mm.end() )
559 throw AttributeError(
"method '" + name +
"' does not exist." );
563 self[0] = Object(
this );
566 MethodDefExt<T> *method_definition = mm[
name ];
568 PyObject *func = PyCFunction_New( &method_definition->ext_meth_def, self.ptr() );
570 return Object(func,
true);
573 static void add_varargs_method(
const char *name, method_varargs_function_t function,
const char *doc=
"" )
575 method_map_t &mm = methods();
577 MethodDefExt<T> *method_definition =
new MethodDefExt<T>
581 method_varargs_call_handler,
585 mm[std::string( name )] = method_definition;
588 static void add_keyword_method(
const char *name, method_keyword_function_t function,
const char *doc=
"" )
590 method_map_t &mm = methods();
592 MethodDefExt<T> *method_definition =
new MethodDefExt<T>
596 method_keyword_call_handler,
600 mm[std::string( name )] = method_definition;
604 static method_map_t &methods(
void)
606 static method_map_t *map_of_methods = NULL;
607 if( map_of_methods == NULL )
608 map_of_methods =
new method_map_t;
610 return *map_of_methods;
613 static PyObject *method_keyword_call_handler( PyObject *_self_and_name_tuple, PyObject *_args, PyObject *_keywords )
617 Tuple self_and_name_tuple( _self_and_name_tuple );
619 PyObject *self_in_cobject = self_and_name_tuple[0].ptr();
620 T *self =
static_cast<T *
>( self_in_cobject );
624 method_map_t &mm = methods();
625 MethodDefExt<T> *meth_def = mm[
name ];
626 if( meth_def == NULL )
633 if( _keywords != NULL )
634 keywords = Dict( _keywords );
636 Object result( (self->*meth_def->ext_keyword_function)( args, keywords ) );
638 return new_reference_to( result.ptr() );
646 static PyObject *method_varargs_call_handler( PyObject *_self_and_name_tuple, PyObject *_args )
650 Tuple self_and_name_tuple( _self_and_name_tuple );
652 PyObject *self_in_cobject = self_and_name_tuple[0].ptr();
653 T *self =
static_cast<T *
>( self_in_cobject );
657 method_map_t &mm = methods();
658 MethodDefExt<T> *meth_def = mm[
name ];
659 if( meth_def == NULL )
670 result = (self->*meth_def->ext_varargs_function)( args );
672 catch (std::__stl_debug_exception)
675 throw cxx::RuntimeError(
"Error message not set yet." );
678 result = (self->*meth_def->ext_varargs_function)( args );
681 return new_reference_to( result.ptr() );
689 static void extension_object_deallocator ( PyObject* t )
697 explicit PythonExtension(
const PythonExtension<T>& other );
698 void operator=(
const PythonExtension<T>& rhs );
705 class ExtensionObject:
public Object
709 explicit ExtensionObject ( PyObject *pyob )
715 ExtensionObject(
const ExtensionObject<T>& other )
721 ExtensionObject(
const Object& other )
727 ExtensionObject& operator= (
const Object& rhs )
729 return (*
this = *rhs );
732 ExtensionObject& operator= ( PyObject* rhsp )
740 virtual bool accepts ( PyObject *pyob )
const
742 return ( pyob && T::check( pyob ));
748 T *extensionObject(
void)
750 return static_cast<T *
>( ptr() );
TDEAction * print(const TQObject *recvr, const char *slot, TDEActionCollection *parent, const char *name=0)
const char * name(StdAction id)
@ List
TQValueList<TQVariant>