[C++-sig] weakref to shared_ptr conversion

Roman Yakovenko roman.yakovenko at gmail.com
Wed Jan 11 06:47:12 CET 2006


> How do i get such an implicit conversion from weakref to shared_ptr
> working? Is it possible to define an implicit converter to solve this?
> (I'm using boost 1.33.1, MSVC7.1 and Python 2.4.2)

1. See manuals for HeldType of class
http://boost.org/libs/python/doc/v2/class.htm

2. See http://boost.org/libs/python/doc/v2/register_ptr_to_python.html

3. See http://boost.org/libs/python/doc/v2/implicit.html

4. Create simple use case and try it with one of the boost.python code
generators.
By the way, pyplusplus has small user friendly GUI, so you have to
learn almost nothing
in order to try.

I attach 4 files, unit tests of pyplusplus, that checks smart pointers
code generation.
They could be useful for understanding of the subject.

smart_pointers.cpp contains code created by pyplusplus
smart_pointers_to_be_exported.[hpp|cpp] contains code that should be
exported :-)
smart_pointers_tester.py contains tests, see run_tests function



> Any help would be appreciated,
> andreas rose

Roman Yakovenko
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smart_pointers.cpp
Type: application/octet-stream
Size: 6156 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20060111/26b5d3d8/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smart_pointers_to_be_exported.hpp
Type: application/octet-stream
Size: 1392 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20060111/26b5d3d8/attachment-0001.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smart_pointers_to_be_exported.cpp
Type: application/octet-stream
Size: 1241 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20060111/26b5d3d8/attachment-0002.obj>
-------------- next part --------------
# 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 pygccxml import declarations
from pyplusplus import code_creators

class tester_t(fundamental_tester_base.fundamental_tester_base_t):
    EXTENSION_NAME = 'smart_pointers'
    
    def __init__( self, *args ):
        fundamental_tester_base.fundamental_tester_base_t.__init__( 
            self
            , tester_t.EXTENSION_NAME
            , *args )

    def customize_decls(self, decls):
        def should_stay(decl):
            if isinstance( decl, declarations.namespace_t ):
                return True
            fname = declarations.full_name( decl )
            return fname.startswith( '::smart_pointers' ) 
        return declarations.filtering.user_defined( decls, should_stay )    

    def customize_module(self, extmodule ):
        pass
   
    def run_tests( self, module):
        da = module.create_auto()
        self.failUnless( 11 == da.value )
        ds = module.create_shared()
        self.failUnless( 11 == ds.value )
        
        self.failUnless( 11 == module.ref_auto(da) )
        self.failUnless( 11 == module.ref_shared(ds) )

        self.failUnless( 11 == module.val_auto(da) )
        self.failUnless( 11 == module.val_shared(ds) )

        da = module.create_auto()

        self.failUnless( 11 == module.const_ref_auto(da) )
        self.failUnless( 11 == module.const_ref_shared(ds) )
        
        #TODO: find out why this fails
        #self.failUnless( 19 == module.ref_auto_base_value(da) )
        #self.failUnless( 19 == module.ref_shared_base_value(ds) )

        da = module.create_auto()

        self.failUnless( 19 == module.const_ref_auto_base_value(da) )
        self.failUnless( 19 == module.const_ref_shared_base_value(ds) )

        da = module.create_auto()

        self.failUnless( 19 == module.val_auto_base_value(da) )
        self.failUnless( 19 == module.val_shared_base_value(ds) )
    
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()




More information about the Cplusplus-sig mailing list