[pygccxml-commit] SF.net SVN: pygccxml: [763] pyplusplus_dev_ft/pyplusplus
Brought to you by:
mbaas,
roman_yakovenko
From: <rom...@us...> - 2006-11-29 12:24:55
|
Revision: 763 http://svn.sourceforge.net/pygccxml/?rev=763&view=rev Author: roman_yakovenko Date: 2006-11-29 04:24:50 -0800 (Wed, 29 Nov 2006) Log Message: ----------- templates.py file contains function definition too. Modified Paths: -------------- pyplusplus_dev_ft/pyplusplus/code_creators/calldef_transformed.py pyplusplus_dev_ft/pyplusplus/function_transformers/controllers.py pyplusplus_dev_ft/pyplusplus/function_transformers/templates.py pyplusplus_dev_ft/pyplusplus/function_transformers/transformers.py Modified: pyplusplus_dev_ft/pyplusplus/code_creators/calldef_transformed.py =================================================================== --- pyplusplus_dev_ft/pyplusplus/code_creators/calldef_transformed.py 2006-11-28 21:23:41 UTC (rev 762) +++ pyplusplus_dev_ft/pyplusplus/code_creators/calldef_transformed.py 2006-11-29 12:24:50 UTC (rev 763) @@ -67,58 +67,54 @@ def controller( self ): return self.ft.controller - def create_declaration(self, name): - template = 'static %(return_type)s %(name)s( %(args)s )' - - return template % { - 'return_type' : self.controller.wrapper_return_type.decl_string - , 'name' : self.wrapper_name() - , 'args' : self.args_declaration() - } - def resolve_function_ref( self ): raise NotImplementedError() - def create_body(self): + def create_fun_definition(self): cntrl = self.controller make_object = algorithm.create_identifier( self, 'pyplusplus::call_policies::make_object' ) make_tuple = algorithm.create_identifier( self, 'boost::python::make_tuple' ) - result_var_assign = '' + tmpl_values = dict() + + tmpl_values['unique_function_name'] = self.wrapper_name() + tmpl_values['return_type'] = self.controller.wrapper_return_type.decl_string + tmpl_values['arg_declarations'] = self.args_declaration() + + tmpl_values['declare_variables'] \ + = os.linesep + os.linesep.join( map( lambda var: self.indent( var.declare_var_string() ) + , cntrl.variables ) ) + + tmpl_values['pre_call'] = os.linesep + self.indent( os.linesep.join( cntrl.pre_call ) ) + + tmpl_values['save_result'] = '' if not declarations.is_void( self.declaration.return_type ): - result_var_assign = '%(type)s %(name)s = ' \ - % { 'type': cntrl.result_variable.type.decl_string - , 'name' : cntrl.result_variable.name } + tmpl_values['save_result'] \ + = '%(type)s %(name)s = ' \ + % { 'type': cntrl.result_variable.type.decl_string + , 'name' : cntrl.result_variable.name } + tmpl_values['function_name'] = self.resolve_function_ref() + tmpl_values['arg_expressions'] = self.PARAM_SEPARATOR.join( cntrl.arg_expressions ) return_stmt_creator = calldef_utils.return_stmt_creator_t( self , self.controller , self.controller.result_variable , self.controller.return_variables ) - post_call = os.linesep.join( cntrl.post_call ) + tmpl_values['post_call'] = os.linesep + self.indent( os.linesep.join( cntrl.post_call ) ) if return_stmt_creator.pre_return_code: - post_call = os.linesep.join( [post_call, return_stmt_creator.pre_return_code] ) + tmpl_values['post_call'] \ + = os.linesep.join([ tmpl_values['post_call'] + , self.indent( return_stmt_creator.pre_return_code )]) + tmpl_values['return'] = os.linesep + self.indent( return_stmt_creator.statement ) + + f_def = self.controller.template.substitute(tmpl_values) + return remove_duplicate_linesep( f_def ) - 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.resolve_function_ref() - , 'arg_expressions' : self.PARAM_SEPARATOR.join( cntrl.arg_expressions ) - , 'return' : return_stmt_creator.statement - }) - def _create_impl(self): - answer = [ self.create_declaration(self.declaration.alias) + '{' ] - answer.append( self.indent( '// Transformed wrapper function for "%s"' % self.declaration ) ) - body = self.create_body() - body = remove_duplicate_linesep( body ) - answer.append( self.indent( body ) ) - answer.append( '}' ) - return os.linesep.join( answer ) - + return self.create_fun_definition() + class free_fun_transformed_t( sealed_fun_transformed_t ): """Creates code for public non-virtual member functions. """ @@ -313,6 +309,9 @@ def default_full_name(self): return self.parent.full_name + '::default_' + self.declaration.alias + def args_override_declaration( self ): + return self.args_declaration() + def function_type(self): return declarations.member_function_type_t( return_type=self.declaration.return_type @@ -320,22 +319,17 @@ , arguments_types=map( lambda arg: arg.type, self.declaration.arguments ) , has_const=self.declaration.has_const ) - def create_declaration(self, name, has_virtual=True): - template = '%(virtual)s%(return_type)s %(name)s( %(args)s )%(constness)s %(throw)s' + def create_default_declaration(self): + template = '%(return_type)s %(name)s( %(args)s )%(constness)s %(throw)s' - virtual = 'virtual ' - if not has_virtual: - virtual = '' - constness = '' if self.declaration.has_const: constness = ' const ' return template % { - 'virtual' : virtual - , 'return_type' : self.declaration.return_type.decl_string - , 'name' : name - , 'args' : self.args_declaration() + 'return_type' : self.declaration.return_type.decl_string + , 'name' : 'default_' + self.declaration.alias + , 'args' : self.args_override_declaration() , 'constness' : constness , 'throw' : self.throw_specifier_code() } @@ -343,10 +337,17 @@ def wrapped_class_identifier( self ): return algorithm.create_identifier( self, declarations.full_name( self.declaration.parent ) ) - def create_virtual_body(self): - cntrl = self.controller.override_controller + def create_override(self): + cntrl = self.controller.override_controller tmpl_values = dict() + tmpl_values['return_type' ] = self.declaration.return_type.decl_string + tmpl_values['function_name'] = self.declaration.name + tmpl_values['arg_declarations'] = self.args_override_declaration() + tmpl_values['constness'] = '' + if self.declaration.has_const: + tmpl_values['constness'] = ' const ' + tmpl_values['throw'] = self.throw_specifier_code() tmpl_values['py_function_var'] = cntrl.py_function_var tmpl_values['function_alias'] = self.declaration.alias tmpl_values['declare_py_variables'] \ @@ -369,15 +370,15 @@ tmpl_values['cpp_return'] = 'return ' tmpl_values['wrapped_class'] = self.wrapped_class_identifier() - tmpl_values['function_name'] = self.declaration.name arg_utils = calldef_utils.argument_utils_t( self.declaration , algorithm.make_id_creator( self ) , self.declaration.arguments ) tmpl_values['cpp_arg_expressions'] = arg_utils.call_args() - return cntrl.template.substitute(tmpl_values) - + f_def_code = cntrl.template.substitute(tmpl_values) + return remove_duplicate_linesep( f_def_code ) + def create_default_body(self): function_call = declarations.call_invocation.join( self.declaration.name , [ self.function_call_args() ] ) @@ -386,22 +387,14 @@ body = 'return ' + body return body - def create_function(self): - answer = [ self.create_declaration(self.declaration.name) + '{' ] - body = self.create_virtual_body() - body = remove_duplicate_linesep( body ) - answer.append( self.indent( body ) ) - answer.append( '}' ) - return os.linesep.join( answer ) - def create_default_function( self ): - answer = [ self.create_declaration('default_' + self.declaration.alias, False) + '{' ] + answer = [ self.create_default_declaration() + '{' ] answer.append( self.indent( self.create_default_body() ) ) answer.append( '}' ) return os.linesep.join( answer ) def _create_impl(self): - answer = [ self.create_function() ] + answer = [ self.create_override() ] answer.append( os.linesep ) answer.append( self.create_default_function() ) return os.linesep.join( answer ) Modified: pyplusplus_dev_ft/pyplusplus/function_transformers/controllers.py =================================================================== --- pyplusplus_dev_ft/pyplusplus/function_transformers/controllers.py 2006-11-28 21:23:41 UTC (rev 762) +++ pyplusplus_dev_ft/pyplusplus/function_transformers/controllers.py 2006-11-29 12:24:50 UTC (rev 763) @@ -246,11 +246,27 @@ def modify_py_arg_expression( self, index, expression ): self.arg_expressions[ index ] = expression + + class default_fun_controller_t( sealed_fun_controller_t ): + def __init__( self, function ): + sealed_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 = declarations.argument_t( name=self.register_variable_name( 'inst' ) + , type=inst_arg_type ) - + @property + def inst_arg( self ): + return self.__inst_arg + def __init__( self, function ): controller_base_t.__init__( self, function ) self.__override_cntrl = self.override_fun_controller_t( function ) + self.__default_cntrl = self.default_fun_controller_t( function ) def apply( self, transformations ): map( lambda t: t.configure_virtual_mem_fun( self ), transformations ) @@ -258,3 +274,7 @@ @property def override_controller( self ): return self.__override_cntrl + + @property + def default_controller( self ): + return self.__default_cntrl Modified: pyplusplus_dev_ft/pyplusplus/function_transformers/templates.py =================================================================== --- pyplusplus_dev_ft/pyplusplus/function_transformers/templates.py 2006-11-28 21:23:41 UTC (rev 762) +++ pyplusplus_dev_ft/pyplusplus/function_transformers/templates.py 2006-11-29 12:24:50 UTC (rev 763) @@ -8,51 +8,50 @@ import os from string import Template -# function_name - C++ function name -# function_alias - function name that Python user will see - #TODO: pre_call, post_call terminology should be changed. Use prefix and suffix #instead: http://boost.org/libs/smart_ptr/sp_techniques.html#wrapper class sealed_fun: body = Template( os.linesep.join([ - '$declare_variables' - , '$pre_call' - , '$save_result$function($arg_expressions);' - , '$post_call' - , '$return' + 'static $return_type $unique_function_name( $arg_declarations ){' + , ' $declare_variables' + , ' $pre_call' + , ' $save_result$function_name($arg_expressions);' + , ' $post_call' + , ' $return' + , '}' ])) class virtual_mem_fun: override_body = Template( os.linesep.join([ - 'namespace bpl = boost::python;' - , 'if( bpl::override $py_function_var = this->get_override( "$function_alias" ) ){' - , ' $declare_py_variables' - , ' $py_pre_call' - , ' ${save_py_result}bpl::call<bpl::object>( $py_function_var.ptr()$py_arg_expressions );' - , ' $py_post_call' - , ' $py_return' + 'virtual $return_type $function_name( $arg_declarations )$constness $throw{' + , ' namespace bpl = boost::python;' + , ' if( bpl::override $py_function_var = this->get_override( "$function_alias" ) ){' + , ' $declare_py_variables' + , ' $py_pre_call' + , ' ${save_py_result}bpl::call<bpl::object>( $py_function_var.ptr()$py_arg_expressions );' + , ' $py_post_call' + , ' $py_return' + , ' }' + , ' else{' + , ' $cpp_return$wrapped_class::$function_name( $cpp_arg_expressions );' + , ' }' , '}' - , 'else{' - , ' $cpp_return$wrapped_class::$function_name( $cpp_arg_expressions );' - , '}' ])) default_body = Template( os.linesep.join([ - '$declare_variables' - , '$pre_call' - , 'if( $wrapper_class $py_wrapper_var = dynamic_cast< $wrapper_class >( boost::addressof( $wrapped_inst ) ) ){' - , ' // The following call is done on an instance created in Python i.e.' - , ' // a wrapper instance. this call might invoke python code.' - , ' $py_save_result$wrapped_inst->$wrapped_class::$function_name($py_arg_expressions);' + 'static $return_type $unique_function_name( $arg_declarations ){' + , ' $declare_variables' + , ' $pre_call' + , ' if( $wrapper_class $py_wrapper_var = dynamic_cast< $wrapper_class >( boost::addressof( $wrapped_inst ) ) ){' + , ' $save_result$wrapped_inst->$wrapped_class::$function_name($arg_expressions);' + , ' }' + , ' else{' + , ' $save_result$wrapped_inst.$function_name($arg_expressions);' + , ' }' + , ' $post_call' + , ' $return' , '}' - , 'else{' - , " // The following call is done on an instance created in C++, so it won't " - , ' // invoke python code.' - , ' $cpp_save_result$wrapped_inst.$function_name($cpp_arg_expressions);' - , '}' - , '$post_call' - , '$return' ])) Modified: pyplusplus_dev_ft/pyplusplus/function_transformers/transformers.py =================================================================== --- pyplusplus_dev_ft/pyplusplus/function_transformers/transformers.py 2006-11-28 21:23:41 UTC (rev 762) +++ pyplusplus_dev_ft/pyplusplus/function_transformers/transformers.py 2006-11-29 12:24:50 UTC (rev 763) @@ -82,24 +82,30 @@ #adding the variable to return variables list controller.return_variable( var_name ) + def __configure_v_mem_fun_default( self, controller ): + self.__configure_sealed( controller ) + + def __configure_v_mem_fun_override( self, controller ): + controller.remove_py_arg( self.arg_index ) + tmpl = string.Template( + '$name = boost::python::extract< $type >( pyplus_conv::get_out_argument( $py_result, "$name" ) );' ) + store_py_result_in_arg = tmpl.substitute( name=self.arg.name + , type=remove_ref_or_ptr( self.arg.type ).decl_string + , py_result=controller.py_result_variable.name ) + controller.add_py_post_call_code( store_py_result_in_arg ) + def configure_mem_fun( self, controller ): self.__configure_sealed( controller ) def configure_free_fun(self, controller ): self.__configure_sealed( controller ) + def configure_virtual_mem_fun( self, controller ): - assert isinstance( controller, controllers.virtual_mem_fun_controller_t ) - override_cntrl = controller.override_controller - override_cntrl.remove_py_arg( self.arg_index ) - tmpl = string.Template( - '$name = boost::python::extract< $type >( pyplus_conv::get_out_argument( $py_result, "$name" ) );' ) - store_py_result_in_arg = tmpl.substitute( name=self.arg.name - , type=remove_ref_or_ptr( self.arg.type ).decl_string - , py_result=override_cntrl.py_result_variable.name ) - override_cntrl.add_py_post_call_code( store_py_result_in_arg ) + self.__configure_v_mem_fun_default( controller.default_controller ) + self.__configure_v_mem_fun_override( controller.override_controller ) + - # input_t class input_t(transformer.transformer_t): """Handles a single input variable. This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |