WPILibC++ 2025.1.1
Loading...
Searching...
No Matches
container.hpp
Go to the documentation of this file.
1// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors
2// SPDX-License-Identifier: Zlib
3
4#ifndef WPI_MEMORY_CONTAINER_HPP_INCLUDED
5#define WPI_MEMORY_CONTAINER_HPP_INCLUDED
6
7/// \file
8/// Aliasas for STL containers using a certain RawAllocator.
9/// \note Only available on a hosted implementation.
10
11#include "config.hpp"
12#if !WPI_HOSTED_IMPLEMENTATION
13#error "This header is only available for a hosted implementation."
14#endif
15
16#include <functional>
17#include <utility>
18
19#include <deque>
20#include <forward_list>
21#include <list>
22#include <map>
23#include <queue>
24#include <scoped_allocator>
25#include <set>
26#include <stack>
27#include <string>
28#include <unordered_map>
29#include <unordered_set>
30#include <vector>
31
32#include "std_allocator.hpp"
33#include "threading.hpp"
34
35namespace wpi
36{
37 namespace memory
38 {
39 /// \ingroup memory_adapter
40 /// @{
41
42 /// Alias template for an STL container that uses a certain
43 /// RawAllocator. It is just a shorthand for a passing in the \c
44 /// RawAllocator wrapped in a \ref wpi::memory::std_allocator.
45 template <typename T, class RawAllocator>
47
48 /// Same as above but uses \c std::scoped_allocator_adaptor so the allocator is inherited by all
49 /// nested containers.
50 template <typename T, class RawAllocator>
53 std::vector<T, std::scoped_allocator_adaptor<std_allocator<T, RawAllocator>>>);
54
55 /// \copydoc vector
56 template <typename T, class RawAllocator>
58 /// \copydoc vector_scoped_alloc
59 template <typename T, class RawAllocator>
62 std::deque<T, std::scoped_allocator_adaptor<std_allocator<T, RawAllocator>>>);
63
64 /// \copydoc vector
65 template <typename T, class RawAllocator>
67 /// \copydoc vector_scoped_alloc
68 template <typename T, class RawAllocator>
71 std::list<T, std::scoped_allocator_adaptor<std_allocator<T, RawAllocator>>>);
72
73 /// \copydoc vector
74 template <typename T, class RawAllocator>
76 std::forward_list<T, std_allocator<T, RawAllocator>>);
77 /// \copydoc vector_scoped_alloc
78 template <typename T, class RawAllocator>
81 std::forward_list<T, std::scoped_allocator_adaptor<std_allocator<T, RawAllocator>>>);
82
83 /// \copydoc vector
84 template <typename T, class RawAllocator>
86 /// \copydoc vector_scoped_alloc
87 template <typename T, class RawAllocator>
90 std::set<T, std::less<T>,
91 std::scoped_allocator_adaptor<std_allocator<T, RawAllocator>>>);
92
93 /// \copydoc vector
94 template <typename T, class RawAllocator>
96 std::multiset<T, std::less<T>, std_allocator<T, RawAllocator>>);
97 /// \copydoc vector_scoped_alloc
98 template <typename T, class RawAllocator>
101 std::multiset<T, std::less<T>,
102 std::scoped_allocator_adaptor<std_allocator<T, RawAllocator>>>);
103
104 /// \copydoc vector
105 template <typename Key, typename Value, class RawAllocator>
107 map, std::map<Key, Value, std::less<Key>,
108 std_allocator<std::pair<const Key, Value>, RawAllocator>>);
109 /// \copydoc vector_scoped_alloc
110 template <typename Key, typename Value, class RawAllocator>
113 std::map<Key, Value, std::less<Key>,
114 std::scoped_allocator_adaptor<
115 std_allocator<std::pair<const Key, Value>, RawAllocator>>>);
116
117 /// \copydoc vector
118 template <typename Key, typename Value, class RawAllocator>
120 multimap, std::multimap<Key, Value, std::less<Key>,
121 std_allocator<std::pair<const Key, Value>, RawAllocator>>);
122 /// \copydoc vector_scoped_alloc
123 template <typename Key, typename Value, class RawAllocator>
126 std::multimap<Key, Value, std::less<Key>,
127 std::scoped_allocator_adaptor<
128 std_allocator<std::pair<const Key, Value>, RawAllocator>>>);
129
130 /// \copydoc vector
131 template <typename T, class RawAllocator>
134 std::unordered_set<T, std::hash<T>, std::equal_to<T>, std_allocator<T, RawAllocator>>);
135 /// \copydoc vector_scoped_alloc
136 template <typename T, class RawAllocator>
139 std::unordered_set<T, std::hash<T>, std::equal_to<T>,
140 std::scoped_allocator_adaptor<std_allocator<T, RawAllocator>>>);
141
142 /// \copydoc vector
143 template <typename T, class RawAllocator>
145 std::unordered_multiset<T, std::hash<T>, std::equal_to<T>,
147 /// \copydoc vector_scoped_alloc
148 template <typename T, class RawAllocator>
151 std::unordered_multiset<T, std::hash<T>, std::equal_to<T>,
152 std::scoped_allocator_adaptor<std_allocator<T, RawAllocator>>>);
153
154 /// \copydoc vector
155 template <typename Key, typename Value, class RawAllocator>
158 std::unordered_map<Key, Value, std::hash<Key>, std::equal_to<Key>,
159 std_allocator<std::pair<const Key, Value>, RawAllocator>>);
160 /// \copydoc vector_scoped_alloc
161 template <typename Key, typename Value, class RawAllocator>
164 std::unordered_map<Key, Value, std::hash<Key>, std::equal_to<Key>,
165 std::scoped_allocator_adaptor<
166 std_allocator<std::pair<const Key, Value>, RawAllocator>>>);
167
168 /// \copydoc vector
169 template <typename Key, typename Value, class RawAllocator>
172 std::unordered_multimap<Key, Value, std::hash<Key>, std::equal_to<Key>,
173 std_allocator<std::pair<const Key, Value>, RawAllocator>>);
174 /// \copydoc vector_scoped_alloc
175 template <typename Key, typename Value, class RawAllocator>
178 std::unordered_multimap<Key, Value, std::hash<Key>, std::equal_to<Key>,
179 std::scoped_allocator_adaptor<
180 std_allocator<std::pair<const Key, Value>, RawAllocator>>>);
181
182 /// \copydoc vector
183 template <typename T, class RawAllocator>
185 /// \copydoc vector_scoped_alloc
186 template <typename T, class RawAllocator>
189
190 /// \copydoc vector
191 template <typename T, class RawAllocator>
193 /// \copydoc vector_scoped_alloc
194 template <typename T, class RawAllocator>
197
198 /// \copydoc vector
199 template <typename T, class RawAllocator>
201 /// \copydoc vector_scoped_alloc
202 template <typename T, class RawAllocator>
204 std::priority_queue<T, deque_scoped_alloc<T, RawAllocator>>);
205
206 /// \copydoc vector
207 template <class RawAllocator>
209 string,
210 std::basic_string<char, std::char_traits<char>, std_allocator<char, RawAllocator>>);
211 /// @}
212
213 /// @{
214 /// Convenience function to create a container adapter using a certain
215 /// RawAllocator. \returns An empty adapter with an
216 /// implementation container using a reference to a given allocator. \ingroup memory_adapter
217 template <typename T, class RawAllocator, class Container = deque<T, RawAllocator>>
218 std::stack<T, Container> make_stack(RawAllocator& allocator)
219 {
220 return std::stack<T, Container>{Container(allocator)};
221 }
222
223 /// \copydoc make_stack
224 template <typename T, class RawAllocator, class Container = deque<T, RawAllocator>>
225 std::queue<T, Container> make_queue(RawAllocator& allocator)
226 {
227 return std::queue<T, Container>{Container(allocator)};
228 }
229
230 /// \copydoc make_stack
231 template <typename T, class RawAllocator, class Container = deque<T, RawAllocator>,
232 class Compare = std::less<T>>
233 std::priority_queue<T, Container, Compare> make_priority_queue(RawAllocator& allocator,
234 Compare comp = {})
235 {
236 return std::priority_queue<T, Container, Compare>{detail::move(comp),
237 Container(allocator)};
238 }
239 /// @}
240
241#if !defined(DOXYGEN)
242
244
245#if !defined(WPI_MEMORY_NO_NODE_SIZE)
246 /// \exclude
247 namespace detail
248 {
249 template <typename T, class StdAllocator>
250 struct shared_ptr_node_size
251 {
252 static_assert(sizeof(T) != sizeof(T), "unsupported allocator type");
253 };
254
255 template <typename T, class RawAllocator>
256 struct shared_ptr_node_size<T, std_allocator<T, RawAllocator>>
257 : std::conditional<allocator_traits<RawAllocator>::is_stateful::value,
258 memory::shared_ptr_stateful_node_size<T>,
259 memory::shared_ptr_stateless_node_size<T>>::type
260 {
261 static_assert(sizeof(std_allocator<T, RawAllocator>) <= sizeof(void*),
262 "fix node size debugger");
263 };
264
265 } // namespace detail
266
267 template <typename T, class StdAllocator>
268 struct shared_ptr_node_size : detail::shared_ptr_node_size<T, StdAllocator>
269 {
270 };
271#endif
272
273#else
274 /// \ingroup memory_adapter
275 /// @{
276
277 /// Contains the node size of a node based STL container with a specific type.
278 ///
279 /// This trait is auto-generated and may not be available depending on the build configuration,
280 /// especially when doing cross compilation.
281 template <typename T>
282 struct forward_list_node_size : std::integral_constant<std::size_t, implementation_defined>
283 {
284 };
285
286 /// \copydoc forward_list_node_size
287 template <typename T>
288 struct list_node_size : std::integral_constant<std::size_t, implementation_defined>
289 {
290 };
291
292 /// \copydoc forward_list_node_size
293 template <typename T>
294 struct set_node_size : std::integral_constant<std::size_t, implementation_defined>
295 {
296 };
297
298 /// \copydoc forward_list_node_size
299 template <typename T>
300 struct multiset_node_size : std::integral_constant<std::size_t, implementation_defined>
301 {
302 };
303
304 /// \copydoc forward_list_node_size
305 template <typename T>
306 struct unordered_set_node_size : std::integral_constant<std::size_t, implementation_defined>
307 {
308 };
309
310 /// \copydoc forward_list_node_size
311 template <typename T>
313 : std::integral_constant<std::size_t, implementation_defined>
314 {
315 };
316
317 /// Contains the node size of a node based STL container with a specific type.
318 ///
319 /// This trait is auto-generated and may not be available depending on the build configuration,
320 /// especially when doing cross compilation.
321 ///
322 /// \notes `T` is always the `value_type` of the container, e.g. `std::pair<const Key, Value>`.
323 template <typename T>
324 struct map_node_size : std::integral_constant<std::size_t, implementation_defined>
325 {
326 };
327
328 /// \copydoc map_node_size
329 template <typename T>
330 struct multimap_node_size : std::integral_constant<std::size_t, implementation_defined>
331 {
332 };
333
334 /// \copydoc map_node_size
335 template <typename T>
336 struct unordered_map_node_size : std::integral_constant<std::size_t, implementation_defined>
337 {
338 };
339
340 /// \copydoc map_node_size
341 template <typename T>
343 : std::integral_constant<std::size_t, implementation_defined>
344 {
345 };
346
347 /// \copydoc forward_list_node_size
348 template <typename T, class StdAllocator>
349 struct shared_ptr_node_size : std::integral_constant<std::size_t, implementation_defined>
350 {
351 };
352/// @}
353#endif
354
355#if !defined(WPI_MEMORY_NO_NODE_SIZE)
356 /// The node size required by \ref allocate_shared.
357 /// \note This is similar to \ref shared_ptr_node_size but takes a
358 /// RawAllocator instead.
359 template <typename T, class RawAllocator>
360 struct allocate_shared_node_size : shared_ptr_node_size<T, std_allocator<T, RawAllocator>>
361 {
362 };
363#endif
364 } // namespace memory
365} // namespace wpi
366
367#endif // WPI_MEMORY_CONTAINER_HPP_INCLUDED
Same as above but uses std::scoped_allocator_adaptor so the allocator is inherited by all nested cont...
Definition container.hpp:62
Alias template for an STL container that uses a certain RawAllocator.
Definition container.hpp:57
Same as above but uses std::scoped_allocator_adaptor so the allocator is inherited by all nested cont...
Definition container.hpp:81
Alias template for an STL container that uses a certain RawAllocator.
Definition container.hpp:76
Same as above but uses std::scoped_allocator_adaptor so the allocator is inherited by all nested cont...
Definition container.hpp:71
Alias template for an STL container that uses a certain RawAllocator.
Definition container.hpp:66
Same as above but uses std::scoped_allocator_adaptor so the allocator is inherited by all nested cont...
Definition container.hpp:115
Alias template for an STL container that uses a certain RawAllocator.
Definition container.hpp:108
Same as above but uses std::scoped_allocator_adaptor so the allocator is inherited by all nested cont...
Definition container.hpp:128
Alias template for an STL container that uses a certain RawAllocator.
Definition container.hpp:121
Same as above but uses std::scoped_allocator_adaptor so the allocator is inherited by all nested cont...
Definition container.hpp:102
Alias template for an STL container that uses a certain RawAllocator.
Definition container.hpp:96
Same as above but uses std::scoped_allocator_adaptor so the allocator is inherited by all nested cont...
Definition container.hpp:204
Alias template for an STL container that uses a certain RawAllocator.
Definition container.hpp:200
Same as above but uses std::scoped_allocator_adaptor so the allocator is inherited by all nested cont...
Definition container.hpp:196
Alias template for an STL container that uses a certain RawAllocator.
Definition container.hpp:192
Same as above but uses std::scoped_allocator_adaptor so the allocator is inherited by all nested cont...
Definition container.hpp:91
Alias template for an STL container that uses a certain RawAllocator.
Definition container.hpp:85
Same as above but uses std::scoped_allocator_adaptor so the allocator is inherited by all nested cont...
Definition container.hpp:188
Alias template for an STL container that uses a certain RawAllocator.
Definition container.hpp:184
Wraps a RawAllocator and makes it a "normal" Allocator.
Definition std_allocator.hpp:80
Same as above but uses std::scoped_allocator_adaptor so the allocator is inherited by all nested cont...
Definition container.hpp:166
Alias template for an STL container that uses a certain RawAllocator.
Definition container.hpp:159
Same as above but uses std::scoped_allocator_adaptor so the allocator is inherited by all nested cont...
Definition container.hpp:180
Alias template for an STL container that uses a certain RawAllocator.
Definition container.hpp:173
Same as above but uses std::scoped_allocator_adaptor so the allocator is inherited by all nested cont...
Definition container.hpp:152
Alias template for an STL container that uses a certain RawAllocator.
Definition container.hpp:146
Same as above but uses std::scoped_allocator_adaptor so the allocator is inherited by all nested cont...
Definition container.hpp:140
Alias template for an STL container that uses a certain RawAllocator.
Definition container.hpp:134
Same as above but uses std::scoped_allocator_adaptor so the allocator is inherited by all nested cont...
Definition container.hpp:53
Alias template for an STL container that uses a certain RawAllocator.
Definition container.hpp:46
Configuration macros.
#define WPI_ALIAS_TEMPLATE(Name,...)
Definition config.hpp:73
std::stack< T, Container > make_stack(RawAllocator &allocator)
Definition container.hpp:218
std::priority_queue< T, Container, Compare > make_priority_queue(RawAllocator &allocator, Compare comp={})
Definition container.hpp:233
std::queue< T, Container > make_queue(RawAllocator &allocator)
Definition container.hpp:225
detail namespace with internal helper functions
Definition input_adapters.h:32
std::remove_reference< T >::type && move(T &&arg) noexcept
Definition utility.hpp:25
Memory namespace.
Definition heap_allocator.hpp:20
Foonathan namespace.
Definition ntcore_cpp.h:26
Class wpi::memory::std_allocator and related classes and functions.
The node size required by allocate_shared.
Definition container.hpp:361
Contains the node size of a node based STL container with a specific type.
Definition container.hpp:283
Contains the node size of a node based STL container with a specific type.
Definition container.hpp:289
Contains the node size of a node based STL container with a specific type.
Definition container.hpp:325
Contains the node size of a node based STL container with a specific type.
Definition container.hpp:331
Contains the node size of a node based STL container with a specific type.
Definition container.hpp:301
Contains the node size of a node based STL container with a specific type.
Definition container.hpp:295
Contains the node size of a node based STL container with a specific type.
Definition container.hpp:350
Contains the node size of a node based STL container with a specific type.
Definition container.hpp:337
Contains the node size of a node based STL container with a specific type.
Definition container.hpp:344
Contains the node size of a node based STL container with a specific type.
Definition container.hpp:314
Contains the node size of a node based STL container with a specific type.
Definition container.hpp:307
The mutex types.