1*38fd1498Szrj // Allocator traits -*- C++ -*-
2*38fd1498Szrj
3*38fd1498Szrj // Copyright (C) 2011-2018 Free Software Foundation, Inc.
4*38fd1498Szrj //
5*38fd1498Szrj // This file is part of the GNU ISO C++ Library. This library is free
6*38fd1498Szrj // software; you can redistribute it and/or modify it under the
7*38fd1498Szrj // terms of the GNU General Public License as published by the
8*38fd1498Szrj // Free Software Foundation; either version 3, or (at your option)
9*38fd1498Szrj // any later version.
10*38fd1498Szrj
11*38fd1498Szrj // This library is distributed in the hope that it will be useful,
12*38fd1498Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of
13*38fd1498Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*38fd1498Szrj // GNU General Public License for more details.
15*38fd1498Szrj
16*38fd1498Szrj // Under Section 7 of GPL version 3, you are granted additional
17*38fd1498Szrj // permissions described in the GCC Runtime Library Exception, version
18*38fd1498Szrj // 3.1, as published by the Free Software Foundation.
19*38fd1498Szrj
20*38fd1498Szrj // You should have received a copy of the GNU General Public License and
21*38fd1498Szrj // a copy of the GCC Runtime Library Exception along with this program;
22*38fd1498Szrj // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23*38fd1498Szrj // <http://www.gnu.org/licenses/>.
24*38fd1498Szrj
25*38fd1498Szrj /** @file ext/alloc_traits.h
26*38fd1498Szrj * This file is a GNU extension to the Standard C++ Library.
27*38fd1498Szrj */
28*38fd1498Szrj
29*38fd1498Szrj #ifndef _EXT_ALLOC_TRAITS_H
30*38fd1498Szrj #define _EXT_ALLOC_TRAITS_H 1
31*38fd1498Szrj
32*38fd1498Szrj #pragma GCC system_header
33*38fd1498Szrj
34*38fd1498Szrj #if __cplusplus >= 201103L
35*38fd1498Szrj # include <bits/move.h>
36*38fd1498Szrj # include <bits/alloc_traits.h>
37*38fd1498Szrj #else
38*38fd1498Szrj # include <bits/allocator.h> // for __alloc_swap
39*38fd1498Szrj #endif
40*38fd1498Szrj
_GLIBCXX_VISIBILITY(default)41*38fd1498Szrj namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
42*38fd1498Szrj {
43*38fd1498Szrj _GLIBCXX_BEGIN_NAMESPACE_VERSION
44*38fd1498Szrj
45*38fd1498Szrj /**
46*38fd1498Szrj * @brief Uniform interface to C++98 and C++11 allocators.
47*38fd1498Szrj * @ingroup allocators
48*38fd1498Szrj */
49*38fd1498Szrj template<typename _Alloc, typename = typename _Alloc::value_type>
50*38fd1498Szrj struct __alloc_traits
51*38fd1498Szrj #if __cplusplus >= 201103L
52*38fd1498Szrj : std::allocator_traits<_Alloc>
53*38fd1498Szrj #endif
54*38fd1498Szrj {
55*38fd1498Szrj typedef _Alloc allocator_type;
56*38fd1498Szrj #if __cplusplus >= 201103L
57*38fd1498Szrj typedef std::allocator_traits<_Alloc> _Base_type;
58*38fd1498Szrj typedef typename _Base_type::value_type value_type;
59*38fd1498Szrj typedef typename _Base_type::pointer pointer;
60*38fd1498Szrj typedef typename _Base_type::const_pointer const_pointer;
61*38fd1498Szrj typedef typename _Base_type::size_type size_type;
62*38fd1498Szrj typedef typename _Base_type::difference_type difference_type;
63*38fd1498Szrj // C++11 allocators do not define reference or const_reference
64*38fd1498Szrj typedef value_type& reference;
65*38fd1498Szrj typedef const value_type& const_reference;
66*38fd1498Szrj using _Base_type::allocate;
67*38fd1498Szrj using _Base_type::deallocate;
68*38fd1498Szrj using _Base_type::construct;
69*38fd1498Szrj using _Base_type::destroy;
70*38fd1498Szrj using _Base_type::max_size;
71*38fd1498Szrj
72*38fd1498Szrj private:
73*38fd1498Szrj template<typename _Ptr>
74*38fd1498Szrj using __is_custom_pointer
75*38fd1498Szrj = std::__and_<std::is_same<pointer, _Ptr>,
76*38fd1498Szrj std::__not_<std::is_pointer<_Ptr>>>;
77*38fd1498Szrj
78*38fd1498Szrj public:
79*38fd1498Szrj // overload construct for non-standard pointer types
80*38fd1498Szrj template<typename _Ptr, typename... _Args>
81*38fd1498Szrj static typename std::enable_if<__is_custom_pointer<_Ptr>::value>::type
82*38fd1498Szrj construct(_Alloc& __a, _Ptr __p, _Args&&... __args)
83*38fd1498Szrj {
84*38fd1498Szrj _Base_type::construct(__a, std::__to_address(__p),
85*38fd1498Szrj std::forward<_Args>(__args)...);
86*38fd1498Szrj }
87*38fd1498Szrj
88*38fd1498Szrj // overload destroy for non-standard pointer types
89*38fd1498Szrj template<typename _Ptr>
90*38fd1498Szrj static typename std::enable_if<__is_custom_pointer<_Ptr>::value>::type
91*38fd1498Szrj destroy(_Alloc& __a, _Ptr __p)
92*38fd1498Szrj { _Base_type::destroy(__a, std::__to_address(__p)); }
93*38fd1498Szrj
94*38fd1498Szrj static _Alloc _S_select_on_copy(const _Alloc& __a)
95*38fd1498Szrj { return _Base_type::select_on_container_copy_construction(__a); }
96*38fd1498Szrj
97*38fd1498Szrj static void _S_on_swap(_Alloc& __a, _Alloc& __b)
98*38fd1498Szrj { std::__alloc_on_swap(__a, __b); }
99*38fd1498Szrj
100*38fd1498Szrj static constexpr bool _S_propagate_on_copy_assign()
101*38fd1498Szrj { return _Base_type::propagate_on_container_copy_assignment::value; }
102*38fd1498Szrj
103*38fd1498Szrj static constexpr bool _S_propagate_on_move_assign()
104*38fd1498Szrj { return _Base_type::propagate_on_container_move_assignment::value; }
105*38fd1498Szrj
106*38fd1498Szrj static constexpr bool _S_propagate_on_swap()
107*38fd1498Szrj { return _Base_type::propagate_on_container_swap::value; }
108*38fd1498Szrj
109*38fd1498Szrj static constexpr bool _S_always_equal()
110*38fd1498Szrj { return _Base_type::is_always_equal::value; }
111*38fd1498Szrj
112*38fd1498Szrj static constexpr bool _S_nothrow_move()
113*38fd1498Szrj { return _S_propagate_on_move_assign() || _S_always_equal(); }
114*38fd1498Szrj
115*38fd1498Szrj template<typename _Tp>
116*38fd1498Szrj struct rebind
117*38fd1498Szrj { typedef typename _Base_type::template rebind_alloc<_Tp> other; };
118*38fd1498Szrj #else
119*38fd1498Szrj
120*38fd1498Szrj typedef typename _Alloc::pointer pointer;
121*38fd1498Szrj typedef typename _Alloc::const_pointer const_pointer;
122*38fd1498Szrj typedef typename _Alloc::value_type value_type;
123*38fd1498Szrj typedef typename _Alloc::reference reference;
124*38fd1498Szrj typedef typename _Alloc::const_reference const_reference;
125*38fd1498Szrj typedef typename _Alloc::size_type size_type;
126*38fd1498Szrj typedef typename _Alloc::difference_type difference_type;
127*38fd1498Szrj
128*38fd1498Szrj static pointer
129*38fd1498Szrj allocate(_Alloc& __a, size_type __n)
130*38fd1498Szrj { return __a.allocate(__n); }
131*38fd1498Szrj
132*38fd1498Szrj static void deallocate(_Alloc& __a, pointer __p, size_type __n)
133*38fd1498Szrj { __a.deallocate(__p, __n); }
134*38fd1498Szrj
135*38fd1498Szrj template<typename _Tp>
136*38fd1498Szrj static void construct(_Alloc& __a, pointer __p, const _Tp& __arg)
137*38fd1498Szrj { __a.construct(__p, __arg); }
138*38fd1498Szrj
139*38fd1498Szrj static void destroy(_Alloc& __a, pointer __p)
140*38fd1498Szrj { __a.destroy(__p); }
141*38fd1498Szrj
142*38fd1498Szrj static size_type max_size(const _Alloc& __a)
143*38fd1498Szrj { return __a.max_size(); }
144*38fd1498Szrj
145*38fd1498Szrj static const _Alloc& _S_select_on_copy(const _Alloc& __a) { return __a; }
146*38fd1498Szrj
147*38fd1498Szrj static void _S_on_swap(_Alloc& __a, _Alloc& __b)
148*38fd1498Szrj {
149*38fd1498Szrj // _GLIBCXX_RESOLVE_LIB_DEFECTS
150*38fd1498Szrj // 431. Swapping containers with unequal allocators.
151*38fd1498Szrj std::__alloc_swap<_Alloc>::_S_do_it(__a, __b);
152*38fd1498Szrj }
153*38fd1498Szrj
154*38fd1498Szrj template<typename _Tp>
155*38fd1498Szrj struct rebind
156*38fd1498Szrj { typedef typename _Alloc::template rebind<_Tp>::other other; };
157*38fd1498Szrj #endif
158*38fd1498Szrj };
159*38fd1498Szrj
160*38fd1498Szrj _GLIBCXX_END_NAMESPACE_VERSION
161*38fd1498Szrj } // namespace __gnu_cxx
162*38fd1498Szrj
163*38fd1498Szrj #endif
164