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
19namespace boost
20{
21 /////////////////////////////////////////////////////////////////////////
22 // Clonable concept
23 /////////////////////////////////////////////////////////////////////////
24
25 template< class T >
26 inline T* new_clone( const T& r )
27 {
28 //
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.
33 //
34 T* res = new T( r );
35 BOOST_ASSERT( typeid(r) == typeid(*res) &&
36 "Default new_clone() sliced object!" );
37 return res;
38 }
39
40 template< class T >
41 inline T* new_clone( const T* r )
42 {
43 return r ? new_clone( *r ) : 0;
44 }
45
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 //
52
53 template< class T >
54 inline void delete_clone( const T* r )
55 {
56 checked_delete( r );
57 }
58
59 /////////////////////////////////////////////////////////////////////////
60 // CloneAllocator concept
61 /////////////////////////////////////////////////////////////////////////
62
63 struct heap_clone_allocator
64 {
65 template< class U >
66 static U* allocate_clone( const U& r )
67 {
68 return new_clone( r );
69 }
70
71 template< class U >
72 static void deallocate_clone( const U* r )
73 {
74 delete_clone( r );
75 }
76
77 };
78
79
80
81 struct view_clone_allocator
82 {
83 template< class U >
84 static U* allocate_clone( const U& r )
85 {
86 return const_cast<U*>(&r);
87 }
88
89 template< class U >
90 static void deallocate_clone( const U* /*r*/ )
91 {
92 // do nothing
93 }
94 };
95
96} // namespace 'boost'
97
98#endif
99
100

source code of boost/boost/ptr_container/clone_allocator.hpp