tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
allocator_traits.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2024 Tobias Hienzsch
3
4#ifndef TETL_MEMORY_ALLOCATOR_TRAITS_HPP
5#define TETL_MEMORY_ALLOCATOR_TRAITS_HPP
6
7#include <etl/_memory/pointer_traits.hpp>
8#include <etl/_type_traits/make_unsigned.hpp>
9
10namespace etl {
11
12namespace detail {
13
14template <typename Alloc>
15struct allocator_traits_pointer {
16 using type = typename Alloc::value_type*;
17};
18
19template <typename Alloc>
20 requires requires { typename Alloc::pointer; }
21struct allocator_traits_pointer<Alloc> {
22 using type = typename Alloc::pointer;
23};
24
25template <typename Alloc>
26struct allocator_traits_const_pointer {
27 using pointer = typename allocator_traits_pointer<Alloc>::type;
28 using type = typename etl::pointer_traits<pointer>::template rebind<typename Alloc::value_type const>;
29};
30
31template <typename Alloc>
32 requires requires { typename Alloc::const_pointer; }
33struct allocator_traits_const_pointer<Alloc> {
34 using type = typename Alloc::const_pointer;
35};
36
37template <typename Alloc>
38struct allocator_traits_void_pointer {
39 using pointer = typename allocator_traits_pointer<Alloc>::type;
40 using type = typename etl::pointer_traits<pointer>::template rebind<void>;
41};
42
43template <typename Alloc>
44 requires requires { typename Alloc::void_pointer; }
45struct allocator_traits_void_pointer<Alloc> {
46 using type = typename Alloc::void_pointer;
47};
48
49template <typename Alloc>
50struct allocator_traits_const_void_pointer {
51 using pointer = typename allocator_traits_pointer<Alloc>::type;
52 using type = typename etl::pointer_traits<pointer>::template rebind<void const>;
53};
54
55template <typename Alloc>
56 requires requires { typename Alloc::const_void_pointer; }
57struct allocator_traits_const_void_pointer<Alloc> {
58 using type = typename Alloc::const_void_pointer;
59};
60
61template <typename Alloc>
62struct allocator_traits_difference_type {
63 using pointer = typename allocator_traits_pointer<Alloc>::type;
64 using type = typename etl::pointer_traits<pointer>::difference_type;
65};
66
67template <typename Alloc>
68 requires requires { typename Alloc::difference_type; }
69struct allocator_traits_difference_type<Alloc> {
70 using type = typename Alloc::difference_type;
71};
72
73template <typename Alloc>
74struct allocator_traits_size_type {
75 using difference_type = typename allocator_traits_difference_type<Alloc>::type;
76 using type = etl::make_unsigned_t<difference_type>;
77};
78
79template <typename Alloc>
80 requires requires { typename Alloc::size_type; }
81struct allocator_traits_size_type<Alloc> {
82 using type = typename Alloc::size_type;
83};
84
85} // namespace detail
86
87template <typename Alloc>
89 using allocator_type = Alloc;
90 using value_type = typename Alloc::value_type;
91 using pointer = typename detail::allocator_traits_pointer<Alloc>::type;
92 using const_pointer = typename detail::allocator_traits_const_pointer<Alloc>::type;
93 using void_pointer = typename detail::allocator_traits_void_pointer<Alloc>::type;
94 using const_void_pointer = typename detail::allocator_traits_const_void_pointer<Alloc>::type;
95 using difference_type = typename detail::allocator_traits_difference_type<Alloc>::type;
96 using size_type = typename detail::allocator_traits_size_type<Alloc>::type;
97
98 [[nodiscard]] static constexpr auto allocate(Alloc& a, size_type n)
99 {
100 return a.allocate(n);
101 }
102
103 static constexpr void deallocate(Alloc& a, pointer p, size_type n)
104 {
105 a.deallocate(p, n);
106 }
107};
108
109} // namespace etl
110
111#endif // TETL_MEMORY_ALLOCATOR_TRAITS_HPP
Definition adjacent_find.hpp:9
Definition allocator_traits.hpp:88
static constexpr auto allocate(Alloc &a, size_type n)
Definition allocator_traits.hpp:98
static constexpr void deallocate(Alloc &a, pointer p, size_type n)
Definition allocator_traits.hpp:103
The pointer_traits class template provides the standardized way to access certain properties of point...
Definition pointer_traits.hpp:18