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