Revision: 726
http://svn.sourceforge.net/pygccxml/?rev=726&view=rev
Author: roman_yakovenko
Date: 2006-11-15 06:46:17 -0800 (Wed, 15 Nov 2006)
Log Message:
-----------
Added Paths:
-----------
pyplusplus_dev/pyplusplus/function_transformers/controllers.py
pyplusplus_dev/pyplusplus/function_transformers/templates.py
Added: pyplusplus_dev/pyplusplus/function_transformers/controllers.py
===================================================================
--- pyplusplus_dev/pyplusplus/function_transformers/controllers.py (rev 0)
+++ pyplusplus_dev/pyplusplus/function_transformers/controllers.py 2006-11-15 14:46:17 UTC (rev 726)
@@ -0,0 +1,108 @@
+import string
+import templates
+from pygccxml import declarations
+
+class variable_t( object ):
+ def __init__( self, name, type, initialize_expr='' ):
+ self.__name = name
+ self.__type = type
+ self.__initialize_expr = initialize_expr
+
+ @property
+ def name( self ):
+ return self.__name
+
+ @property
+ def type( self ):
+ return self.__type
+
+ @property
+ def initialize_expr( self ):
+ return self.__initialize_expr
+
+ def declare_var_string( self ):
+ return templates.substitute( "$type $name$initialize_expr;"
+ , name=self.name
+ , type=self.type
+ , initialize_expr=self.initialize_expr )
+
+class controller_base_t( object ):
+ def __init__( self, function ):
+ object.__init__( self )
+ self.__function = function
+ self.__variables = {} #name : variable
+ self.__names_in_use = set( map( lambda arg: arg.name, self.function.arguments ) )
+
+ def declare_variable( self, type, name, initialize_expr='' ):
+ unique_name = self.__create_unique_var_name( name )
+ self.__variables[ unique_name ] = variable_t( type, unique_name, initialize_expr )
+
+ def register_variable_name( self, name ):
+ return self.__create_unique_var_name( name )
+
+ def __create_unique_var_name( self, name ):
+ n = 2
+ unique_name = name
+ while 1:
+ if unique_name in self.__names_in_use:
+ unique_name = "%s_%d" % ( name, n )
+ n += 1
+ else:
+ self.__names_in_use.add( unique_name )
+ return unique_name
+
+class mem_fun_controller_t( controller_base_t ):
+ def __init__( self, function ):
+ controller_base_t.__init__( self, function )
+ self.__transformed_args = function.arguments[:]
+ self.__transformed_return_type = function.return_type
+
+ self.__return_variables = []
+ self.__pre_call = []
+ self.__post_call = []
+ self.__save_return_value_stmt = None
+ self.__input_params = [ arg.name for arg in function.arguments ]
+ self.__return_stmt = None
+
+ @property
+ def transformed_args( self ):
+ return self.__transformed_args
+
+ def __get_transformed_return_type( self ):
+ return self.__transformed_return_type
+ def __set_transformed_return_type( self, type_ ):
+ if isinstane( type, types.StringTypes ):
+ type_ = declarations.dummy_type_t( type_ )
+ self.__transformed_return_type = type_
+ transformed_return_type = property( __get_transformed_return_type, __set_transformed_return_type )
+
+ def add_return_variable( self, variable_name ):
+ self.__return_variables.append( name )
+
+ @property
+ def pre_call( self ):
+ return self.__pre_call
+
+ def add_pre_call_code( self, code ):
+ self.__pre_call.append( code )
+
+ @property
+ def pos_call( self ):
+ return self.__post_call
+
+ def add_post_call_code( self, code ):
+ self.__post_call.append( code )
+
+ def __get_save_return_value_stmt( self ):
+ return self.__save_return_value_stmt
+ def __set_save_return_value_stmt( self, expr ):
+ self.__save_return_value_stmt = expr
+ save_return_value_stmt = property( __get_save_return_value_stmt, __set_save_return_value_stmt )
+
+ def set_input_param( self, index, var_name_or_expr ):
+ self.__input_params[ index ] = var_name_or_expr
+
+ @property
+ def return_stmt
+
+
Added: pyplusplus_dev/pyplusplus/function_transformers/templates.py
===================================================================
--- pyplusplus_dev/pyplusplus/function_transformers/templates.py (rev 0)
+++ pyplusplus_dev/pyplusplus/function_transformers/templates.py 2006-11-15 14:46:17 UTC (rev 726)
@@ -0,0 +1,82 @@
+# Copyright 2006 Roman Yakovenko.
+# Distributed under the Boost Software License, Version 1.0. (See
+# accompanying file LICENSE_1_0.txt or copy at
+# http://www.boost.org/LICENSE_1_0.txt)
+
+# Matthias Baas is an initial author of the templates.
+
+import os
+from string import Template
+
+# function_name - C++ function name
+# function_alias - function name that Python user will see
+
+class mem_fun:
+ body = Template( os.linesep.join([
+ '$declare_variables'
+ , '$pre_call'
+ , '$save_return_value_stmt$function_name($input_params);'
+ , '$post_call'
+ , '$return_stmt'
+ ]))
+
+class mem_fun_v:
+
+ original_function_call = Template( "$return_$wrapped_class::%function_name( $args );" )
+
+ override_body = Template( os.linesep.join([
+ '$declare_variables'
+ , '$declare_override_function = this->get_override( "$function_alias" );'
+ , 'if( $override_function_var_name ){'
+ , ' $declare_override_variables'
+ , ' $override_pre_call'
+ , ' ${save_override_return_value}boost::python::call<$override_return_type>( $override_function_var_name$override_input_params;'
+ , ' $override_post_call'
+ , ' $override_return_stmt'
+ , '}'
+ , 'else{'
+ , ' ' + original_function_call.template
+ , '}'
+ ]))
+
+ override_body_safe = Template( os.linesep.join([
+ '$declare_gil_guard'
+ , '$declare_variables'
+ , '$lock_gil_guard'
+ , '$declare_override_function = this->get_override( "$function_alias" );'
+ , '$unlock_gil_guard'
+ , 'if( $override_function_var_name ){'
+ , ' $lock_gil_guard //release() will be called from destructor'
+ , ' $declare_override_variables'
+ , ' $override_pre_call'
+ , ' ${save_override_return_value}boost::python::call<$override_return_type>( $override_function_var_name$override_input_params;'
+ , ' $override_post_call'
+ , ' $override_return_stmt'
+ , '}'
+ , 'else{'
+ , ' ' + original_function_call.template
+ , '}'
+ ]))
+
+ default_body = Template( os.linesep.join([
+ '$declare_variables'
+ , '$pre_call'
+ , '$declare_wrapped_class_inst'
+ , 'if( dynamic_cast< $wrapped_class* >( boost::addressof( $wrapped_class_inst_var_name ) ) ){'
+ # The following call is done on an instance created in Python i.e. a
+ # wrapper instance. this call might invoke python code.
+ , ' $save_return_value_stmt$wrapped_class_inst_var_name->$wrapped_class::$function_name($input_params);'
+ , '}'
+ , 'else{'
+ # The following call is done on an instance created in C++, so it won't
+ # invoke python code.
+ , ' $save_return_value_stmt$function_name($input_params);'
+ , '}'
+ , '$post_call'
+ , '$return_stmt'
+ ]))
+
+
+def substitute( text, **keywd ):
+ return Template( text ).substitute( **keywd )
+
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|