[pygccxml-commit] SF.net SVN: pygccxml: [919] pyplusplus_dev
Brought to you by:
mbaas,
roman_yakovenko
From: <rom...@us...> - 2007-02-22 12:19:30
|
Revision: 919 http://svn.sourceforge.net/pygccxml/?rev=919&view=rev Author: roman_yakovenko Date: 2007-02-22 04:19:27 -0800 (Thu, 22 Feb 2007) Log Message: ----------- adding functionality necessary to implement correct "transfer_ownership" behaviour Modified Paths: -------------- pyplusplus_dev/pyplusplus/code_creators/calldef.py pyplusplus_dev/pyplusplus/decl_wrappers/calldef_wrapper.py pyplusplus_dev/pyplusplus/module_creator/creator.py Added Paths: ----------- pyplusplus_dev/unittests/data/transfer_ownership_to_be_exported.hpp pyplusplus_dev/unittests/transfer_ownership_tester.py Modified: pyplusplus_dev/pyplusplus/code_creators/calldef.py =================================================================== --- pyplusplus_dev/pyplusplus/code_creators/calldef.py 2007-02-20 19:41:57 UTC (rev 918) +++ pyplusplus_dev/pyplusplus/code_creators/calldef.py 2007-02-22 12:19:27 UTC (rev 919) @@ -260,6 +260,9 @@ if not self.declaration.overridable: return self.unoverriden_function_body() template = [] + precall_code = self.declaration.override_precall_code + if precall_code: + template.append( os.linesep.join( precall_code ) ) template.append( '%(override)s func_%(alias)s = this->get_override( "%(alias)s" );' ) template.append( '%(return_)sfunc_%(alias)s( %(args)s );') template = os.linesep.join( template ) @@ -360,6 +363,9 @@ def create_virtual_body(self): template = [] + precall_code = self.declaration.override_precall_code + if precall_code: + template.append( os.linesep.join( precall_code ) ) template.append( 'if( %(override)s func_%(alias)s = this->get_override( "%(alias)s" ) )' ) template.append( self.indent('%(return_)sfunc_%(alias)s( %(args)s );') ) template.append( 'else' ) @@ -385,8 +391,12 @@ body = self.wrapped_class_identifier() + '::' + function_call + ';' if not declarations.is_void( self.declaration.return_type ): body = 'return ' + body + precall_code = self.declaration.default_precall_code + if precall_code: + body = os.linesep.join( precall_code ) + os.linesep + body return body + def create_function(self): answer = [ self.create_declaration(self.declaration.name) + '{' ] answer.append( self.indent( self.create_virtual_body() ) ) Modified: pyplusplus_dev/pyplusplus/decl_wrappers/calldef_wrapper.py =================================================================== --- pyplusplus_dev/pyplusplus/decl_wrappers/calldef_wrapper.py 2007-02-20 19:41:57 UTC (rev 918) +++ pyplusplus_dev/pyplusplus/decl_wrappers/calldef_wrapper.py 2007-02-22 12:19:27 UTC (rev 919) @@ -215,7 +215,23 @@ declarations.member_function_t.__init__( self, *arguments, **keywords ) calldef_t.__init__( self ) self._use_overload_macro = False + self._override_precall_code = [] + self._default_precall_code = [] + def add_override_precall_code(self, code): + self._override_precall_code.append( code ) + + @property + def override_precall_code(self): + return self._override_precall_code + + def add_default_precall_code(self, code): + self._default_precall_code.append( code ) + + @property + def default_precall_code(self): + return self._default_precall_code + def get_use_overload_macro(self): return self._use_overload_macro def set_use_overload_macro(self, use_macro): Modified: pyplusplus_dev/pyplusplus/module_creator/creator.py =================================================================== --- pyplusplus_dev/pyplusplus/module_creator/creator.py 2007-02-20 19:41:57 UTC (rev 918) +++ pyplusplus_dev/pyplusplus/module_creator/creator.py 2007-02-22 12:19:27 UTC (rev 919) @@ -154,7 +154,9 @@ if doc_extractor and decl.exportable: if decl.documentation: - decl.documentation = decl.documentation + doc_extractor( decl ) + extracted_doc = doc_extractor( decl ) + if extracted_doc != decl.documentation: + decl.documentation = decl.documentation + extracted_doc else: decl.documentation = doc_extractor( decl ) Added: pyplusplus_dev/unittests/data/transfer_ownership_to_be_exported.hpp =================================================================== --- pyplusplus_dev/unittests/data/transfer_ownership_to_be_exported.hpp (rev 0) +++ pyplusplus_dev/unittests/data/transfer_ownership_to_be_exported.hpp 2007-02-22 12:19:27 UTC (rev 919) @@ -0,0 +1,31 @@ +#ifndef __transfer_ownership_to_be_exported_hpp__ +#define __transfer_ownership_to_be_exported_hpp__ + +struct event_t +{ + virtual void notify() = 0; + virtual ~event_t(){} +}; + +struct do_nothing_t : event_t{ + virtual void notify(){}; +}; + +struct simulator_t{ + + void schedule(event_t *event) { + m_event = event; + }; + + void run() { + m_event->notify(); + delete m_event; + m_event = 0; + }; + +private: + event_t* m_event; +}; + + +#endif//__transfer_ownership_to_be_exported_hpp__ Added: pyplusplus_dev/unittests/transfer_ownership_tester.py =================================================================== --- pyplusplus_dev/unittests/transfer_ownership_tester.py (rev 0) +++ pyplusplus_dev/unittests/transfer_ownership_tester.py 2007-02-22 12:19:27 UTC (rev 919) @@ -0,0 +1,107 @@ +# 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) + +import os +import sys +import unittest +import fundamental_tester_base +from pyplusplus import code_creators +from pyplusplus import function_transformers as ft + +decref_code = \ +""" +virtual ~%(cls)s(){ + if (this->m_pyobj) { + //Py_DECREF(this->m_pyobj); + this->m_pyobj = 0; + } +} +""" + +incref_code = \ +""" +if( !this->m_pyobj) { + this->m_pyobj = boost::python::detail::wrapper_base_::get_owner(*this); + Py_INCREF(this->m_pyobj); +} +""" + +impl_conv_code = \ +""" +boost::python::implicitly_convertible< std::auto_ptr< %(from)s >, std::auto_ptr< %(to)s > >(); +""" + +class tester_t(fundamental_tester_base.fundamental_tester_base_t): + EXTENSION_NAME = 'transfer_ownership' + + def __init__( self, *args ): + fundamental_tester_base.fundamental_tester_base_t.__init__( + self + , tester_t.EXTENSION_NAME + , *args ) + + def customize( self, mb ): + event_clss = mb.classes( lambda cls: cls.name in ( 'event_t', 'do_nothing_t' ) ) + for cls in event_clss: + cls.add_wrapper_code( decref_code % { 'cls' : cls.wrapper_alias } ) + cls.add_wrapper_code( 'PyObject* m_pyobj;' ) + cls.set_constructors_body( 'm_pyobj=0;' ) + cls.mem_fun( 'notify' ).add_override_precall_code( incref_code ) + cls.mem_fun( 'notify' ).add_default_precall_code( incref_code ) + + cls.held_type = 'std::auto_ptr< %s >' % cls.wrapper_alias + cls.add_registration_code( impl_conv_code % { 'from' : cls.wrapper_alias + , 'to' : cls.decl_string } + , False) + for base in cls.recursive_bases: + if base.access_type == 'public': + cls.add_registration_code( #from class to its base + impl_conv_code % { 'from' : cls.decl_string + , 'to' : base.related_class.decl_string } + , False) + + cls.add_registration_code( #from wrapper to clas base class + impl_conv_code % { 'from' : cls.wrapper_alias + , 'to' : base.related_class.decl_string } + , False) + + schedule = mb.mem_fun( 'schedule' ) + schedule.add_transformation( ft.transfer_ownership(0), alias='schedule' ) + + def run_tests( self, module): + class py_event_t( module.event_t ): + def __init__( self, container ): + module.event_t.__init__( self ) + self.container = container + + def notify( self ): + print 1 + #self.container.append( 1 ) + + print '1' + notify_data = [] + simulator = module.simulator_t() + print '2' + event = py_event_t( notify_data ) + print '3' + simulator.schedule( event ) + print 'refcount: ', sys.getrefcount( event ) + print '4' + del event + print '5' + simulator.run() + print '6' + self.failUnless( notify_data[0] == 1 ) + +def create_suite(): + suite = unittest.TestSuite() + suite.addTest( unittest.makeSuite(tester_t)) + return suite + +def run_suite(): + unittest.TextTestRunner(verbosity=2).run( create_suite() ) + +if __name__ == "__main__": + run_suite() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |