[pygccxml-commit] SF.net SVN: pygccxml: [489] pyplusplus_dev/unittests
Brought to you by:
mbaas,
roman_yakovenko
From: <rom...@us...> - 2006-08-29 11:29:05
|
Revision: 489 Author: roman_yakovenko Date: 2006-08-29 04:28:51 -0700 (Tue, 29 Aug 2006) ViewCVS: http://svn.sourceforge.net/pygccxml/?rev=489&view=rev Log Message: ----------- adding custom smart pointers tester Added Paths: ----------- pyplusplus_dev/unittests/custom_smart_ptr_classes_tester.py pyplusplus_dev/unittests/data/custom_smart_ptr.h pyplusplus_dev/unittests/data/custom_smart_ptr_classes_to_be_exported.hpp Added: pyplusplus_dev/unittests/custom_smart_ptr_classes_tester.py =================================================================== --- pyplusplus_dev/unittests/custom_smart_ptr_classes_tester.py (rev 0) +++ pyplusplus_dev/unittests/custom_smart_ptr_classes_tester.py 2006-08-29 11:28:51 UTC (rev 489) @@ -0,0 +1,64 @@ +# 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 + +class tester_t(fundamental_tester_base.fundamental_tester_base_t): + EXTENSION_NAME = 'custom_smart_ptr_classes' + + def __init__( self, *args ): + fundamental_tester_base.fundamental_tester_base_t.__init__( + self + , tester_t.EXTENSION_NAME + , *args ) + + def customize( self, mb ): + mb.classes( lambda cls: 'ptr' in cls.name).exclude() + return + cls = mb.class_( 'value_plus_x_t' ) + cls.add_registration_code( 'bp::register_ptr_to_python< boost::shared_ptr< no_init_ns::value_plus_x_t > >();', False ) + + def create_identity_value( self, module, v ): + class identity_value_t( module.value_i ): + def __init__( self, v ): + module.value_i.__init__( self ) + self.value = v + + def get_value(self): + return self.value + + return identity_value_t(v) + + def create_add_5_value( self, module, v ): + class add_5_value_t( module.value_plus_x_t ): + def __init__( self, v ): + module.value_plus_x_t.__init__( self, v ) + + def get_plus_value(self): + print "I am here" + return 5 + return add_5_value_t( v ) + + def run_tests(self, module): + add_0 = module.add_x_t( 23 ) + self.failUnless( 23 == add_0.get_value() ) + self.failUnless( 23 == module.ref_get_value( add_0 ) ) + self.failUnless( 23 == module.val_get_value( add_0 ) ) + self.failUnless( 23 == module.const_ref_get_value( add_0 ) ) + + +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() Added: pyplusplus_dev/unittests/data/custom_smart_ptr.h =================================================================== --- pyplusplus_dev/unittests/data/custom_smart_ptr.h (rev 0) +++ pyplusplus_dev/unittests/data/custom_smart_ptr.h 2006-08-29 11:28:51 UTC (rev 489) @@ -0,0 +1,125 @@ +#ifndef __my_smart_ptr_t__ +#define __my_smart_ptr_t__ + +#include <assert.h> + +template<class T> class my_smart_ptr_t { +protected: + T* pRep; + unsigned int* pUseCount; +public: + + my_smart_ptr_t() + : pRep(0), pUseCount(0) + {} + + //What will happen if rep is NULL? -> bug + explicit my_smart_ptr_t(T* rep) + : pRep(rep), pUseCount( new unsigned int(1) ) + {} + + my_smart_ptr_t(const my_smart_ptr_t<T>& r) + : pRep(0), pUseCount(0) + { + pRep = r.pRep; + pUseCount = r.pUseCount; + if(pUseCount){ + ++(*pUseCount); + } + } + + my_smart_ptr_t& operator=(const my_smart_ptr_t<T>& r){ + if( pRep == r.pRep ){ + return *this; + } + + release(); + + pRep = r.pRep; + pUseCount = r.pUseCount; + if(pUseCount){ + ++(*pUseCount); + } + return *this; + } + + virtual ~my_smart_ptr_t() { + release(); + } + + inline T& operator*() const { + assert(pRep); return *pRep; + } + + inline T* operator->() const { + assert(pRep); return pRep; + } + + inline T* get() const { + return pRep; + } + + inline bool unique() const { + assert(pUseCount); + return *pUseCount == 1; + } + + inline unsigned int useCount() const { + assert(pUseCount); + return *pUseCount; + } + + inline unsigned int* useCountPointer() const { + return pUseCount; + } + + inline T* getPointer() const { + return pRep; + } + + inline bool isNull(void) const { + return pRep == 0; + } + + inline void setNull(void) { + if (pRep){ + release(); + pRep = 0; + pUseCount = 0; + } + } + +protected: + + inline void release(void){ + bool destroyThis = false; + + if( pUseCount ){ + if( --(*pUseCount) == 0){ + destroyThis = true; + } + } + if (destroyThis){ + destroy(); + } + } + + virtual void destroy(void){ + delete pRep; + delete pUseCount; + } +}; + +template<class T, class U> inline bool operator==(my_smart_ptr_t<T> const& a, my_smart_ptr_t<U> const& b) +{ + return a.get() == b.get(); +} + +template<class T, class U> inline bool operator!=(my_smart_ptr_t<T> const& a, my_smart_ptr_t<U> const& b) +{ + return a.get() != b.get(); +} + + +#endif //__my_smart_ptr_t__ + Added: pyplusplus_dev/unittests/data/custom_smart_ptr_classes_to_be_exported.hpp =================================================================== --- pyplusplus_dev/unittests/data/custom_smart_ptr_classes_to_be_exported.hpp (rev 0) +++ pyplusplus_dev/unittests/data/custom_smart_ptr_classes_to_be_exported.hpp 2006-08-29 11:28:51 UTC (rev 489) @@ -0,0 +1,113 @@ +#include "custom_smart_ptr.h" + +namespace controllers{ + +struct controller_i{ +public: + virtual ~controller_i() { } + virtual int get_value(void) const = 0; +}; + +typedef my_smart_ptr_t<controller_i> controller_ptr_i; + +struct multiply_x_t : controller_i{ + multiply_x_t( int value ) + : m_value( value ) + {} + + virtual int get_value(void) const{ + return m_value * get_multiply_value(); + } + + virtual int get_multiply_value() const{ + return 1; + } + +private: + int m_value; +}; + +typedef my_smart_ptr_t<multiply_x_t> multiply_x_ptr_t; + +struct add_x_t : controller_i{ + add_x_t( int value ) + : m_value( value ) + {} + + virtual int get_value(void) const{ + return m_value + get_add_value(); + } + + virtual int get_add_value() const{ + return 0; + } + +private: + int m_value; +}; + + +struct add_x_ptr_t : public my_smart_ptr_t< add_x_t >{ + + explicit add_x_ptr_t(add_x_t* rep) + : my_smart_ptr_t<add_x_t>(rep) + {} + + add_x_ptr_t(const add_x_ptr_t& r) + : my_smart_ptr_t<add_x_t>(r) + {} + + //added by me( Roman ), to allow implicit conversion between add_x_ptr_t and add_x_ptr_t + //operator my_smart_ptr_t<resource_t>() const { + // return my_smart_ptr_t<resource_t>( *this ); + //} + + /// Operator used to convert a add_x_ptr_t to a add_x_ptr_t + add_x_ptr_t& operator=(const add_x_ptr_t& r){ + + if( pRep == static_cast<add_x_t*>(r.getPointer()) ){ + return *this; + } + + release(); + + pRep = static_cast<add_x_t*>(r.getPointer()); + pUseCount = r.useCountPointer(); + + if (pUseCount){ + ++(*pUseCount); + } + + return *this; + } +}; + +add_x_ptr_t create_sptr_add_x(){ + return add_x_ptr_t( new add_x_t(12) ); +} + +} + + + +//not supported +//add_x_ptr_t create_shared_resource(){ +// return add_x_ptr_t( new add_x_t() ); +//} + +inline int +ref_get_value( controllers::controller_ptr_i& a ){ + return a->get_value(); +} + +inline int +val_get_value( controllers::controller_ptr_i a ){ + return a->get_value(); +} + +inline int +const_ref_get_value( const controllers::controller_ptr_i& a ){ + return a->get_value(); +} + + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |