1*e4b17023SJohn Marino // Uses-allocator Construction -*- C++ -*-
2*e4b17023SJohn Marino
3*e4b17023SJohn Marino // Copyright (C) 2010, 2011 Free Software Foundation, Inc.
4*e4b17023SJohn Marino //
5*e4b17023SJohn Marino // This file is part of the GNU ISO C++ Library. This library is free
6*e4b17023SJohn Marino // software; you can redistribute it and/or modify it under the
7*e4b17023SJohn Marino // terms of the GNU General Public License as published by the
8*e4b17023SJohn Marino // Free Software Foundation; either version 3, or (at your option)
9*e4b17023SJohn Marino // any later version.
10*e4b17023SJohn Marino
11*e4b17023SJohn Marino // This library is distributed in the hope that it will be useful,
12*e4b17023SJohn Marino // but WITHOUT ANY WARRANTY; without even the implied warranty of
13*e4b17023SJohn Marino // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*e4b17023SJohn Marino // GNU General Public License for more details.
15*e4b17023SJohn Marino
16*e4b17023SJohn Marino // Under Section 7 of GPL version 3, you are granted additional
17*e4b17023SJohn Marino // permissions described in the GCC Runtime Library Exception, version
18*e4b17023SJohn Marino // 3.1, as published by the Free Software Foundation.
19*e4b17023SJohn Marino
20*e4b17023SJohn Marino // You should have received a copy of the GNU General Public License and
21*e4b17023SJohn Marino // a copy of the GCC Runtime Library Exception along with this program;
22*e4b17023SJohn Marino // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23*e4b17023SJohn Marino // <http://www.gnu.org/licenses/>.
24*e4b17023SJohn Marino
25*e4b17023SJohn Marino #ifndef _USES_ALLOCATOR_H
26*e4b17023SJohn Marino #define _USES_ALLOCATOR_H 1
27*e4b17023SJohn Marino
28*e4b17023SJohn Marino #ifndef __GXX_EXPERIMENTAL_CXX0X__
29*e4b17023SJohn Marino # include <bits/c++0x_warning.h>
30*e4b17023SJohn Marino #else
31*e4b17023SJohn Marino
32*e4b17023SJohn Marino #include <type_traits>
33*e4b17023SJohn Marino
_GLIBCXX_VISIBILITY(default)34*e4b17023SJohn Marino namespace std _GLIBCXX_VISIBILITY(default)
35*e4b17023SJohn Marino {
36*e4b17023SJohn Marino _GLIBCXX_BEGIN_NAMESPACE_VERSION
37*e4b17023SJohn Marino
38*e4b17023SJohn Marino /// [allocator.tag]
39*e4b17023SJohn Marino struct allocator_arg_t { };
40*e4b17023SJohn Marino
41*e4b17023SJohn Marino constexpr allocator_arg_t allocator_arg = allocator_arg_t();
42*e4b17023SJohn Marino
43*e4b17023SJohn Marino _GLIBCXX_HAS_NESTED_TYPE(allocator_type)
44*e4b17023SJohn Marino
45*e4b17023SJohn Marino template<typename _Tp, typename _Alloc,
46*e4b17023SJohn Marino bool = __has_allocator_type<_Tp>::value>
47*e4b17023SJohn Marino struct __uses_allocator_helper
48*e4b17023SJohn Marino : public false_type { };
49*e4b17023SJohn Marino
50*e4b17023SJohn Marino template<typename _Tp, typename _Alloc>
51*e4b17023SJohn Marino struct __uses_allocator_helper<_Tp, _Alloc, true>
52*e4b17023SJohn Marino : public integral_constant<bool, is_convertible<_Alloc,
53*e4b17023SJohn Marino typename _Tp::allocator_type>::value>
54*e4b17023SJohn Marino { };
55*e4b17023SJohn Marino
56*e4b17023SJohn Marino /// [allocator.uses.trait]
57*e4b17023SJohn Marino template<typename _Tp, typename _Alloc>
58*e4b17023SJohn Marino struct uses_allocator
59*e4b17023SJohn Marino : public integral_constant<bool,
60*e4b17023SJohn Marino __uses_allocator_helper<_Tp, _Alloc>::value>
61*e4b17023SJohn Marino { };
62*e4b17023SJohn Marino
63*e4b17023SJohn Marino template<typename _Tp, typename _Alloc, typename... _Args>
64*e4b17023SJohn Marino struct __uses_allocator_arg
65*e4b17023SJohn Marino : is_constructible<_Tp, _Alloc, _Args...>
66*e4b17023SJohn Marino { static_assert( uses_allocator<_Tp, _Alloc>::value, "uses allocator" ); };
67*e4b17023SJohn Marino
68*e4b17023SJohn Marino struct __uses_alloc_base { };
69*e4b17023SJohn Marino struct __uses_alloc0 : __uses_alloc_base
70*e4b17023SJohn Marino { struct _Anything { _Anything(...) { } } _M_a; };
71*e4b17023SJohn Marino template<typename _Alloc>
72*e4b17023SJohn Marino struct __uses_alloc1 : __uses_alloc_base { const _Alloc* _M_a; };
73*e4b17023SJohn Marino template<typename _Alloc>
74*e4b17023SJohn Marino struct __uses_alloc2 : __uses_alloc_base { const _Alloc* _M_a; };
75*e4b17023SJohn Marino
76*e4b17023SJohn Marino template<bool, typename _Alloc, typename... _Args>
77*e4b17023SJohn Marino struct __uses_alloc;
78*e4b17023SJohn Marino
79*e4b17023SJohn Marino template<typename _Tp, typename _Alloc, typename... _Args>
80*e4b17023SJohn Marino struct __uses_alloc<true, _Tp, _Alloc, _Args...>
81*e4b17023SJohn Marino : conditional<
82*e4b17023SJohn Marino is_constructible<_Tp, allocator_arg_t, _Alloc, _Args...>::value,
83*e4b17023SJohn Marino __uses_alloc1<_Alloc>,
84*e4b17023SJohn Marino __uses_alloc2<_Alloc>>::type
85*e4b17023SJohn Marino { };
86*e4b17023SJohn Marino
87*e4b17023SJohn Marino template<typename _Tp, typename _Alloc, typename... _Args>
88*e4b17023SJohn Marino struct __uses_alloc<false, _Tp, _Alloc, _Args...>
89*e4b17023SJohn Marino : __uses_alloc0 { };
90*e4b17023SJohn Marino
91*e4b17023SJohn Marino template<typename _Tp, typename _Alloc, typename... _Args>
92*e4b17023SJohn Marino struct __uses_alloc_impl
93*e4b17023SJohn Marino : __uses_alloc<uses_allocator<_Tp, _Alloc>::value, _Tp, _Alloc, _Args...>
94*e4b17023SJohn Marino { };
95*e4b17023SJohn Marino
96*e4b17023SJohn Marino template<typename _Tp, typename _Alloc, typename... _Args>
97*e4b17023SJohn Marino __uses_alloc_impl<_Tp, _Alloc, _Args...>
98*e4b17023SJohn Marino __use_alloc(const _Alloc& __a)
99*e4b17023SJohn Marino {
100*e4b17023SJohn Marino __uses_alloc_impl<_Tp, _Alloc, _Args...> __ret;
101*e4b17023SJohn Marino __ret._M_a = &__a;
102*e4b17023SJohn Marino return __ret;
103*e4b17023SJohn Marino }
104*e4b17023SJohn Marino
105*e4b17023SJohn Marino _GLIBCXX_END_NAMESPACE_VERSION
106*e4b17023SJohn Marino } // namespace std
107*e4b17023SJohn Marino
108*e4b17023SJohn Marino #endif
109*e4b17023SJohn Marino #endif
110