[pygccxml-commit] SF.net SVN: pygccxml: [788] pyplusplus_dev
Brought to you by:
mbaas,
roman_yakovenko
|
From: <rom...@us...> - 2006-12-10 06:27:22
|
Revision: 788
http://svn.sourceforge.net/pygccxml/?rev=788&view=rev
Author: roman_yakovenko
Date: 2006-12-09 22:27:20 -0800 (Sat, 09 Dec 2006)
Log Message:
-----------
adding smart ptr work around for member variables
Modified Paths:
--------------
pyplusplus_dev/pyplusplus/code_creators/member_variable.py
pyplusplus_dev/pyplusplus/decl_wrappers/variable_wrapper.py
pyplusplus_dev/unittests/data/smart_pointers_to_be_exported.hpp
pyplusplus_dev/unittests/smart_pointers_tester.py
Modified: pyplusplus_dev/pyplusplus/code_creators/member_variable.py
===================================================================
--- pyplusplus_dev/pyplusplus/code_creators/member_variable.py 2006-12-08 19:47:42 UTC (rev 787)
+++ pyplusplus_dev/pyplusplus/code_creators/member_variable.py 2006-12-10 06:27:20 UTC (rev 788)
@@ -95,7 +95,7 @@
tmpl = '%(access)s( "%(alias)s", &%(name)s%(doc)s )'
access = 'def_readwrite'
- if self.is_read_only():
+ if self.declaration.is_read_only:
access = 'def_readonly'
doc = ''
if self.documentation:
@@ -103,38 +103,56 @@
result = tmpl % {
'access' : access
, 'alias' : self.alias
- , 'name' : algorithm.create_identifier( self, self.declaration.decl_string )
+ , 'name' : self.decl_identifier
, 'doc' : doc }
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 )
+ def _generate_for_smart_ptr( self ):
+ doc = ''
+ add_property = ''
+ make_getter = algorithm.create_identifier( self, '::boost::python::make_getter')
+ make_setter = algorithm.create_identifier( self, '::boost::python::make_setter')
+ if self.declaration.type_qualifiers.has_static:
+ add_property = 'add_static_property'
+ else:
+ if self.documentation:
+ doc = self.documentation
+ add_property = 'add_property'
+ add_property_args = [ '"%s"' % self.alias ]
+ getter_code = declarations.call_invocation.join(
+ make_getter
+ , [ '&' + self.decl_identifier
+ , self.declaration.getter_call_policies.create( self ) ]
+ , os.linesep + self.indent( self.PARAM_SEPARATOR, 6) )
+
+ add_property_args.append( getter_code )
+ if not self.declaration.is_read_only:
+ setter_code = ''
+ setter_args = [ '&' + self.decl_identifier ]
+ if self.declaration.setter_call_policies \
+ and not self.declaration.setter_call_policies.is_default():
+ setter_args.append( self.declaration.setter_call_policies.create( self ) )
+ setter_code = declarations.call_invocation.join(
+ make_setter
+ , setter_args
+ , os.linesep + self.indent( self.PARAM_SEPARATOR, 6) )
+ add_property_args.append( setter_code)
+ if doc:
+ add_property_args.append( doc )
+ return declarations.call_invocation.join(
+ add_property
+ , add_property_args
+ , os.linesep + self.indent( self.PARAM_SEPARATOR, 4 ) )
- 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 ):
+ def _create_impl( self ):
if declarations.is_pointer( self.declaration.type ):
return self._generate_for_pointer()
+ elif self.declaration.apply_smart_ptr_wa:
+ return self._generate_for_smart_ptr()
else:
return self._generate_for_none_pointer()
- def _create_impl(self):
- return self._generate_variable()
-
class member_variable_wrapper_t( code_creator.code_creator_t
, declaration_based.declaration_based_t ):
"""
Modified: pyplusplus_dev/pyplusplus/decl_wrappers/variable_wrapper.py
===================================================================
--- pyplusplus_dev/pyplusplus/decl_wrappers/variable_wrapper.py 2006-12-08 19:47:42 UTC (rev 787)
+++ pyplusplus_dev/pyplusplus/decl_wrappers/variable_wrapper.py 2006-12-10 06:27:20 UTC (rev 788)
@@ -6,9 +6,10 @@
"""defines class that configure global and member variable exposing"""
import decl_wrapper
+import python_traits
+import call_policies
from pyplusplus import messages
from pygccxml import declarations
-from pyplusplus.decl_wrappers import python_traits
class variable_t(decl_wrapper.decl_wrapper_t, declarations.variable_t):
"""defines a set of properties, that will instruct Py++ how to expose the variable"""
@@ -17,6 +18,8 @@
decl_wrapper.decl_wrapper_t.__init__( self )
self._getter_call_policies = None
self._setter_call_policies = None
+ self._apply_smart_ptr_wa = False
+ self._is_read_only = None
__call_policies_doc__ = \
"""There are usecase, when exporting member variable forces Py++ to
@@ -29,6 +32,14 @@
"""
def get_getter_call_policies( self ):
+ if None is self._getter_call_policies:
+ if self.apply_smart_ptr_wa:
+ value_policy = ''
+ if self.is_read_only:
+ value_policy = call_policies.copy_const_reference
+ else:
+ value_policy = call_policies.copy_non_const_reference
+ self._getter_call_policies = call_policies.return_value_policy( value_policy )
return self._getter_call_policies
def set_getter_call_policies( self, call_policies ):
self._getter_call_policies = call_policies
@@ -36,12 +47,55 @@
, doc=__call_policies_doc__ )
def get_setter_call_policies( self ):
+ if None is self._getter_call_policies:
+ if self.apply_smart_ptr_wa:
+ self._setter_call_policies = call_policies.default_call_policies()
return self._setter_call_policies
def set_setter_call_policies( self, call_policies ):
self._setter_call_policies = call_policies
setter_call_policies = property( get_setter_call_policies, set_setter_call_policies
, doc=__call_policies_doc__ )
+ def get_apply_smart_ptr_wa( self ):
+ return self._apply_smart_ptr_wa
+ def set_apply_smart_ptr_wa( self, value):
+ self._apply_smart_ptr_wa = value
+ apply_smart_ptr_wa = property( get_apply_smart_ptr_wa, set_apply_smart_ptr_wa
+ , doc="" )
+
+
+ def __find_out_is_read_only(self):
+ type_ = declarations.remove_alias( self.type )
+
+ if isinstance( type_, declarations.const_t ):
+ return True
+
+ 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 ):
+ return True
+
+ if self.apply_smart_ptr_wa:
+ return False #all smart pointers has assign operator
+
+ 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 get_is_read_only( self ):
+ if None is self._is_read_only:
+ self._is_read_only = self.__find_out_is_read_only()
+ return self._is_read_only
+ def set_is_read_only( self, v ):
+ self._is_read_only = v
+ is_read_only = property( get_is_read_only, set_is_read_only )
+
def _exportable_impl( self ):
if not self.name:
return messages.W1033
Modified: pyplusplus_dev/unittests/data/smart_pointers_to_be_exported.hpp
===================================================================
--- pyplusplus_dev/unittests/data/smart_pointers_to_be_exported.hpp 2006-12-08 19:47:42 UTC (rev 787)
+++ pyplusplus_dev/unittests/data/smart_pointers_to_be_exported.hpp 2006-12-10 06:27:20 UTC (rev 788)
@@ -1,56 +1,56 @@
-// Copyright 2004 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)
-
-#ifndef __smart_pointers_to_be_exported_hpp__
-#define __smart_pointers_to_be_exported_hpp__
-#include <memory>
-#include "boost/shared_ptr.hpp"
-
-namespace smart_pointers{
-
-struct base{
- base() : base_value(19) {}
- int base_value;
+// Copyright 2004 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)
+
+#ifndef __smart_pointers_to_be_exported_hpp__
+#define __smart_pointers_to_be_exported_hpp__
+#include <memory>
+#include "boost/shared_ptr.hpp"
+
+namespace smart_pointers{
+
+struct base{
+ base() : base_value(19) {}
+ int base_value;
virtual int get_base_value(){ return base_value; }
- virtual int get_some_value() = 0;
-};
-
-struct data : base{
- data() : value(11){}
- int value;
+ virtual int get_some_value() = 0;
+};
+
+struct data : base{
+ data() : value(11){}
+ int value;
virtual int get_value(){ return value; }
- virtual int get_some_value(){ return 23; }
-};
-
-typedef std::auto_ptr< base > base_a_ptr;
-typedef boost::shared_ptr< base > base_s_ptr;
-
-typedef std::auto_ptr< data > data_a_ptr;
-typedef boost::shared_ptr< data > data_s_ptr;
-
-data_a_ptr create_auto();
-data_s_ptr create_shared();
-
-int ref_auto( data_a_ptr& a );
-int ref_shared( data_s_ptr& a );
-
-int val_auto( data_a_ptr a );
-int val_shared( data_s_ptr a );
-
-int const_ref_auto( const data_a_ptr& a );
-int const_ref_shared( const data_s_ptr& a );
-
-int ref_auto_base_value( base_a_ptr& a );
-int ref_shared_base_value( base_s_ptr& a );
-
-int val_auto_base_value( base_a_ptr a );
-int val_shared_base_value( base_s_ptr a );
-
-int const_ref_auto_base_value( const base_a_ptr& a );
-int const_ref_shared_base_value( const base_s_ptr& a );
+ virtual int get_some_value(){ return 23; }
+};
+typedef std::auto_ptr< base > base_a_ptr;
+typedef boost::shared_ptr< base > base_s_ptr;
+
+typedef std::auto_ptr< data > data_a_ptr;
+typedef boost::shared_ptr< data > data_s_ptr;
+
+data_a_ptr create_auto();
+data_s_ptr create_shared();
+
+int ref_auto( data_a_ptr& a );
+int ref_shared( data_s_ptr& a );
+
+int val_auto( data_a_ptr a );
+int val_shared( data_s_ptr a );
+
+int const_ref_auto( const data_a_ptr& a );
+int const_ref_shared( const data_s_ptr& a );
+
+int ref_auto_base_value( base_a_ptr& a );
+int ref_shared_base_value( base_s_ptr& a );
+
+int val_auto_base_value( base_a_ptr a );
+int val_shared_base_value( base_s_ptr a );
+
+int const_ref_auto_base_value( const base_a_ptr& a );
+int const_ref_shared_base_value( const base_s_ptr& a );
+
int ref_auto_some_value( base_a_ptr& a );
int ref_shared_some_value( base_s_ptr& a );
@@ -60,7 +60,24 @@
int const_ref_auto_some_value( const base_a_ptr& a );
int const_ref_shared_some_value( const base_s_ptr& a );
-
-}
-
-#endif//__smart_pointers_to_be_exported_hpp__
+struct shared_data_buffer_t{
+ shared_data_buffer_t()
+ : size( 0 )
+ {}
+ int size;
+};
+
+struct shared_data_buffer_holder_t{
+ typedef boost::shared_ptr<shared_data_buffer_t> holder_impl_t;
+ shared_data_buffer_holder_t()
+ : buffer( new shared_data_buffer_t() )
+ , const_buffer( new shared_data_buffer_t() )
+ {}
+
+ holder_impl_t buffer;
+ const holder_impl_t const_buffer;
+};
+
+}
+
+#endif//__smart_pointers_to_be_exported_hpp__
Modified: pyplusplus_dev/unittests/smart_pointers_tester.py
===================================================================
--- pyplusplus_dev/unittests/smart_pointers_tester.py 2006-12-08 19:47:42 UTC (rev 787)
+++ pyplusplus_dev/unittests/smart_pointers_tester.py 2006-12-10 06:27:20 UTC (rev 788)
@@ -23,6 +23,8 @@
base = mb.class_( 'base' )
shared_ptrs = mb.decls( lambda decl: decl.name.startswith( 'shared_ptr<' ) )
shared_ptrs.disable_warnings( messages.W1040 )
+ mb.variable( 'buffer' ).apply_smart_ptr_wa = True
+ mb.variable( 'const_buffer' ).apply_smart_ptr_wa = True
def create_py_derived( self, module ):
class py_derived_t( module.base ):
@@ -82,6 +84,16 @@
self.failUnless( 23 == module.const_ref_auto_some_value(da) )
self.failUnless( 28 == module.const_ref_shared_some_value(py_derived) )
+ holder1 = module.shared_data_buffer_holder_t()
+ self.failUnless( holder1.buffer.size == 0 )
+
+ holder2 = module.shared_data_buffer_holder_t()
+ holder2.buffer.size = 2
+
+ holder1.buffer = holder2.buffer
+ self.failUnless( holder1.buffer.size == 2 )
+ holder1.buffer.size = 3
+ self.failUnless( holder2.buffer.size == 3 )
def create_suite():
suite = unittest.TestSuite()
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|