4#ifndef TETL_MEMORY_SMALL_PTR_HPP
5#define TETL_MEMORY_SMALL_PTR_HPP
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>
20template <
typename Type, intptr_t BaseAddress = 0,
typename StorageType = uint16_t>
34 : _value{compress(ptr)}
41 return reinterpret_cast<Type*>(BaseAddress + _value);
47 return reinterpret_cast<Type
const*>(BaseAddress + _value);
80 _value = compress(ptr);
89 _value = compress(ptr);
99 _value = compress(ptr);
108 _value = compress(ptr);
115 return get() - other.get();
131 [[
nodiscard]]
static auto compress(Type* ptr) -> StorageType
133 auto const obj =
reinterpret_cast<intptr_t>(ptr);
134 return StorageType(obj - BaseAddress);
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