[pygccxml-commit] SF.net SVN: pygccxml: [1097] pyplusplus_dev
Brought to you by:
mbaas,
roman_yakovenko
From: <rom...@us...> - 2007-08-10 20:58:26
|
Revision: 1097 http://pygccxml.svn.sourceforge.net/pygccxml/?rev=1097&view=rev Author: roman_yakovenko Date: 2007-08-10 13:58:25 -0700 (Fri, 10 Aug 2007) Log Message: ----------- adding ability to register array class wrapper only once Modified Paths: -------------- pyplusplus_dev/pyplusplus/code_creators/array_1_registrator.py pyplusplus_dev/pyplusplus/code_repository/array_1.py pyplusplus_dev/unittests/arrays_bug_tester.py pyplusplus_dev/unittests/data/arrays_bug_to_be_exported.hpp Modified: pyplusplus_dev/pyplusplus/code_creators/array_1_registrator.py =================================================================== --- pyplusplus_dev/pyplusplus/code_creators/array_1_registrator.py 2007-08-10 20:57:07 UTC (rev 1096) +++ pyplusplus_dev/pyplusplus/code_creators/array_1_registrator.py 2007-08-10 20:58:25 UTC (rev 1097) @@ -57,8 +57,10 @@ fn_name = 'register_array_1' fn_def_tmpl_args = [ declarations.array_item_type(self.array_type).decl_string - , str( declarations.array_size(self.array_type) ) - , self.call_policies.create(self, call_policies.CREATION_POLICY.AS_TEMPLATE_ARGUMENT )] + , str( declarations.array_size(self.array_type) ) ] + if not self.call_policies.is_default(): + fn_def_tmpl_args.append( + self.call_policies.create(self, call_policies.CREATION_POLICY.AS_TEMPLATE_ARGUMENT ) ) fn_def = templates.join( '::'.join( [ns_name, fn_name] ), fn_def_tmpl_args ) return call_invocation.join( fn_def, [ '"%s"' % self._create_name() ] ) + ';' Modified: pyplusplus_dev/pyplusplus/code_repository/array_1.py =================================================================== --- pyplusplus_dev/pyplusplus/code_repository/array_1.py 2007-08-10 20:57:07 UTC (rev 1096) +++ pyplusplus_dev/pyplusplus/code_repository/array_1.py 2007-08-10 20:58:25 UTC (rev 1097) @@ -25,7 +25,10 @@ #include "boost/mpl/if.hpp" #include "boost/type_traits/is_same.hpp" #include "boost/type_traits/is_fundamental.hpp" +#include "boost/python/converter/registry.hpp" +#include <iostream> + //1 - dimension namespace pyplusplus{ namespace containers{ namespace static_sized{ @@ -50,6 +53,20 @@ }; +template<class T> +bool is_registered(){ + namespace bpl = boost::python; + bpl::handle<> class_obj( bpl::objects::registered_class_object( bpl::type_id< T >())); + return class_obj.get() ? true : false; +} + +template< class T > +void register_alias( const char* name ){ + namespace bpl = boost::python; + bpl::handle<> class_obj( bpl::objects::registered_class_object( bpl::type_id< T >())); + boost::python::scope().attr( name ) = bpl::object( class_obj ); +} + }//details template< class TItemType, long unsigned int size > @@ -124,32 +141,53 @@ }; -template< class TItemType, long unsigned int size, typename CallPolicies > -void register_const_array_1(const char* name){ - typedef const_array_1_t< TItemType, size > wrapper_t; - boost::python::class_< wrapper_t >( name, boost::python::no_init ) - .def( "__getitem__" - , &wrapper_t::item_ref - , ( boost::python::arg("index") ) - , CallPolicies() ) - .def( "__len__", &wrapper_t::len ); -} +template< class TItemType + , long unsigned int size + , typename CallPolicies=boost::python::default_call_policies > +struct register_const_array_1{ + register_const_array_1(const char* name){ + namespace bpl = boost::python; + typedef const_array_1_t< TItemType, size > wrapper_t; -template< class TItemType, long unsigned int size, typename CallPolicies > -void register_array_1(const char* name){ - typedef array_1_t< TItemType, size > wrapper_t; - boost::python::class_< wrapper_t >( name, boost::python::no_init ) - .def( "__getitem__" - , &wrapper_t::item_ref - , ( boost::python::arg("index") ) - , CallPolicies() ) - .def( "__setitem__" - , &wrapper_t::set_item - , ( boost::python::arg("index"), boost::python::arg("value") ) - , CallPolicies() ) - .def( "__len__", &wrapper_t::len ); -} + if( details::is_registered< wrapper_t >() ){ + details::register_alias< wrapper_t >( name ); + } + else{ + bpl::class_< wrapper_t >( name, bpl::no_init ) + .def( "__getitem__" + , &wrapper_t::item_ref + , ( bpl::arg("index") ) + , CallPolicies() ) + .def( "__len__", &wrapper_t::len ); + } + } +}; +template< class TItemType + , long unsigned int size + , typename CallPolicies=boost::python::default_call_policies > +struct register_array_1{ + register_array_1(const char* name){ + namespace bpl = boost::python; + typedef array_1_t< TItemType, size > wrapper_t; + if( details::is_registered< wrapper_t >() ){ + details::register_alias< wrapper_t >( name ); + } + else{ + bpl::class_< wrapper_t >( name, bpl::no_init ) + .def( "__getitem__" + , &wrapper_t::item_ref + , ( bpl::arg("index") ) + , CallPolicies() ) + .def( "__setitem__" + , &wrapper_t::set_item + , ( bpl::arg("index"), bpl::arg("value") ) + , CallPolicies() ) + .def( "__len__", &wrapper_t::len ); + } + } +}; + } /*pyplusplus*/ } /*containers*/ } /*static_sized*/ Modified: pyplusplus_dev/unittests/arrays_bug_tester.py =================================================================== --- pyplusplus_dev/unittests/arrays_bug_tester.py 2007-08-10 20:57:07 UTC (rev 1096) +++ pyplusplus_dev/unittests/arrays_bug_tester.py 2007-08-10 20:58:25 UTC (rev 1097) @@ -17,7 +17,11 @@ self , tester_t.EXTENSION_NAME , *args ) - + + def customize(self, mb ): + mb.add_registration_code( 'pyplusplus::containers::static_sized::register_array_1< int, 10 >( "X1" );' ) + mb.add_registration_code( 'pyplusplus::containers::static_sized::register_array_1< int, 10 >( "X2" );' ) + def run_tests( self, module): m = module.arrays_bug c = m.container() @@ -26,6 +30,8 @@ y = c.items[0] c.items[0] = m.item() + self.failUnless( id(module.X1) == id(module.X2) == id( c.items[0].values.__class__ ) ) + def create_suite(): suite = unittest.TestSuite() suite.addTest( unittest.makeSuite(tester_t)) Modified: pyplusplus_dev/unittests/data/arrays_bug_to_be_exported.hpp =================================================================== --- pyplusplus_dev/unittests/data/arrays_bug_to_be_exported.hpp 2007-08-10 20:57:07 UTC (rev 1096) +++ pyplusplus_dev/unittests/data/arrays_bug_to_be_exported.hpp 2007-08-10 20:58:25 UTC (rev 1097) @@ -12,6 +12,11 @@ struct container{ item items[10]; }; +struct const_item{ const int values[10]; }; + +struct const_container{ const const_item items[10]; }; + + }; #endif//__arrays_bug_to_be_exported_hpp__ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |