[pygccxml-commit] SF.net SVN: pygccxml: [368] pyplusplus_dev/unittests
Brought to you by:
mbaas,
roman_yakovenko
From: <rom...@us...> - 2006-07-30 14:05:09
|
Revision: 368 Author: roman_yakovenko Date: 2006-07-30 07:04:45 -0700 (Sun, 30 Jul 2006) ViewCVS: http://svn.sourceforge.net/pygccxml/?rev=368&view=rev Log Message: ----------- "inserting code" document has been finished Modified Paths: -------------- pyplusplus_dev/docs/documentation/inserting_code.rest pyplusplus_dev/pyplusplus/code_creators/module.py pyplusplus_dev/pyplusplus/module_builder/builder.py pyplusplus_dev/unittests/module_body_tester.py Modified: pyplusplus_dev/docs/documentation/inserting_code.rest =================================================================== --- pyplusplus_dev/docs/documentation/inserting_code.rest 2006-07-30 08:58:23 UTC (rev 367) +++ pyplusplus_dev/docs/documentation/inserting_code.rest 2006-07-30 14:04:45 UTC (rev 368) @@ -24,7 +24,7 @@ ... BOOST_PYTHON_MODULE(X) { - //registration code + //registrations code ... } @@ -41,10 +41,6 @@ This function will ass a code to the registration section. If you want to add the code to the head of the section, pass ``tail=False`` to the method. - -Both these methods have same precondition: they should be called after -``build_code_creator`` method has been called. Both these methods work directly -with code creators tree, hence the precondition. Example ------- @@ -72,13 +68,12 @@ ... }; +``int`` is immutable type in Python. So you can not expose ``get_size`` member +function as is. You need to create a wrapper and expose it. - ``int`` is immutable type in Python. So you can not expose ``get_size`` member - function as is. You need to create a wrapper and expose it. +In the near future ``pyplusplus`` will eliminate the need of creating hand +written wrapper for this use case. - In the near future ``pyplusplus`` will eliminate the need of creating hand - written wrapper for this use case. - :: boost::python::tuple get_window_size( const window_t& win ){ @@ -103,7 +98,9 @@ * ``add_declaration_code( self, code )`` **Not implemented.** + **Feedback is wanted.** + **Please consider the relationship between this code and class wrapper code.** This method will add the code to the declaration section within the module. @@ -121,7 +118,7 @@ #From Python user can call this method like this: win = window_t( ) height, width = win.get_size() - + If you will pass ``works_on_instance=False`` next code will be generated: :: @@ -131,7 +128,7 @@ ... def( "get_size", &::get_window_size ); } - + And in this case, user will be forced to pass reference to ``window_t`` object: :: @@ -154,8 +151,23 @@ ---------------------------- I don't know what about you, but I don't like to create free functions in global -namespace.I prefer to add ``get_window_size`` function to +namespace. I prefer to add ``get_window_size`` function to ``window_t`` +`class wrapper`__. Class declaration exposes ``add_wrapper_code( self, code )`` +method. This method will add the code to the end of the class declaration. +Example +------- + +:: + + mb = module_builder_t( ... ) + window = mb.class_( 'window_t' ) + window.add_wrapper_code( get_window_size definition ) + window.add_registration_code( 'def( "get_size", &%s::get_window_size )' % window.wrapper_alias ) + +.. __ : http://boost.org/libs/python/doc/tutorial/doc/html/python/exposing.html#python.class_virtual_functions + + .. _`pyplusplus` : ./../pyplusplus.html .. _`pygccxml` : ./../../pygccxml/pygccxml.html .. _`boost.python`: http://www.boost.org/libs/python/doc/index.html Modified: pyplusplus_dev/pyplusplus/code_creators/module.py =================================================================== --- pyplusplus_dev/pyplusplus/code_creators/module.py 2006-07-30 08:58:23 UTC (rev 367) +++ pyplusplus_dev/pyplusplus/code_creators/module.py 2006-07-30 14:04:45 UTC (rev 368) @@ -187,7 +187,10 @@ def add_declaration_code( self, code, position ): creator = custom.custom_text_t( code ) - self.adopt_creator( creator, min( position, self.creators.index( self.body ) ) ) + last_include = self.last_include_index() + pos = max( last_include + 1, position ) + pos = min( pos, self.creators.index( self.body ) ) + self.adopt_creator( creator, pos ) Modified: pyplusplus_dev/pyplusplus/module_builder/builder.py =================================================================== --- pyplusplus_dev/pyplusplus/module_builder/builder.py 2006-07-30 08:58:23 UTC (rev 367) +++ pyplusplus_dev/pyplusplus/module_builder/builder.py 2006-07-30 14:04:45 UTC (rev 368) @@ -89,8 +89,14 @@ , indexing_suite_version) self.__code_creator = None if optimize_queries: - self.run_query_optimizer() - + self.run_query_optimizer() + + self.__declarations_code_head = [] + self.__declarations_code_tail = [] + + self.__registrations_code_head = [] + self.__registrations_code_tail = [] + def _get_global_ns( self ): return self.__global_ns global_ns = property( _get_global_ns, doc="reference to global namespace" ) @@ -161,7 +167,27 @@ , mem_vars ) map( lambda mem_var: mem_var.set_setter_call_policies( call_policies_resolver( mem_var, 'set' ) ) , mem_vars ) + + @property + def declarations_code_head( self ): + "List of user code, that will be added to the head of the declarations section." + return self.__declarations_code_head + @property + def declarations_code_tail( self ): + "List of user code, that will be added to the tail of the declarations section." + return self.__declarations_code_tail + + @property + def registrations_code_head( self ): + "List of user code, that will be added to the head of the registrations section." + return self.__registrations_code_head + + @property + def registrations_code_tail( self ): + "List of user code, that will be added to the tail of the registrations section." + return self.__registrations_code_tail + def print_declarations(self, decl=None, detailed=True, recursive=True, writer=sys.stdout.write): """ This function will print detailed description of all declarations or @@ -230,13 +256,42 @@ called and False otherwise """ return not ( None is self.__code_creator ) - + + def add_declaration_code( self, code, tail=True ): + if tail: + self.__declarations_code_tail.append( code ) + else: + self.__declarations_code_head.append( code ) + + def add_registration_code( self, code, tail=True ): + if tail: + self.__registrations_code_tail.append( code ) + else: + self.__registrations_code_head.append( code ) + + def __merge_user_code( self ): + for code in self.__declarations_code_tail: + self.code_creator.add_declaration_code( code, -1 ) + + for code in self.__declarations_code_head: + self.code_creator.add_declaration_code( code, 0 ) + + body = self.code_creator.body + + for code in self.__registrations_code_tail: + body.adopt_creator( code_creators.custom_text_t( code ), -1 ) + + for code in self.__registrations_code_head: + body.adopt_creator( code_creators.custom_text_t( code ), 0 ) + + def write_module( self, file_name ): """ Writes module to single file @param file_name: file name @type file_name: string - """ + """ + self.__merge_user_code() file_writers.write_file( self.code_creator, file_name ) def split_module(self, dir_name, huge_classes=None): @@ -248,6 +303,7 @@ @param huge_classes: list that contains reference to classes, that should be split """ + self.__merge_user_code() if None is huge_classes: file_writers.write_multiple_files( self.code_creator, dir_name ) else: @@ -508,16 +564,3 @@ def _set_BOOST_PYTHON_MAX_ARITY( self, value ): decl_wrappers.calldef_t.BOOST_PYTHON_MAX_ARITY = value BOOST_PYTHON_MAX_ARITY = property( _get_BOOST_PYTHON_MAX_ARITY, _set_BOOST_PYTHON_MAX_ARITY ) - - def add_declaration_code( self, code, tail=True ): - position = 0 - if tail: - position = -1 - self.code_creator.add_declaration_code( code, position ) - - def add_registration_code( self, code, tail=True ): - position = 0 - if tail: - position = -1 - creator = code_creators.custom_text_t( code ) - self.code_creator.body.adopt_creator( creator, position ) Modified: pyplusplus_dev/unittests/module_body_tester.py =================================================================== --- pyplusplus_dev/unittests/module_body_tester.py 2006-07-30 08:58:23 UTC (rev 367) +++ pyplusplus_dev/unittests/module_body_tester.py 2006-07-30 14:04:45 UTC (rev 368) @@ -20,7 +20,9 @@ def customize(self, mb): mb.build_code_creator( self.EXTENSION_NAME ) mb.add_declaration_code( "int get1(){ return 1;} ") + mb.add_declaration_code( "//this is a comment", False ) mb.add_registration_code( 'bp::def( "get1", &get1 );' ) + mb.add_registration_code( '//this is another comment', False ) def run_tests(self, module): self.failUnless( 1 == module.get1() ) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |