Chris@16
|
1 //
|
Chris@16
|
2 // Boost.Pointer Container
|
Chris@16
|
3 //
|
Chris@16
|
4 // Copyright Thorsten Ottosen 2003-2005. Use, modification and
|
Chris@16
|
5 // distribution is subject to the Boost Software License, Version
|
Chris@16
|
6 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
7 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
8 //
|
Chris@16
|
9 // For more information, see http://www.boost.org/libs/ptr_container/
|
Chris@16
|
10 //
|
Chris@16
|
11
|
Chris@16
|
12 #ifndef BOOST_PTR_CONTAINER_CLONE_ALLOCATOR_HPP
|
Chris@16
|
13 #define BOOST_PTR_CONTAINER_CLONE_ALLOCATOR_HPP
|
Chris@16
|
14
|
Chris@16
|
15 #include <boost/assert.hpp>
|
Chris@16
|
16 #include <boost/checked_delete.hpp>
|
Chris@16
|
17 #include <typeinfo>
|
Chris@16
|
18
|
Chris@16
|
19 namespace boost
|
Chris@16
|
20 {
|
Chris@16
|
21 /////////////////////////////////////////////////////////////////////////
|
Chris@16
|
22 // Clonable concept
|
Chris@16
|
23 /////////////////////////////////////////////////////////////////////////
|
Chris@16
|
24
|
Chris@16
|
25 template< class T >
|
Chris@16
|
26 inline T* new_clone( const T& r )
|
Chris@16
|
27 {
|
Chris@16
|
28 //
|
Chris@16
|
29 // @remark: if you get a compile-error here,
|
Chris@16
|
30 // it is most likely because you did not
|
Chris@16
|
31 // define new_clone( const T& ) in the namespace
|
Chris@16
|
32 // of T.
|
Chris@16
|
33 //
|
Chris@16
|
34 T* res = new T( r );
|
Chris@16
|
35 BOOST_ASSERT( typeid(r) == typeid(*res) &&
|
Chris@16
|
36 "Default new_clone() sliced object!" );
|
Chris@16
|
37 return res;
|
Chris@16
|
38 }
|
Chris@16
|
39
|
Chris@16
|
40 template< class T >
|
Chris@16
|
41 inline T* new_clone( const T* r )
|
Chris@16
|
42 {
|
Chris@16
|
43 return r ? new_clone( *r ) : 0;
|
Chris@16
|
44 }
|
Chris@16
|
45
|
Chris@16
|
46 //
|
Chris@16
|
47 // @remark: to make new_clone() work
|
Chris@16
|
48 // with scope_ptr/shared_ptr ect.
|
Chris@16
|
49 // simply overload for those types
|
Chris@16
|
50 // in the appropriate namespace.
|
Chris@16
|
51 //
|
Chris@16
|
52
|
Chris@16
|
53 template< class T >
|
Chris@16
|
54 inline void delete_clone( const T* r )
|
Chris@16
|
55 {
|
Chris@16
|
56 checked_delete( r );
|
Chris@16
|
57 }
|
Chris@16
|
58
|
Chris@16
|
59 /////////////////////////////////////////////////////////////////////////
|
Chris@16
|
60 // CloneAllocator concept
|
Chris@16
|
61 /////////////////////////////////////////////////////////////////////////
|
Chris@16
|
62
|
Chris@16
|
63 struct heap_clone_allocator
|
Chris@16
|
64 {
|
Chris@16
|
65 template< class U >
|
Chris@16
|
66 static U* allocate_clone( const U& r )
|
Chris@16
|
67 {
|
Chris@16
|
68 return new_clone( r );
|
Chris@16
|
69 }
|
Chris@16
|
70
|
Chris@16
|
71 template< class U >
|
Chris@16
|
72 static void deallocate_clone( const U* r )
|
Chris@16
|
73 {
|
Chris@16
|
74 delete_clone( r );
|
Chris@16
|
75 }
|
Chris@16
|
76
|
Chris@16
|
77 };
|
Chris@16
|
78
|
Chris@16
|
79
|
Chris@16
|
80
|
Chris@16
|
81 struct view_clone_allocator
|
Chris@16
|
82 {
|
Chris@16
|
83 template< class U >
|
Chris@16
|
84 static U* allocate_clone( const U& r )
|
Chris@16
|
85 {
|
Chris@16
|
86 return const_cast<U*>(&r);
|
Chris@16
|
87 }
|
Chris@16
|
88
|
Chris@16
|
89 template< class U >
|
Chris@16
|
90 static void deallocate_clone( const U* /*r*/ )
|
Chris@16
|
91 {
|
Chris@16
|
92 // do nothing
|
Chris@16
|
93 }
|
Chris@16
|
94 };
|
Chris@16
|
95
|
Chris@16
|
96 } // namespace 'boost'
|
Chris@16
|
97
|
Chris@16
|
98 #endif
|
Chris@16
|
99
|