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/get_pointer.hpp>
|
Chris@16
|
19 # include <boost/non_type.hpp>
|
Chris@16
|
20 # include <boost/type_traits/remove_cv.hpp>
|
Chris@16
|
21
|
Chris@101
|
22 # if defined(BOOST_FUNCTIONAL_FACTORY_SUPPORT_NONE_T)
|
Chris@101
|
23 # include <boost/none_t.hpp>
|
Chris@101
|
24 # endif
|
Chris@101
|
25
|
Chris@16
|
26 # ifndef BOOST_FUNCTIONAL_FACTORY_MAX_ARITY
|
Chris@16
|
27 # define BOOST_FUNCTIONAL_FACTORY_MAX_ARITY 10
|
Chris@16
|
28 # elif BOOST_FUNCTIONAL_FACTORY_MAX_ARITY < 3
|
Chris@16
|
29 # undef BOOST_FUNCTIONAL_FACTORY_MAX_ARITY
|
Chris@16
|
30 # define BOOST_FUNCTIONAL_FACTORY_MAX_ARITY 3
|
Chris@16
|
31 # endif
|
Chris@16
|
32
|
Chris@16
|
33 namespace boost
|
Chris@16
|
34 {
|
Chris@16
|
35 enum factory_alloc_propagation
|
Chris@16
|
36 {
|
Chris@16
|
37 factory_alloc_for_pointee_and_deleter,
|
Chris@16
|
38 factory_passes_alloc_to_smart_pointer
|
Chris@16
|
39 };
|
Chris@16
|
40
|
Chris@101
|
41 #if defined(BOOST_FUNCTIONAL_FACTORY_SUPPORT_NONE_T)
|
Chris@16
|
42 template< typename Pointer, class Allocator = boost::none_t,
|
Chris@16
|
43 factory_alloc_propagation AP = factory_alloc_for_pointee_and_deleter >
|
Chris@16
|
44 class factory;
|
Chris@101
|
45 #else
|
Chris@101
|
46 template< typename Pointer, class Allocator = void,
|
Chris@101
|
47 factory_alloc_propagation AP = factory_alloc_for_pointee_and_deleter >
|
Chris@101
|
48 class factory;
|
Chris@101
|
49 #endif
|
Chris@16
|
50
|
Chris@16
|
51 //----- ---- --- -- - - - -
|
Chris@16
|
52
|
Chris@16
|
53 template< typename Pointer, factory_alloc_propagation AP >
|
Chris@101
|
54 class factory<Pointer, void, AP>
|
Chris@16
|
55 {
|
Chris@16
|
56 public:
|
Chris@16
|
57 typedef typename boost::remove_cv<Pointer>::type result_type;
|
Chris@16
|
58 typedef typename boost::pointee<result_type>::type value_type;
|
Chris@16
|
59
|
Chris@16
|
60 factory()
|
Chris@16
|
61 { }
|
Chris@16
|
62
|
Chris@16
|
63 # define BOOST_PP_FILENAME_1 <boost/functional/factory.hpp>
|
Chris@16
|
64 # define BOOST_PP_ITERATION_LIMITS (0,BOOST_FUNCTIONAL_FACTORY_MAX_ARITY)
|
Chris@16
|
65 # include BOOST_PP_ITERATE()
|
Chris@16
|
66 };
|
Chris@16
|
67
|
Chris@101
|
68 #if defined(BOOST_FUNCTIONAL_FACTORY_SUPPORT_NONE_T)
|
Chris@101
|
69 template< typename Pointer, factory_alloc_propagation AP >
|
Chris@101
|
70 class factory<Pointer, boost::none_t, AP>
|
Chris@101
|
71 : public factory<Pointer, void, AP>
|
Chris@101
|
72 {};
|
Chris@101
|
73 #endif
|
Chris@101
|
74
|
Chris@16
|
75 template< class Pointer, class Allocator, factory_alloc_propagation AP >
|
Chris@16
|
76 class factory
|
Chris@16
|
77 : private Allocator::template rebind< typename boost::pointee<
|
Chris@16
|
78 typename boost::remove_cv<Pointer>::type >::type >::other
|
Chris@16
|
79 {
|
Chris@16
|
80 public:
|
Chris@16
|
81 typedef typename boost::remove_cv<Pointer>::type result_type;
|
Chris@16
|
82 typedef typename boost::pointee<result_type>::type value_type;
|
Chris@16
|
83
|
Chris@16
|
84 typedef typename Allocator::template rebind<value_type>::other
|
Chris@16
|
85 allocator_type;
|
Chris@16
|
86
|
Chris@16
|
87 explicit factory(allocator_type const & a = allocator_type())
|
Chris@16
|
88 : allocator_type(a)
|
Chris@16
|
89 { }
|
Chris@16
|
90
|
Chris@16
|
91 private:
|
Chris@16
|
92
|
Chris@16
|
93 struct deleter
|
Chris@16
|
94 : allocator_type
|
Chris@16
|
95 {
|
Chris@16
|
96 inline deleter(allocator_type const& that)
|
Chris@16
|
97 : allocator_type(that)
|
Chris@16
|
98 { }
|
Chris@16
|
99
|
Chris@16
|
100 allocator_type& get_allocator() const
|
Chris@16
|
101 {
|
Chris@16
|
102 return *const_cast<allocator_type*>(
|
Chris@16
|
103 static_cast<allocator_type const*>(this));
|
Chris@16
|
104 }
|
Chris@16
|
105
|
Chris@16
|
106 void operator()(value_type* ptr) const
|
Chris@16
|
107 {
|
Chris@16
|
108 if (!! ptr) ptr->~value_type();
|
Chris@16
|
109 const_cast<allocator_type*>(static_cast<allocator_type const*>(
|
Chris@16
|
110 this))->deallocate(ptr,1);
|
Chris@16
|
111 }
|
Chris@16
|
112 };
|
Chris@16
|
113
|
Chris@16
|
114 inline allocator_type& get_allocator() const
|
Chris@16
|
115 {
|
Chris@16
|
116 return *const_cast<allocator_type*>(
|
Chris@16
|
117 static_cast<allocator_type const*>(this));
|
Chris@16
|
118 }
|
Chris@16
|
119
|
Chris@16
|
120 inline result_type make_pointer(value_type* ptr, boost::non_type<
|
Chris@16
|
121 factory_alloc_propagation,factory_passes_alloc_to_smart_pointer>)
|
Chris@16
|
122 const
|
Chris@16
|
123 {
|
Chris@16
|
124 return result_type(ptr,deleter(this->get_allocator()));
|
Chris@16
|
125 }
|
Chris@16
|
126 inline result_type make_pointer(value_type* ptr, boost::non_type<
|
Chris@16
|
127 factory_alloc_propagation,factory_alloc_for_pointee_and_deleter>)
|
Chris@16
|
128 const
|
Chris@16
|
129 {
|
Chris@16
|
130 return result_type(ptr,deleter(this->get_allocator()),
|
Chris@16
|
131 this->get_allocator());
|
Chris@16
|
132 }
|
Chris@16
|
133
|
Chris@16
|
134 public:
|
Chris@16
|
135
|
Chris@16
|
136 # define BOOST_TMP_MACRO
|
Chris@16
|
137 # define BOOST_PP_FILENAME_1 <boost/functional/factory.hpp>
|
Chris@16
|
138 # define BOOST_PP_ITERATION_LIMITS (0,BOOST_FUNCTIONAL_FACTORY_MAX_ARITY)
|
Chris@16
|
139 # include BOOST_PP_ITERATE()
|
Chris@16
|
140 # undef BOOST_TMP_MACRO
|
Chris@16
|
141 };
|
Chris@16
|
142
|
Chris@16
|
143 template< typename Pointer, class Allocator, factory_alloc_propagation AP >
|
Chris@16
|
144 class factory<Pointer&, Allocator, AP>;
|
Chris@16
|
145 // forbidden, would create a dangling reference
|
Chris@16
|
146 }
|
Chris@16
|
147
|
Chris@16
|
148 # define BOOST_FUNCTIONAL_FACTORY_HPP_INCLUDED
|
Chris@16
|
149 # else // defined(BOOST_PP_IS_ITERATING)
|
Chris@16
|
150 # define N BOOST_PP_ITERATION()
|
Chris@16
|
151 # if !defined(BOOST_TMP_MACRO)
|
Chris@16
|
152 # if N > 0
|
Chris@16
|
153 template< BOOST_PP_ENUM_PARAMS(N, typename T) >
|
Chris@16
|
154 # endif
|
Chris@16
|
155 inline result_type operator()(BOOST_PP_ENUM_BINARY_PARAMS(N,T,& a)) const
|
Chris@16
|
156 {
|
Chris@16
|
157 return result_type( new value_type(BOOST_PP_ENUM_PARAMS(N,a)) );
|
Chris@16
|
158 }
|
Chris@16
|
159 # else // defined(BOOST_TMP_MACRO)
|
Chris@16
|
160 # if N > 0
|
Chris@16
|
161 template< BOOST_PP_ENUM_PARAMS(N, typename T) >
|
Chris@16
|
162 # endif
|
Chris@16
|
163 inline result_type operator()(BOOST_PP_ENUM_BINARY_PARAMS(N,T,& a)) const
|
Chris@16
|
164 {
|
Chris@16
|
165 value_type* memory = this->get_allocator().allocate(1);
|
Chris@16
|
166 try
|
Chris@16
|
167 {
|
Chris@16
|
168 return make_pointer(
|
Chris@16
|
169 new(memory) value_type(BOOST_PP_ENUM_PARAMS(N,a)),
|
Chris@16
|
170 boost::non_type<factory_alloc_propagation,AP>() );
|
Chris@16
|
171 }
|
Chris@16
|
172 catch (...) { this->get_allocator().deallocate(memory,1); throw; }
|
Chris@16
|
173 }
|
Chris@16
|
174 # endif
|
Chris@16
|
175 # undef N
|
Chris@16
|
176 # endif // defined(BOOST_PP_IS_ITERATING)
|
Chris@16
|
177
|
Chris@16
|
178 #endif // include guard
|
Chris@16
|
179
|