tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
pointer_int_pair.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_MEMORY_POINTER_INT_PAIR_HPP
4#define TETL_MEMORY_POINTER_INT_PAIR_HPP
5
10
11namespace etl {
12
27template <
28 typename PointerT,
29 unsigned IntBits,
30 typename IntType = unsigned,
31 typename PtrTraits = pointer_like_traits<PointerT>,
34 using pointer_type = PointerT;
35 using pointer_traits = PtrTraits;
36 using pointer_info = Info;
37 using int_type = IntType;
38 static constexpr auto int_bits = IntBits;
39
40 constexpr pointer_int_pair() = default;
41
42 pointer_int_pair(pointer_type pointerValue, int_type intValue) { set_ptr_and_int(pointerValue, intValue); }
43
44 explicit pointer_int_pair(pointer_type pointerValue) { init_with_ptr(pointerValue); }
45
46 void set_pointer(pointer_type pointerValue) { _value = pointer_info::update_ptr(_value, pointerValue); }
47
48 void set_int(int_type intValue) { _value = pointer_info::update_int(_value, static_cast<intptr_t>(intValue)); }
49
50 [[nodiscard]] auto get_pointer() const -> pointer_type { return pointer_info::get_pointer(_value); }
51
52 [[nodiscard]] auto get_int() const -> int_type { return static_cast<int_type>(pointer_info::get_int(_value)); }
53
54 void set_ptr_and_int(pointer_type pointerValue, int_type intValue)
55 {
56 _value = pointer_info::update_int(pointer_info::update_ptr(0, pointerValue), static_cast<intptr_t>(intValue));
57 }
58
59 [[nodiscard]] auto get_addr_of_pointer() const -> pointer_type const*
60 {
61 return const_cast<pointer_int_pair*>(this)->get_addr_of_pointer();
62 }
63
65
66 [[nodiscard]] auto get_opaque_value() const -> void* { return bit_cast<void*>(_value); }
67
68 void set_from_opaque_value(void* val) { _value = bit_cast<intptr_t>(val); }
69
71 {
74 return p;
75 }
76
79 static auto get_from_opaque_value(void const* v) -> pointer_int_pair
80 {
81 (void)pointer_traits::get_from_void_pointer(v);
82 return get_from_opaque_value(const_cast<void*>(v));
83 }
84
85 [[nodiscard]] friend auto operator==(pointer_int_pair const& lhs, pointer_int_pair const& rhs) -> bool
86 {
87 return lhs._value == rhs._value;
88 }
89
90 [[nodiscard]] friend auto operator!=(pointer_int_pair const& lhs, pointer_int_pair const& rhs) -> bool
91 {
92 return lhs._value != rhs._value;
93 }
94
95 [[nodiscard]] friend auto operator<(pointer_int_pair const& lhs, pointer_int_pair const& rhs) -> bool
96 {
97 return lhs._value < rhs._value;
98 }
99
100 [[nodiscard]] friend auto operator>(pointer_int_pair const& lhs, pointer_int_pair const& rhs) -> bool
101 {
102 return lhs._value > rhs._value;
103 }
104
105 [[nodiscard]] friend auto operator<=(pointer_int_pair const& lhs, pointer_int_pair const& rhs) -> bool
106 {
107 return lhs._value <= rhs._value;
108 }
109
110 [[nodiscard]] friend auto operator>=(pointer_int_pair const& lhs, pointer_int_pair const& rhs) -> bool
111 {
112 return lhs._value >= rhs._value;
113 }
114
115private:
116 auto init_with_ptr(pointer_type pointerValue) -> void { _value = pointer_info::update_ptr(0, pointerValue); }
117
118 intptr_t _value = 0;
119};
120
121template <typename PtrT, unsigned IntBits, typename IntT, typename PtrTraits>
122struct pointer_like_traits<pointer_int_pair<PtrT, IntBits, IntT, PtrTraits>> {
124 {
125 return p.get_opaque_value();
126 }
127
132
137
138 static constexpr size_t free_bits = PtrTraits::free_bits - IntBits;
139};
140
141} // namespace etl
142
143#endif // TETL_MEMORY_POINTER_INT_PAIR_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_INTPTR intptr_t
Signed integer type capable of holding a pointer.
Definition intptr_t.hpp:11
Definition pointer_int_pair_info.hpp:15
This struct implements a pair of a pointer and small integer. It is designed to represent this in the...
Definition pointer_int_pair.hpp:33
void set_from_opaque_value(void *val)
Definition pointer_int_pair.hpp:68
friend auto operator>=(pointer_int_pair const &lhs, pointer_int_pair const &rhs) -> bool
Definition pointer_int_pair.hpp:110
void set_ptr_and_int(pointer_type pointerValue, int_type intValue)
Definition pointer_int_pair.hpp:54
static constexpr auto int_bits
Definition pointer_int_pair.hpp:38
pointer_int_pair(pointer_type pointerValue)
Definition pointer_int_pair.hpp:44
auto get_addr_of_pointer() -> pointer_type *
Definition pointer_int_pair.hpp:64
PointerT pointer_type
Definition pointer_int_pair.hpp:34
PtrTraits pointer_traits
Definition pointer_int_pair.hpp:35
Info pointer_info
Definition pointer_int_pair.hpp:36
auto get_pointer() const -> pointer_type
Definition pointer_int_pair.hpp:50
friend auto operator<=(pointer_int_pair const &lhs, pointer_int_pair const &rhs) -> bool
Definition pointer_int_pair.hpp:105
auto get_addr_of_pointer() const -> pointer_type const *
Definition pointer_int_pair.hpp:59
void set_pointer(pointer_type pointerValue)
Definition pointer_int_pair.hpp:46
friend auto operator<(pointer_int_pair const &lhs, pointer_int_pair const &rhs) -> bool
Definition pointer_int_pair.hpp:95
auto get_int() const -> int_type
Definition pointer_int_pair.hpp:52
constexpr pointer_int_pair()=default
IntType int_type
Definition pointer_int_pair.hpp:37
friend auto operator>(pointer_int_pair const &lhs, pointer_int_pair const &rhs) -> bool
Definition pointer_int_pair.hpp:100
static auto get_from_opaque_value(void const *v) -> pointer_int_pair
Allow pointer_int_pairs to be created from const void * if and only if the pointer type could be crea...
Definition pointer_int_pair.hpp:79
pointer_int_pair(pointer_type pointerValue, int_type intValue)
Definition pointer_int_pair.hpp:42
static auto get_from_opaque_value(void *v) -> pointer_int_pair
Definition pointer_int_pair.hpp:70
friend auto operator!=(pointer_int_pair const &lhs, pointer_int_pair const &rhs) -> bool
Definition pointer_int_pair.hpp:90
void set_int(int_type intValue)
Definition pointer_int_pair.hpp:48
friend auto operator==(pointer_int_pair const &lhs, pointer_int_pair const &rhs) -> bool
Definition pointer_int_pair.hpp:85
auto get_opaque_value() const -> void *
Definition pointer_int_pair.hpp:66
static auto get_as_void_pointer(pointer_int_pair< PtrT, IntBits, IntT > const &p) -> void *
Definition pointer_int_pair.hpp:123
static constexpr size_t free_bits
Definition pointer_int_pair.hpp:138
static auto get_from_void_pointer(void const *p) -> pointer_int_pair< PtrT, IntBits, IntT >
Definition pointer_int_pair.hpp:133
static auto get_from_void_pointer(void *p) -> pointer_int_pair< PtrT, IntBits, IntT >
Definition pointer_int_pair.hpp:128
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