Update of /cvsroot/pygccxml/source/pyplusplus/code_creators
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv29364/pyplusplus/code_creators
Modified Files:
__init__.py code_creator.py member_variable.py
Log Message:
adding new functionality:
if class is used as member variabel, but does not have public assign operator, def_readonly will be used
if class has pointer as member variable, it is exposed correctly
Index: code_creator.py
===================================================================
RCS file: /cvsroot/pygccxml/source/pyplusplus/code_creators/code_creator.py,v
retrieving revision 1.10
retrieving revision 1.11
diff -C2 -d -r1.10 -r1.11
*** code_creator.py 16 Mar 2006 14:48:45 -0000 1.10
--- code_creator.py 23 Apr 2006 14:39:00 -0000 1.11
***************
*** 23,26 ****
--- 23,27 ----
__INDENTATION = ' '
LINE_LENGTH = 80
+ PARAM_SEPARATOR = ', '
def __init__(self, parent=None):
"""Constructor.
Index: __init__.py
===================================================================
RCS file: /cvsroot/pygccxml/source/pyplusplus/code_creators/__init__.py,v
retrieving revision 1.24
retrieving revision 1.25
diff -C2 -d -r1.24 -r1.25
*** __init__.py 16 Mar 2006 14:48:45 -0000 1.24
--- __init__.py 23 Apr 2006 14:39:00 -0000 1.25
***************
*** 61,64 ****
--- 61,65 ----
from member_variable import member_variable_base_t
from member_variable import member_variable_t
+ from member_variable import member_variable_wrapper_t
from member_variable import bit_field_t
from member_variable import bit_field_wrapper_t
Index: member_variable.py
===================================================================
RCS file: /cvsroot/pygccxml/source/pyplusplus/code_creators/member_variable.py,v
retrieving revision 1.14
retrieving revision 1.15
diff -C2 -d -r1.14 -r1.15
*** member_variable.py 20 Apr 2006 05:36:43 -0000 1.14
--- member_variable.py 23 Apr 2006 14:39:00 -0000 1.15
***************
*** 28,36 ****
self._wrapper = new_wrapper
wrapper = property( _get_wrapper, _set_wrapper )
-
- #TODO:
- #> On Wednesday, 19. April 2006 23:05, Ralf W. Grosse-Kunstleve wrote:
- #> .add_property("p", make_function(&A::get_p,
- #> return_value_policy<reference_existing_object>()))
class member_variable_t( member_variable_base_t ):
--- 28,31 ----
***************
*** 38,81 ****
Creates boost.python code that exposes member variable.
"""
! __PARAM_SEPARATOR = ', '
! def __init__(self, variable, parent=None ):
! member_variable_base_t.__init__( self, variable=variable, parent=parent)
! def _generate_variable_body( self ):
tmpl = None
! if not self.declaration.type_qualifiers.has_static:
! tmpl = '( "%(alias)s", &%(name)s )'
else:
! tmpl = '( "%(alias)s", %(name)s )'
result = tmpl % {
! 'alias' : self.alias
, 'name' : algorithm.create_identifier( self, self.declaration.decl_string ) }
return result
! def _generate_variable( self ):
! result = []
! type_ = self.declaration.type
if declarations.is_pointer( type_ ):
type_ = declarations.remove_pointer( type_ )
if declarations.is_reference( type_ ):
type_ = declarations.remove_reference( type_ )
if isinstance( type_, declarations.const_t ):
! result.append( 'def_readonly' )
else:
! result.append( 'def_readwrite' )
! result.append( self._generate_variable_body() )
! return ''.join( result )
def _create_impl(self):
return self._generate_variable()
class bit_field_t( member_variable_base_t ):
"""
Creates boost.python code that exposes bit fields member variables
"""
-
- _PARAM_SEPARATOR = ', '
def __init__(self, variable, wrapper, parent=None ):
member_variable_base_t.__init__( self
--- 33,236 ----
Creates boost.python code that exposes member variable.
"""
+ def __init__(self, variable, wrapper=None, parent=None ):
+ member_variable_base_t.__init__( self, variable=variable, wrapper=wrapper, parent=parent)
! #> On Wednesday, 19. April 2006 23:05, Ralf W. Grosse-Kunstleve wrote:
! #> .add_property("p", make_function(&A::get_p, return_value_policy<reference_existing_object>()))
! def _generate_for_pointer( self ):
! if self.declaration.type_qualifiers.has_static:
! add_property = 'add_static_property'
! else:
! add_property = 'add_property'
! answer = [ add_property ]
! answer.append( '( ' )
! answer.append('"%s"' % self.alias)
! answer.append( self.PARAM_SEPARATOR )
!
! call_pol = call_policies.return_value_policy( call_policies.reference_existing_object ).create( self )
! make_function = algorithm.create_identifier( self, '::boost::python::make_function' )
! answer.append( '%(mk_func)s( (%(getter_type)s)(&%(wfname)s), %(call_pol)s )'
! % { 'mk_func' : make_function
! , 'getter_type' : self.wrapper.getter_type
! , 'wfname' : self.wrapper.getter_full_name
! , 'call_pol' : call_pol } )
!
! #don't generate setter method, right now I don't know how to do it.
! if False and self.wrapper.has_setter:
! answer.append( self.PARAM_SEPARATOR )
! if self.declaration.type_qualifiers.has_static:
! call_pol = call_policies.default_call_policies().create(self)
! else:
! call_pol = call_policies.with_custodian_and_ward_postcall( 0, 1 ).create(self)
! answer.append( '%(mk_func)s( (%(setter_type)s)(&%(wfname)s), %(call_pol)s )'
! % { 'mk_func' : make_function
! , 'setter_type' : self.wrapper.setter_type
! , 'wfname' : self.wrapper.setter_full_name
! , 'call_pol' : call_pol } )
! answer.append( ' ) ' )
!
! code = ''.join( answer )
! if len( code ) <= self.LINE_LENGTH:
! return code
! else:
! for i in range( len( answer ) ):
! if answer[i] == self.PARAM_SEPARATOR:
! answer[i] = os.linesep + self.indent( self.indent( self.indent( answer[i] ) ) )
! return ''.join( answer )
!
! def _generate_for_none_pointer( self ):
tmpl = None
! if self.declaration.type_qualifiers.has_static:
! tmpl = '%(access)s( "%(alias)s", %(name)s )'
else:
! tmpl = '%(access)s( "%(alias)s", &%(name)s )'
!
! access = 'def_readwrite'
! if self.is_read_only():
! access = 'def_readonly'
result = tmpl % {
! 'access' : access
! , 'alias' : self.alias
, 'name' : algorithm.create_identifier( self, self.declaration.decl_string ) }
+
return result
! def is_read_only( self ):
! type_ = declarations.remove_alias( self.declaration.type )
if declarations.is_pointer( type_ ):
type_ = declarations.remove_pointer( type_ )
+ return isinstance( type_, declarations.const_t )
+
if declarations.is_reference( type_ ):
type_ = declarations.remove_reference( type_ )
+
if isinstance( type_, declarations.const_t ):
! return True
!
! if isinstance( type_, declarations.declarated_t ) \
! and isinstance( type_.declaration, declarations.class_t ) \
! and not declarations.has_public_assign( type_.declaration ):
! return True
! return False
!
! def _generate_variable( self ):
! if declarations.is_pointer( self.declaration.type ):
! return self._generate_for_pointer()
else:
! return self._generate_for_none_pointer()
def _create_impl(self):
return self._generate_variable()
+ class member_variable_wrapper_t( declaration_based.declaration_based_t ):
+ """
+ Creates C++ code that creates accessor for pointer class variables
+ """
+ #TODO: give user a way to set call policies
+ # treat void* pointer
+ indent = declaration_based.declaration_based_t.indent
+ MV_GET_TEMPLATE = os.linesep.join([
+ 'static %(type)s get_%(name)s(%(cls_type)s inst ){'
+ , indent( 'return inst.%(name)s;' )
+ , '}'
+ , ''
+ ])
+
+ MV_STATIC_GET_TEMPLATE = os.linesep.join([
+ 'static %(type)s get_%(name)s(){'
+ , indent( 'return %(cls_type)s::%(name)s;' )
+ , '}'
+ , ''
+ ])
+
+ MV_SET_TEMPLATE = os.linesep.join([
+ 'static void set_%(name)s( %(cls_type)s inst, %(type)s new_value ){ '
+ , indent( 'inst.%(name)s = new_value;' )
+ , '}'
+ , ''
+ ])
+
+ MV_STATIC_SET_TEMPLATE = os.linesep.join([
+ 'static void set_%(name)s( %(type)s new_value ){ '
+ , indent( '%(cls_type)s::%(name)s = new_value;' )
+ , '}'
+ , ''
+ ])
+
+ def __init__(self, variable, parent=None ):
+ declaration_based.declaration_based_t.__init__( self
+ , parent=parent
+ , declaration=variable)
+
+ def _get_getter_full_name(self):
+ return self.parent.full_name + '::' + 'get_' + self.declaration.name
+ getter_full_name = property( _get_getter_full_name )
+
+ def inst_arg_type( self, has_const ):
+ inst_arg_type = declarations.declarated_t( self.declaration.parent )
+ if has_const:
+ inst_arg_type = declarations.const_t(inst_arg_type)
+ inst_arg_type = declarations.reference_t(inst_arg_type)
+ return inst_arg_type
+
+ def _get_getter_type(self):
+ if self.declaration.type_qualifiers.has_static:
+ arguments_types=[]
+ else:
+ arguments_types=[ self.inst_arg_type(True) ]
+
+ return declarations.free_function_type_t.create_decl_string(
+ return_type=self.declaration.type
+ , arguments_types=arguments_types )
+ getter_type = property( _get_getter_type )
+
+ def _get_setter_full_name(self):
+ return self.parent.full_name + '::' + 'set_' + self.declaration.name
+ setter_full_name = property(_get_setter_full_name)
+
+ def _get_setter_type(self):
+ if self.declaration.type_qualifiers.has_static:
+ arguments_types=[ self.declaration.type ]
+ else:
+ arguments_types=[ self.inst_arg_type(False), self.declaration.type ]
+
+ return declarations.free_function_type_t.create_decl_string(
+ return_type=declarations.void_t()
+ , arguments_types=arguments_types )
+ setter_type = property( _get_setter_type )
+
+ def _get_has_setter( self ):
+ return not declarations.is_const( self.declaration.type.base )
+ has_setter = property( _get_has_setter )
+
+ def _create_impl(self):
+ answer = []
+ if self.declaration.type_qualifiers.has_static:
+ substitutions = {
+ 'type' : self.declaration.type.decl_string
+ , 'name' : self.declaration.name
+ , 'cls_type' : declarations.full_name( self.declaration.parent )
+ }
+ answer.append( self.MV_STATIC_GET_TEMPLATE % substitutions)
+ if self.has_setter:
+ answer.append( self.MV_STATIC_SET_TEMPLATE % substitutions )
+ else:
+ answer.append( self.MV_GET_TEMPLATE % {
+ 'type' : self.declaration.type.decl_string
+ , 'name' : self.declaration.name
+ , 'cls_type' : self.inst_arg_type( has_const=True ) })
+ if self.has_setter:
+ answer.append( self.MV_SET_TEMPLATE % {
+ 'type' : self.declaration.type.decl_string
+ , 'name' : self.declaration.name
+ , 'cls_type' : self.inst_arg_type( has_const=False ) })
+ return os.linesep.join( answer )
+
class bit_field_t( member_variable_base_t ):
"""
Creates boost.python code that exposes bit fields member variables
"""
def __init__(self, variable, wrapper, parent=None ):
member_variable_base_t.__init__( self
***************
*** 92,101 ****
answer.append( '( ' )
answer.append('"%s"' % self.alias)
! answer.append( self._PARAM_SEPARATOR )
answer.append( '(%s)(&%s)'
% ( self.wrapper.getter_type, self.wrapper.getter_full_name ) )
if self.wrapper.has_setter:
! answer.append( self._PARAM_SEPARATOR )
answer.append( '(%s)(&%s)'
% ( self.wrapper.setter_type, self.wrapper.setter_full_name ) )
--- 247,256 ----
answer.append( '( ' )
answer.append('"%s"' % self.alias)
! answer.append( self.PARAM_SEPARATOR )
answer.append( '(%s)(&%s)'
% ( self.wrapper.getter_type, self.wrapper.getter_full_name ) )
if self.wrapper.has_setter:
! answer.append( self.PARAM_SEPARATOR )
answer.append( '(%s)(&%s)'
% ( self.wrapper.setter_type, self.wrapper.setter_full_name ) )
***************
*** 107,114 ****
else:
for i in range( len( answer ) ):
! if answer[i] == self._PARAM_SEPARATOR:
answer[i] = os.linesep + self.indent( self.indent( self.indent( answer[i] ) ) )
return ''.join( answer )
!
class bit_field_wrapper_t( declaration_based.declaration_based_t ):
"""
--- 262,269 ----
else:
for i in range( len( answer ) ):
! if answer[i] == self.PARAM_SEPARATOR:
answer[i] = os.linesep + self.indent( self.indent( self.indent( answer[i] ) ) )
return ''.join( answer )
!
class bit_field_wrapper_t( declaration_based.declaration_based_t ):
"""
***************
*** 177,182 ****
Creates boost.python code that exposes array member variable.
"""
-
- _PARAM_SEPARATOR = ', '
def __init__(self, variable, wrapper, parent=None ):
member_variable_base_t.__init__( self
--- 332,335 ----
***************
*** 193,197 ****
answer.append( '( ' )
answer.append('"%s"' % self.declaration.name )
! answer.append( os.linesep + self.indent( self._PARAM_SEPARATOR ) )
temp = [ algorithm.create_identifier( self, "::boost::python::make_function" ) ]
temp.append( '( ' )
--- 346,350 ----
answer.append( '( ' )
answer.append('"%s"' % self.declaration.name )
! answer.append( os.linesep + self.indent( self.PARAM_SEPARATOR ) )
temp = [ algorithm.create_identifier( self, "::boost::python::make_function" ) ]
temp.append( '( ' )
***************
*** 200,204 ****
, self.wrapper.wrapper_creator_full_name ) )
if not self.declaration.type_qualifiers.has_static:
! temp.append( os.linesep + self.indent( self._PARAM_SEPARATOR, 4 ) )
temp.append( call_policies.with_custodian_and_ward_postcall( 0, 1 ).create(self) )
temp.append( ' )' )
--- 353,357 ----
, self.wrapper.wrapper_creator_full_name ) )
if not self.declaration.type_qualifiers.has_static:
! temp.append( os.linesep + self.indent( self.PARAM_SEPARATOR, 4 ) )
temp.append( call_policies.with_custodian_and_ward_postcall( 0, 1 ).create(self) )
temp.append( ' )' )
|