tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
pointer_like_traits.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_MEMORY_POINTER_LIKE_TRAITS_HPP
4#define TETL_MEMORY_POINTER_LIKE_TRAITS_HPP
5
8#include <etl/_math/ilog2.hpp>
9
10namespace etl {
11
14template <typename T>
16
18template <typename T>
19struct pointer_like_traits<T*> {
20 [[nodiscard]] static auto get_as_void_pointer(T* p) -> void* { return p; }
21
22 [[nodiscard]] static auto get_from_void_pointer(void* p) -> T* { return static_cast<T*>(p); }
23
24 static constexpr size_t free_bits = ilog2(alignof(T));
25};
26
28template <typename T>
29struct pointer_like_traits<T const> {
31
32 [[nodiscard]] static auto get_as_void_pointer(T const p) -> void const*
33 {
34 return non_const::get_as_void_pointer(p);
35 }
36
37 // NOLINTNEXTLINE(readability-const-return-type)
38 [[nodiscard]] static auto get_from_void_pointer(void const* p) -> T const
39 {
40 return non_const::get_from_void_pointer(const_cast<void*>(p));
41 }
42
43 static constexpr size_t free_bits = non_const::free_bits;
44};
45
47template <typename T>
48struct pointer_like_traits<T const*> {
50
51 [[nodiscard]] static auto get_as_void_pointer(T const* p) -> void const*
52 {
53 return non_const::get_as_void_pointer(const_cast<T*>(p));
54 }
55
56 [[nodiscard]] static auto get_from_void_pointer(void const* p) -> T const*
57 {
58 return non_const::get_from_void_pointer(const_cast<void*>(p));
59 }
60
61 static constexpr size_t free_bits = non_const::free_bits;
62};
63
65template <>
67 [[nodiscard]] static auto get_as_void_pointer(uintptr_t p) -> void* { return bit_cast<void*>(p); }
68
69 [[nodiscard]] static auto get_from_void_pointer(void* p) -> uintptr_t { return bit_cast<uintptr_t>(p); }
70
71 // No bits are available!
72 static constexpr size_t free_bits = 0;
73};
74
75} // namespace etl
76
77#endif // TETL_MEMORY_POINTER_LIKE_TRAITS_HPP
constexpr auto bit_cast(From const &src) noexcept -> To
Obtain a value of type To by reinterpreting the object representation of from. Every bit in the value...
Definition bit_cast.hpp:38
Definition adjacent_find.hpp:8
TETL_BUILTIN_UINTPTR uintptr_t
Unsigned integer type capable of holding a pointer.
Definition uintptr_t.hpp:11
constexpr auto ilog2(Int x) noexcept -> Int
Definition ilog2.hpp:11
static constexpr size_t free_bits
Definition pointer_like_traits.hpp:43
pointer_like_traits< T > non_const
Definition pointer_like_traits.hpp:30
static auto get_as_void_pointer(T const p) -> void const *
Definition pointer_like_traits.hpp:32
static auto get_from_void_pointer(void const *p) -> T const
Definition pointer_like_traits.hpp:38
static auto get_from_void_pointer(void const *p) -> T const *
Definition pointer_like_traits.hpp:56
static constexpr size_t free_bits
Definition pointer_like_traits.hpp:61
static auto get_as_void_pointer(T const *p) -> void const *
Definition pointer_like_traits.hpp:51
pointer_like_traits< T * > non_const
Definition pointer_like_traits.hpp:49
static constexpr size_t free_bits
Definition pointer_like_traits.hpp:72
static auto get_from_void_pointer(void *p) -> uintptr_t
Definition pointer_like_traits.hpp:69
static auto get_as_void_pointer(uintptr_t p) -> void *
Definition pointer_like_traits.hpp:67
A traits type that is used to handle pointer types and things that are just wrappers for pointers as ...
Definition pointer_like_traits.hpp:15