[pygccxml-commit] SF.net SVN: pygccxml: [754] pyplusplus_dev_ft/pyplusplus
Brought to you by:
mbaas,
roman_yakovenko
From: <rom...@us...> - 2006-11-26 19:22:50
|
Revision: 754 http://svn.sourceforge.net/pygccxml/?rev=754&view=rev Author: roman_yakovenko Date: 2006-11-26 11:22:48 -0800 (Sun, 26 Nov 2006) Log Message: ----------- small refactoring to separate mem and free functions Modified Paths: -------------- pyplusplus_dev_ft/pyplusplus/code_creators/calldef_transformed.py pyplusplus_dev_ft/pyplusplus/decl_wrappers/calldef_wrapper.py pyplusplus_dev_ft/pyplusplus/function_transformers/controllers.py Modified: pyplusplus_dev_ft/pyplusplus/code_creators/calldef_transformed.py =================================================================== --- pyplusplus_dev_ft/pyplusplus/code_creators/calldef_transformed.py 2006-11-26 06:23:23 UTC (rev 753) +++ pyplusplus_dev_ft/pyplusplus/code_creators/calldef_transformed.py 2006-11-26 19:22:48 UTC (rev 754) @@ -22,25 +22,17 @@ return __REMOVE_DUPLICAET_LINESEP.sub( os.linesep, code ) 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] + @property + def controller( self ): + return self.ft.controller + def _get_alias_impl( self ): return self.wrapper.ft.alias @@ -61,85 +53,37 @@ return '&%s' % full_name def create_call_policies( self ): - if self.ft.controller.return_variables: + if self.controller.wrapper_call_policies.is_default(): return '' else: - return super( mem_fun_transformed_t, self ).create_call_policies() + return super( non_virtual_fun_transformed_t, self ).create_call_policies() - def create_keywords_args(self): - arg_utils = calldef_utils.argument_utils_t( self.declaration - , algorithm.make_id_creator( self ) - , self.ft.controller.wrapper_args ) - return arg_utils.keywords_args() - - class non_virtual_fun_transformed_wrapper_t( calldef_wrapper_t ): def __init__( self, function ): - """Constructor. - - @param function: Function declaration - @type function: calldef_t - """ calldef_wrapper_t.__init__( self, function=function ) - - 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=args ) + @property + def controller( self ): + return self.ft.controller - def wrapper_name( self ): - if self.__is_global(): - return self.ft.unique_name - else: - if self.declaration.overloads: - #it is possible that other functions will have same signature - return self.ft.unique_name - else: - return self.declaration.name - - def full_name(self): - if self.__is_global(): - return self.ft.unique_name - else: - 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 ) - , args ) - return arg_utils.args_declaration() - def create_declaration(self, name): template = 'static %(return_type)s %(name)s( %(args)s )' return template % { - 'return_type' : self.ft.controller.wrapper_return_type.decl_string + '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): - cntrl = self.ft.controller + cntrl = self.controller make_object = algorithm.create_identifier( self, 'pyplusplus::call_policies::make_object' ) make_tuple = algorithm.create_identifier( self, 'boost::python::make_tuple' ) @@ -182,358 +126,147 @@ 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' : f_invokation + , 'function' : self.resolve_function_ref() , 'arg_expressions' : self.PARAM_SEPARATOR.join( cntrl.arg_expressions ) , 'return' : return_stmt }) def _create_impl(self): - answer = ['// Transformed wrapper function for "%s"' % self.declaration ] - answer.append( self.create_declaration(self.declaration.alias) + '{') + answer = [ self.create_declaration(self.declaration.alias) + '{' ] + answer.append( '// 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 ) -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 free_fun_transformed_t( non_virtual_fun_transformed_t ): + """Creates code for public non-virtual member functions. + """ -#~ ###################################################################### + def __init__( self, function, wrapper=None ): + non_virtual_fun_transformed_t.__init__( self, function=function, wrapper=wrapper ) + self.works_on_instance = False -#~ class mem_fun_v_transformed_t( calldef_t ): - #~ """Creates code for (public) virtual member functions. - #~ """ + def create_def_code( self ): + return self.def_identifier() - #~ def __init__( self, function, wrapper=None ): - #~ calldef_t.__init__( self, function=function, wrapper=wrapper ) - #~ self.default_function_type_alias = 'default_' + self.function_type_alias + def create_keywords_args(self): + arg_utils = calldef_utils.argument_utils_t( self.declaration + , algorithm.make_id_creator( self ) + , self.controller.wrapper_args ) + return arg_utils.keywords_args() - #~ def create_function_type_alias_code( self, exported_class_alias=None ): - #~ if self.wrapper==None: - #~ ftype = self.declaration.function_type() - #~ else: - #~ ftype = self.wrapper.function_type() - #~ result = [] - #~ result.append( 'typedef %s;' % ftype.create_typedef( self.function_type_alias, exported_class_alias ) ) - #~ return ''.join( result ) +class free_fun_transformed_wrapper_t( non_virtual_fun_transformed_wrapper_t ): + def __init__( self, function ): + """Constructor. - #~ def create_doc(self): - #~ return None - - #~ def create_function_ref_code(self, use_function_alias=False): - #~ if self.wrapper: - #~ full_name = self.wrapper.default_full_name() - #~ else: - #~ full_name = declarations.full_name( self.declaration ) - - #~ result = [] - #~ if use_function_alias: - #~ result.append( '%s(&%s)' - #~ % ( self.function_type_alias, full_name ) ) - #~ elif self.declaration.create_with_signature: - #~ if self.wrapper: - #~ func_type = self.wrapper.function_type() - #~ else: - #~ func_type = self.declaration.function_type().decl_string - #~ result.append( '(%s)(&%s)' - #~ % ( func_type, full_name ) ) - #~ else: - #~ result.append( '&%s' % full_name ) - - #~ return ''.join( result ) - - -#~ class mem_fun_v_transformed_wrapper_t( calldef_wrapper_t ): - #~ """Creates wrapper code for (public) virtual member functions. - - #~ The generated code consists of two functions: the virtual function - #~ and the 'default' function. - #~ """ - - #~ def __init__( self, function ): - #~ """Constructor. - - #~ @param function: Function declaration - #~ @type function: calldef_t - #~ """ - #~ calldef_wrapper_t.__init__( self, function=function ) - - #~ # Create the substitution manager - #~ sm = function_transformers.substitution_manager_t(function - #~ , transformers=function.transformations[0].transformers ) + @param function: Function declaration + @type function: calldef_t + """ + non_virtual_fun_transformed_wrapper_t .__init__( self, function=function ) - #~ sm.init_funcs() - #~ self._subst_manager = sm + def function_type(self): + return declarations.free_function_type_t( + return_type=self.controller.wrapper_return_type + , arguments_types=self.controller.wrapper_args ) - #~ # Stores the name of the variable that holds the override - #~ self._override_var \ - #~ = sm.virtual_func.declare_variable(function.alias + "_callable", 'boost::python::override') - #~ # Stores the name of the 'gstate' variable - #~ self._gstate_var \ - #~ = sm.virtual_func.declare_variable("gstate", 'pyplusplus::threading ::gil_guard_t' ) + def wrapper_name( self ): + return self.ft.unique_name - #~ def default_name(self): - #~ """Return the name of the 'default' function. + def full_name(self): + return self.ft.unique_name - #~ @rtype: str - #~ """ - #~ return "default_" + self.declaration.alias + def args_declaration( self ): + arg_utils = calldef_utils.argument_utils_t( + self.declaration + , algorithm.make_id_creator( self ) + , self.controller.wrapper_args ) + return arg_utils.args_declaration() - #~ def default_full_name(self): - #~ """Return the full name of the 'default' function. + def create_declaration(self, name): + template = 'static %(return_type)s %(name)s( %(args)s )' - #~ The returned name also includes the class name. + return template % { + 'return_type' : self.controller.wrapper_return_type.decl_string + , 'name' : self.wrapper_name() + , 'args' : self.args_declaration() + } - #~ @rtype: str - #~ """ - #~ return self.parent.full_name + '::default_' + self.declaration.alias + def resolve_function_ref( self ): + return declarations.full_name( self.declaration ) - #~ def virtual_name(self): - #~ """Return the name of the 'virtual' function. - #~ @rtype: str - #~ """ - #~ return self.declaration.name +class mem_fun_transformed_t( non_virtual_fun_transformed_t ): + """Creates code for public non-virtual member functions. + """ + def __init__( self, function, wrapper=None ): + non_virtual_fun_transformed_t.__init__( self, function=function, wrapper=wrapper ) - #~ def base_name(self): - #~ """Return the name of the 'base' function. + def create_keywords_args(self): + args = self.controller.wrapper_args[:] + if self.controller.inst_arg: + args.insert( 0, self.controller.inst_arg ) - #~ @rtype: str - #~ """ - #~ return "base_" + self.declaration.name + arg_utils = calldef_utils.argument_utils_t( self.declaration + , algorithm.make_id_creator( self ) + , args ) + return arg_utils.keywords_args() - #~ def function_type(self): - #~ template = '$RET_TYPE' - #~ rettype = self._subst_manager.subst_wrapper(template) - #~ rettype = declarations.dummy_type_t(rettype) +class mem_fun_transformed_wrapper_t( non_virtual_fun_transformed_wrapper_t ): + def __init__( self, function ): + """Constructor. - #~ return declarations.free_function_type_t( - #~ return_type=rettype - #~ , arguments_types=map( lambda arg: arg.type, self.declaration.arguments ) ) + @param function: Function declaration + @type function: calldef_t + """ + non_virtual_fun_transformed_wrapper_t.__init__( self, function=function ) - #~ return declarations.member_function_type_t( - #~ return_type=self.declaration.return_type - #~ , class_inst=declarations.dummy_type_t( self.parent.full_name ) - #~ , arguments_types=map( lambda arg: arg.type, self.declaration.arguments ) - #~ , has_const=self.declaration.has_const ) + def __is_global( self ): + return not isinstance( self.parent, class_declaration.class_wrapper_t ) - #~ def create_declaration(self, name, virtual=True): - #~ """Create the function header. + def function_type(self): + args = map( lambda arg: arg.type, self.controller.wrapper_args ) + if self.controller.inst_arg: + args.insert( 0, self.controller.inst_arg.type ) + return declarations.free_function_type_t( + return_type=self.controller.wrapper_return_type + , arguments_types=args ) - #~ This method is used for the virtual function (and the base_ function), - #~ but not for the default function. - #~ """ - #~ template = '%(virtual)s$RET_TYPE %(name)s( $ARG_LIST_DEF )%(constness)s %(throw)s' + def wrapper_name( self ): + if self.__is_global(): + return self.ft.unique_name + else: + if self.declaration.overloads: + #it is possible that other functions will have same signature + return self.ft.unique_name + else: + return self.declaration.name - #~ # Substitute the $-variables - #~ template = self._subst_manager.subst_virtual(template) + def full_name(self): + if self.__is_global(): + return self.ft.unique_name + else: + return self.parent.full_name + '::' + self.wrapper_name() - #~ virtualspec = '' - #~ if virtual: - #~ virtualspec = 'virtual ' + def args_declaration( self ): + args = self.controller.wrapper_args[:] + if self.controller.inst_arg: + args.insert( 0, self.controller.inst_arg ) + arg_utils = calldef_utils.argument_utils_t( + self.declaration + , algorithm.make_id_creator( self ) + , args ) + return arg_utils.args_declaration() - #~ constness = '' - #~ if self.declaration.has_const: - #~ constness = ' const ' - - #~ return template % { - #~ 'virtual' : virtualspec - #~ , 'name' : name - #~ , 'constness' : constness - #~ , 'throw' : self.throw_specifier_code() - #~ } - - #~ def create_base_body(self): - #~ body = "%(return_)s%(wrapped_class)s::%(name)s( %(args)s );" - - #~ return_ = '' - #~ if not declarations.is_void( self.declaration.return_type ): - #~ return_ = 'return ' - - #~ return body % { - #~ 'name' : self.declaration.name - #~ , 'args' : self.function_call_args() - #~ , 'return_' : return_ - #~ , 'wrapped_class' : self.wrapped_class_identifier() - #~ } - - #~ def create_virtual_body(self): - - #~ thread_safe = self.declaration.transformations[0].thread_safe - - #~ if thread_safe: - #~ body = """ -#~ pyplusplus::threading::gil_guard_t %(gstate_var)s; - -#~ %(gstate_var)s.ensure(); -#~ boost::python::override %(override_var)s = this->get_override( "%(alias)s" ); -#~ %(gstate_var)s.release(); - -#~ if( %(override_var)s ) -#~ { - #~ // The corresponding release() is done in the destructor of %(gstate_var)s - #~ %(gstate_var)s.ensure(); - - #~ $DECLARATIONS - - #~ try { - #~ $PRE_CALL - - #~ ${RESULT_VAR_ASSIGNMENT}boost::python::call<$RESULT_TYPE>($INPUT_PARAMS); - - #~ $POST_CALL - - #~ $RETURN_STMT - #~ } - #~ catch(...) - #~ { - #~ if (PyErr_Occurred()) - #~ { - #~ PyErr_Print(); - #~ } - - #~ $CLEANUP - - #~ $EXCEPTION_HANDLER_EXIT - #~ } -#~ } -#~ else -#~ { - #~ %(inherited)s -#~ } -#~ """ - - #~ if not thread_safe: - #~ body = """ -#~ boost::python::override %(override_var)s = this->get_override( "%(alias)s" ); - -#~ if( %(override_var)s ) -#~ { - #~ $DECLARATIONS - - #~ $PRE_CALL - - #~ ${RESULT_VAR_ASSIGNMENT}boost::python::call<$RESULT_TYPE>($INPUT_PARAMS); - - #~ $POST_CALL - - #~ $RETURN_STMT -#~ } -#~ else -#~ { - #~ %(inherited)s -#~ } -#~ """ - - #~ vf = self._subst_manager.virtual_func - #~ arg0 = "%s.ptr()"%self._override_var - #~ if vf.INPUT_PARAMS=="": - #~ vf.INPUT_PARAMS = arg0 - #~ else: - #~ vf.INPUT_PARAMS = arg0+", "+vf.INPUT_PARAMS - - #~ # Replace the $-variables - #~ body = self._subst_manager.subst_virtual(body) - - #~ return body % { - #~ 'override_var' : self._override_var - #~ , 'gstate_var' : self._gstate_var - #~ , 'alias' : self.declaration.alias - #~ , 'inherited' : self.create_base_body() - #~ } - - #~ def create_default_body(self): - #~ cls_wrapper_type = self.parent.full_name - #~ cls_wrapper = self._subst_manager.wrapper_func.declare_variable("cls_wrapper", cls_wrapper_type); - #~ # The name of the 'self' variable (i.e. first argument) - #~ selfname = self._subst_manager.wrapper_func.arg_list[0].name - - #~ body = """$DECLARATIONS - -#~ $PRE_CALL - -#~ %(cls_wrapper_type)s* %(cls_wrapper)s = dynamic_cast<%(cls_wrapper_type)s*>(boost::addressof(%(self)s)); -#~ if (%(cls_wrapper)s==0) -#~ { - #~ // The following call is done on an instance created in C++, - #~ // so it won't invoke Python code. - #~ $RESULT_VAR_ASSIGNMENT$CALL_FUNC_NAME($INPUT_PARAMS); -#~ } -#~ else -#~ { - #~ // The following call is done on an instance created in Python, - #~ // i.e. a wrapper instance. This call might invoke Python code. - #~ $RESULT_VAR_ASSIGNMENT%(cls_wrapper)s->%(base_name)s($INPUT_PARAMS); -#~ } - -#~ $POST_CALL - -#~ $RETURN_STMT -#~ """ - - #~ # Replace the $-variables - #~ body = self._subst_manager.subst_wrapper(body) - - #~ # Replace the remaining parameters - #~ body = body%{"cls_wrapper_type" : cls_wrapper_type, - #~ "cls_wrapper" : cls_wrapper, - #~ "self" : selfname, - #~ "base_name" : self.base_name() } - #~ return body - - #~ def create_function(self): - #~ answer = [ self.create_declaration(self.declaration.name) + '{' ] - #~ answer.append( self.indent( self.create_virtual_body() ) ) - #~ answer.append( '}' ) - #~ return os.linesep.join( answer ) - - #~ def create_base_function( self ): - #~ answer = [ self.create_declaration("base_"+self.declaration.name, False) + '{' ] - #~ body = "%(return_)s%(wrapped_class)s::%(name)s( %(args)s );" - #~ answer.append( self.indent( self.create_base_body() ) ) - #~ answer.append( '}' ) - #~ return os.linesep.join( answer ) - - #~ def create_default_function( self ): - - #~ header = 'static $RET_TYPE %s( $ARG_LIST_DEF ) {'%self.default_name() - #~ header = self._subst_manager.subst_wrapper(header) - - #~ answer = [ header ] - #~ answer.append( self.indent( self.create_default_body() ) ) - #~ answer.append( '}' ) - #~ return os.linesep.join( answer ) - - #~ def _create_impl(self): - - #~ answer = [ self.create_function() ] - #~ answer.append( os.linesep ) - #~ answer.append( self.create_base_function() ) - #~ answer.append( os.linesep ) - #~ answer.append( self.create_default_function() ) - #~ answer = os.linesep.join( answer ) - - #~ # Replace the argument list of the declaration so that in the - #~ # case that keywords are created, the correct arguments will be - #~ # picked up (i.e. the modified argument list and not the original - #~ # argument list) - #~ self.declaration.arguments = self._subst_manager.wrapper_func.arg_list - - #~ return answer - + def resolve_function_ref( self ): + if self.controller.inst_arg: + return self.controller.inst_arg.name + '.' + self.declaration.name + else: + return declarations.full_name( self.declaration ) Modified: pyplusplus_dev_ft/pyplusplus/decl_wrappers/calldef_wrapper.py =================================================================== --- pyplusplus_dev_ft/pyplusplus/decl_wrappers/calldef_wrapper.py 2006-11-26 06:23:23 UTC (rev 753) +++ pyplusplus_dev_ft/pyplusplus/decl_wrappers/calldef_wrapper.py 2006-11-26 19:22:48 UTC (rev 754) @@ -168,7 +168,7 @@ if len( self.arguments ) > calldef_t.BOOST_PYTHON_MAX_ARITY: tmp = [ "The function has more than %d arguments ( %d ). " ] tmp.append( "You should adjust BOOST_PYTHON_MAX_ARITY macro." ) - tmp.append( "For more information see: http://mail.python.org/pipermail/c++-sig/2002-June/001554.html" ) + tmp.append( "For more information see: http://www.boost.org/libs/python/doc/v2/configuration.html" ) tmp = ' '.join( tmp ) msgs.append( tmp % ( calldef_t.BOOST_PYTHON_MAX_ARITY, len( self.arguments ) ) ) Modified: pyplusplus_dev_ft/pyplusplus/function_transformers/controllers.py =================================================================== --- pyplusplus_dev_ft/pyplusplus/function_transformers/controllers.py 2006-11-26 06:23:23 UTC (rev 753) +++ pyplusplus_dev_ft/pyplusplus/function_transformers/controllers.py 2006-11-26 19:22:48 UTC (rev 754) @@ -114,6 +114,15 @@ return self.function.return_type @property + def wrapper_call_policies( self ): + #prevent recursive import + from pyplusplus import decl_wrappers + if 'boost::python::tuple' == self.wrapper_return_type.decl_string: + return decl_wrappers.default_call_policies() + else: + return self.function.call_policies + + @property def return_variables( self ): return self.__return_variables @@ -136,14 +145,8 @@ def set_input_param( self, index, var_name_or_expr ): self.__input_params[ index ] = var_name_or_expr - - @property - def return_stmt( self ): - return self.__return_stmt - - 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 ) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |