-
Notifications
You must be signed in to change notification settings - Fork 0
/
ptr.cpp
297 lines (236 loc) · 6.27 KB
/
ptr.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include "ptr.hpp"
namespace dcdevelop {
namespace parent {
template <typename T>
ptr<T>::ptr() {
this->raw_pointer = nullptr;
}
template <typename T>
ptr<T>::ptr(T* pointer) {
this->raw_pointer = pointer;
}
template <typename T>
ptr<T>::ptr(T& reference) {
this->raw_pointer = &reference;
}
template <typename T>
T* ptr<T>::addr() {
return this->raw_pointer;
}
template <typename T>
T* ptr<T>::address() {
return this->addr();
}
template <typename T>
ptr<T>::operator T *() {
return this->raw_pointer;
}
template <typename T>
T& ptr<T>::deref() {
return *(this->raw_pointer);
}
template <typename T>
T& ptr<T>::dereference() {
return this->deref();
}
template <typename T>
ptr<T>::operator T &() {
return *(this->raw_pointer);
}
template <typename T>
template <typename Result>
Result& ptr<T>::map(Result& (*fp)(T value)) {
return (*fp)(this->deref());
}
template <typename T>
template <typename Result>
Result& ptr<T>::map_with_default(Result& (*fp)(T value), Result& default_value) {
if (this->is_null()) {
return default_value;
}
else {
return (*fp)(this->deref());
}
}
template <typename T>
bool ptr<T>::is_null() {
return this->raw_pointer == nullptr;
}
template <typename T>
void ptr<T>::set_null() {
this->raw_pointer = nullptr;
}
template <typename T>
ptr<T> ptr<T>::operator+(ptrdiff_t number) {
return this->raw_pointer + number;
}
template <typename T>
ptr<T> ptr<T>::next() {
return *this + 1;
}
template <typename T>
ptr<T>& ptr<T>::operator++() {
this->raw_pointer + 1;
return *this;
}
template <typename T>
ptr<T> ptr<T>::operator++(int) {
auto copy = *this;
this->raw_pointer + 1;
return copy;
}
template <typename T>
template <typename IncrementType>
void ptr<T>::operator+=(IncrementType number) {
this->raw_pointer += number;
}
template <typename T>
ptrdiff_t ptr<T>::operator-(const ptr<T>& other_ptr) {
return this->raw_pointer - other_ptr.raw_pointer;
}
template <typename T>
ptrdiff_t ptr<T>::operator-(T* other_pointer) {
return this->raw_pointer - other_pointer;
}
template <typename T>
ptr<T> ptr<T>::operator-(ptrdiff_t number) {
return this->raw_pointer - number;
}
template <typename T>
ptr<T> ptr<T>::prev() {
return *this - 1;
}
template <typename T>
ptr<T>& ptr<T>::operator--() {
this->raw_pointer - 1;
return *this;
}
template <typename T>
ptr<T> ptr<T>::operator--(int) {
auto copy = *this;
this->raw_pointer - 1;
return copy;
}
template <typename T>
template <typename DecrementType>
void ptr<T>::operator-=(DecrementType number) {
this->raw_pointer -= number;
}
template <typename T>
bool ptr<T>::operator==(T*& compareTo) {
return this->raw_pointer == compareTo;
}
template <typename T>
template <typename EqualType>
bool ptr<T>::operator==(EqualType& compareTo) {
return this->raw_pointer == (T*)compareTo;
}
template <typename T>
bool ptr<T>::operator==(ptr<T>& compareTo) {
return this->raw_pointer == compareTo.raw_pointer;
}
template <typename T>
template <typename EqualType>
bool ptr<T>::operator!=(EqualType& compareTo) {
return !(*this == compareTo);
}
template <typename T>
template <typename ComparisonType>
bool ptr<T>::operator>(ComparisonType& compareTo) {
return this->raw_pointer > compareTo;
}
template <typename T>
bool ptr<T>::operator>(ptr<T>& compareTo) {
return this->raw_pointer > compareTo.raw_pointer;
}
template <typename T>
template <typename ComparisonType>
bool ptr<T>::operator>=(ComparisonType& compareTo) {
return *this == compareTo || *this > compareTo;
}
template <typename T>
template <typename ComparisonType>
bool ptr<T>::operator<(ComparisonType& compareTo) {
return !(*this >= compareTo);
}
template <typename T>
template <typename ComparisonType>
bool ptr<T>::operator<=(ComparisonType& compareTo) {
return !(*this > compareTo);
}
}
char* ptr<char>::cstring() {
return this->addr();
}
bool ptr<char>::is_empty() {
return this->is_null();
}
#ifdef DCDEVELOP_PTR_CLASS_CSTRING
bool ptr<char>::operator==(const ptr<char>& compareTo) {
return this->operator==(compareTo.raw_pointer);
}
bool ptr<char>::operator==(const char*& compareTo) {
return strcmp(this->raw_pointer, compareTo);
}
bool ptr<char>::operator>(const char*& compareTo) {
return strcmp(this->raw_pointer, compareTo) > 0;
}
void ptr<char>::copy_to(char* destination) {
strcpy(destination, this->raw_pointer);
}
void ptr<char>::copy_to(ptr<char>& destination) {
this->copy_to(destination.raw_pointer);
}
void ptr<char>::replace_with(char* source) {
strcpy(this->raw_pointer, source);
}
void ptr<char>::replace_with(ptr<char>& source) {
this->replace_with(source.raw_pointer);
}
void ptr<char>::append(char* source) {
strcat(this->raw_pointer, source);
}
void ptr<char>::append(const ptr<char>& source) {
this->append(source.raw_pointer);
}
void ptr<char>::concat(char* source) {
this->append(source);
}
void ptr<char>::concat(const ptr<char>& source) {
this->append(source);
}
ptr<char> ptr<char>::find(char character) {
return strchr(this->raw_pointer, character);
}
ptr<char> ptr<char>::find(const char*& cstring) {
return strstr(this->raw_pointer, cstring);
}
ptr<char> ptr<char>::find(const ptr<char>& cstring) {
return this->find(cstring.raw_pointer);
}
ptr<char> ptr<char>::find_any(const char*& cstring) {
return strpbrk(this->raw_pointer, cstring);
}
ptr<char> ptr<char>::find_any(const ptr<char>& cstring) {
return this->find_any(cstring.raw_pointer);
}
ptr<char> ptr<char>::find_last(char character) {
return strrchr(this->raw_pointer, character);
}
size_t ptr<char>::span_containing(const char*& cstring) {
return strspn(this->raw_pointer, cstring);
}
size_t ptr<char>::span_containing(const ptr<char>& cstring) {
return this->span_containing(cstring.raw_pointer);
}
size_t ptr<char>::span_until(const char*& cstring) {
return strcspn(this->raw_pointer, cstring);
}
size_t ptr<char>::span_until(const ptr<char>& cstring) {
return this->span_until(cstring.raw_pointer);
}
size_t ptr<char>::length() {
return strlen(this->raw_pointer);
}
#endif
}