[pygccxml-commit] SF.net SVN: pygccxml: [752] pyplusplus_dev_ft
Brought to you by:
mbaas,
roman_yakovenko
From: <rom...@us...> - 2006-11-25 19:25:50
|
Revision: 752 http://svn.sourceforge.net/pygccxml/?rev=752&view=rev Author: roman_yakovenko Date: 2006-11-25 11:25:49 -0800 (Sat, 25 Nov 2006) Log Message: ----------- adding ft for free functions Modified Paths: -------------- pyplusplus_dev_ft/pyplusplus/code_creators/__init__.py pyplusplus_dev_ft/pyplusplus/code_creators/calldef_transformed.py pyplusplus_dev_ft/pyplusplus/function_transformers/__init__.py pyplusplus_dev_ft/pyplusplus/function_transformers/controllers.py pyplusplus_dev_ft/pyplusplus/function_transformers/function_transformation.py pyplusplus_dev_ft/pyplusplus/function_transformers/templates.py pyplusplus_dev_ft/pyplusplus/function_transformers/transformer.py pyplusplus_dev_ft/pyplusplus/function_transformers/transformers.py pyplusplus_dev_ft/pyplusplus/module_creator/creator.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/__init__.py =================================================================== --- pyplusplus_dev_ft/pyplusplus/code_creators/__init__.py 2006-11-23 20:26:16 UTC (rev 751) +++ pyplusplus_dev_ft/pyplusplus/code_creators/__init__.py 2006-11-25 19:25:49 UTC (rev 752) @@ -78,6 +78,9 @@ from calldef_transformed import mem_fun_transformed_t from calldef_transformed import mem_fun_transformed_wrapper_t +from calldef_transformed import free_fun_transformed_t +from calldef_transformed import free_fun_transformed_wrapper_t + #~ from calldef_transformed import mem_fun_v_transformed_t #~ from calldef_transformed import mem_fun_v_transformed_wrapper_t Modified: pyplusplus_dev_ft/pyplusplus/code_creators/calldef_transformed.py =================================================================== --- pyplusplus_dev_ft/pyplusplus/code_creators/calldef_transformed.py 2006-11-23 20:26:16 UTC (rev 751) +++ pyplusplus_dev_ft/pyplusplus/code_creators/calldef_transformed.py 2006-11-25 19:25:49 UTC (rev 752) @@ -21,14 +21,22 @@ def remove_duplicate_linesep( code ): return __REMOVE_DUPLICAET_LINESEP.sub( os.linesep, code ) -class mem_fun_transformed_t( calldef_t ): +class non_virtual_fun_transformed_t( calldef_t ): """Creates code for public non-virtual member functions. """ def __init__( self, function, wrapper=None ): calldef_t.__init__( self, function=function, wrapper=wrapper ) + if isinstance( self.declaration.parent, declarations.namespace_t ): + self.works_on_instance = False #map( lambda transformer: transformer.configure_mem_fun - + + def create_def_code( self ): + if isinstance( self.declaration.parent, declarations.namespace_t ): + return self.def_identifier() + else: + return super( non_virtual_fun_transformed_t, self ).create_def_code() + @property def ft( self ): #function transformation return self.declaration.transformations[0] @@ -65,7 +73,7 @@ return arg_utils.keywords_args() -class mem_fun_transformed_wrapper_t( calldef_wrapper_t ): +class non_virtual_fun_transformed_wrapper_t( calldef_wrapper_t ): def __init__( self, function ): """Constructor. @@ -74,27 +82,26 @@ """ calldef_wrapper_t.__init__( self, function=function ) - this_arg_type = declarations.declarated_t( self.declaration.parent ) - if self.declaration.has_const: - this_arg_type = declarations.const_t( this_arg_type ) - this_arg_type = declarations.reference_t( this_arg_type ) - - self.__this_arg = declarations.argument_t( - name=self.ft.controller.register_variable_name( 'inst' ) - , type=this_arg_type ) - def __is_global( self ): return not isinstance( self.parent, class_declaration.class_wrapper_t ) + def inst_arg( self ): + if isinstance( self.declaration.parent, declarations.class_t ): + return self.ft.controller.inst_arg + else: + return None + @property def ft( self ): #function transformation return self.declaration.transformations[0] def function_type(self): + args = map( lambda arg: arg.type, self.ft.controller.wrapper_args ) + if self.inst_arg(): + args.insert( 0, self.inst_arg().type ) return declarations.free_function_type_t( - return_type=self.ft.controller.wrapper_return_type - , arguments_types=[ self.__this_arg.type ] - + map( lambda arg: arg.type, self.ft.controller.wrapper_args ) ) + return_type=self.ft.controller.wrapper_return_type + , arguments_types=args ) def wrapper_name( self ): if self.__is_global(): @@ -113,10 +120,13 @@ return self.parent.full_name + '::' + self.wrapper_name() def args_declaration( self ): + args = self.ft.controller.wrapper_args[:] + if self.inst_arg(): + args.insert( 0, self.inst_arg() ) arg_utils = calldef_utils.argument_utils_t( self.declaration , algorithm.make_id_creator( self ) - , [ self.__this_arg ] + self.ft.controller.wrapper_args ) + , args ) return arg_utils.args_declaration() def create_declaration(self, name): @@ -171,13 +181,20 @@ pass if return_stmt: return_stmt = 'return ' + return_stmt + ';' + + f_invokation = None + if self.inst_arg(): + f_invokation = self.inst_arg().name + '.' + self.declaration.name + else: + f_invokation = declarations.full_name( self.declaration ) + return cntrl.template.substitute({ 'declare_variables' : os.linesep.join( map( lambda var: var.declare_var_string(), cntrl.variables ) ) , 'pre_call' : os.linesep.join( cntrl.pre_call ) , 'post_call' : post_call , 'save_result' : result_var_assign - , 'function' : self.__this_arg.name + '.' + self.declaration.name + , 'function' : f_invokation , 'arg_expressions' : self.PARAM_SEPARATOR.join( cntrl.arg_expressions ) , 'return' : return_stmt }) @@ -191,6 +208,11 @@ answer.append( '}' ) return os.linesep.join( answer ) +mem_fun_transformed_t = non_virtual_fun_transformed_t +mem_fun_transformed_wrapper_t = non_virtual_fun_transformed_wrapper_t +free_fun_transformed_t = non_virtual_fun_transformed_t +free_fun_transformed_wrapper_t = non_virtual_fun_transformed_wrapper_t + #~ ###################################################################### #~ class mem_fun_v_transformed_t( calldef_t ): Modified: pyplusplus_dev_ft/pyplusplus/function_transformers/__init__.py =================================================================== --- pyplusplus_dev_ft/pyplusplus/function_transformers/__init__.py 2006-11-23 20:26:16 UTC (rev 751) +++ pyplusplus_dev_ft/pyplusplus/function_transformers/__init__.py 2006-11-25 19:25:49 UTC (rev 752) @@ -44,4 +44,4 @@ def output_array( *args, **keywd ): def creator( function ): return transformers.output_array_t( function, *args, **keywd ) - return creator + return creator \ No newline at end of file Modified: pyplusplus_dev_ft/pyplusplus/function_transformers/controllers.py =================================================================== --- pyplusplus_dev_ft/pyplusplus/function_transformers/controllers.py 2006-11-23 20:26:16 UTC (rev 751) +++ pyplusplus_dev_ft/pyplusplus/function_transformers/controllers.py 2006-11-25 19:25:49 UTC (rev 752) @@ -63,8 +63,8 @@ def apply( self, transformations ): raise NotImplementedError() -class mem_fun_controller_t( controller_base_t ): - +class non_virtual_fun_controller_t( controller_base_t ): + #base class for free and member function controllers def __init__( self, function ): controller_base_t.__init__( self, function ) self.__wrapper_args = [ arg.clone() for arg in function.arguments ] @@ -75,9 +75,6 @@ self.__post_call = [] self.__arg_expressions = [ arg.name for arg in function.arguments ] - def apply( self, transformations ): - map( lambda t: t.configure_mem_fun( self ), transformations ) - @property def result_variable( self ): return self.__result_var @@ -147,4 +144,31 @@ def set_return_stmt( self, stmt ): self.__return_stmt = stmt +class mem_fun_controller_t( non_virtual_fun_controller_t ): + def __init__( self, function ): + non_virtual_fun_controller_t.__init__( self, function ) + + inst_arg_type = declarations.declarated_t( self.function.parent ) + if self.function.has_const: + inst_arg_type = declarations.const_t( inst_arg_type ) + inst_arg_type = declarations.reference_t( inst_arg_type ) + + self.__inst_arg = None + if not self.function.has_static: + self.__inst_arg = declarations.argument_t( name=self.register_variable_name( 'inst' ) + , type=inst_arg_type ) + def apply( self, transformations ): + map( lambda t: t.configure_mem_fun( self ), transformations ) + + @property + def inst_arg( self ): + return self.__inst_arg + +class free_fun_controller_t( non_virtual_fun_controller_t ): + def __init__( self, function ): + non_virtual_fun_controller_t.__init__( self, function ) + + def apply( self, transformations ): + map( lambda t: t.configure_free_fun( self ), transformations ) + Modified: pyplusplus_dev_ft/pyplusplus/function_transformers/function_transformation.py =================================================================== --- pyplusplus_dev_ft/pyplusplus/function_transformers/function_transformation.py 2006-11-23 20:26:16 UTC (rev 751) +++ pyplusplus_dev_ft/pyplusplus/function_transformers/function_transformation.py 2006-11-25 19:25:49 UTC (rev 752) @@ -7,13 +7,18 @@ """ import md5 import controllers +from pygccxml import declarations from pyplusplus import code_repository class function_transformation_t: def __init__(self, function, transformer_creator, **keywd): """Constructor. """ self.__function = function - self.__controller = controllers.mem_fun_controller_t( function ) + self.__controller = None + if isinstance( function.parent, declarations.class_t ): + self.__controller = controllers.mem_fun_controller_t( function ) + else: + self.__controller = controllers.free_fun_controller_t( function ) self.__transformers = map( lambda tr_creator: tr_creator( function ), transformer_creator ) self.__thread_safe = keywd.get( 'thread_safe', False ) self.__controller.apply( self.__transformers ) Modified: pyplusplus_dev_ft/pyplusplus/function_transformers/templates.py =================================================================== --- pyplusplus_dev_ft/pyplusplus/function_transformers/templates.py 2006-11-23 20:26:16 UTC (rev 751) +++ pyplusplus_dev_ft/pyplusplus/function_transformers/templates.py 2006-11-25 19:25:49 UTC (rev 752) @@ -11,6 +11,16 @@ # function_name - C++ function name # function_alias - function name that Python user will see +class free_fun: + body = Template( os.linesep.join([ + '$declare_variables' + , '$pre_call' + , '$save_result$function($arg_expressions);' + , '$post_call' + , '$return' + ])) + + class mem_fun: body = Template( os.linesep.join([ '$declare_variables' Modified: pyplusplus_dev_ft/pyplusplus/function_transformers/transformer.py =================================================================== --- pyplusplus_dev_ft/pyplusplus/function_transformers/transformer.py 2006-11-23 20:26:16 UTC (rev 751) +++ pyplusplus_dev_ft/pyplusplus/function_transformers/transformer.py 2006-11-25 19:25:49 UTC (rev 752) @@ -60,4 +60,9 @@ return self.get_argument( reference ).type def configure_mem_fun( self, controller ): - pass \ No newline at end of file + pass + + def configure_free_fun( self, controller ): + pass + + \ No newline at end of file Modified: pyplusplus_dev_ft/pyplusplus/function_transformers/transformers.py =================================================================== --- pyplusplus_dev_ft/pyplusplus/function_transformers/transformers.py 2006-11-23 20:26:16 UTC (rev 751) +++ pyplusplus_dev_ft/pyplusplus/function_transformers/transformers.py 2006-11-25 19:25:49 UTC (rev 752) @@ -68,8 +68,7 @@ def __str__(self): return "output(%d)"%(self.arg_index) - def configure_mem_fun( self, controller ): - assert isinstance( controller, controllers.mem_fun_controller_t ) + def __configure_non_virtual( self, controller ): #removing arg from the function wrapper definition controller.remove_wrapper_arg( self.arg.name ) #declaring new variable, which will keep result @@ -79,6 +78,12 @@ #adding the variable to return variables list controller.return_variable( var_name ) + def configure_mem_fun( self, controller ): + self.__configure_non_virtual( controller ) + + def configure_free_fun(self, controller ): + self.__configure_non_virtual( controller ) + # input_t class input_t(transformer.transformer_t): """Handles a single input variable. @@ -107,11 +112,16 @@ def __str__(self): return "input(%d)"%(self.idx) - def configure_mem_fun( self, controller ): - assert isinstance( controller, controllers.mem_fun_controller_t ) + def __configure_non_virtual( self, controller ): w_arg = controller.find_wrapper_arg( self.arg.name ) w_arg.type = remove_ref_or_ptr( self.arg.type ) + def configure_mem_fun( self, controller ): + self.__configure_non_virtual( controller ) + + def configure_free_fun(self, controller ): + self.__configure_non_virtual( controller ) + # inout_t class inout_t(transformer.transformer_t): """Handles a single input/output variable. @@ -138,14 +148,19 @@ def __str__(self): return "inout(%d)"%(self.arg_index) - def configure_mem_fun(self, controller): - assert isinstance( controller, controllers.mem_fun_controller_t ) + def __configure_non_virtual(self, controller): 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.name ) + def configure_mem_fun( self, controller ): + self.__configure_non_virtual( controller ) + + def configure_free_fun(self, controller ): + self.__configure_non_virtual( controller ) + class input_array_t(transformer.transformer_t): """Handles an input array with fixed size. @@ -185,9 +200,7 @@ 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 configure_mem_fun(self, controller): - assert isinstance( controller, controllers.mem_fun_controller_t ) - + def __configure_non_virtual(self, controller): w_arg = controller.find_wrapper_arg( self.arg.name ) w_arg.type = declarations.dummy_type_t( "boost::python::object" ) @@ -205,6 +218,11 @@ controller.modify_argument_expression( self.arg_index, native_array ) + def configure_mem_fun( self, controller ): + self.__configure_non_virtual( controller ) + + def configure_free_fun(self, controller ): + self.__configure_non_virtual( controller ) # output_array_t class output_array_t(transformer.transformer_t): @@ -244,8 +262,7 @@ def post_call_tmpl( self ): return string.Template( 'pyplus_conv::copy_container( $native_array, $native_array + $array_size, pyplus_conv::list_inserter( $pylist ) );' ) - def configure_mem_fun(self, controller): - assert isinstance( controller, controllers.mem_fun_controller_t ) + def __configure_non_virtual(self, controller): #removing arg from the function wrapper definition controller.remove_wrapper_arg( self.arg.name ) @@ -269,4 +286,10 @@ #adding the variable to return variables list controller.return_variable( pylist ) + + def configure_mem_fun( self, controller ): + self.__configure_non_virtual( controller ) + + def configure_free_fun(self, controller ): + self.__configure_non_virtual( controller ) Modified: pyplusplus_dev_ft/pyplusplus/module_creator/creator.py =================================================================== --- pyplusplus_dev_ft/pyplusplus/module_creator/creator.py 2006-11-23 20:26:16 UTC (rev 751) +++ pyplusplus_dev_ft/pyplusplus/module_creator/creator.py 2006-11-25 19:25:49 UTC (rev 752) @@ -533,7 +533,14 @@ self.curr_decl.call_policies = self.__call_policies_resolver( self.curr_decl ) self.__on_demand_include_call_policies( self.curr_decl.call_policies ) - maker = code_creators.free_function_t( function=self.curr_decl ) + maker = None + if self.curr_decl.transformations: + wrapper = code_creators.free_fun_transformed_wrapper_t( self.curr_decl ) + self.__extmodule.adopt_declaration_creator( wrapper ) + maker = code_creators.free_fun_transformed_t( self.curr_decl, wrapper ) + maker.associated_decl_creators.append( wrapper ) + else: + maker = code_creators.free_function_t( function=self.curr_decl ) self.curr_code_creator.adopt_creator( maker ) self.__opaque_types_manager.register_opaque( maker, self.curr_decl ) 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-23 20:26:16 UTC (rev 751) +++ pyplusplus_dev_ft/unittests/data/function_transformations_to_be_exported.hpp 2006-11-25 19:25:49 UTC (rev 752) @@ -6,8 +6,15 @@ #ifndef __function_transformations_to_be_exported_hpp__ #define __function_transformations_to_be_exported_hpp__ +#include <cmath> +#include <string> + namespace ft2{ +void hello_world( std::string& hw ){ + hw = "hello world!"; +} + //used to check output transformer struct calculator_t{ calculator_t(){ @@ -57,6 +64,7 @@ }; struct point3d_t{ + point3d_t() : x( 0 ), y(0), z(0) {} @@ -74,6 +82,10 @@ v[2] = z; } + static void distance( const point3d_t& pt, double& dist ){ + dist = sqrt( pt.x*pt.x + pt.y*pt.y + pt.z*pt.z ); + } + int x, y, z; }; Modified: pyplusplus_dev_ft/unittests/function_transformations_tester.py =================================================================== --- pyplusplus_dev_ft/unittests/function_transformations_tester.py 2006-11-23 20:26:16 UTC (rev 751) +++ pyplusplus_dev_ft/unittests/function_transformations_tester.py 2006-11-25 19:25:49 UTC (rev 752) @@ -5,6 +5,7 @@ import os import sys +import math import unittest import fundamental_tester_base from pyplusplus import function_transformers as ft @@ -24,6 +25,9 @@ mb.global_ns.calldefs().create_with_signature = True + hello_world = mb.free_fun( 'hello_world' ) + hello_world.add_transformation( ft.output(0) ) + calc = mb.class_('calculator_t' ) assign_funs = calc.mem_funs( lambda decl: decl.name.startswith( 'assign' ) ) assign_funs.add_transformation( ft.output(0), ft.output(1) ) @@ -40,6 +44,7 @@ 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) ) + point3d.mem_fun( 'distance' ).add_transformation( ft.output(1) ) #~ image = mb.class_( "image_t" ) #~ image.include() @@ -91,6 +96,9 @@ 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] ) + self.failUnless( module.point3d_t.distance( point3d )[0] == math.sqrt( 1*1 + 2*2 + 3*3 ) ) + + self.failUnless( module.hello_world()[0] == "hello world!" ) #~ 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. |