fix doc example typo
[boost.git] / boost / ptr_container / clone_allocator.hpp
blob6d396e808a591623c7b4031c9d027102442f6f34
1 //
2 // Boost.Pointer Container
3 //
4 // Copyright Thorsten Ottosen 2003-2005. Use, modification and
5 // distribution is subject to the Boost Software License, Version
6 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 //
9 // For more information, see http://www.boost.org/libs/ptr_container/
12 #ifndef BOOST_PTR_CONTAINER_CLONE_ALLOCATOR_HPP
13 #define BOOST_PTR_CONTAINER_CLONE_ALLOCATOR_HPP
15 #include <boost/assert.hpp>
16 #include <boost/checked_delete.hpp>
17 #include <typeinfo>
19 namespace boost
21 /////////////////////////////////////////////////////////////////////////
22 // Clonable concept
23 /////////////////////////////////////////////////////////////////////////
25 template< class T >
26 inline T* new_clone( const T& r )
29 // @remark: if you get a compile-error here,
30 // it is most likely because you did not
31 // define new_clone( const T& ) in the namespace
32 // of T.
34 T* res = new T( r );
35 BOOST_ASSERT( typeid(r) == typeid(*res) &&
36 "Default new_clone() sliced object!" );
37 return res;
40 template< class T >
41 inline T* new_clone( const T* r )
43 return r ? new_clone( *r ) : 0;
46 //
47 // @remark: to make new_clone() work
48 // with scope_ptr/shared_ptr ect.
49 // simply overload for those types
50 // in the appropriate namespace.
51 //
53 template< class T >
54 inline void delete_clone( const T* r )
56 checked_delete( r );
59 /////////////////////////////////////////////////////////////////////////
60 // CloneAllocator concept
61 /////////////////////////////////////////////////////////////////////////
63 struct heap_clone_allocator
65 template< class U >
66 static U* allocate_clone( const U& r )
68 return new_clone( r );
71 template< class U >
72 static void deallocate_clone( const U* r )
74 delete_clone( r );
81 struct view_clone_allocator
83 template< class U >
84 static U* allocate_clone( const U& r )
86 return const_cast<U*>(&r);
89 template< class U >
90 static void deallocate_clone( const U* /*r*/ )
92 // do nothing
96 } // namespace 'boost'
98 #endif