-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbasictypes.H
More file actions
362 lines (352 loc) · 8.38 KB
/
basictypes.H
File metadata and controls
362 lines (352 loc) · 8.38 KB
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
* */
/*
* basictypes.H
*
* Created on: Apr 26, 2013
* Author: xaxaxa
*/
#ifndef BASICTYPES_H_
#define BASICTYPES_H_
#include <string>
#include <stdexcept>
#include <rgc.H>
#include <delegate.H>
#include <string.h>
#ifndef DEPRECATED
#ifdef __GNUC__
#define DEPRECATED(func) func __attribute__ ((deprecated))
#elif defined(_MSC_VER)
#define DEPRECATED(func) __declspec(deprecated) func
#else
#pragma message("WARNING: You need to implement DEPRECATED for this compiler")
#define DEPRECATED(func) func
#endif
#endif
using namespace std;
namespace CP
{
typedef int32_t HANDLE; //file descriptor or handle
typedef Delegate<void(int32_t)> Callback;
typedef uint8_t event_t;
struct String;
void * memrmem(void *s, size_t slen, void *t, size_t tlen);
class MemoryBuffer: virtual public RGC::Object
{
public:
virtual uint8_t* data() const=0;
virtual int length() const=0;
inline CP::String String();
};
struct String
{
char* d;
int len;
String() :
d(nullptr), len(0) {
}
String(std::nullptr_t) :
d(nullptr), len(0) {
}
String(char* data, int length) :
d(data), len(length) {
}
String(char* data) :
d(data), len(data == nullptr ? 0 : strlen(data)) {
}
String(const char* data, int length) :
d(const_cast<char*>(data)), len(length) {
}
String(const void* data, int length) :
d((char*) (data)), len(length) {
}
String(const char* data) :
d(const_cast<char*>(data)), len(strlen(data)) {
}
String(std::string s) :
d((char*) s.data()), len(s.length()) {
}
String(MemoryBuffer& mb) :
d((char*) mb.data()), len(mb.length()) {
}
int compare(const String& other) const {
if (len < other.len)
return -1;
else if (len > other.len)
return 1;
else if (len == 0)
return 0;
else return memcmp(d, other.d, len);
}
int alphabeticalCompare(const String& other) const {
int l = len;
if (other.len < l) l = other.len;
for (int i = 0; i < l; i++) {
char c1 = d[i], c2 = other.d[i];
if (c1 < c2) return -1;
if (c1 > c2) return 1;
}
if (len < other.len) return -1;
else if (len > other.len) return 1;
else return 0;
}
inline bool operator<(const String& other) const {
return compare(other) < 0;
}
inline bool operator>(const String& other) const {
return compare(other) > 0;
}
inline bool operator<=(const String& other) const {
return compare(other) <= 0;
}
inline bool operator>=(const String& other) const {
return compare(other) >= 0;
}
inline bool operator==(const String& other) const {
return compare(other) == 0;
}
inline bool operator==(std::nullptr_t other) const {
return len == 0;
}
inline char* data() const {
return d;
}
inline int length() const {
return len;
}
inline std::string toSTDString() const {
return std::string(d, len);
}
inline String subString(int i, int l) {
if (i < 0 || i + l > this->len || l < 0)
throw range_error("String::subString() out of range");
return String(d + i, l);
}
inline String subString(int i) {
return subString(i, len - i);
}
inline void clip(int index, int length) {
if (index < 0 || index + length > this->len || length < 0)
throw range_error("String::clip() out of range");
d += index;
len = length;
}
inline void clip(int index) {
if (index < 0 || index > this->len) throw range_error("String::clip() out of range");
d += index;
len -= index;
}
inline int indexOf(String s, int i = 0) {
void* tmp = memmem(d + i, len - i, s.d, s.len);
return tmp == NULL ? -1 : (((char*) tmp) - d);
}
inline int indexOf(char c, int i = 0) {
void* tmp = memchr(d + i, c, len - i);
return tmp == NULL ? -1 : (((char*) tmp) - d);
}
inline int lastIndexOf(String s, int i = -1) {
if (i < 0) i = len - 1;
void* tmp = memrmem(d, i + 1, s.d, s.len);
return tmp == NULL ? -1 : (((char*) tmp) - d);
}
inline int lastIndexOf(char c, int i = -1) {
if (i < 0) i = len - 1;
void* tmp = memrchr(d, c, i + 1);
return tmp == NULL ? -1 : (((char*) tmp) - d);
}
inline char& operator[](int i) {
return d[i];
}
String copy() {
String tmp;
tmp.d = (char*) malloc((tmp.len = len));
memcpy(tmp.d, d, len);
return tmp;
}
void dealloc() {
free(d);
d = NULL;
len = 0;
}
};
template<class ... T>
String concat(RGC::Allocator& alloc, T ... s...) {
int l=sizeof...(s);
String tmp[sizeof...(s)] {s...};
int size=0;
for(int i=0;i<l;i++) size+=tmp[i].len;
String r {(char*)alloc.alloc(size),0};
for(int i=0;i<l;i++) {
memcpy(r.d+r.len,tmp[i].d,tmp[i].len);
r.len+=tmp[i].len;
}
return r;
}
#define __CPOLL_DO_ATOI(func,str) char tmp[str.len + 1];\
memcpy(tmp, str.d, str.len);\
tmp[str.len] = 0;\
return func(tmp);
static int atoi(String s) {
__CPOLL_DO_ATOI(::atoi, s);
}
static long atol(String s) {
__CPOLL_DO_ATOI(::atol, s);
}
static long long atoll(String s) {
__CPOLL_DO_ATOI(::atoll, s);
}
static double atof(String s) {
__CPOLL_DO_ATOI(::atof, s);
}
/*
static long long atoq(String s) {
__CPOLL_DO_ATOI(::atoq, s);
}*/
CP::String MemoryBuffer::String() {
return {(char*)data(),length()};
}
struct Undefined
{
};
static Undefined undefined;
template<class T> class Optional
{
public:
T value;
bool exists;
T& operator()() {
return value;
}
T& operator*() {
return value;
}
T* operator->() {
return &value;
}
operator bool() {
return exists;
}
bool operator==(Undefined) {
return !exists;
}
Optional<T>& operator=(const T& other) {
value = other;
return *this;
}
Optional<T>& operator=(const Optional<T>& other) {
if ((exists = other.exists)) value = other.value;
return *this;
}
Optional<T>& operator=(Undefined) {
exists = false;
return *this;
}
Optional(const T& other) :
value(other) {
exists = true;
}
Optional(const Optional<T>& other) :
value(other.value), exists(other.exists) {
}
struct _placeholder1
{
};
template<class ... A> Optional(_placeholder1, A&&... a) :
value(std::forward<A>(a)...), exists(true) {
}
template<class ... A>
static Optional<T> create(A&&... a) {
return Optional<T>(_placeholder1(), std::forward<A>(a)...);
}
Optional() :
exists(false) {
}
Optional(const Undefined& u) :
exists(false) {
}
};
template<class T>
struct Future
{
Delegate<void(T, exception*)>* cbAddr;
Future(Delegate<void(T, exception*)>* cbAddr) :
cbAddr(cbAddr) {
}
};
template<class T> class AsyncValue
{
public:
T value;
Delegate<void(T, exception*)>* cbAddr;
bool async;
T& operator()() {
return value;
}
T& operator*() {
return value;
}
T* operator->() {
return &value;
}
operator bool() {
return !async;
}
bool operator==(Undefined) {
return async;
}
AsyncValue<T>& operator=(const T& other) {
value = other;
return *this;
}
AsyncValue<T>& operator=(const AsyncValue<T>& other) {
if (!(async = other.async))
value = other.value;
else cbAddr = other.cbAddr;
return *this;
}
AsyncValue<T>& operator=(Undefined) {
async = true;
return *this;
}
AsyncValue(const T& other) :
value(other) {
async = false;
}
AsyncValue(const AsyncValue<T>& other) :
value(other.value), cbAddr(other.cbAddr), async(other.async) {
}
AsyncValue(Future<T> f) :
cbAddr(f.cbAddr), async(true) {
}
struct _placeholder1
{
};
template<class ... A> AsyncValue(_placeholder1, A&&... a) :
value(std::forward<A>(a)...), async(false) {
}
template<class ... A>
static AsyncValue<T> create(A&&... a) {
return AsyncValue<T>(_placeholder1(), std::forward<A>(a)...);
}
AsyncValue() :
async(true) {
}
AsyncValue(const Undefined& u) :
async(true) {
}
void wait(Delegate<void(T, exception*)> cb) {
*cbAddr = cb;
}
};
}
#endif /* BASICTYPES_H_ */