MSTL 1.4.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
allocator_traits.hpp
浏览该文件的文档.
1#ifndef MSTL_CORE_MEMORY_ALLOCATOR_TRAITS_HPP__
2#define MSTL_CORE_MEMORY_ALLOCATOR_TRAITS_HPP__
3
11
15
21
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
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, Args&&... args)
139 noexcept(noexcept(alloc.construct(ptr, _MSTL forward<Args>(args)...))) {
140 alloc.construct(ptr, _MSTL forward<Args>(args)...);
141 }
142
143 template <typename T, typename... Args>
144 static constexpr
145 enable_if_t<conjunction_v<negation<has_construct<Alloc, T, Args...>>, is_constructible<T, Args...>>>
146 __construct_aux(Alloc&, T* ptr, Args&&... args)
147 noexcept(_MSTL is_nothrow_constructible<T, Args...>::value) {
148 _MSTL construct(ptr, _MSTL forward<Args>(args)...);
149 }
150
151 template <typename Alloc2, typename T>
152 static constexpr auto __destroy_aux(Alloc2& alloc, T* ptr, int)
153 noexcept(noexcept(alloc.destroy(ptr))) -> decltype(alloc.destroy(ptr)) {
154 alloc.destroy(ptr);
155 }
156
157 template <typename Alloc2, typename T>
158 static constexpr void __destroy_aux(Alloc2&, T* ptr, ...)
159 noexcept(is_nothrow_destructible_v<T>) {
160 _MSTL destroy(ptr);
161 }
162
163 template <typename Alloc2>
164 static constexpr auto __max_size_aux(Alloc2& alloc, int)
165 noexcept(noexcept(alloc.max_size()))
166 -> decltype(alloc.max_size()) {
167 return alloc.max_size();
168 }
169
170 template <typename Alloc2>
171 static constexpr size_type __max_size_aux(Alloc2&, ...) noexcept {
172 return _MSTL numeric_traits<size_type>::max() / sizeof(value_type);
173 }
174
175public:
182 MSTL_NODISCARD static MSTL_CONSTEXPR20 pointer allocate(Alloc& alloc, size_type n) {
183 return alloc.allocate(n);
184 }
185
192 static MSTL_CONSTEXPR20 void deallocate(Alloc& alloc, pointer ptr, size_type n) {
193 alloc.deallocate(ptr, n);
194 }
195
204 template <typename T, typename... Args>
205 static MSTL_CONSTEXPR20 void construct(Alloc& alloc, T* ptr, Args&&... args)
206 noexcept(noexcept(allocator_traits::__construct_aux(alloc, ptr, _MSTL forward<Args>(args)...))) {
207 allocator_traits::__construct_aux(alloc, ptr, _MSTL forward<Args>(args)...);
208 }
209
216 template <typename T>
217 static MSTL_CONSTEXPR20 void destroy(Alloc& alloc, T* ptr)
218 noexcept(noexcept(allocator_traits::__destroy_aux(alloc, ptr, 0))) {
219 allocator_traits::__destroy_aux(alloc, ptr, 0);
220 }
221
227 static MSTL_CONSTEXPR20 size_type max_size(const Alloc& alloc)
228 noexcept(noexcept(allocator_traits::__max_size_aux(alloc, 0))) {
229 return allocator_traits::__max_size_aux(alloc, 0);
230 }
231};
232 // AllocationTraits
234
236#endif // MSTL_CORE_MEMORY_ALLOCATOR_TRAITS_HPP__
static MSTL_NODISCARD constexpr T max() noexcept
获取类型的最大值
MSTL内存构造和销毁函数
MSTL_NODISCARD constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
#define _MSTL
全局命名空间MSTL前缀
#define MSTL_END_INNER__
结束inner命名空间
#define _INNER
inner命名空间前缀
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
#define MSTL_BEGIN_INNER__
开始inner命名空间
typename replace_first_para< T, U >::type replace_first_para_t
replace_first_para的便捷别名
typename detected_or< Default, Op, Args... >::type detected_or_t
detected_or的便捷别名,返回检测到的类型或默认类型
void void_t
将任意类型映射为void
typename enable_if< Test, T >::type enable_if_t
enable_if的便捷别名
MSTL数值特征
分配器特性模板
typename real_size< Alloc, difference_type >::type size_type
大小类型
static MSTL_CONSTEXPR20 size_type max_size(const Alloc &alloc) noexcept(noexcept(allocator_traits::__max_size_aux(alloc, 0)))
获取分配器支持的最大大小
typename real_ptr< __c_pointer, const value_type >::type const_pointer
常量指针类型
static MSTL_CONSTEXPR20 void destroy(Alloc &alloc, T *ptr) noexcept(noexcept(allocator_traits::__destroy_aux(alloc, ptr, 0)))
销毁对象
static MSTL_NODISCARD MSTL_CONSTEXPR20 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 MSTL_CONSTEXPR20 void deallocate(Alloc &alloc, pointer ptr, size_type n)
释放内存
static MSTL_CONSTEXPR20 void construct(Alloc &alloc, T *ptr, Args &&... args) noexcept(noexcept(allocator_traits::__construct_aux(alloc, ptr, _MSTL forward< Args >(args)...)))
在已分配内存上构造对象
判断分配器是否具有construct成员函数
判断类型是否可以使用指定参数构造
判断类型是否可以使用指定参数无异常构造
将类整数类型转换为对应的无符号类型
逻辑非包装器
指针特性主模板