3#ifndef TETL_MEMORY_SMALL_PTR_HPP
4#define TETL_MEMORY_SMALL_PTR_HPP
19template <
typename Type,
intptr_t BaseAddress = 0,
typename StorageType = u
int16_t>
33 : _value{compress(ptr)}
38 [[nodiscard]]
auto get() noexcept -> Type* {
return reinterpret_cast<Type*
>(BaseAddress + _value); }
41 [[nodiscard]]
auto get() const noexcept -> Type const*
43 return reinterpret_cast<Type const*
>(BaseAddress + _value);
56 [[nodiscard]]
auto operator*() const -> Type const& {
return *
get(); }
64 _value = compress(ptr);
73 _value = compress(ptr);
83 _value = compress(ptr);
92 _value = compress(ptr);
100 [[nodiscard]]
operator Type*()
noexcept {
return get(); }
103 [[nodiscard]]
operator Type
const*()
const noexcept {
return get(); }
106 [[nodiscard]]
static auto compress(Type* ptr) -> StorageType
108 auto const obj =
reinterpret_cast<intptr_t>(ptr);
109 return StorageType(obj - BaseAddress);
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