[pygccxml-commit] SF.net SVN: pygccxml: [715] pyplusplus_dev/pyplusplus
Brought to you by:
mbaas,
roman_yakovenko
From: <rom...@us...> - 2006-11-13 19:55:40
|
Revision: 715 http://svn.sourceforge.net/pygccxml/?rev=715&view=rev Author: roman_yakovenko Date: 2006-11-12 01:53:26 -0800 (Sun, 12 Nov 2006) Log Message: ----------- adding reference to declaration to transformers Modified Paths: -------------- pyplusplus_dev/pyplusplus/decl_wrappers/calldef_wrapper.py pyplusplus_dev/pyplusplus/function_transformers/__init__.py pyplusplus_dev/pyplusplus/function_transformers/function_transformation.py pyplusplus_dev/pyplusplus/function_transformers/substitution_manager.py pyplusplus_dev/pyplusplus/function_transformers/transformer.py pyplusplus_dev/pyplusplus/function_transformers/transformers.py Modified: pyplusplus_dev/pyplusplus/decl_wrappers/calldef_wrapper.py =================================================================== --- pyplusplus_dev/pyplusplus/decl_wrappers/calldef_wrapper.py 2006-11-12 07:23:32 UTC (rev 714) +++ pyplusplus_dev/pyplusplus/decl_wrappers/calldef_wrapper.py 2006-11-12 09:53:26 UTC (rev 715) @@ -123,7 +123,7 @@ args is a list of transformers """ - self.transformations.append( ft.function_transformation_t( args, **keywd ) ) + self.transformations.append( ft.function_transformation_t( self, args, **keywd ) ) def _exportable_impl_derived( self ): return '' Modified: pyplusplus_dev/pyplusplus/function_transformers/__init__.py =================================================================== --- pyplusplus_dev/pyplusplus/function_transformers/__init__.py 2006-11-12 07:23:32 UTC (rev 714) +++ pyplusplus_dev/pyplusplus/function_transformers/__init__.py 2006-11-12 09:53:26 UTC (rev 715) @@ -22,16 +22,26 @@ from function_transformation import function_transformation_t def output( *args, **keywd ): - return transformers.output_t( *args, **keywd ) + def creator( function ): + return transformers.output_t( function, *args, **keywd ) + return creator def input( *args, **keywd ): - return transformers.input_t( *args, **keywd ) + def creator( function ): + return transformers.input_t( function, *args, **keywd ) + return creator def inout( *args, **keywd ): - return transformers.inout_t( *args, **keywd ) + def creator( function ): + return transformers.inout_t( function, *args, **keywd ) + return creator def input_array( *args, **keywd ): - return transformers.input_array_t( *args, **keywd ) + def creator( function ): + return transformers.input_array_t( function, *args, **keywd ) + return creator def output_array( *args, **keywd ): - return transformers.output_array_t( *args, **keywd ) + def creator( function ): + return transformers.output_array_t( function, *args, **keywd ) + return creator Modified: pyplusplus_dev/pyplusplus/function_transformers/function_transformation.py =================================================================== --- pyplusplus_dev/pyplusplus/function_transformers/function_transformation.py 2006-11-12 07:23:32 UTC (rev 714) +++ pyplusplus_dev/pyplusplus/function_transformers/function_transformation.py 2006-11-12 09:53:26 UTC (rev 715) @@ -9,10 +9,10 @@ class function_transformation_t: - def __init__(self, transformers, **keywd): - """Constructor. - """ - self.__transformers = list(transformers) + def __init__(self, function, transformer_creator, **keywd): + """Constructor. """ + self.__function = function + self.__transformers = map( lambda tr_creator: tr_creator( function ), transformer_creator ) self.__thread_safe = keywd.get( 'thread_safe', False ) @property @@ -27,4 +27,4 @@ @property def thread_safe( self ): - return self.__thread_safe \ No newline at end of file + return self.__thread_safe Modified: pyplusplus_dev/pyplusplus/function_transformers/substitution_manager.py =================================================================== --- pyplusplus_dev/pyplusplus/function_transformers/substitution_manager.py 2006-11-12 07:23:32 UTC (rev 714) +++ pyplusplus_dev/pyplusplus/function_transformers/substitution_manager.py 2006-11-12 09:53:26 UTC (rev 715) @@ -12,6 +12,7 @@ from pygccxml import declarations from code_manager import code_manager_t, wrapper_code_manager_t from transformer import transformer_t +from transformer import return_ # substitution_manager_t class substitution_manager_t: @@ -262,7 +263,7 @@ """ # Append the default return_virtual_result_t code modifier - transformers = self.transformers+[return_virtual_result_t()] + transformers = self.transformers+[return_virtual_result_t(self.decl)] for cb in transformers: if hasattr(cb, "init_funcs"): @@ -361,6 +362,7 @@ return arg + # insert_arg def insert_arg(self, idx, arg, inputexpr): """Insert a new argument into the argument list of the wrapper function. @@ -475,8 +477,8 @@ to the list of code blocks inside the substitution_manager_t class. """ - def __init__(self): - transformer_t.__init__(self) + def __init__(self, function): + transformer_t.__init__(self, function) self.result_var = "<not initialized>" def __str__(self): Modified: pyplusplus_dev/pyplusplus/function_transformers/transformer.py =================================================================== --- pyplusplus_dev/pyplusplus/function_transformers/transformer.py 2006-11-12 07:23:32 UTC (rev 714) +++ pyplusplus_dev/pyplusplus/function_transformers/transformer.py 2006-11-12 09:53:26 UTC (rev 715) @@ -11,8 +11,11 @@ import sys, os.path, copy, re, types from pygccxml import declarations, parser +return_ = -1 class transformer_t: + USE_1_BASED_INDEXING = False + """Base class for a function transformer. This class specifies the interface that a user written transformer @@ -23,19 +26,39 @@ @author: Matthias Baas """ - def __init__(self): + def __init__(self, function): """Constructor. """ - pass + self.__function = function + @property + def function( self ): + """reference to the function, for which a wrapper will be generated""" + return self.__function + def required_headers( self ): """Returns list of header files that transformer generated code depends on.""" return [] - def validate( self, function ): - """returns error message or None""" - raise NotImplementedError() + def get_argument( self, reference ): + if isinstance( reference, str ): + found = filter( lambda arg: arg.name == reference, self.function.arguments ) + if len( found ) == 1: + return found[0] + raise RuntimeError( "Argument with %s was not found" % reference ) + else: + assert isinstance( reference, int ) + if transformer_t.USE_1_BASED_INDEXING: + reference += 1 + return self.function.arguments[ reference ] + def get_type( self, reference ): + global return_ + if isinstance( reference, int ) and reference == return_: + return self.function.return_type + else: + return self.get_argument( reference ).type + def init_funcs(self, sm): """Wrapper initialization. @@ -114,4 +137,4 @@ def virtual_cleanup(self, sm): pass - \ No newline at end of file + Modified: pyplusplus_dev/pyplusplus/function_transformers/transformers.py =================================================================== --- pyplusplus_dev/pyplusplus/function_transformers/transformers.py 2006-11-12 07:23:32 UTC (rev 714) +++ pyplusplus_dev/pyplusplus/function_transformers/transformers.py 2006-11-12 09:53:26 UTC (rev 715) @@ -30,53 +30,52 @@ void getValue(int& v) -> v = getValue() """ - def __init__(self, idx): - transformer.transformer_t.__init__( self ) + def __init__(self, function, arg_ref): + transformer.transformer_t.__init__( self, function ) """Constructor. The specified argument must be a reference or a pointer. - @param idx: Index of the argument that is an output value (the first arg has index 1). - @type idx: int + @param arg_ref: Index of the argument that is an output value (the first arg has index 1). + @type arg_ref: int """ - self.idx = idx + self.arg = self.get_argument( arg_ref ) + self.arg_index = self.function.arguments.index( self.arg ) self.local_var = "<not initialized>" + if not declarations.is_pointer( self.arg.type ) and not declarations.is_reference( self.arg.type ): + raise ValueError( '%s\nin order to use "output" transformation, argument %s type must be a reference or a pointer (got %s).' ) \ + % ( function, self.arg_ref.name, arg.type) + def __str__(self): - return "output(%d)"%(self.idx) + return "output(%d)"%(self.arg_index) def init_funcs(self, sm): # Remove the specified output argument from the wrapper function - arg = sm.remove_arg(self.idx) + sm.remove_arg(self.arg_index+1) - # Do some sanity checking (whether the argument can actually be - # an output argument, i.e. it has to be a reference or a pointer) - reftype = arg.type - if not declarations.is_pointer( reftype ) and not declarations.is_reference( reftype ): - raise ValueError, '%s\nOutput variable %d ("%s") must be a reference or a pointer (got %s)'%(sm.decl, self.idx, arg.name, arg.type) - # Declare a local variable that will receive the output value - self.local_var = sm.wrapper_func.declare_variable(arg.name, str(reftype.base)) + self.local_var = sm.wrapper_func.declare_variable( self.arg.name, str(self.arg.type.base) ) # Append the output to the result tuple sm.wrapper_func.result_exprs.append(self.local_var) # Replace the expression in the C++ function call - if isinstance(reftype, declarations.pointer_t): - sm.wrapper_func.input_params[self.idx-1] = "&%s"%self.local_var - else: - sm.wrapper_func.input_params[self.idx-1] = self.local_var + input_param = self.local_var + if declarations.is_pointer( self.arg.type ): + input_param = "&%s" % self.local_var + + sm.wrapper_func.input_params[self.arg_index] = input_param def virtual_post_call(self, sm): - """Extract the C++ value after the call to the Python function. - """ - arg = sm.virtual_func.arg_list[self.idx-1] - res = "// Extract the C++ value for output argument '%s' (index: %d)\n"%(arg.name, self.idx) - if isinstance(arg.type, declarations.pointer_t): - res += "*" - res += "%s = boost::python::extract<%s>(%s);"%(arg.name, arg.type.base, sm.py_result_expr(self.local_var)) - return res - + """Extract the C++ value after the call to the Python function.""" + res = [] + if declarations.is_pointer( self.arg.type ): + res.append( "*" ) + res.append( "%s = boost::python::extract<%s>(%s);" \ + % ( self.arg.name, self.arg.type.base, sm.py_result_expr(self.local_var) ) ) + return ''.join( res ) + # input_t class input_t(transformer.transformer_t): """Handles a single input variable. @@ -86,7 +85,7 @@ void setValue(int& v) -> setValue(v) """ - def __init__(self, idx): + def __init__(self, function, idx): """Constructor. The specified argument must be a reference or a pointer. @@ -94,8 +93,8 @@ @param idx: Index of the argument that is an output value (the first arg has index 1). @type idx: int """ - transformer.transformer_t.__init__( self ) - self.idx = idx + transformer.transformer_t.__init__( self, function ) + self.idx = idx + 1 def __str__(self): return "input(%d)"%(self.idx) @@ -121,7 +120,7 @@ void foo(int& v) -> v = foo(v) """ - def __init__(self, idx): + def __init__(self, function, idx): """Constructor. The specified argument must be a reference or a pointer. @@ -129,8 +128,8 @@ @param idx: Index of the argument that is an in/out value (the first arg has index 1). @type idx: int """ - transformer.transformer_t.__init__( self ) - self.idx = idx + transformer.transformer_t.__init__( self, function ) + self.idx = idx + 1 self.local_var = "<not initialized>" def __str__(self): @@ -182,7 +181,7 @@ # v must be a sequence of 3 floats """ - def __init__(self, idx, size): + def __init__(self, function, idx, size): """Constructor. @param idx: Index of the argument that is an input array (the first arg has index 1). @@ -190,8 +189,8 @@ @param size: The fixed size of the input array @type size: int """ - transformer.transformer_t.__init__( self ) - self.idx = idx + transformer.transformer_t.__init__( self, function ) + self.idx = idx + 1 self.size = size self.argname = None @@ -264,7 +263,7 @@ # v will be a list with 3 floats """ - def __init__(self, idx, size): + def __init__(self, function, idx, size): """Constructor. @param idx: Index of the argument that is an output array (the first arg has index 1). @@ -272,8 +271,8 @@ @param size: The fixed size of the output array @type size: int """ - transformer.transformer_t.__init__( self ) - self.idx = idx + transformer.transformer_t.__init__( self, function ) + self.idx = idx + 1 self.size = size self.argname = None @@ -335,4 +334,4 @@ , 'size' : self.size , 'array_name' : self.argname } - \ No newline at end of file + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |