[pygccxml-commit] SF.net SVN: pygccxml: [749] pyplusplus_dev_ft
Brought to you by:
mbaas,
roman_yakovenko
From: <rom...@us...> - 2006-11-23 08:09:54
|
Revision: 749 http://svn.sourceforge.net/pygccxml/?rev=749&view=rev Author: roman_yakovenko Date: 2006-11-23 00:09:53 -0800 (Thu, 23 Nov 2006) Log Message: ----------- fixing few bugs + adding missing transformers Modified Paths: -------------- pyplusplus_dev_ft/pyplusplus/code_creators/calldef_transformed.py pyplusplus_dev_ft/pyplusplus/code_repository/convenience.py pyplusplus_dev_ft/pyplusplus/function_transformers/__init__.py pyplusplus_dev_ft/pyplusplus/function_transformers/transformers.py pyplusplus_dev_ft/unittests/data/function_transformations_to_be_exported.hpp pyplusplus_dev_ft/unittests/function_transformations_tester.py Modified: pyplusplus_dev_ft/pyplusplus/code_creators/calldef_transformed.py =================================================================== --- pyplusplus_dev_ft/pyplusplus/code_creators/calldef_transformed.py 2006-11-22 15:20:49 UTC (rev 748) +++ pyplusplus_dev_ft/pyplusplus/code_creators/calldef_transformed.py 2006-11-23 08:09:53 UTC (rev 749) @@ -14,6 +14,9 @@ import pyplusplus.function_transformers as function_transformers from pyplusplus import code_repository +#TODO: constructors also can have transformation defined. We should use make _init +# function for this purpose + __REMOVE_DUPLICAET_LINESEP = re.compile( '(%s){2,}' % os.linesep ) def remove_duplicate_linesep( code ): return __REMOVE_DUPLICAET_LINESEP.sub( os.linesep, code ) @@ -74,7 +77,7 @@ this_arg_type = declarations.reference_t( this_arg_type ) self.__this_arg = declarations.argument_t( - name=self.ft.controller.register_variable_name( 'self' ) + name=self.ft.controller.register_variable_name( 'inst' ) , type=this_arg_type ) @property @@ -83,8 +86,8 @@ def function_type(self): return declarations.free_function_type_t( - return_type=self.declaration.return_type - , arguments_types=[ declarations.dummy_type_t( self.parent.full_name ) ] + return_type=self.ft.controller.wrapper_return_type + , arguments_types=[ self.__this_arg.type ] + map( lambda arg: arg.type, self.ft.controller.wrapper_args ) ) def full_name(self): Modified: pyplusplus_dev_ft/pyplusplus/code_repository/convenience.py =================================================================== --- pyplusplus_dev_ft/pyplusplus/code_repository/convenience.py 2006-11-22 15:20:49 UTC (rev 748) +++ pyplusplus_dev_ft/pyplusplus/code_repository/convenience.py 2006-11-23 08:09:53 UTC (rev 749) @@ -137,6 +137,7 @@ } /*pyplusplus*/ } /*convenience*/ +namespace pyplus_conv = pyplusplus::convenience; #endif//__convenience_pyplusplus_hpp__ Modified: pyplusplus_dev_ft/pyplusplus/function_transformers/__init__.py =================================================================== --- pyplusplus_dev_ft/pyplusplus/function_transformers/__init__.py 2006-11-22 15:20:49 UTC (rev 748) +++ pyplusplus_dev_ft/pyplusplus/function_transformers/__init__.py 2006-11-23 08:09:53 UTC (rev 749) @@ -36,12 +36,12 @@ return transformers.inout_t( function, *args, **keywd ) return creator -#def input_array( *args, **keywd ): - #def creator( function ): - #return transformers.input_array_t( function, *args, **keywd ) - #return creator +def input_array( *args, **keywd ): + def creator( function ): + return transformers.input_array_t( function, *args, **keywd ) + return creator -#def output_array( *args, **keywd ): - #def creator( function ): - #return transformers.output_array_t( function, *args, **keywd ) - #return creator +def output_array( *args, **keywd ): + def creator( function ): + return transformers.output_array_t( function, *args, **keywd ) + return creator Modified: pyplusplus_dev_ft/pyplusplus/function_transformers/transformers.py =================================================================== --- pyplusplus_dev_ft/pyplusplus/function_transformers/transformers.py 2006-11-22 15:20:49 UTC (rev 748) +++ pyplusplus_dev_ft/pyplusplus/function_transformers/transformers.py 2006-11-23 08:09:53 UTC (rev 749) @@ -16,6 +16,7 @@ - L{output_array_t} """ import os +import string import transformer import controllers from pygccxml import declarations @@ -137,169 +138,135 @@ def __str__(self): return "inout(%d)"%(self.arg_index) - def init_funcs(self, sm): + def configure_mem_fun(self, controller): assert isinstance( controller, controllers.mem_fun_controller_t ) w_arg = controller.find_wrapper_arg( self.arg.name ) w_arg.type = remove_ref_or_ptr( self.arg.type ) #adding the variable to return variables list - controller.return_variable( w_arg ) + controller.return_variable( w_arg.name ) -#~ # input_array_t -#~ class input_array_t(transformer.transformer_t): - #~ """Handles an input array with fixed size. +class input_array_t(transformer.transformer_t): + """Handles an input array with fixed size. - #~ void setVec3(double* v) -> setVec3(object v) - #~ # v must be a sequence of 3 floats - #~ """ + void setVec3(double* v) -> setVec3(object v) + # v must be a sequence of 3 floats + """ - #~ def __init__(self, function, arg_ref, array_size): - #~ """Constructor. + def __init__(self, function, arg_ref, size): + """Constructor. - #~ @param size: The fixed size of the input array - #~ @type size: int - #~ """ - #~ transformer.transformer_t.__init__( self, function ) + @param size: The fixed size of the input array + @type size: int + """ + transformer.transformer_t.__init__( self, function ) - #~ self.arg = self.get_argument( arg_ref ) - #~ self.arg_index = self.function.arguments.index( self.arg ) + self.arg = self.get_argument( arg_ref ) + self.arg_index = self.function.arguments.index( self.arg ) + + if not is_ptr_or_array( self.arg.type ): + raise ValueError( '%s\nin order to use "input_array" transformation, argument %s type must be a array or a pointer (got %s).' ) \ + % ( function, self.arg_ref.name, arg.type) - #~ if not is_ptr_or_array( self.arg.type ): - #~ raise ValueError( '%s\nin order to use "input_array" transformation, argument %s type must be a array or a pointer (got %s).' ) \ - #~ % ( function, self.arg_ref.name, arg.type) + self.array_size = size + self.array_item_type = declarations.array_item_type( self.arg.type ) - #~ self.array_size = array_size - #~ self.native_array = None - #~ self.pylist = None + def __str__(self): + return "input_array(%s,%d)"%( self.arg.name, self.array_size) - #~ def __str__(self): - #~ return "input_array(%s,%d)"%( self.arg.name, self.array_size) + def required_headers( self ): + """Returns list of header files that transformer generated code depends on.""" + return [ code_repository.convenience.file_name ] - #~ def required_headers( self ): - #~ """Returns list of header files that transformer generated code depends on.""" - #~ return [ code_repository.convenience.file_name ] + @property + def pre_call_tmpl( self ): + pre_call = [] + pre_call.append( 'pyplus_conv::ensure_uniform_sequence< $type >( $pylist, $array_size );' ) + pre_call.append( 'pyplus_conv::copy_sequence( $pylist, pyplus_conv::array_inserter( $native_array, $array_size ) );' ) + return string.Template( os.linesep.join( pre_call ) ) - #~ def init_funcs(self, sm): - #~ # Remove the original argument... - #~ sm.remove_arg(self.arg_index + 1) + def configure_mem_fun(self, controller): + assert isinstance( controller, controllers.mem_fun_controller_t ) + + w_arg = controller.find_wrapper_arg( self.arg.name ) + w_arg.type = declarations.dummy_type_t( "boost::python::object" ) - #~ # Declare a variable that will hold the Python list - #~ # (this will be the input of the Python call in the virtual function) - #~ self.pylist = sm.virtual_func.declare_variable("py_" + self.arg.name, "boost::python::list") + # Declare a variable that will hold the C array... + native_array = controller.declare_variable( self.array_item_type + , "native_" + self.arg.name + , '[%d]' % self.array_size ) + + seq2arr = self.pre_call_tmpl.substitute( type=self.array_item_type + , pylist=w_arg.name + , array_size=self.array_size + , native_array=native_array ) + + controller.add_pre_call_code( seq2arr ) + + controller.modify_argument_expression( self.arg_index, native_array ) - #~ # Replace the removed argument with a Python object. - #~ newarg = self.arg.clone( type=declarations.dummy_type_t("boost::python::object") ) - #~ sm.insert_arg(self.arg_index+1, newarg, self.pylist) - #~ # Declare a variable that will hold the C array... - #~ self.native_array = sm.wrapper_func.declare_variable( - #~ "native_" + self.arg.name - #~ , declarations.array_item_type( self.arg.type ) - #~ , '[%d]' % self.array_size) +# output_array_t +class output_array_t(transformer.transformer_t): + """Handles an output array of a fixed size. - #~ # Replace the input parameter with the C array - #~ sm.wrapper_func.input_params[self.arg_index] = self.native_array + void getVec3(double* v) -> v = getVec3() + # v will be a list with 3 floats + """ - #~ def wrapper_pre_call(self, sm): - #~ """Wrapper function code. - #~ """ - #~ tmpl = [] - #~ tmpl.append( '%(pypp_con)s::ensure_uniform_sequence< %(type)s >( %(pylist)s, %(array_size)d );' ) - #~ tmpl.append( '%(pypp_con)s::copy_sequence( %(pylist)s, %(pypp_con)s::array_inserter( %(native_array)s, %(array_size)d ) );' ) - #~ return os.linesep.join( tmpl ) % { - #~ 'type' : declarations.array_item_type( self.arg.type ) - #~ , 'pypp_con' : 'pyplusplus::convenience' - #~ , 'pylist' : self.arg.name - #~ , 'array_size' : self.array_size - #~ , 'native_array' : self.native_array - #~ } + def __init__(self, function, arg_ref, size): + """Constructor. - #~ def virtual_pre_call(self, sm): - #~ """Virtual function code.""" - #~ tmpl = '%(pypp_con)s::copy_container( %(native_array)s, %(native_array)s + %(array_size)d, %(pypp_con)s::list_inserter( %(pylist)s ) );' - #~ return tmpl % { - #~ 'pypp_con' : 'pyplusplus::convenience' - #~ , 'native_array' : self.arg.name - #~ , 'array_size' : self.array_size - #~ , 'pylist' : self.pylist - #~ } + @param idx: Index of the argument that is an output array (the first arg has index 1). + @type idx: int + @param size: The fixed size of the output array + @type size: int + """ + transformer.transformer_t.__init__( self, function ) + self.arg = self.get_argument( arg_ref ) + self.arg_index = self.function.arguments.index( self.arg ) + if not is_ptr_or_array( self.arg.type ): + raise ValueError( '%s\nin order to use "input_array" transformation, argument %s type must be a array or a pointer (got %s).' ) \ + % ( function, self.arg_ref.name, arg.type) -#~ # output_array_t -#~ class output_array_t(transformer.transformer_t): - #~ """Handles an output array of a fixed size. + self.array_size = size + self.array_item_type = declarations.array_item_type( self.arg.type ) - #~ void getVec3(double* v) -> v = getVec3() - #~ # v will be a list with 3 floats - #~ """ + def __str__(self): + return "output_array(%s,%d)"%( self.arg.name, self.array_size) - #~ def __init__(self, function, arg_ref, size): - #~ """Constructor. + def required_headers( self ): + """Returns list of header files that transformer generated code depends on.""" + return [ code_repository.convenience.file_name ] - #~ @param idx: Index of the argument that is an output array (the first arg has index 1). - #~ @type idx: int - #~ @param size: The fixed size of the output array - #~ @type size: int - #~ """ - #~ transformer.transformer_t.__init__( self, function ) - #~ self.arg = self.get_argument( arg_ref ) - #~ self.arg_index = self.function.arguments.index( self.arg ) + @property + def post_call_tmpl( self ): + return string.Template( 'pyplus_conv::copy_container( $native_array, $native_array + $array_size, pyplus_conv::list_inserter( $pylist ) );' ) - #~ if not is_ptr_or_array( self.arg.type ): - #~ raise ValueError( '%s\nin order to use "input_array" transformation, argument %s type must be a array or a pointer (got %s).' ) \ - #~ % ( function, self.arg_ref.name, arg.type) + def configure_mem_fun(self, controller): + assert isinstance( controller, controllers.mem_fun_controller_t ) + #removing arg from the function wrapper definition + controller.remove_wrapper_arg( self.arg.name ) - #~ self.array_size = size - #~ self.native_array = None - #~ self.pylist = None + # Declare a variable that will hold the C array... + native_array = controller.declare_variable( self.array_item_type + , "native_" + self.arg.name + , '[%d]' % self.array_size ) - #~ def __str__(self): - #~ return "output_array(%s,%d)"%( self.arg.name, self.array_size) + #adding just declared variable to the original function call expression + controller.modify_argument_expression( self.arg_index, native_array ) - #~ def required_headers( self ): - #~ """Returns list of header files that transformer generated code depends on.""" - #~ return [ code_repository.convenience.file_name ] - - #~ def init_funcs(self, sm): - #~ # Remove the original argument... - #~ sm.remove_arg(self.arg_index + 1) - - #~ # Declare a variable that will hold the C array... - #~ self.native_array = sm.wrapper_func.declare_variable( - #~ "native_" + self.arg.name - #~ , declarations.array_item_type( self.arg.type ) - #~ , '[%d]' % self.array_size) - - #~ # Declare a Python list which will receive the output... - #~ self.pylist = sm.wrapper_func.declare_variable( - #~ 'py_' + self.arg.name - #~ , "boost::python::list" ) + # Declare a Python list which will receive the output... + pylist = controller.declare_variable( declarations.dummy_type_t( "boost::python::list" ) + , 'py_' + self.arg.name ) + + arr2seq = self.post_call_tmpl.substitute( native_array=native_array + , array_size=self.array_size + , pylist=pylist ) - #~ # ...and add it to the result - #~ sm.wrapper_func.result_exprs.append(self.pylist) + controller.add_post_call_code( arr2seq ) - #~ sm.wrapper_func.input_params[ self.arg_index ] = self.native_array + #adding the variable to return variables list + controller.return_variable( pylist ) - #~ self.virtual_pylist = sm.virtual_func.declare_variable("py_"+self.arg.name, "boost::python::object") - - #~ def wrapper_post_call(self, sm): - #~ tmpl = '%(pypp_con)s::copy_container( %(native_array)s, %(native_array)s + %(array_size)d, %(pypp_con)s::list_inserter( %(pylist)s ) );' - #~ return tmpl % { - #~ 'pypp_con' : 'pyplusplus::convenience' - #~ , 'native_array' : self.native_array - #~ , 'array_size' : self.array_size - #~ , 'pylist' : self.pylist - #~ } - - #~ def virtual_post_call(self, sm): - #~ tmpl = [] - #~ tmpl.append( '%(pypp_con)s::ensure_uniform_sequence< %(type)s >( %(pylist)s, %(array_size)d );' ) - #~ tmpl.append( '%(pypp_con)s::copy_sequence( %(pylist)s, %(pypp_con)s::array_inserter( %(native_array)s, %(array_size)d ) );' ) - #~ return os.linesep.join( tmpl ) % { - #~ 'type' : declarations.array_item_type( self.arg.type ) - #~ , 'pypp_con' : 'pyplusplus::convenience' - #~ , 'pylist' : sm.py_result_expr(self.pylist) - #~ , 'array_size' : self.array_size - #~ , 'native_array' : self.arg.name - #~ } - Modified: pyplusplus_dev_ft/unittests/data/function_transformations_to_be_exported.hpp =================================================================== --- pyplusplus_dev_ft/unittests/data/function_transformations_to_be_exported.hpp 2006-11-22 15:20:49 UTC (rev 748) +++ pyplusplus_dev_ft/unittests/data/function_transformations_to_be_exported.hpp 2006-11-23 08:09:53 UTC (rev 749) @@ -6,18 +6,14 @@ #ifndef __function_transformations_to_be_exported_hpp__ #define __function_transformations_to_be_exported_hpp__ -#include <iostream> - namespace ft2{ //used to check output transformer struct calculator_t{ calculator_t(){ - std::cout << std::endl << "=> calculator_t created"; } virtual ~calculator_t(){ - std::cout << std::endl << "=> calculator_t destroyed" << std::endl; } int assign_0_1_2( int& one, int& two ){ @@ -36,7 +32,7 @@ } }; -//used to check input transformer +//used to check input\inout transformers struct window_t{ window_t() : height( 0 ) @@ -48,11 +44,39 @@ width = w; } + int resize_in_out( int& h, int& w){ + height *= h; + h = height; + width *= w; + w = width; + return h*w; + } + int height; int width; }; - +struct point3d_t{ + point3d_t() + : x( 0 ), y(0), z(0) + {} + + int initialize( int v[3] ){ + x = v[0]; + y = v[1]; + z = v[2]; + return x*y*z; + } + + void position( int v[3] ){ + v[0] = x; + v[1] = y; + v[2] = z; + } + + int x, y, z; +}; + } //~ namespace ft{ Modified: pyplusplus_dev_ft/unittests/function_transformations_tester.py =================================================================== --- pyplusplus_dev_ft/unittests/function_transformations_tester.py 2006-11-22 15:20:49 UTC (rev 748) +++ pyplusplus_dev_ft/unittests/function_transformations_tester.py 2006-11-23 08:09:53 UTC (rev 749) @@ -21,6 +21,9 @@ , *args ) def customize( self, mb ): + + mb.global_ns.calldefs().create_with_signature = True + calc = mb.class_('calculator_t' ) calc.add_wrapper_code( '' ) assign_funs = calc.mem_funs( lambda decl: decl.name.startswith( 'assign' ) ) @@ -33,7 +36,12 @@ window = mb.class_( 'window_t' ) window.add_wrapper_code( '' ) window.mem_fun( 'resize' ).add_transformation( ft.input(0), ft.input(1) ) + window.mem_fun( 'resize_in_out' ).add_transformation( ft.inout(0), ft.inout(1) ) + point3d = mb.class_( 'point3d_t' ) + point3d.add_wrapper_code( '' ) + point3d.mem_fun( 'initialize' ).add_transformation( ft.input_array(0, size=3) ) + point3d.mem_fun( 'position' ).add_transformation( ft.output_array(0, size=3) ) #~ image = mb.class_( "image_t" ) #~ image.include() @@ -75,9 +83,16 @@ window = module.window_t() window.height = 0 window.width = 0 - window.resize( h=1, w=2 ) + window.resize( 1, 2 ) self.failUnless( window.height==1 and window.width==2 ) + square, h, w = window.resize_in_out( 3, 7 ) + self.failUnless( square == 1*3*2*7 and h==3 and w==2*7 ) + self.failUnless( window.height==3 and window.width==2*7 ) + point3d = module.point3d_t() + result = point3d.initialize( [ 1,2,3 ] ) + self.failUnless( result== 1*2*3 and point3d.x == 1 and point3d.y==2 and point3d.z==3 ) + self.failUnless( [1,2,3] == point3d.position()[0] ) #~ img = module.image_t( 2, 6) # Check a method that returns two values by reference This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |