-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.hpp
575 lines (468 loc) · 21.1 KB
/
util.hpp
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/* $Id: util.hpp 168 2009-10-23 14:58:32Z sausage $
* EOSERV is released under the zlib license.
* See LICENSE.txt for more info.
*/
#ifndef UTIL_HPP_INCLUDED
#define UTIL_HPP_INCLUDED
#include <cmath>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <stdexcept>
#include <string>
#include <vector>
/**
* Utility functions to assist with common tasks
*/
namespace util
{
// A gigantic pile of fail
// Uglified it to make it generate standard C++ code
#define UTIL_FOREACH_GENERIC(iterator, start, end, type, as) \
for (int util_i = 0; util_i < 1; ++util_i) \
for (iterator util_start = start; util_i < 1; ++util_i) \
for (iterator util_end = end; util_i < 1; ++util_i) \
for (type as = *util_start; util_i < 1; ++util_i) \
for(iterator util_iter = util_start; util_iter != util_end; as = (++util_iter == util_end)?*util_start:*util_iter)
#define UTIL_IFOREACH_GENERIC(iterator, start, end, type, as) \
for (int util_i = 0; util_i < 1; ++util_i) \
for (iterator util_start = start; util_i < 1; ++util_i) \
for (iterator util_end = end; util_i < 1; ++util_i) \
for (iterator as = util_start; as != util_end; ++as)
#define UTIL_FOREACH_GENERIC2(iterator, iterator2, start, end, type, as) \
for (int util_i = 0; util_i < 1; ++util_i) \
for (iterator,iterator2 util_start = start; util_i < 1; ++util_i) \
for (iterator,iterator2 util_end = end; util_i < 1; ++util_i) \
for (type as = *util_start; util_i < 1; ++util_i) \
for(iterator,iterator2 util_iter = util_start; util_iter != util_end; as = (++util_iter == util_end)?*util_start:*util_iter)
#define UTIL_IFOREACH_GENERIC2(iterator, iterator2, start, end, type, as) \
for (int util_i = 0; util_i < 1; ++util_i) \
for (iterator,iterator2 util_start = container.begin(); util_i < 1; ++util_i) \
for (iterator,iterator2 util_end = contianer.end(); util_i < 1; ++util_i) \
for (iterator,iterator2 as = util_start; as != util_end; ++as)
#define UTIL_ARRAY_FOREACH(start, end, type, type2, as) UTIL_FOREACH_GENERIC2(util::array<type, type2 >::iterator, start, end, type, as)
#define UTIL_DEQUE_FOREACH(start, end, type, as) UTIL_FOREACH_GENERIC(std::deque<type >::iterator, start, end, type, as)
#define UTIL_LIST_FOREACH(start, end, type, as) UTIL_FOREACH_GENERIC(std::list<type >::iterator, start, end, type, as)
#define UTIL_MAP_FOREACH(start, end, type, type2, as) UTIL_FOREACH_GENERIC2(std::map<type, type2>::iterator, start, end, type, as)
#define UTIL_MULTIMAP_FOREACH(start, end, type, type2, as) UTIL_FOREACH_GENERIC2(std::multimap<type >::iterator, start, end, type, as)
#define UTIL_SET_FOREACH(start, end, type, as) UTIL_FOREACH_GENERIC(std::set<type >::iterator, start, end, type, as)
#define UTIL_MULTISET_FOREACH(start, end, type, as) UTIL_FOREACH_GENERIC(std::multiset<type >::iterator, start, end, type, as)
#define UTIL_STACK_FOREACH(start, end, type, as) UTIL_FOREACH_GENERIC(std::stack<type >::iterator, start, end, type, as)
#define UTIL_VECTOR_FOREACH(start, end, type, as) UTIL_FOREACH_GENERIC(std::vector<type >::iterator, start, end, type, as)
#define UTIL_ARRAY_IFOREACH(start, end, type, type2, as) UTIL_IFOREACH_GENERIC2(util::array<type, type2 >::iterator, start, end, type, as)
#define UTIL_DEQUE_IFOREACH(start, end, type, as) UTIL_IFOREACH_GENERIC(std::deque<type >::iterator, start, end, type, as)
#define UTIL_LIST_IFOREACH(start, end, type, as) UTIL_IFOREACH_GENERIC(std::list<type >::iterator, start, end, type, as)
#define UTIL_MAP_IFOREACH(start, end, type, type2, as) UTIL_IFOREACH_GENERIC2(std::map<type, type2 >::iterator, start, end, type, as)
#define UTIL_MULTIMAP_IFOREACH(start, end, type, type2, as) UTIL_IFOREACH_GENERIC2(std::multimap<type, type2 >::iterator, start, end, type, as)
#define UTIL_SET_IFOREACH(start, end, type, as) UTIL_IFOREACH_GENERIC(std::set<type >::iterator, start, end, type, as)
#define UTIL_MULTISET_IFOREACH(start, end, type, as) UTIL_IFOREACH_GENERIC(std::multiset<type >::iterator, start, end, type, as)
#define UTIL_STACK_IFOREACH(start, end, type, as) UTIL_IFOREACH_GENERIC(std::stack<type >::iterator, start, end, type, as)
#define UTIL_VECTOR_IFOREACH(start, end, type, as) UTIL_IFOREACH_GENERIC(std::vector<type >::iterator, start, end, type, as)
#define UTIL_ARRAY_FOREACH_ALL(container, type, type2, as) if (!container.empty()) UTIL_ARRAY_FOREACH(container.begin(), container.end(), type, type2, as)
#define UTIL_DEQUE_FOREACH_ALL(container, type, as) if (!container.empty()) UTIL_DEQUE_FOREACH(container.begin(), container.end() type, as)
#define UTIL_LIST_FOREACH_ALL(container, type, as) if (!container.empty()) UTIL_LIST_FOREACH(container.begin(), container.end(), type, as)
#define UTIL_MAP_FOREACH_ALL(container, type, type2, as) if (!container.empty()) UTIL_MAP_FOREACH(container.begin(), container.end(), type, type2, as)
#define UTIL_MULTIMAP_FOREACH_ALL(container, type, type2, as) if (!container.empty()) UTIL_MULTIMAP_FOREACH(container.begin(), container.end(), type, type2, as)
#define UTIL_SET_FOREACH_ALL(container, type, as) if (!container.empty()) UTIL_SET_FOREACH(container.begin(), container.end(), type, as)
#define UTIL_MULTISET_FOREACH_ALL(container, type, as) if (!container.empty()) UTIL_MULTISET_FOREACH(container.begin(), container.end(), type, as)
#define UTIL_STACK_FOREACH_ALL(container, type, as) if (!container.empty()) UTIL_STACK_FOREACH(container.begin(), container.end(), type, as)
#define UTIL_VECTOR_FOREACH_ALL(container, type, as) if (!container.empty()) UTIL_VECTOR_FOREACH(container.begin(), container.end(), type, as)
#define UTIL_ARRAY_IFOREACH_ALL(container, type, type2, as) if (!container.empty()) UTIL_ARRAY_IFOREACH(container.begin(), container.end(), type, type2, as)
#define UTIL_DEQUE_IFOREACH_ALL(container, type, as) if (!container.empty()) UTIL_DEQUE_IFOREACH(container.begin(), container.end(), type, as)
#define UTIL_LIST_IFOREACH_ALL(container, type, as) if (!container.empty()) UTIL_LIST_IFOREACH(container.begin(), container.end(), type, as)
#define UTIL_MAP_IFOREACH_ALL(container, type, type2, as) if (!container.empty()) UTIL_MAP_IFOREACH(container.begin(), container.end(), type, type2, as)
#define UTIL_MULTIMAP_IFOREACH_ALL(container, type, type2, as) if (!container.empty()) UTIL_MULTIMAP_IFOREACH(container.begin(), container.end(), type, type2, as)
#define UTIL_SET_IFOREACH_ALL(container, type, as) if (!container.empty()) UTIL_SET_IFOREACH(container.begin(), container.end(), type, as)
#define UTIL_MULTISET_IFOREACH_ALL(container, type, as) if (!container.empty()) UTIL_MULTISET_IFOREACH(container.begin(), container.end(), type, as)
#define UTIL_STACK_IFOREACH_ALL(container, type, as) if (!container.empty()) UTIL_STACK_IFOREACH(container.begin(), container.end(), type, as)
#define UTIL_VECTOR_IFOREACH_ALL(container, type, as) if (!container.empty()) UTIL_VECTOR_IFOREACH(container.begin(), container.end(), type, as)
#define UTIL_TPL_ARRAY_FOREACH(start, end, type, type2, as) UTIL_FOREACH_GENERIC2(class util::array<type, type2 >::iterator, start, end, type, as)
#define UTIL_TPL_DEQUE_FOREACH(start, end, type, as) UTIL_FOREACH_GENERIC(class std::deque<type >::iterator, start, end, type, as)
#define UTIL_TPL_LIST_FOREACH(start, end, type, as) UTIL_FOREACH_GENERIC(class std::list<type >::iterator, start, end, type, as)
#define UTIL_TPL_MAP_FOREACH(start, end, type, type2, as) UTIL_FOREACH_GENERIC2(class std::map<type >::iterator, start, end, type, as)
#define UTIL_TPL_MULTIMAP_FOREACH(start, end, type, type2, as) UTIL_FOREACH_GENERIC2(class std::multimap<type >::iterator, start, end, type, as)
#define UTIL_TPL_SET_FOREACH(start, end, type, as) UTIL_FOREACH_GENERIC(class std::set<type >::iterator, start, end, type, as)
#define UTIL_TPL_MULTISET_FOREACH(start, end, type, as) UTIL_FOREACH_GENERIC(class std::multiset<type >::iterator, start, end, type, as)
#define UTIL_TPL_STACK_FOREACH(start, end, type, as) UTIL_FOREACH_GENERIC(class std::stack<type >::iterator, start, end, type, as)
#define UTIL_TPL_VECTOR_FOREACH(start, end, type, as) UTIL_FOREACH_GENERIC(class std::vector<type >::iterator, start, end, type, as)
#define UTIL_TPL_ARRAY_IFOREACH(start, end, type, type2, as) UTIL_IFOREACH_GENERIC2(class util::array<type, type2 >::iterator, start, end, type, as)
#define UTIL_TPL_DEQUE_IFOREACH(start, end, type, as) UTIL_IFOREACH_GENERIC(class std::deque<type >::iterator, start, end, type, as)
#define UTIL_TPL_LIST_IFOREACH(start, end, type, as) UTIL_IFOREACH_GENERIC(class std::list<type >::iterator, start, end, type, as)
#define UTIL_TPL_MAP_IFOREACH(start, end, type, type2, as) UTIL_IFOREACH_GENERIC2(class std::map<type, type2 >::iterator, start, end, type, as)
#define UTIL_TPL_MULTIMAP_IFOREACH(start, end, type, type2, as) UTIL_IFOREACH_GENERIC2(class std::multimap<type, type2 >::iterator, start, end, type, as)
#define UTIL_TPL_SET_IFOREACH(start, end, type, as) UTIL_IFOREACH_GENERIC(class std::set<type >::iterator, start, end, type, as)
#define UTIL_TPL_MULTISET_IFOREACH(start, end, type, as) UTIL_IFOREACH_GENERIC(class std::multiset<type >::iterator, start, end, type, as)
#define UTIL_TPL_STACK_IFOREACH(start, end, type, as) UTIL_IFOREACH_GENERIC(class std::stack<type >::iterator, start, end, type, as)
#define UTIL_TPL_VECTOR_IFOREACH(start, end, type, as) UTIL_IFOREACH_GENERIC(class std::vector<type >::iterator, start, end, type, as)
#define UTIL_TPL_ARRAY_FOREACH_ALL(container, type, type2, as) if (!container.empty()) UTIL_TPL_ARRAY_FOREACH(container.begin(), container.end(), type, type2, as)
#define UTIL_TPL_DEQUE_FOREACH_ALL(container, type, as) if (!container.empty()) UTIL_TPL_DEQUE_FOREACH(container.begin(), container.end() type, as)
#define UTIL_TPL_LIST_FOREACH_ALL(container, type, as) if (!container.empty()) UTIL_TPL_LIST_FOREACH(container.begin(), container.end(), type, as)
#define UTIL_TPL_MAP_FOREACH_ALL(container, type, type2, as) if (!container.empty()) UTIL_TPL_MAP_FOREACH(container.begin(), container.end(), type, type2, as)
#define UTIL_TPL_MULTIMAP_FOREACH_ALL(container, type, type2, as) if (!container.empty()) UTIL_TPL_MULTIMAP_FOREACH(container.begin(), container.end(), type, type2, as)
#define UTIL_TPL_SET_FOREACH_ALL(container, type, as) if (!container.empty()) UTIL_TPL_SET_FOREACH(container.begin(), container.end(), type, as)
#define UTIL_TPL_MULTISET_FOREACH_ALL(container, type, as) if (!container.empty()) UTIL_TPL_MULTISET_FOREACH(container.begin(), container.end(), type, as)
#define UTIL_TPL_STACK_FOREACH_ALL(container, type, as) if (!container.empty()) UTIL_TPL_STACK_FOREACH(container.begin(), container.end(), type, as)
#define UTIL_TPL_VECTOR_FOREACH_ALL(container, type, as) if (!container.empty()) UTIL_TPL_VECTOR_FOREACH(container.begin(), container.end(), type, as)
#define UTIL_TPL_ARRAY_IFOREACH_ALL(container, type, type2, as) if (!container.empty()) UTIL_TPL_ARRAY_IFOREACH(container.begin(), container.end(), type, type2, as)
#define UTIL_TPL_DEQUE_IFOREACH_ALL(container, type, as) if (!container.empty()) UTIL_TPL_DEQUE_IFOREACH(container.begin(), container.end(), type, as)
#define UTIL_TPL_LIST_IFOREACH_ALL(container, type, as) if (!container.empty()) UTIL_TPL_LIST_IFOREACH(container.begin(), container.end(), type, as)
#define UTIL_TPL_MAP_IFOREACH_ALL(container, type, type2, as) if (!container.empty()) UTIL_TPL_MAP_IFOREACH(container.begin(), container.end(), type, type2, as)
#define UTIL_TPL_MULTIMAP_IFOREACH_ALL(container, type, type2, as) if (!container.empty()) UTIL_TPL_MULTIMAP_IFOREACH(container.begin(), container.end(), type, type2, as)
#define UTIL_TPL_SET_IFOREACH_ALL(container, type, as) if (!container.empty()) UTIL_TPL_SET_IFOREACH(container.begin(), container.end(), type, as)
#define UTIL_TPL_MULTISET_IFOREACH_ALL(container, type, as) if (!container.empty()) UTIL_TPL_MULTISET_IFOREACH(container.begin(), container.end(), type, as)
#define UTIL_TPL_STACK_IFOREACH_ALL(container, type, as) if (!container.empty()) UTIL_TPL_STACK_IFOREACH(container.begin(), container.end(), type, as)
#define UTIL_TPL_VECTOR_IFOREACH_ALL(container, type, as) if (!container.empty()) UTIL_TPL_VECTOR_IFOREACH(container.begin(), container.end(), type, as)
#define UTIL_CARRAY_FOREACH(array, start, end, type, as) \
for (int util_i = 0; util_i < 1; ++util_i) \
for (type as = array[start]; util_i < 1; ++util_i) \
for(std::size_t util_iter = start; util_iter <= end; as = array[++util_iter])
#define UTIL_CARRAY_FOREACH_ALL(array, type, as) \
for (int util_i = 0; util_i < 1; ++util_i) \
for (type as = array[0]; util_i < 1; ++util_i) \
for(std::size_t util_iter = 0; util_iter < sizeof(array)/sizeof(type); as = array[++util_iter])
#define UTIL_REPEAT(amt) \
for (int util_i = 0; util_i < amt; ++util_i)
// DEPRECATED
// Relies on a GCC (typeof) or c++0x (decltype) only feature, do not use
#ifdef __GNUC__
#define UTIL_FOREACH(container, as) if (!container.empty()) for (int util_i = 0; util_i < 1; ++util_i) for (typeof(*(container.begin())) as; util_i < 1; ++util_i) for (typeof(container.begin()) util_it = container.begin(); ((util_it != container.end())?(as = *(util_it)):(as = *container.begin())), util_it != container.end(); as = *(util_it++))
#define UTIL_IFOREACH(container, as) if (!container.empty()) for (int util_i = 0; util_i < 1; ++util_i) for (typeof(container.begin()) as; util_i < 1; ++util_i) for (typeof(container.begin()) util_it = container.begin(); ((util_it != container.end())?(as = util_it):(as = container.begin())), util_it != container.end(); as = util_it++)
#else // __GNUC__
#define UTIL_FOREACH(container, as) if (!container.empty()) for (int util_i = 0; util_i < 1; ++util_i) for (decltype(*(container.begin())) as; util_i < 1; ++util_i) for (decltype(container.begin()) util_it = container.begin(); ((util_it != container.end())?(as = *(util_it)):(as = *container.begin())), util_it != container.end(); as = *(util_it++))
#define UTIL_IFOREACH(container, as) if (!container.empty()) for (int util_i = 0; util_i < 1; ++util_i) for (decltype(container.begin()) as; util_i < 1; ++util_i) for (decltype(container.begin()) util_it = container.begin(); ((util_it != container.end())?(as = util_it):(as = container.begin())), util_it != container.end(); as = util_it++)
#endif // __GNUC__
#ifdef __GXX_EXPERIMENTAL_CXX0X__
#define UTIL_EXTEND_ENUM(T) : T
#else // __GXX_EXPERIMENTAL_CXX0X__
#define UTIL_EXTEND_ENUM(T)
#endif // __GXX_EXPERIMENTAL_CXX0X__
/**
* Generic and simple array class.
*/
template <typename T, std::size_t N> class array
{
private:
T elements[N];
public:
typedef T value_type;
typedef T *iterator;
typedef const T *const_iterator;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
typedef T &reference;
typedef const T &const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
iterator begin()
{
return this->elements;
}
const_iterator begin() const
{
return this->elements;
}
iterator end()
{
return this->elements + N;
}
const_iterator end() const
{
return this->elements + N;
}
reverse_iterator rbegin()
{
return reverse_iterator(this->end());
}
const_reverse_iterator rbegin() const
{
return const_reverse_iterator(end());
}
reverse_iterator rend()
{
return reverse_iterator(begin());
}
const_reverse_iterator rend() const
{
return const_reverse_iterator(begin());
}
array() {}
array(const_reference value)
{
this->assign(value);
}
array(T init[N])
{
std::memcpy(this->elements, init, sizeof(value_type) * N);
}
reference at(size_type index)
{
if (index >= N)
{
throw std::out_of_range("util::array::at");
}
return this->elements[index];
}
const_reference at(size_type index) const
{
if (index >= N)
{
throw std::out_of_range("util::array::at");
}
return this->elements[index];
}
reference operator[](size_type index)
{
return this->at(index);
}
const_reference operator[](size_type index) const
{
return this->at(index);
}
reference front()
{
return this->begin();
}
const_reference front() const
{
return this->begin();
}
reference back()
{
return this->elements[this->end() - 1];
}
const_reference back() const
{
return this->elements[this->end() - 1];
}
size_type size() const
{
return N;
}
size_type max_size() const
{
return N;
}
bool empty() const
{
return this->size() == 0;
}
void assign(const_reference value)
{
std::fill_n(this->begin(), this->size(), value);
}
void swap(util::array<T, N> &second)
{
std::swap_ranges(this->begin(), this->end(), second.begin());
}
const T *data() const
{
return this->elements;
}
T *data()
{
return this->data;
}
};
/**
* A type that can store any numeric/string value and convert between them.
* It takes way too much effort to use, so it's only used by the Config class.
*/
class variant
{
protected:
/**
* Value stored as an integer.
*/
int val_int;
/**
* Value stored as a float.
*/
double val_float;
/**
* Value stored as a string.
*/
std::string val_string;
/**
* Value stored as a bool.
*/
bool val_bool;
enum var_type
{
type_int,
type_float,
type_string,
type_bool
};
bool cache_val[4];
/**
* Current type the value is stored as.
* Accessing as this type will need no conversion.
*/
var_type type;
/**
* Return the value as an integer, casting if neccessary.
*/
int GetInt();
/**
* Return the value as a float, casting if neccessary.
*/
double GetFloat();
/**
* Return the value as a string, casting if neccessary.
*/
std::string GetString();
/**
* Return the value as a bool, casting if neccessary.
*/
bool GetBool();
/**
* Invalidates the cache values and changes the type.
*/
void SetType(var_type);
/**
* Set the value to an integer.
*/
variant &SetInt(int);
/**
* Set the value to a float.
*/
variant &SetFloat(double);
/**
* Set the value to a string.
*/
variant &SetString(const std::string &);
/**
* Set the value to a bool.
*/
variant &SetBool(bool);
/**
* Helper function that returns the string length of a number in decimal format.
*/
int int_length(int);
public:
/**
* Initialize the variant to an integer with the value 0.
*/
variant();
/**
* Initialize the variant to an integer with the specified value.
*/
variant(int);
/**
* Initialize the variant to a float with the specified value.
*/
variant(double);
/**
* Initialize the variant to a string with the specified value.
*/
variant(const std::string &);
/**
* Initialize the variant to a string with the specified value.
*/
variant(const char *);
/**
* Initialize the variant to a bool with the specified value.
*/
variant(bool);
/**
* Set the value to an integer.
*/
variant &operator =(int);
/**
* Set the value to a float.
*/
variant &operator =(double);
/**
* Set the value to a string.
*/
variant &operator =(const std::string &);
/**
* Set the value to a string.
*/
variant &operator =(const char *);
/**
* Set the value to a bool.
*/
variant &operator =(bool);
/**
* Return the value as an integer, casting if neccessary.
*/
operator int();
/**
* Return the value as a float, casting if neccessary.
*/
operator double();
/**
* Return the value as a string, casting if neccessary.
*/
operator std::string();
/**
* Return the value as an boolean, casting if neccessary.
*/
operator bool();
};
/**
* Commonly used array type in EOSERV.
*/
typedef array<unsigned char, 2> pairchar;
/**
* Commonly used array type in EOSERV.
*/
typedef array<unsigned char, 4> quadchar;
/**
* Trims whitespace from the left of a string.
* Whitespace is defined as space, tab, CR and LF.
*/
std::string ltrim(const std::string &);
/**
* Trims whitespace from the right of a string.
* Whitespace is defined as space, tab, CR and LF.
*/
std::string rtrim(const std::string &);
/**
* Trims whitespace from both sides of a string.
* Whitespace is defined as space, tab, CR and LF.
*/
std::string trim(const std::string &);
/**
* Split a string in to a vector with a specified delimiter
*/
std::vector<std::string> explode(char delimiter, std::string);
/**
* Split a string in to a vector with a specified delimiter
*/
std::vector<std::string> explode(std::string delimiter, std::string);
/**
* Alternate name for variant.
*/
typedef variant var;
/**
* Parse a string time period to a number
* @param timestr amount of time in a human readable format (eg. 2h30m)
* @return number of seconds
*/
double tdparse(std::string timestr);
int to_int(const std::string &);
double to_float(const std::string &);
std::string to_string(int);
std::string to_string(double);
void lowercase(std::string &);
void uppercase(std::string &);
void ucfirst(std::string &);
int rand(int min, int max);
double rand(double min, double max);
double round(double);
void sleep(double seconds);
std::string DecodeEMFString(std::string chars);
std::string EncodeEMFString(std::string chars);
std::string timeago(double time, double current_time);
/**
* Finds the distance IN TILES between a pair of x,y coordinates
*/
inline int path_length(int x1, int y1, int x2, int y2)
{
return std::abs(x1 - x2) + std::abs(y1 - y2);
}
}
#endif // UTIL_HPP_INCLUDED