tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
small_ptr.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_MEMORY_SMALL_PTR_HPP
4#define TETL_MEMORY_SMALL_PTR_HPP
5
10
11namespace etl {
12
19template <typename Type, intptr_t BaseAddress = 0, typename StorageType = uint16_t>
20struct small_ptr {
22 small_ptr() = default;
23
26 : _value{0}
27 {
28 ignore_unused(null);
29 }
30
32 small_ptr(Type* ptr)
33 : _value{compress(ptr)}
34 {
35 }
36
38 [[nodiscard]] auto get() noexcept -> Type* { return reinterpret_cast<Type*>(BaseAddress + _value); }
39
41 [[nodiscard]] auto get() const noexcept -> Type const*
42 {
43 return reinterpret_cast<Type const*>(BaseAddress + _value);
44 }
45
47 [[nodiscard]] auto compressed_value() const noexcept -> StorageType { return _value; }
48
50 [[nodiscard]] auto operator->() const -> Type* { return get(); }
51
53 [[nodiscard]] auto operator*() -> Type& { return *get(); }
54
56 [[nodiscard]] auto operator*() const -> Type const& { return *get(); }
57
59 [[nodiscard]] auto operator++(int) noexcept -> small_ptr
60 {
61 auto temp = *this;
62 auto* ptr = get();
63 ++ptr;
64 _value = compress(ptr);
65 return temp;
66 }
67
69 [[nodiscard]] auto operator++() noexcept -> small_ptr&
70 {
71 auto* ptr = get();
72 ptr++;
73 _value = compress(ptr);
74 return *this;
75 }
76
78 [[nodiscard]] auto operator--(int) noexcept -> small_ptr
79 {
80 auto temp = *this;
81 auto* ptr = get();
82 --ptr;
83 _value = compress(ptr);
84 return temp;
85 }
86
88 [[nodiscard]] auto operator--() noexcept -> small_ptr&
89 {
90 auto* ptr = get();
91 ptr--;
92 _value = compress(ptr);
93 return *this;
94 }
95
97 [[nodiscard]] auto operator-(small_ptr other) const noexcept -> ptrdiff_t { return get() - other.get(); }
98
100 [[nodiscard]] operator Type*() noexcept { return get(); }
101
103 [[nodiscard]] operator Type const*() const noexcept { return get(); }
104
105private:
106 [[nodiscard]] static auto compress(Type* ptr) -> StorageType
107 {
108 auto const obj = reinterpret_cast<intptr_t>(ptr);
109 return StorageType(obj - BaseAddress);
110 }
111
112 StorageType _value;
113};
114
115} // namespace etl
116
117#endif // TETL_MEMORY_SMALL_PTR_HPP
Definition adjacent_find.hpp:8
TETL_BUILTIN_INTPTR intptr_t
Signed integer type capable of holding a pointer.
Definition intptr_t.hpp:11
TETL_BUILTIN_PTRDIFF ptrdiff_t
etl::ptrdiff_t is the signed integer type of the result of subtracting two pointers.
Definition ptrdiff_t.hpp:14
constexpr auto ignore_unused(Types &&...) -> void
Explicitly ignore arguments or variables.
Definition ignore_unused.hpp:17
decltype(nullptr) nullptr_t
etl::nullptr_t is the type of the null pointer literal, nullptr. It is a distinct type that is not it...
Definition nullptr_t.hpp:13
auto get() const noexcept -> Type const *
Returns a raw pointer to const Type.
Definition small_ptr.hpp:41
auto get() noexcept -> Type *
Returns a raw pointer to Type.
Definition small_ptr.hpp:38
auto operator++() noexcept -> small_ptr &
Post increment of pointer.
Definition small_ptr.hpp:69
small_ptr(nullptr_t null)
Construct from nullptr.
Definition small_ptr.hpp:25
small_ptr(Type *ptr)
Construct from raw pointer.
Definition small_ptr.hpp:32
auto operator--(int) noexcept -> small_ptr
Pre decrement of pointer.
Definition small_ptr.hpp:78
auto operator*() const -> Type const &
Dereference pointer to Type const&.
Definition small_ptr.hpp:56
auto operator-(small_ptr other) const noexcept -> ptrdiff_t
Returns distance from this to other.
Definition small_ptr.hpp:97
auto operator--() noexcept -> small_ptr &
Post decrement of pointer.
Definition small_ptr.hpp:88
auto operator++(int) noexcept -> small_ptr
Pre increment of pointer.
Definition small_ptr.hpp:59
auto operator->() const -> Type *
Returns a raw pointer to Type.
Definition small_ptr.hpp:50
auto operator*() -> Type &
Dereference pointer to Type&.
Definition small_ptr.hpp:53
small_ptr()=default
Default construct empty small_ptr. May contain garbage.
auto compressed_value() const noexcept -> StorageType
Returns the compressed underlying integer address.
Definition small_ptr.hpp:47