1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2015-2015. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org/libs/container for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_CONTAINER_DISPATCH_USES_ALLOCATOR_HPP
12 #define BOOST_CONTAINER_DISPATCH_USES_ALLOCATOR_HPP
14 #if defined (_MSC_VER)
18 #include <boost/container/detail/config_begin.hpp>
19 #include <boost/container/detail/workaround.hpp>
21 #include <boost/container/allocator_traits.hpp>
22 #include <boost/container/uses_allocator.hpp>
24 #include <boost/container/detail/addressof.hpp>
25 #include <boost/container/detail/mpl.hpp>
26 #include <boost/container/detail/pair.hpp>
27 #include <boost/container/detail/type_traits.hpp>
29 #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
30 #include <boost/move/detail/fwd_macros.hpp>
32 #include <boost/move/utility_core.hpp>
34 #include <boost/core/no_exceptions_support.hpp>
36 namespace boost { namespace container {
38 namespace container_detail {
41 // Check if we can detect is_convertible using advanced SFINAE expressions
42 #if !defined(BOOST_NO_CXX11_DECLTYPE) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
44 //! Code inspired by Mathias Gaunard's is_convertible.cpp found in the Boost mailing list
45 //! http://boost.2283326.n4.nabble.com/type-traits-is-constructible-when-decltype-is-supported-td3575452.html
48 //With variadic templates, we need a single class to implement the trait
49 template<class T, class ...Args>
50 struct is_constructible
52 typedef char yes_type;
56 template<std::size_t N>
60 static decltype(X(boost::move_detail::declval<Args>()...), true_type()) test(int);
63 static no_type test(...);
65 static const bool value = sizeof(test<T>(0)) == sizeof(yes_type);
68 template <class T, class InnerAlloc, class ...Args>
69 struct is_constructible_with_allocator_prefix
70 : is_constructible<T, allocator_arg_t, InnerAlloc, Args...>
73 #else // #if !defined(BOOST_NO_SFINAE_EXPR) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
75 //Without advanced SFINAE expressions, we can't use is_constructible
76 //so backup to constructible_with_allocator_xxx
78 #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
80 template <class T, class InnerAlloc, class ...Args>
81 struct is_constructible_with_allocator_prefix
82 : constructible_with_allocator_prefix<T>
85 template <class T, class InnerAlloc, class ...Args>
86 struct is_constructible_with_allocator_suffix
87 : constructible_with_allocator_suffix<T>
90 #else // #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
92 template <class T, class InnerAlloc, BOOST_MOVE_CLASSDFLT9>
93 struct is_constructible_with_allocator_prefix
94 : constructible_with_allocator_prefix<T>
97 template <class T, class InnerAlloc, BOOST_MOVE_CLASSDFLT9>
98 struct is_constructible_with_allocator_suffix
99 : constructible_with_allocator_suffix<T>
102 #endif // #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
104 #endif // #if !defined(BOOST_NO_SFINAE_EXPR)
106 #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
108 template < typename ConstructAlloc
113 inline typename container_detail::enable_if_and
115 , container_detail::is_not_pair<T>
116 , container_detail::not_< uses_allocator<T, ArgAlloc> >
117 >::type dispatch_uses_allocator
118 ( ConstructAlloc & construct_alloc, BOOST_FWD_REF(ArgAlloc) arg_alloc, T* p, BOOST_FWD_REF(Args)...args)
121 allocator_traits<ConstructAlloc>::construct(construct_alloc, p, ::boost::forward<Args>(args)...);
125 template < typename ConstructAlloc
130 inline typename container_detail::enable_if_and
132 , container_detail::is_not_pair<T>
133 , uses_allocator<T, ArgAlloc>
134 , is_constructible_with_allocator_prefix<T, ArgAlloc, Args...>
135 >::type dispatch_uses_allocator
136 ( ConstructAlloc& construct_alloc, BOOST_FWD_REF(ArgAlloc) arg_alloc, T* p, BOOST_FWD_REF(Args) ...args)
138 allocator_traits<ConstructAlloc>::construct
139 ( construct_alloc, p, allocator_arg
140 , ::boost::forward<ArgAlloc>(arg_alloc), ::boost::forward<Args>(args)...);
144 template < typename ConstructAlloc
149 inline typename container_detail::enable_if_and
151 , container_detail::is_not_pair<T>
152 , uses_allocator<T, ArgAlloc>
153 , container_detail::not_<is_constructible_with_allocator_prefix<T, ArgAlloc, Args...> >
154 >::type dispatch_uses_allocator
155 ( ConstructAlloc& construct_alloc, BOOST_FWD_REF(ArgAlloc) arg_alloc, T* p, BOOST_FWD_REF(Args)...args)
157 allocator_traits<ConstructAlloc>::construct
158 (construct_alloc, p, ::boost::forward<Args>(args)..., ::boost::forward<ArgAlloc>(arg_alloc));
161 #else //#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
163 #define BOOST_CONTAINER_SCOPED_ALLOCATOR_DISPATCH_USES_ALLOCATOR_CODE(N) \
164 template <typename ConstructAlloc, typename ArgAlloc, typename T BOOST_MOVE_I##N BOOST_MOVE_CLASS##N >\
165 inline typename container_detail::enable_if_and\
167 , container_detail::is_not_pair<T>\
168 , container_detail::not_<uses_allocator<T, ArgAlloc> >\
170 dispatch_uses_allocator\
171 (ConstructAlloc &construct_alloc, BOOST_FWD_REF(ArgAlloc) arg_alloc, T* p BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
174 allocator_traits<ConstructAlloc>::construct(construct_alloc, p BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
177 BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_SCOPED_ALLOCATOR_DISPATCH_USES_ALLOCATOR_CODE)
178 #undef BOOST_CONTAINER_SCOPED_ALLOCATOR_DISPATCH_USES_ALLOCATOR_CODE
180 #define BOOST_CONTAINER_SCOPED_ALLOCATOR_DISPATCH_USES_ALLOCATOR_CODE(N) \
181 template < typename ConstructAlloc, typename ArgAlloc, typename T BOOST_MOVE_I##N BOOST_MOVE_CLASS##N >\
182 inline typename container_detail::enable_if_and\
184 , container_detail::is_not_pair<T>\
185 , uses_allocator<T, ArgAlloc>\
186 , is_constructible_with_allocator_prefix<T, ArgAlloc BOOST_MOVE_I##N BOOST_MOVE_TARG##N>\
188 dispatch_uses_allocator\
189 (ConstructAlloc& construct_alloc, BOOST_FWD_REF(ArgAlloc) arg_alloc, T* p BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
191 allocator_traits<ConstructAlloc>::construct\
192 (construct_alloc, p, allocator_arg, ::boost::forward<ArgAlloc>(arg_alloc) BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
195 BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_SCOPED_ALLOCATOR_DISPATCH_USES_ALLOCATOR_CODE)
196 #undef BOOST_CONTAINER_SCOPED_ALLOCATOR_DISPATCH_USES_ALLOCATOR_CODE
198 #define BOOST_CONTAINER_SCOPED_ALLOCATOR_DISPATCH_USES_ALLOCATOR_CODE(N) \
199 template < typename ConstructAlloc, typename ArgAlloc, typename T BOOST_MOVE_I##N BOOST_MOVE_CLASS##N >\
200 inline typename container_detail::enable_if_and\
202 , container_detail::is_not_pair<T>\
203 , uses_allocator<T, ArgAlloc>\
204 , container_detail::not_<is_constructible_with_allocator_prefix<T, ArgAlloc BOOST_MOVE_I##N BOOST_MOVE_TARG##N> >\
206 dispatch_uses_allocator\
207 (ConstructAlloc& construct_alloc, BOOST_FWD_REF(ArgAlloc) arg_alloc, T* p BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
209 allocator_traits<ConstructAlloc>::construct\
210 (construct_alloc, p BOOST_MOVE_I##N BOOST_MOVE_FWD##N, ::boost::forward<ArgAlloc>(arg_alloc));\
213 BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_SCOPED_ALLOCATOR_DISPATCH_USES_ALLOCATOR_CODE)
214 #undef BOOST_CONTAINER_SCOPED_ALLOCATOR_DISPATCH_USES_ALLOCATOR_CODE
216 #endif //#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
218 template < typename ConstructAlloc
222 BOOST_CONTAINER_DOC1ST(void, typename container_detail::enable_if<container_detail::is_pair<Pair> >::type)
223 dispatch_uses_allocator
224 ( ConstructAlloc & construct_alloc
225 , BOOST_FWD_REF(ArgAlloc) arg_alloc
228 (dispatch_uses_allocator)(construct_alloc, arg_alloc, container_detail::addressof(p->first));
230 (dispatch_uses_allocator)(construct_alloc, arg_alloc, container_detail::addressof(p->second));
233 allocator_traits<ConstructAlloc>::destroy(construct_alloc, container_detail::addressof(p->first));
240 template < typename ConstructAlloc
242 , class Pair, class U, class V>
243 BOOST_CONTAINER_DOC1ST(void, typename container_detail::enable_if<container_detail::is_pair<Pair> >::type)
244 dispatch_uses_allocator
245 ( ConstructAlloc & construct_alloc
246 , BOOST_FWD_REF(ArgAlloc) arg_alloc
247 , Pair* p, BOOST_FWD_REF(U) x, BOOST_FWD_REF(V) y)
249 (dispatch_uses_allocator)(construct_alloc, arg_alloc, container_detail::addressof(p->first), ::boost::forward<U>(x));
251 (dispatch_uses_allocator)(construct_alloc, arg_alloc, container_detail::addressof(p->second), ::boost::forward<V>(y));
254 allocator_traits<ConstructAlloc>::destroy(construct_alloc, container_detail::addressof(p->first));
260 template < typename ConstructAlloc
262 , class Pair, class Pair2>
263 BOOST_CONTAINER_DOC1ST(void, typename container_detail::enable_if< container_detail::is_pair<Pair> >::type)
264 dispatch_uses_allocator
265 (ConstructAlloc & construct_alloc
266 , BOOST_FWD_REF(ArgAlloc) arg_alloc
268 { (dispatch_uses_allocator)(construct_alloc, arg_alloc, p, x.first, x.second); }
270 template < typename ConstructAlloc
272 , class Pair, class Pair2>
273 typename container_detail::enable_if_and
275 , container_detail::is_pair<Pair>
276 , container_detail::not_<boost::move_detail::is_reference<Pair2> > >::type //This is needed for MSVC10 and ambiguous overloads
277 dispatch_uses_allocator
278 (ConstructAlloc & construct_alloc
279 , BOOST_FWD_REF(ArgAlloc) arg_alloc
280 , Pair* p, BOOST_RV_REF_BEG Pair2 BOOST_RV_REF_END x)
281 { (dispatch_uses_allocator)(construct_alloc, arg_alloc, p, ::boost::move(x.first), ::boost::move(x.second)); }
284 //piecewise construction from boost::tuple
285 #define BOOST_DISPATCH_USES_ALLOCATOR_PIECEWISE_CONSTRUCT_BOOST_TUPLE_CODE(N,M)\
286 template< typename ConstructAlloc, typename ArgAlloc, class Pair \
287 , template<class, class, class, class, class, class, class, class, class, class> class BoostTuple \
288 BOOST_MOVE_I_IF(BOOST_MOVE_OR(N,M)) BOOST_MOVE_CLASS##N BOOST_MOVE_I_IF(BOOST_MOVE_AND(N,M)) BOOST_MOVE_CLASSQ##M > \
289 typename container_detail::enable_if< container_detail::is_pair<Pair> >::type\
290 dispatch_uses_allocator( ConstructAlloc & construct_alloc, BOOST_FWD_REF(ArgAlloc) arg_alloc, Pair* pair, piecewise_construct_t\
291 , BoostTuple<BOOST_MOVE_TARG##N BOOST_MOVE_I##N BOOST_MOVE_REPEAT(BOOST_MOVE_SUB(10,N),::boost::tuples::null_type)> p\
292 , BoostTuple<BOOST_MOVE_TARGQ##M BOOST_MOVE_I##M BOOST_MOVE_REPEAT(BOOST_MOVE_SUB(10,M),::boost::tuples::null_type)> q)\
295 (dispatch_uses_allocator)\
296 (construct_alloc, arg_alloc, container_detail::addressof(pair->first) BOOST_MOVE_I_IF(N) BOOST_MOVE_TMPL_GET##N);\
298 (dispatch_uses_allocator)\
299 (construct_alloc, arg_alloc, container_detail::addressof(pair->second) BOOST_MOVE_I_IF(M) BOOST_MOVE_TMPL_GETQ##M);\
302 allocator_traits<ConstructAlloc>::destroy(construct_alloc, container_detail::addressof(pair->first));\
308 BOOST_MOVE_ITER2D_0TOMAX(9, BOOST_DISPATCH_USES_ALLOCATOR_PIECEWISE_CONSTRUCT_BOOST_TUPLE_CODE)
309 #undef BOOST_DISPATCH_USES_ALLOCATOR_PIECEWISE_CONSTRUCT_BOOST_TUPLE_CODE
311 //piecewise construction from Std Tuple
312 #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
314 template< typename ConstructAlloc, typename ArgAlloc, class Pair
315 , template<class ...> class Tuple, class... Args1, class... Args2, size_t... Indexes1, size_t... Indexes2>
316 void dispatch_uses_allocator_index( ConstructAlloc & construct_alloc, BOOST_FWD_REF(ArgAlloc) arg_alloc, Pair* pair
317 , Tuple<Args1...>& t1, Tuple<Args2...>& t2, index_tuple<Indexes1...>, index_tuple<Indexes2...>)
320 (dispatch_uses_allocator)(construct_alloc, arg_alloc, container_detail::addressof(pair->first), ::boost::forward<Args1>(get<Indexes1>(t1))...);
322 (dispatch_uses_allocator)(construct_alloc, arg_alloc, container_detail::addressof(pair->second), ::boost::forward<Args2>(get<Indexes2>(t2))...);
325 allocator_traits<ConstructAlloc>::destroy(construct_alloc, container_detail::addressof(pair->first));
331 template< typename ConstructAlloc, typename ArgAlloc, class Pair
332 , template<class ...> class Tuple, class... Args1, class... Args2>
333 typename container_detail::enable_if< container_detail::is_pair<Pair> >::type
334 dispatch_uses_allocator( ConstructAlloc & construct_alloc, BOOST_FWD_REF(ArgAlloc) arg_alloc, Pair* pair, piecewise_construct_t
335 , Tuple<Args1...> t1, Tuple<Args2...> t2)
337 (dispatch_uses_allocator_index)( construct_alloc, arg_alloc, pair, t1, t2
338 , typename build_number_seq<sizeof...(Args1)>::type()
339 , typename build_number_seq<sizeof...(Args2)>::type());
342 #elif defined(BOOST_MSVC) && (_CPPLIB_VER == 520)
344 //MSVC 2010 tuple implementation
345 #define BOOST_DISPATCH_USES_ALLOCATOR_PIECEWISE_CONSTRUCT_MSVC2010_TUPLE_CODE(N,M)\
346 template< typename ConstructAlloc, typename ArgAlloc, class Pair\
347 , template<class, class, class, class, class, class, class, class, class, class> class StdTuple\
348 BOOST_MOVE_I_IF(BOOST_MOVE_OR(N,M)) BOOST_MOVE_CLASS##N BOOST_MOVE_I_IF(BOOST_MOVE_AND(N,M)) BOOST_MOVE_CLASSQ##M > \
349 typename container_detail::enable_if< container_detail::is_pair<Pair> >::type\
350 dispatch_uses_allocator(ConstructAlloc & construct_alloc, BOOST_FWD_REF(ArgAlloc) arg_alloc, Pair* pair, piecewise_construct_t\
351 , StdTuple<BOOST_MOVE_TARG##N BOOST_MOVE_I##N BOOST_MOVE_REPEAT(BOOST_MOVE_SUB(10,N),::std::tr1::_Nil)> p\
352 , StdTuple<BOOST_MOVE_TARGQ##M BOOST_MOVE_I##M BOOST_MOVE_REPEAT(BOOST_MOVE_SUB(10,M),::std::tr1::_Nil)> q)\
355 (dispatch_uses_allocator)\
356 (construct_alloc, arg_alloc, container_detail::addressof(pair->first) BOOST_MOVE_I_IF(N) BOOST_MOVE_GET_IDX##N);\
358 (dispatch_uses_allocator)\
359 (construct_alloc, arg_alloc, container_detail::addressof(pair->second) BOOST_MOVE_I_IF(M) BOOST_MOVE_GET_IDXQ##M);\
362 allocator_traits<ConstructAlloc>::destroy(construct_alloc, container_detail::addressof(pair->first));\
368 BOOST_MOVE_ITER2D_0TOMAX(9, BOOST_DISPATCH_USES_ALLOCATOR_PIECEWISE_CONSTRUCT_MSVC2010_TUPLE_CODE)
369 #undef BOOST_DISPATCH_USES_ALLOCATOR_PIECEWISE_CONSTRUCT_MSVC2010_TUPLE_CODE
371 #elif defined(BOOST_MSVC) && (_CPPLIB_VER == 540)
372 #if _VARIADIC_MAX >= 9
373 #define BOOST_DISPATCH_USES_ALLOCATOR_PIECEWISE_CONSTRUCT_MSVC2012_TUPLE_MAX_IT 9
375 #define BOOST_DISPATCH_USES_ALLOCATOR_PIECEWISE_CONSTRUCT_MSVC2012_TUPLE_MAX_IT BOOST_MOVE_ADD(_VARIADIC_MAX, 1)
378 //MSVC 2012 tuple implementation
379 #define BOOST_DISPATCH_USES_ALLOCATOR_PIECEWISE_CONSTRUCT_MSVC2012_TUPLE_CODE(N,M)\
380 template< typename ConstructAlloc, typename ArgAlloc, class Pair\
381 , template<BOOST_MOVE_REPEAT(_VARIADIC_MAX, class), class, class, class> class StdTuple \
382 BOOST_MOVE_I_IF(BOOST_MOVE_OR(N,M)) BOOST_MOVE_CLASS##N BOOST_MOVE_I_IF(BOOST_MOVE_AND(N,M)) BOOST_MOVE_CLASSQ##M > \
383 typename container_detail::enable_if< container_detail::is_pair<Pair> >::type\
384 dispatch_uses_allocator\
385 ( ConstructAlloc & construct_alloc, BOOST_FWD_REF(ArgAlloc) arg_alloc, Pair* pair, piecewise_construct_t\
386 , StdTuple<BOOST_MOVE_TARG##N BOOST_MOVE_I##N BOOST_MOVE_REPEAT(BOOST_MOVE_SUB(BOOST_MOVE_ADD(_VARIADIC_MAX, 3),N),::std::_Nil) > p\
387 , StdTuple<BOOST_MOVE_TARGQ##M BOOST_MOVE_I##M BOOST_MOVE_REPEAT(BOOST_MOVE_SUB(BOOST_MOVE_ADD(_VARIADIC_MAX, 3),M),::std::_Nil) > q)\
390 (dispatch_uses_allocator)\
391 (construct_alloc, arg_alloc, container_detail::addressof(pair->first) BOOST_MOVE_I_IF(N) BOOST_MOVE_GET_IDX##N);\
393 (dispatch_uses_allocator)\
394 (construct_alloc, arg_alloc, container_detail::addressof(pair->second) BOOST_MOVE_I_IF(M) BOOST_MOVE_GET_IDXQ##M);\
397 allocator_traits<ConstructAlloc>::destroy(construct_alloc, container_detail::addressof(pair->first));\
403 BOOST_MOVE_ITER2D_0TOMAX(BOOST_DISPATCH_USES_ALLOCATOR_PIECEWISE_CONSTRUCT_MSVC2012_TUPLE_MAX_IT, BOOST_DISPATCH_USES_ALLOCATOR_PIECEWISE_CONSTRUCT_MSVC2012_TUPLE_CODE)
404 #undef BOOST_DISPATCH_USES_ALLOCATOR_PIECEWISE_CONSTRUCT_MSVC2010_TUPLE_CODE
405 #undef BOOST_DISPATCH_USES_ALLOCATOR_PIECEWISE_CONSTRUCT_MSVC2012_TUPLE_MAX_IT
407 #endif //!defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
409 #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
411 template < typename ConstructAlloc
413 , class Pair, class KeyType, class ... Args>
414 typename container_detail::enable_if< container_detail::is_pair<Pair>, void >::type
415 dispatch_uses_allocator
416 (ConstructAlloc & construct_alloc, BOOST_FWD_REF(ArgAlloc) arg_alloc, Pair* p, try_emplace_t, BOOST_FWD_REF(KeyType) k, BOOST_FWD_REF(Args) ...args)
418 (dispatch_uses_allocator)(construct_alloc, arg_alloc, container_detail::addressof(p->first), ::boost::forward<KeyType>(k));
420 (dispatch_uses_allocator)(construct_alloc, arg_alloc, container_detail::addressof(p->second), ::boost::forward<Args>(args)...);
423 allocator_traits<ConstructAlloc>::destroy(construct_alloc, container_detail::addressof(p->first));
431 #define BOOST_CONTAINER_DISPATCH_USES_ALLOCATOR_PAIR_TRY_EMPLACE_CODE(N) \
432 template <typename ConstructAlloc, typename ArgAlloc, class Pair, class KeyType BOOST_MOVE_I##N BOOST_MOVE_CLASS##N >\
433 inline typename container_detail::enable_if\
434 < container_detail::is_pair<Pair>, void >::type\
435 dispatch_uses_allocator\
436 (ConstructAlloc &construct_alloc, BOOST_FWD_REF(ArgAlloc) arg_alloc, Pair* p, try_emplace_t, \
437 BOOST_FWD_REF(KeyType) k BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
439 (dispatch_uses_allocator)(construct_alloc, arg_alloc, container_detail::addressof(p->first), ::boost::forward<KeyType>(k));\
441 (dispatch_uses_allocator)(construct_alloc, arg_alloc, container_detail::addressof(p->second) BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
444 allocator_traits<ConstructAlloc>::destroy(construct_alloc, container_detail::addressof(p->first));\
450 BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_DISPATCH_USES_ALLOCATOR_PAIR_TRY_EMPLACE_CODE)
451 #undef BOOST_CONTAINER_DISPATCH_USES_ALLOCATOR_PAIR_TRY_EMPLACE_CODE
453 #endif //!defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
455 } //namespace container_detail
457 }} // namespace boost { namespace container {
459 #include <boost/container/detail/config_end.hpp>
461 #endif // BOOST_CONTAINER_DISPATCH_USES_ALLOCATOR_HPP