Go to the documentation of this file.
9 #ifndef EAGINE_MEMORY_OFFSET_PTR_HPP
10 #define EAGINE_MEMORY_OFFSET_PTR_HPP
12 #include "../assert.hpp"
13 #include "../extract.hpp"
14 #include "../type_identity.hpp"
17 namespace eagine::memory {
23 template <
typename Po
intee,
typename OffsetType>
59 : _offs{_get_offs(
addr)} {}
63 : _offs{_get_offs(ptr)} {}
67 : _offs{_get_offs(that)} {}
71 : _offs{_get_offs(that)} {}
74 template <
typename P,
typename O>
76 std::is_convertible_v<O, OffsetType> &&
77 std::is_convertible_v<P*, Pointee*>>;
79 template <
typename P,
typename O>
80 using enable_if_different = std::enable_if<
81 !std::is_same_v<O, OffsetType> && !std::is_same_v<P, Pointee>>;
88 typename = enable_if_different<P, O>>
90 : _offs{_get_offs(that)} {}
95 if(
this != std::addressof(that)) {
96 _offs = _get_offs(that);
103 _offs = _get_offs(that);
108 auto reset(Pointee* ptr) noexcept ->
auto& {
118 constexpr
auto is_null() const noexcept ->
bool {
124 explicit constexpr
operator bool() const noexcept {
225 static_assert(std::is_signed_v<offset_type>);
230 template <
typename P,
typename O>
232 return address(_rawptr(&that));
235 auto _this_addr() const noexcept {
236 return _that_addr(*
this);
240 return addr ?
addr - _this_addr() : 0;
243 auto _get_offs(Pointee* ptr) noexcept {
244 return _get_offs(
address(ptr));
247 template <
typename P,
typename O>
248 auto _get_offs(
const basic_offset_ptr<P, O>& that) noexcept ->
offset_type {
251 _get_offs(_that_addr(that));
259 template <
typename P,
typename O>
261 return EAGINE_CONSTEXPR_ASSERT(!ptr.is_null(), ptr.get());
265 template <
typename P,
typename O>
266 static constexpr
auto
268 return ptr.is_null() ? fallback : ptr.get();
272 template <
typename P,
typename O,
typename F>
274 -> std::enable_if_t<std::is_convertible_v<F, P>, P> {
275 return ptr.is_null() ? P{std::forward<F>(fallback)} : ptr.get();
280 template <
typename Ptr,
typename U>
281 struct rebind_pointer;
283 template <
typename T,
typename O,
typename U>
289 template <
typename P,
typename O>
297 template <
typename Po
intee>
302 template <
typename Po
intee>
307 #endif // EAGINE_MEMORY_OFFSET_PTR_HPP
OffsetType offset_type
The offset type.
Definition: offset_ptr.hpp:33
auto reset(address adr) noexcept -> auto &
Point this pointer to another address.
Definition: offset_ptr.hpp:113
Class for handling memory addresses as integer values.
Definition: address.hpp:23
auto operator=(const basic_offset_ptr &that) noexcept -> basic_offset_ptr &
Copy assignment operator.
Definition: offset_ptr.hpp:94
const shader_source_header * pointer
The non-const pointer type.
Definition: offset_ptr.hpp:36
basic_offset_ptr(const basic_offset_ptr< P, O > &that) noexcept
Conversion copy constructor.
Definition: offset_ptr.hpp:89
basic_offset_ptr(Pointee *ptr) noexcept
Construction from a pointer.
Definition: offset_ptr.hpp:62
auto get() noexcept -> pointer
Returns the stored pointer. Alias for data().
Definition: offset_ptr.hpp:155
constexpr basic_offset_ptr(address addr) noexcept
Construction from a memory address.
Definition: offset_ptr.hpp:58
auto operator++() noexcept -> auto &
Pointer arithmentic increment.
Definition: offset_ptr.hpp:202
static constexpr auto extract(api_result_value< Result, api_result_validity::never > &) noexcept -> Result &
Overload of extract for api_result_value.
Definition: c_api_wrap.hpp:270
Basic offset pointer class template.
Definition: offset_ptr.hpp:24
constexpr auto addr() const noexcept -> const_address
Returns the pointed-to address.
Definition: offset_ptr.hpp:139
auto operator[](offset_type index) noexcept -> reference
Pointer array subscript operator.
Definition: offset_ptr.hpp:212
constexpr auto data() const noexcept -> const_pointer
Returns the stored pointer.
Definition: offset_ptr.hpp:149
basic_address< std::is_const_v< Pointee > > address
The non-const address types.
Definition: offset_ptr.hpp:27
static constexpr auto limit_cast(Src value) noexcept -> std::enable_if_t< std::is_convertible_v< Src, Dst >, Dst >
Casts value to Dst type if the value fits in that type.
Definition: is_within_limits.hpp:133
static constexpr auto as_address(basic_offset_ptr< P, O > op) noexcept -> basic_address< std::is_const_v< P >>
Converts basic_offset_ptr into basic_address.
Definition: offset_ptr.hpp:290
basic_offset_ptr(const basic_offset_ptr &that) noexcept
Copy constructor.
Definition: offset_ptr.hpp:66
std::conditional_t< IsConst, const void *, void * > pointer
The associated untyped pointer type.
Definition: address.hpp:26
constexpr basic_offset_ptr() noexcept=default
Default constructor.
const shader_source_header & reference
The non-const reference type.
Definition: offset_ptr.hpp:42
std::add_const_t< const shader_source_header > & const_reference
The const reference type.
Definition: offset_ptr.hpp:45
auto operator=(basic_offset_ptr &&that) noexcept -> basic_offset_ptr &
Move assignment operator.
Definition: offset_ptr.hpp:102
auto operator--() noexcept -> auto &
Pointer arithmentic decrement.
Definition: offset_ptr.hpp:207
auto reset(Pointee *ptr) noexcept -> auto &
Point this pointer to another pointee.
Definition: offset_ptr.hpp:108
constexpr auto get() const noexcept -> const_pointer
Returns the stored pointer. Alias for data().
Definition: offset_ptr.hpp:161
static constexpr auto extract_or(basic_offset_ptr< P, O > ptr, P &fallback) noexcept -> P &
Overload of extract_or for basic_offset_ptr.
Definition: offset_ptr.hpp:267
auto addr() noexcept -> address
Returns the pointed-to address.
Definition: offset_ptr.hpp:134
constexpr auto is_null() const noexcept -> bool
Indicates that the pointer is null.
Definition: offset_ptr.hpp:118
basic_offset_ptr(basic_offset_ptr &&that) noexcept
Move constructor.
Definition: offset_ptr.hpp:70
Template type used mostly for function type-tag dispatching.
Definition: type_identity.hpp:19
constexpr auto operator*() const noexcept -> const_reference
Dereferences this pointer.
Definition: offset_ptr.hpp:184
auto operator->() noexcept -> pointer
Dereferences this pointer.
Definition: offset_ptr.hpp:190
std::add_const_t< const shader_source_header > * const_pointer
The const pointer type.
Definition: offset_ptr.hpp:39
std::enable_if< std::is_convertible_v< O, OffsetType > &&std::is_convertible_v< P *, Pointee * > > enable_if_convertible
Enabled if basic_offset_ptr<P, O> is compatible with this.
Definition: offset_ptr.hpp:77
auto operator*() noexcept -> reference
Dereferences this pointer.
Definition: offset_ptr.hpp:178
constexpr auto operator[](offset_type index) const noexcept -> const_reference
Pointer array subscript operator.
Definition: offset_ptr.hpp:218
auto data() noexcept -> pointer
Returns the stored pointer.
Definition: offset_ptr.hpp:144
constexpr auto offset() const noexcept -> offset_type
Returns the byte offset value.
Definition: offset_ptr.hpp:129
constexpr auto operator->() const noexcept -> const_pointer
Dereferences this pointer.
Definition: offset_ptr.hpp:196