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/ |
---|
10 | // |
---|
11 | |
---|
12 | #ifndef BOOST_PTR_CONTAINER_CLONE_ALLOCATOR_HPP |
---|
13 | #define BOOST_PTR_CONTAINER_CLONE_ALLOCATOR_HPP |
---|
14 | |
---|
15 | #include <boost/assert.hpp> |
---|
16 | #include <boost/checked_delete.hpp> |
---|
17 | #include <typeinfo> |
---|
18 | |
---|
19 | namespace boost |
---|
20 | { |
---|
21 | ///////////////////////////////////////////////////////////////////////// |
---|
22 | // Clonable concept |
---|
23 | ///////////////////////////////////////////////////////////////////////// |
---|
24 | |
---|
25 | template< class T > |
---|
26 | inline T* new_clone( const T& r ) |
---|
27 | { |
---|
28 | T* res = new T( r ); |
---|
29 | BOOST_ASSERT( typeid(r) == typeid(*res) && |
---|
30 | "Default new_clone() sliced object!" ); |
---|
31 | return res; |
---|
32 | } |
---|
33 | |
---|
34 | template< class T > |
---|
35 | inline void delete_clone( const T* r ) |
---|
36 | { |
---|
37 | checked_delete( r ); |
---|
38 | } |
---|
39 | |
---|
40 | ///////////////////////////////////////////////////////////////////////// |
---|
41 | // CloneAllocator concept |
---|
42 | ///////////////////////////////////////////////////////////////////////// |
---|
43 | |
---|
44 | struct heap_clone_allocator |
---|
45 | { |
---|
46 | template< class U > |
---|
47 | static U* allocate_clone( const U& r ) |
---|
48 | { |
---|
49 | return new_clone( r ); |
---|
50 | } |
---|
51 | |
---|
52 | template< class U > |
---|
53 | static void deallocate_clone( const U* r ) |
---|
54 | { |
---|
55 | delete_clone( r ); |
---|
56 | } |
---|
57 | |
---|
58 | }; |
---|
59 | |
---|
60 | |
---|
61 | |
---|
62 | struct view_clone_allocator |
---|
63 | { |
---|
64 | template< class U > |
---|
65 | static U* allocate_clone( const U& r ) |
---|
66 | { |
---|
67 | return const_cast<U*>( &r ); |
---|
68 | } |
---|
69 | |
---|
70 | template< class U > |
---|
71 | static void deallocate_clone( const U* r ) |
---|
72 | { |
---|
73 | // do nothing |
---|
74 | } |
---|
75 | }; |
---|
76 | |
---|
77 | } // namespace 'boost' |
---|
78 | |
---|
79 | #endif |
---|
80 | |
---|