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// SPDX-FileCopyrightText: Copyright (C) 2021 Tobias Hienzsch
3
4#ifndef TETL_MEMORY_SMALL_PTR_HPP
5#define TETL_MEMORY_SMALL_PTR_HPP
6
7#include <etl/_cstddef/nullptr_t.hpp>
8#include <etl/_cstddef/ptrdiff_t.hpp>
9#include <etl/_cstdint/uint_t.hpp>
10#include <etl/_utility/ignore_unused.hpp>
11
12namespace etl {
13
14/// \brief Compressed pointer to specified size. Intended to be used as a drop
15/// in replacement for native pointers.
16///
17/// \details Uses the base address to calculate an offset, which will be stored
18/// internally. If used on micro controllers, the base address should be set to
19/// the start of RAM. See your linker script.
20template <typename Type, intptr_t BaseAddress = 0, typename StorageType = uint16_t>
21struct small_ptr {
22 /// \brief Default construct empty small_ptr. May contain garbage.
23 small_ptr() = default;
24
25 /// \brief Construct from nullptr.
26 small_ptr(nullptr_t null)
27 : _value{0}
28 {
30 }
31
32 /// \brief Construct from raw pointer.
33 small_ptr(Type* ptr)
34 : _value{compress(ptr)}
35 {
36 }
37
38 /// \brief Returns a raw pointer to Type.
39 [[nodiscard]] auto get() noexcept -> Type*
40 {
41 return reinterpret_cast<Type*>(BaseAddress + _value);
42 }
43
44 /// \brief Returns a raw pointer to const Type.
45 [[nodiscard]] auto get() const noexcept -> Type const*
46 {
47 return reinterpret_cast<Type const*>(BaseAddress + _value);
48 }
49
50 /// \brief Returns the compressed underlying integer address.
51 [[nodiscard]] auto compressed_value() const noexcept -> StorageType
52 {
53 return _value;
54 }
55
56 /// \brief Returns a raw pointer to Type.
57 [[nodiscard]] auto operator->() const -> Type*
58 {
59 return get();
60 }
61
62 /// \brief Dereference pointer to Type&.
63 [[nodiscard]] auto operator*() -> Type&
64 {
65 return *get();
66 }
67
68 /// \brief Dereference pointer to Type const&.
69 [[nodiscard]] auto operator*() const -> Type const&
70 {
71 return *get();
72 }
73
74 /// \brief Pre increment of pointer.
75 [[nodiscard]] auto operator++(int) noexcept -> small_ptr
76 {
77 auto temp = *this;
78 auto* ptr = get();
79 ++ptr;
80 _value = compress(ptr);
81 return temp;
82 }
83
84 /// \brief Post increment of pointer.
85 [[nodiscard]] auto operator++() noexcept -> small_ptr&
86 {
87 auto* ptr = get();
88 ptr++;
89 _value = compress(ptr);
90 return *this;
91 }
92
93 /// \brief Pre decrement of pointer.
94 [[nodiscard]] auto operator--(int) noexcept -> small_ptr
95 {
96 auto temp = *this;
97 auto* ptr = get();
98 --ptr;
99 _value = compress(ptr);
100 return temp;
101 }
102
103 /// \brief Post decrement of pointer.
104 [[nodiscard]] auto operator--() noexcept -> small_ptr&
105 {
106 auto* ptr = get();
107 ptr--;
108 _value = compress(ptr);
109 return *this;
110 }
111
112 /// \brief Returns distance from this to other.
113 [[nodiscard]] auto operator-(small_ptr other) const noexcept -> ptrdiff_t
114 {
115 return get() - other.get();
116 }
117
118 /// \brief Implicit conversion to raw pointer to mutable.
119 [[nodiscard]] operator Type*() noexcept
120 {
121 return get();
122 }
123
124 /// \brief Implicit conversion to raw pointer to const.
125 [[nodiscard]] operator Type const*() const noexcept
126 {
127 return get();
128 }
129
130private:
131 [[nodiscard]] static auto compress(Type* ptr) -> StorageType
132 {
133 auto const obj = reinterpret_cast<intptr_t>(ptr);
134 return StorageType(obj - BaseAddress);
135 }
136
137 StorageType _value;
138};
139
140} // namespace etl
141
142#endif // TETL_MEMORY_SMALL_PTR_HPP
Definition adjacent_find.hpp:9
constexpr auto ignore_unused(Types &&...) -> void
Explicitly ignore arguments or variables.
Definition ignore_unused.hpp:18
Compressed pointer to specified size. Intended to be used as a drop in replacement for native pointer...
Definition small_ptr.hpp:21
auto get() const noexcept -> Type const *
Returns a raw pointer to const Type.
Definition small_ptr.hpp:45
auto get() noexcept -> Type *
Returns a raw pointer to Type.
Definition small_ptr.hpp:39
auto operator++() noexcept -> small_ptr &
Post increment of pointer.
Definition small_ptr.hpp:85
small_ptr(nullptr_t null)
Construct from nullptr.
Definition small_ptr.hpp:26
small_ptr(Type *ptr)
Construct from raw pointer.
Definition small_ptr.hpp:33
auto operator--(int) noexcept -> small_ptr
Pre decrement of pointer.
Definition small_ptr.hpp:94
auto operator*() const -> Type const &
Dereference pointer to Type const&.
Definition small_ptr.hpp:69
auto operator-(small_ptr other) const noexcept -> ptrdiff_t
Returns distance from this to other.
Definition small_ptr.hpp:113
auto operator--() noexcept -> small_ptr &
Post decrement of pointer.
Definition small_ptr.hpp:104
operator Type *() noexcept
Implicit conversion to raw pointer to mutable.
Definition small_ptr.hpp:119
auto operator++(int) noexcept -> small_ptr
Pre increment of pointer.
Definition small_ptr.hpp:75
auto operator->() const -> Type *
Returns a raw pointer to Type.
Definition small_ptr.hpp:57
operator Type const *() const noexcept
Implicit conversion to raw pointer to const.
Definition small_ptr.hpp:125
auto operator*() -> Type &
Dereference pointer to Type&.
Definition small_ptr.hpp:63
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:51