Chris@16
|
1 /*=============================================================================
|
Chris@16
|
2 Copyright (c) 2007 Tobias Schwinger
|
Chris@16
|
3
|
Chris@16
|
4 Use modification and distribution are subject to the Boost Software
|
Chris@16
|
5 License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
6 http://www.boost.org/LICENSE_1_0.txt).
|
Chris@16
|
7 ==============================================================================*/
|
Chris@16
|
8
|
Chris@16
|
9 #ifndef BOOST_FUNCTIONAL_FACTORY_HPP_INCLUDED
|
Chris@16
|
10 # ifndef BOOST_PP_IS_ITERATING
|
Chris@16
|
11
|
Chris@16
|
12 # include <boost/preprocessor/iteration/iterate.hpp>
|
Chris@16
|
13 # include <boost/preprocessor/repetition/enum_params.hpp>
|
Chris@16
|
14 # include <boost/preprocessor/repetition/enum_binary_params.hpp>
|
Chris@16
|
15
|
Chris@16
|
16 # include <new>
|
Chris@16
|
17 # include <boost/pointee.hpp>
|
Chris@16
|
18 # include <boost/none_t.hpp>
|
Chris@16
|
19 # include <boost/get_pointer.hpp>
|
Chris@16
|
20 # include <boost/non_type.hpp>
|
Chris@16
|
21 # include <boost/type_traits/remove_cv.hpp>
|
Chris@16
|
22
|
Chris@16
|
23 # ifndef BOOST_FUNCTIONAL_FACTORY_MAX_ARITY
|
Chris@16
|
24 # define BOOST_FUNCTIONAL_FACTORY_MAX_ARITY 10
|
Chris@16
|
25 # elif BOOST_FUNCTIONAL_FACTORY_MAX_ARITY < 3
|
Chris@16
|
26 # undef BOOST_FUNCTIONAL_FACTORY_MAX_ARITY
|
Chris@16
|
27 # define BOOST_FUNCTIONAL_FACTORY_MAX_ARITY 3
|
Chris@16
|
28 # endif
|
Chris@16
|
29
|
Chris@16
|
30 namespace boost
|
Chris@16
|
31 {
|
Chris@16
|
32 enum factory_alloc_propagation
|
Chris@16
|
33 {
|
Chris@16
|
34 factory_alloc_for_pointee_and_deleter,
|
Chris@16
|
35 factory_passes_alloc_to_smart_pointer
|
Chris@16
|
36 };
|
Chris@16
|
37
|
Chris@16
|
38 template< typename Pointer, class Allocator = boost::none_t,
|
Chris@16
|
39 factory_alloc_propagation AP = factory_alloc_for_pointee_and_deleter >
|
Chris@16
|
40 class factory;
|
Chris@16
|
41
|
Chris@16
|
42 //----- ---- --- -- - - - -
|
Chris@16
|
43
|
Chris@16
|
44 template< typename Pointer, factory_alloc_propagation AP >
|
Chris@16
|
45 class factory<Pointer, boost::none_t, AP>
|
Chris@16
|
46 {
|
Chris@16
|
47 public:
|
Chris@16
|
48 typedef typename boost::remove_cv<Pointer>::type result_type;
|
Chris@16
|
49 typedef typename boost::pointee<result_type>::type value_type;
|
Chris@16
|
50
|
Chris@16
|
51 factory()
|
Chris@16
|
52 { }
|
Chris@16
|
53
|
Chris@16
|
54 # define BOOST_PP_FILENAME_1 <boost/functional/factory.hpp>
|
Chris@16
|
55 # define BOOST_PP_ITERATION_LIMITS (0,BOOST_FUNCTIONAL_FACTORY_MAX_ARITY)
|
Chris@16
|
56 # include BOOST_PP_ITERATE()
|
Chris@16
|
57 };
|
Chris@16
|
58
|
Chris@16
|
59 template< class Pointer, class Allocator, factory_alloc_propagation AP >
|
Chris@16
|
60 class factory
|
Chris@16
|
61 : private Allocator::template rebind< typename boost::pointee<
|
Chris@16
|
62 typename boost::remove_cv<Pointer>::type >::type >::other
|
Chris@16
|
63 {
|
Chris@16
|
64 public:
|
Chris@16
|
65 typedef typename boost::remove_cv<Pointer>::type result_type;
|
Chris@16
|
66 typedef typename boost::pointee<result_type>::type value_type;
|
Chris@16
|
67
|
Chris@16
|
68 typedef typename Allocator::template rebind<value_type>::other
|
Chris@16
|
69 allocator_type;
|
Chris@16
|
70
|
Chris@16
|
71 explicit factory(allocator_type const & a = allocator_type())
|
Chris@16
|
72 : allocator_type(a)
|
Chris@16
|
73 { }
|
Chris@16
|
74
|
Chris@16
|
75 private:
|
Chris@16
|
76
|
Chris@16
|
77 struct deleter
|
Chris@16
|
78 : allocator_type
|
Chris@16
|
79 {
|
Chris@16
|
80 inline deleter(allocator_type const& that)
|
Chris@16
|
81 : allocator_type(that)
|
Chris@16
|
82 { }
|
Chris@16
|
83
|
Chris@16
|
84 allocator_type& get_allocator() const
|
Chris@16
|
85 {
|
Chris@16
|
86 return *const_cast<allocator_type*>(
|
Chris@16
|
87 static_cast<allocator_type const*>(this));
|
Chris@16
|
88 }
|
Chris@16
|
89
|
Chris@16
|
90 void operator()(value_type* ptr) const
|
Chris@16
|
91 {
|
Chris@16
|
92 if (!! ptr) ptr->~value_type();
|
Chris@16
|
93 const_cast<allocator_type*>(static_cast<allocator_type const*>(
|
Chris@16
|
94 this))->deallocate(ptr,1);
|
Chris@16
|
95 }
|
Chris@16
|
96 };
|
Chris@16
|
97
|
Chris@16
|
98 inline allocator_type& get_allocator() const
|
Chris@16
|
99 {
|
Chris@16
|
100 return *const_cast<allocator_type*>(
|
Chris@16
|
101 static_cast<allocator_type const*>(this));
|
Chris@16
|
102 }
|
Chris@16
|
103
|
Chris@16
|
104 inline result_type make_pointer(value_type* ptr, boost::non_type<
|
Chris@16
|
105 factory_alloc_propagation,factory_passes_alloc_to_smart_pointer>)
|
Chris@16
|
106 const
|
Chris@16
|
107 {
|
Chris@16
|
108 return result_type(ptr,deleter(this->get_allocator()));
|
Chris@16
|
109 }
|
Chris@16
|
110 inline result_type make_pointer(value_type* ptr, boost::non_type<
|
Chris@16
|
111 factory_alloc_propagation,factory_alloc_for_pointee_and_deleter>)
|
Chris@16
|
112 const
|
Chris@16
|
113 {
|
Chris@16
|
114 return result_type(ptr,deleter(this->get_allocator()),
|
Chris@16
|
115 this->get_allocator());
|
Chris@16
|
116 }
|
Chris@16
|
117
|
Chris@16
|
118 public:
|
Chris@16
|
119
|
Chris@16
|
120 # define BOOST_TMP_MACRO
|
Chris@16
|
121 # define BOOST_PP_FILENAME_1 <boost/functional/factory.hpp>
|
Chris@16
|
122 # define BOOST_PP_ITERATION_LIMITS (0,BOOST_FUNCTIONAL_FACTORY_MAX_ARITY)
|
Chris@16
|
123 # include BOOST_PP_ITERATE()
|
Chris@16
|
124 # undef BOOST_TMP_MACRO
|
Chris@16
|
125 };
|
Chris@16
|
126
|
Chris@16
|
127 template< typename Pointer, class Allocator, factory_alloc_propagation AP >
|
Chris@16
|
128 class factory<Pointer&, Allocator, AP>;
|
Chris@16
|
129 // forbidden, would create a dangling reference
|
Chris@16
|
130 }
|
Chris@16
|
131
|
Chris@16
|
132 # define BOOST_FUNCTIONAL_FACTORY_HPP_INCLUDED
|
Chris@16
|
133 # else // defined(BOOST_PP_IS_ITERATING)
|
Chris@16
|
134 # define N BOOST_PP_ITERATION()
|
Chris@16
|
135 # if !defined(BOOST_TMP_MACRO)
|
Chris@16
|
136 # if N > 0
|
Chris@16
|
137 template< BOOST_PP_ENUM_PARAMS(N, typename T) >
|
Chris@16
|
138 # endif
|
Chris@16
|
139 inline result_type operator()(BOOST_PP_ENUM_BINARY_PARAMS(N,T,& a)) const
|
Chris@16
|
140 {
|
Chris@16
|
141 return result_type( new value_type(BOOST_PP_ENUM_PARAMS(N,a)) );
|
Chris@16
|
142 }
|
Chris@16
|
143 # else // defined(BOOST_TMP_MACRO)
|
Chris@16
|
144 # if N > 0
|
Chris@16
|
145 template< BOOST_PP_ENUM_PARAMS(N, typename T) >
|
Chris@16
|
146 # endif
|
Chris@16
|
147 inline result_type operator()(BOOST_PP_ENUM_BINARY_PARAMS(N,T,& a)) const
|
Chris@16
|
148 {
|
Chris@16
|
149 value_type* memory = this->get_allocator().allocate(1);
|
Chris@16
|
150 try
|
Chris@16
|
151 {
|
Chris@16
|
152 return make_pointer(
|
Chris@16
|
153 new(memory) value_type(BOOST_PP_ENUM_PARAMS(N,a)),
|
Chris@16
|
154 boost::non_type<factory_alloc_propagation,AP>() );
|
Chris@16
|
155 }
|
Chris@16
|
156 catch (...) { this->get_allocator().deallocate(memory,1); throw; }
|
Chris@16
|
157 }
|
Chris@16
|
158 # endif
|
Chris@16
|
159 # undef N
|
Chris@16
|
160 # endif // defined(BOOST_PP_IS_ITERATING)
|
Chris@16
|
161
|
Chris@16
|
162 #endif // include guard
|
Chris@16
|
163
|