NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
allocator_traits.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_MEMORY_ALLOCATOR_TRAITS_HPP__
2#define NEFORCE_CORE_MEMORY_ALLOCATOR_TRAITS_HPP__
3
11
14NEFORCE_BEGIN_NAMESPACE__
15
21
23NEFORCE_BEGIN_INNER__
24
31struct __allocator_traits_base {
37 template <typename T, typename U, typename = void>
38 struct alloc_rebind {
39 using type = replace_first_para_t<U, T>;
40 };
41
47 template <typename T, typename U>
48 struct alloc_rebind<T, U, void_t<typename T::template rebind<U>::other>> {
49 using type = typename T::template rebind<U>::other;
50 };
51
55 template <typename T, typename U>
56 using alloc_rebind_t = typename alloc_rebind<T, U>::type;
57
58protected:
59 template <typename T>
60 using __pointer = typename T::pointer;
61 template <typename T>
62 using __c_pointer = typename T::const_pointer;
63};
64
65NEFORCE_END_INNER__
67
81template <typename Alloc>
82struct allocator_traits : inner::__allocator_traits_base {
83 using allocator_type = Alloc;
84 using value_type = typename Alloc::value_type;
86
87private:
88 template <template <typename> class Func, typename T, typename = void>
89 struct real_ptr {
90 using type = typename pointer_traits<pointer>::template rebind<T>;
91 };
92
93 template <template <typename> class Func, typename T>
94 struct real_ptr<Func, T, void_t<Func<Alloc>>> {
95 using type = Func<Alloc>;
96 };
97
98 template <typename, typename Ptr, typename = void>
99 struct real_diff {
100 using type = typename pointer_traits<Ptr>::difference_type;
101 };
102
103 template <typename AllocU, typename Ptr>
104 struct real_diff<AllocU, Ptr, void_t<typename AllocU::difference_type>> {
105 using type = typename AllocU::difference_type;
106 };
107
108 template <typename, typename Diff, typename = void>
109 struct real_size : make_unsigned<Diff> {};
110
111 template <typename AllocU, typename Diff>
112 struct real_size<AllocU, Diff, void_t<typename AllocU::size_type>> {
113 using type = typename AllocU::size_type;
114 };
115
116public:
117 using const_pointer = typename real_ptr<__c_pointer, const value_type>::type;
118 using difference_type = typename real_diff<Alloc, pointer>::type;
119 using size_type = typename real_size<Alloc, difference_type>::type;
120
125 template <typename T>
126 using rebind_alloc = alloc_rebind<Alloc, T>;
127
132 template <typename T>
134
135private:
136 template <typename T, typename... Args>
137 static constexpr enable_if_t<has_construct_v<Alloc, T, Args...>>
138 __construct_aux(Alloc& alloc, T* ptr,
139 Args&&... args) noexcept(noexcept(alloc.construct(ptr, _NEFORCE forward<Args>(args)...))) {
140 alloc.construct(ptr, _NEFORCE forward<Args>(args)...);
141 }
142
143 template <typename T, typename... Args>
144 static constexpr enable_if_t<
145 conjunction_v<negation<has_construct<Alloc, T, Args...>>, is_constructible<T, Args...>>>
146 __construct_aux(Alloc& /*unused*/, T* ptr,
147 Args&&... args) noexcept(_NEFORCE is_nothrow_constructible<T, Args...>::value) {
148 _NEFORCE construct(ptr, _NEFORCE forward<Args>(args)...);
149 }
150
151 template <typename Alloc2, typename T>
152 static constexpr auto __destroy_aux(Alloc2& alloc, T* ptr, int /*unused*/) noexcept(noexcept(alloc.destroy(ptr)))
153 -> decltype(alloc.destroy(ptr)) {
154 alloc.destroy(ptr);
155 }
156
157 template <typename Alloc2, typename T>
158 static constexpr void __destroy_aux(Alloc2& /*unused*/, T* ptr, ...) noexcept(is_nothrow_destructible_v<T>) {
159 _NEFORCE destroy(ptr);
160 }
161
162 template <typename Alloc2>
163 static constexpr auto __max_size_aux(Alloc2& alloc, int /*unused*/) noexcept(noexcept(alloc.max_size()))
164 -> decltype(alloc.max_size()) {
165 return alloc.max_size();
166 }
167
168 template <typename Alloc2>
169 static constexpr size_type __max_size_aux(Alloc2& /*unused*/, ...) noexcept {
170 return _NEFORCE numeric_traits<size_type>::max() / sizeof(value_type);
171 }
172
173public:
180 NEFORCE_NODISCARD static NEFORCE_CONSTEXPR20 pointer allocate(Alloc& alloc, size_type n) {
181 return alloc.allocate(n);
182 }
183
190 static NEFORCE_CONSTEXPR20 void deallocate(Alloc& alloc, pointer ptr, size_type n) { alloc.deallocate(ptr, n); }
191
200 template <typename T, typename... Args>
201 static NEFORCE_CONSTEXPR20 void construct(Alloc& alloc, T* ptr, Args&&... args) noexcept(
202 noexcept(allocator_traits::__construct_aux(alloc, ptr, _NEFORCE forward<Args>(args)...))) {
203 allocator_traits::__construct_aux(alloc, ptr, _NEFORCE forward<Args>(args)...);
204 }
205
212 template <typename T>
213 static NEFORCE_CONSTEXPR20 void destroy(Alloc& alloc,
214 T* ptr) noexcept(noexcept(allocator_traits::__destroy_aux(alloc, ptr, 0))) {
215 allocator_traits::__destroy_aux(alloc, ptr, 0);
216 }
217
223 static NEFORCE_CONSTEXPR20 size_type
224 max_size(const Alloc& alloc) noexcept(noexcept(allocator_traits::__max_size_aux(alloc, 0))) {
225 return allocator_traits::__max_size_aux(alloc, 0);
226 }
227};
228 // AllocationTraits
230
231NEFORCE_END_NAMESPACE__
232#endif // NEFORCE_CORE_MEMORY_ALLOCATOR_TRAITS_HPP__
static constexpr T max() noexcept
获取类型的最大值
内存构造和销毁函数
constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
typename replace_first_para< T, U >::type replace_first_para_t
replace_first_para的便捷别名
constexpr bool has_construct_v
has_construct的便捷变量模板
typename detected_or< Default, Op, Args... >::type detected_or_t
detected_or的便捷别名,返回检测到的类型或默认类型
constexpr bool is_nothrow_destructible_v
is_nothrow_destructible的便捷变量模板
constexpr bool conjunction_v
conjunction的便捷变量模板
void void_t
将任意类型映射为void
typename enable_if< Test, T >::type enable_if_t
enable_if的便捷别名
数值特征
分配器特性模板
static constexpr void deallocate(Alloc &alloc, pointer ptr, size_type n)
释放内存
static constexpr void destroy(Alloc &alloc, T *ptr) noexcept(noexcept(allocator_traits::__destroy_aux(alloc, ptr, 0)))
销毁对象
typename real_size< Alloc, difference_type >::type size_type
大小类型
static constexpr void construct(Alloc &alloc, T *ptr, Args &&... args) noexcept(noexcept(allocator_traits::__construct_aux(alloc, ptr, _NEFORCE forward< Args >(args)...)))
在已分配内存上构造对象
typename real_ptr< __c_pointer, const value_type >::type const_pointer
常量指针类型
static constexpr pointer allocate(Alloc &alloc, size_type n)
分配内存
typename real_diff< Alloc, pointer >::type difference_type
指针差异类型
alloc_rebind< Alloc, T > rebind_alloc
重新绑定分配器类型
allocator_traits< rebind_alloc< T > > rebind_traits
重新绑定分配器特性类型
static constexpr size_type max_size(const Alloc &alloc) noexcept(noexcept(allocator_traits::__max_size_aux(alloc, 0)))
获取分配器支持的最大大小
判断分配器是否具有construct成员函数
判断类型是否可以使用指定参数构造
判断类型是否可以使用指定参数无异常构造
将类整数类型转换为对应的无符号类型
逻辑非包装器
指针特性主模板