coastline 0.0.4
Loading...
Searching...
No Matches
ptr32.h
Go to the documentation of this file.
1#ifndef COASTLINE_PTR32_H
2#define COASTLINE_PTR32_H
3
12
13#if defined(TGR_PLATFORM) && TGR_PLATFORM == TGR_PLATFORM_SDL && defined(__cplusplus)
14
15#include <cstddef>
16#include <cstdint>
17#include <type_traits>
18
19extern "C" {
24extern const unsigned char g_abPtr32Anchor[];
25}
26
27// The headers of the game may include this one inside extern "C".
28extern "C++" {
29namespace coastline {
30
37inline std::uint32_t EncodePtr32(const volatile void *address) {
38 if (address == nullptr) {
39 return 0;
40 }
41 return static_cast<std::uint32_t>(
42 static_cast<std::int32_t>(reinterpret_cast<std::intptr_t>(address) -
43 reinterpret_cast<std::intptr_t>(g_abPtr32Anchor)));
44}
45
52inline void *DecodePtr32(std::uint32_t offset) {
53 if (offset == 0) {
54 return nullptr;
55 }
56 return reinterpret_cast<void *>(reinterpret_cast<std::uintptr_t>(g_abPtr32Anchor) +
57 static_cast<std::int32_t>(offset));
58}
59
64template <typename T>
65class Ptr32 {
66public:
71 Ptr32 &operator=(T *pointer) {
72 m_offset = EncodePtr32(pointer);
73 return *this;
74 }
78 Ptr32 &operator=(std::nullptr_t) {
79 m_offset = 0;
80 return *this;
81 }
82
84 operator T *() const {
85 return static_cast<T *>(DecodePtr32(m_offset));
86 }
88 T *operator->() const {
89 return *this;
90 }
92 template <typename U = T>
93 requires(!std::is_void_v<U>)
94 U &operator*() const {
95 return *static_cast<T *>(*this);
96 }
101 template <typename U = T>
102 requires(!std::is_void_v<U>)
103 U &operator[](std::ptrdiff_t index) const {
104 return static_cast<T *>(*this)[index];
105 }
109 Ptr32 &operator++() {
110 *this = static_cast<T *>(*this) + 1;
111 return *this;
112 }
116 Ptr32 operator++(int) {
117 Ptr32 old = *this;
118 ++*this;
119 return old;
120 }
125 Ptr32 &operator+=(std::ptrdiff_t count) {
126 *this = static_cast<T *>(*this) + count;
127 return *this;
128 }
129
131 std::uint32_t Raw() const {
132 return m_offset;
133 }
134
136 void SetRaw(std::uint32_t offset) {
137 m_offset = offset;
138 }
139
140 std::uint32_t m_offset;
141};
142
143} // namespace coastline
144}
145
147#define TGR_PTR(T) coastline::Ptr32<T>
148
149#else
150
152#define TGR_PTR(T) T *
153
154#endif
155
156#endif /* COASTLINE_PTR32_H */
T count(T... args)
T is_void_v
Definition assetimport.h:11