Chris@16: // Chris@16: // Boost.Pointer Container Chris@16: // Chris@16: // Copyright Thorsten Ottosen 2003-2005. Use, modification and Chris@16: // distribution is subject to the Boost Software License, Version Chris@16: // 1.0. (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // For more information, see http://www.boost.org/libs/ptr_container/ Chris@16: // Chris@16: Chris@16: #ifndef BOOST_PTR_CONTAINER_CLONE_ALLOCATOR_HPP Chris@16: #define BOOST_PTR_CONTAINER_CLONE_ALLOCATOR_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost Chris@16: { Chris@16: ///////////////////////////////////////////////////////////////////////// Chris@16: // Clonable concept Chris@16: ///////////////////////////////////////////////////////////////////////// Chris@16: Chris@16: template< class T > Chris@16: inline T* new_clone( const T& r ) Chris@16: { Chris@16: // Chris@16: // @remark: if you get a compile-error here, Chris@16: // it is most likely because you did not Chris@16: // define new_clone( const T& ) in the namespace Chris@16: // of T. Chris@16: // Chris@16: T* res = new T( r ); Chris@16: BOOST_ASSERT( typeid(r) == typeid(*res) && Chris@16: "Default new_clone() sliced object!" ); Chris@16: return res; Chris@16: } Chris@16: Chris@16: template< class T > Chris@16: inline T* new_clone( const T* r ) Chris@16: { Chris@16: return r ? new_clone( *r ) : 0; Chris@16: } Chris@16: Chris@16: // Chris@16: // @remark: to make new_clone() work Chris@16: // with scope_ptr/shared_ptr ect. Chris@16: // simply overload for those types Chris@16: // in the appropriate namespace. Chris@16: // Chris@16: Chris@16: template< class T > Chris@16: inline void delete_clone( const T* r ) Chris@16: { Chris@16: checked_delete( r ); Chris@16: } Chris@16: Chris@16: ///////////////////////////////////////////////////////////////////////// Chris@16: // CloneAllocator concept Chris@16: ///////////////////////////////////////////////////////////////////////// Chris@16: Chris@16: struct heap_clone_allocator Chris@16: { Chris@16: template< class U > Chris@16: static U* allocate_clone( const U& r ) Chris@16: { Chris@16: return new_clone( r ); Chris@16: } Chris@16: Chris@16: template< class U > Chris@16: static void deallocate_clone( const U* r ) Chris@16: { Chris@16: delete_clone( r ); Chris@16: } Chris@16: Chris@16: }; Chris@16: Chris@16: Chris@16: Chris@16: struct view_clone_allocator Chris@16: { Chris@16: template< class U > Chris@16: static U* allocate_clone( const U& r ) Chris@16: { Chris@16: return const_cast(&r); Chris@16: } Chris@16: Chris@16: template< class U > Chris@16: static void deallocate_clone( const U* /*r*/ ) Chris@16: { Chris@16: // do nothing Chris@16: } Chris@16: }; Chris@16: Chris@16: } // namespace 'boost' Chris@16: Chris@16: #endif Chris@16: