GeoDesk for C++
Fast and storage-efficient spatial database engine for OpenStreetMap features
Loading...
Searching...
No Matches
pointer.h
Go to the documentation of this file.
1// Copyright (c) 2024 Clarisma / GeoDesk contributors
2// SPDX-License-Identifier: LGPL-3.0-only
3
4#pragma once
5
6#include <cstdint>
7
8namespace clarisma {
9
10// TODO: This name may clash with some std:: classes
11
12// TODO: should make little-Endian byte order explicit
13
14class pointer
15{
16public:
17 pointer() { ptr_ = nullptr; }
18 pointer(const void* p) { ptr_ = reinterpret_cast<const char*>(p); }
19 /*
20 pointer(const char* p) { ptr_ = p; }
21 pointer(const uint8_t* p) { ptr_ = reinterpret_cast<const char*>(p); }
22 */
23 static pointer ofTagged(const void* p, uint64_t mask)
24 {
25 return pointer(reinterpret_cast<const char*>(
26 reinterpret_cast<uint64_t>(p) & mask));
27 }
28
29 pointer follow() const
30 {
31 return pointer(ptr_ + getInt());
32 }
33
34 pointer followUnaligned() const
35 {
36 return pointer(ptr_ + getUnalignedInt());
37 }
38
39 pointer follow(int ofs) const
40 {
41 return pointer(ptr_ + getInt(ofs) + ofs);
42 }
43
44 pointer followUnaligned(int ofs) const
45 {
46 return pointer(ptr_ + getUnalignedInt(ofs) + ofs);
47 }
48
49 pointer followTagged(uint64_t mask) const
50 {
51 return pointer(ptr_ + (getInt() & mask));
52 }
53
54 int16_t getShort() const { return *reinterpret_cast<const int16_t*>(ptr_); }
55 int16_t getShort(int ofs) const { return *reinterpret_cast<const int16_t*>(ptr_ + ofs); }
56 uint16_t getUnsignedShort() const { return *reinterpret_cast<const uint16_t*>(ptr_); }
57 uint16_t getUnsignedShort(int ofs) const { return *reinterpret_cast<const uint16_t*>(ptr_ + ofs); }
58 int32_t getInt() const { return *reinterpret_cast<const int32_t*>(ptr_); }
59 int32_t getInt(int ofs) const { return *reinterpret_cast<const int32_t*>(ptr_ + ofs); }
60 int32_t getUnalignedInt() const { return *reinterpret_cast<const int32_t*>(ptr_); } // TODO: unaligned read
61 int32_t getUnalignedInt(int ofs) const { return *reinterpret_cast<const int32_t*>(ptr_ + ofs); } // TODO: unaligned read
62 uint32_t getUnsignedInt() const { return *reinterpret_cast<const uint32_t*>(ptr_); }
63 uint32_t getUnsignedInt(int ofs) const { return *reinterpret_cast<const uint32_t*>(ptr_ + ofs); }
64 uint32_t getUnalignedUnsignedInt() const { return *reinterpret_cast<const uint32_t*>(ptr_); } // TODO: unaligned read
65 int64_t getLong() const { return *reinterpret_cast<const int64_t*>(ptr_); }
66 uint64_t getUnsignedLong() const { return *reinterpret_cast<const uint64_t*>(ptr_); }
67 uint64_t getUnsignedLong(int ofs) const { return *reinterpret_cast<const uint64_t*>(ptr_ + ofs); }
68 int64_t getUnalignedLong() const { return *reinterpret_cast<const int64_t*>(ptr_); } // TODO: alignment
69 int64_t getUnalignedLong(int ofs) const { return *reinterpret_cast<const int64_t*>(ptr_ + ofs); } // TODO: alignment
70 // double getDouble() const { return *reinterpret_cast<const double*>(ptr_); }
71
72 pointer& operator+=(int32_t delta)
73 {
74 ptr_ += delta;
75 return *this;
76 }
77
78 pointer& operator-=(int32_t delta)
79 {
80 ptr_ -= delta;
81 return *this;
82 }
83
84 pointer operator+(int32_t delta) const { return pointer(ptr_ + delta); }
85 pointer operator-(int32_t delta) const { return pointer(ptr_ - delta); }
86 pointer operator+(uint32_t delta) const { return pointer(ptr_ + delta); }
87 pointer operator-(uint32_t delta) const { return pointer(ptr_ - delta); }
88 int32_t operator-(pointer other) const
89 {
90 return static_cast<int32_t>(ptr_ - other.ptr_);
91 }
92
93 int32_t operator-(const void* other) const
94 {
95 return static_cast<int32_t>(ptr_ - reinterpret_cast<const char*>(other));
96 }
97
98 int32_t operator-(const uint8_t* other) const
99 {
100 return static_cast<int32_t>(ptr_ - reinterpret_cast<const char*>(other));
101 }
102
103 pointer operator&(uint64_t mask) const
104 {
105 return pointer(
106 reinterpret_cast<const void*>(
107 reinterpret_cast<uint64_t>(ptr_) & mask));
108 }
109
110 operator bool() const { return ptr_ != nullptr; }
111
112 bool operator<(const pointer other)
113 {
114 return ptr_ < other.ptr_;
115 }
116
117 operator const uint8_t* () const
118 {
119 return reinterpret_cast<const uint8_t*>(ptr_);
120 }
121
122 operator const char* () const
123 {
124 return reinterpret_cast<const char*>(ptr_);
125 }
126
127 operator const void* () const
128 {
129 return reinterpret_cast<const void*>(ptr_);
130 }
131
132 uint64_t pointerAsULong() const
133 {
134 return reinterpret_cast<uint64_t>(ptr_);
135 }
136
137 const uint8_t* asBytePointer() const
138 {
139 return reinterpret_cast<const uint8_t*>(ptr_);
140 }
141
142 inline bool testAllFlags(int flags)
143 {
144 return (reinterpret_cast<std::uintptr_t>(ptr_) & flags) == flags;
145 }
146
147 inline bool testAnyFlags(int flags)
148 {
149 return (reinterpret_cast<std::uintptr_t>(ptr_) & flags) != 0;
150 }
151
152private:
153 const char* ptr_;
154};
155
156
157
158} // namespace clarisma
Definition Arena.h:17