/
tool.h
498 lines (404 loc) · 11.6 KB
/
tool.h
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
// This code is in the public domain. See LICENSE for details.
#include "types.h"
#include "debug.h"
#ifndef __fr_tool_h_
#define __fr_tool_h_
// ---- safe deletes
#define FRSAFEDELETE(a) { if (a) { delete (a); (a)=0; } }
#define FRSAFEDELETEA(a) { if (a) { delete[] (a); (a)=0; } }
// ---- compile time assertions
namespace
{
template<bool expr> struct CTAssertionHelper
{
};
template<> struct CTAssertionHelper<true>
{
inline static void aCompileTimeAssertionHasHappened() { }
};
};
#define FRCTASSERT(x) { CTAssertionHelper<(bool) (x)>::aCompileTimeAssertionHasHappened(); }
namespace fr
{
// ---- smart pointers
#pragma warning (push)
#pragma warning (disable: 4284)
template<class T> class scopedPtr
{
T *value;
public:
explicit scopedPtr(T *p=0)
: value(p)
{
}
explicit scopedPtr(scopedPtr &x)
: value(x.release())
{
}
~scopedPtr()
{
FRSAFEDELETE(value);
}
inline void reset(T *p=0)
{
if (p!=value)
{
FRSAFEDELETE(value);
value=p;
}
}
inline T *release()
{
T *ret=value;
value=0;
return ret;
}
inline scopedPtr &operator =(const T *p)
{
reset(p);
return *this;
}
inline scopedPtr &operator =(scopedPtr &x)
{
reset(x.release());
return *this;
}
inline T& operator *() const { return *value; }
inline T* operator ->() const { return value; }
inline T* get() const { return value; }
inline operator T *() const { return value; }
};
template<class T, class U> inline bool operator == (const scopedPtr<T> &a, const scopedPtr<U> &b)
{
return a.get()==b.get();
}
template<class T, class U> inline bool operator != (const scopedPtr<T> &a, const scopedPtr<U> &b)
{
return a.get()!=b.get();
}
template<class T> class scopedArrayPtr
{
T *value;
public:
explicit scopedArrayPtr(T *p=0)
: value(p)
{
}
explicit scopedArrayPtr(scopedArrayPtr &x)
: value(x.release())
{
}
~scopedArrayPtr()
{
FRSAFEDELETEA(value);
}
inline void reset(T *p=0)
{
FRSAFEDELETEA(value);
value=p;
}
inline T *release()
{
T *tmp=value;
value=0;
return tmp;
}
inline scopedArrayPtr &operator =(T *p)
{
reset(p);
return *this;
}
inline scopedArrayPtr &operator =(scopedArrayPtr &x)
{
reset(x.release());
return *this;
}
inline T* get() const { return value; }
inline operator T *() const { return value; }
};
template<class T, class U> inline bool operator == (const scopedArrayPtr<T> &a, const scopedArrayPtr<U> &b)
{
return a.get()==b.get();
}
template<class T, class U> inline bool operator != (const scopedArrayPtr<T> &a, const scopedArrayPtr<U> &b)
{
return a.get()!=b.get();
}
template<class T> class sharedPtr
{
T *value;
sUInt *refCount;
inline void release()
{
if (--*refCount==0)
{
FRSAFEDELETE(value);
delete refCount;
}
}
inline void share(T *rval, sUInt *rrc)
{
if (rrc!=refCount)
{
++*rrc;
release();
value=rval;
refCount=rrc;
}
}
public:
explicit sharedPtr(T *p=0)
{
value=p;
refCount=new sUInt(1);
}
sharedPtr(const sharedPtr &p)
: value(p.value)
{
++*(refCount=p.refCount);
}
~sharedPtr()
{
release();
}
inline sharedPtr &operator =(T *p)
{
reset(p);
return *this;
}
inline sharedPtr &operator =(const sharedPtr &x)
{
share(x.value, x.refCount);
return *this;
}
inline void reset(T *p=0)
{
if (value==p)
return;
if (--*refCount==0)
FRSAFEDELETE(value)
else
refCount=new sUInt;
*refCount=1;
value=p;
}
inline T& operator *() const { return *value; }
inline T* operator ->() const { return value; }
inline T* get() const { return value; }
inline operator T *() const { return value; }
inline sUInt useCount() const { return *refCount; }
inline sBool isUnique() const { return *refCount==1; }
};
template<class T, class U> inline bool operator == (const sharedPtr<T> &a, const sharedPtr<U> &b)
{
return a.get()==b.get();
}
template<class T, class U> inline bool operator != (const sharedPtr<T> &a, const sharedPtr<U> &b)
{
return a.get()!=b.get();
}
template<class T> class sharedArrayPtr
{
T *value;
sUInt *refCount;
inline void release()
{
if (--*refCount==0)
{
FRSAFEDELETEA(value);
delete refCount;
}
}
inline void share(T *rval, sUInt *rrc)
{
if (rrc!=refCount)
{
++*rrc;
release();
value=rval;
refCount=rrc;
}
}
public:
explicit sharedArrayPtr(T *p=0)
{
value=p;
refCount=new sUInt(1);
}
sharedArrayPtr(const sharedArrayPtr &p)
: value(p.value)
{
++*(refCount=p.refCount);
}
~sharedArrayPtr()
{
release();
}
inline sharedArrayPtr &operator =(T *p)
{
reset(p);
return *this;
}
inline sharedArrayPtr &operator =(const sharedArrayPtr &x)
{
share(x.value, x.refCount);
return *this;
}
inline void reset(T *p=0)
{
if (value==p)
return;
if (--*refCount==0)
FRSAFEDELETEA(value)
else
refCount=new sUInt;
*refCount=1;
value=p;
}
inline T* get() const { return value; }
inline operator T *() const { return value; }
inline sUInt useCount() const { return *refCount; }
inline sBool isUnique() const { return *refCount==1; }
};
template<class T, class U> inline bool operator == (const sharedArrayPtr<T> &a, const sharedArrayPtr<U> &b)
{
return a.get()==b.get();
}
template<class T, class U> inline bool operator != (const sharedArrayPtr<T> &a, const sharedArrayPtr<U> &b)
{
return a.get()!=b.get();
}
#pragma warning (pop)
// ---- string class
class string
{
public:
struct stringData
{
sInt nRefs;
sInt nDataLength, nAllocLength;
sChar *data()
{
return (sChar *) (this+1);
}
};
protected:
sChar *strData;
stringData *getData() const;
void init();
void allocCopy(string &dest, sInt copyLen, sInt copyIndex, sInt extraLen) const;
sBool allocBuffer(sInt len);
void assignCopy(sInt srcLen, const sChar *srcData);
sBool concatCopy(sInt srcLen1, const sChar *srcData1, sInt srcLen2, const sChar *srcData2);
void concatInPlace(sInt srcLen, const sChar *srcData);
void copyBeforeWrite();
sBool allocBeforeWrite(sInt len);
void release();
static void __stdcall release(stringData *data);
static sInt __stdcall safeStrLen(const sChar *str);
public:
string() { init(); }
string(const string &src);
string(sChar chr, sInt repeat=1);
string(const sChar *str);
string(const sUChar *str);
~string();
inline sInt getLength() const { return getData()->nDataLength; }
inline sInt getAllocLength() const { return getData()->nAllocLength; }
sBool isEmpty() const { return getData()->nDataLength==0; }
void empty();
inline sChar getAt(sInt index) const
{
FRASSERT(index>=0);
FRASSERT(index<=getLength());
return strData[index];
}
inline sChar operator[](sInt index) const
{
return getAt(index);
}
void setAt(sInt index, sChar chr);
inline operator const sChar *() const { return strData; }
const string &operator =(const string &src);
const string &operator =(const sChar ch);
const string &operator =(const sChar *str);
const string &operator =(const sUChar *str);
const string &operator +=(const string &src);
const string &operator +=(const sChar ch);
const string &operator +=(const sChar *str);
const string &operator +=(const sUChar *str);
friend string __stdcall operator +(const string &str1, const string &str2);
friend string __stdcall operator +(const string &str, const sChar ch);
friend string __stdcall operator +(const sChar ch, const string &str);
friend string __stdcall operator +(const string &str1, const sChar *str2);
friend string __stdcall operator +(const sChar *str1, const string &str2);
friend string __stdcall operator +(const string &str1, const sUChar *str2);
friend string __stdcall operator +(const sUChar *str1, const string &str2);
sInt compare(const sChar *str, sBool ignoreCase=sFALSE) const;
string mid(sInt first, sInt count) const;
string mid(sInt first) const;
string left(sInt count) const;
string right(sInt count) const;
void upperCase();
void lowerCase();
void trimLeft();
void trimRight();
sInt insert(sInt index, sChar ch);
sInt insert(sInt index, const sChar *str);
sInt remove(sInt index, sInt count=1);
sInt find(sChar ch) const;
sInt find(const sChar *str) const;
sInt reverseFind(sChar ch) const;
sInt findOneOf(const sChar *set) const;
sChar *getBuffer(sInt minLength);
void releaseBuffer(sInt newLength=-1);
sChar *lockBuffer();
void unlockBuffer();
};
string __stdcall operator +(const string &str1, const string &str2);
string __stdcall operator +(const string &str, const sChar ch);
string __stdcall operator +(const sChar ch, const string &str);
string __stdcall operator +(const sChar *str1, const string &str2);
string __stdcall operator +(const sUChar *str1, const string &str2);
inline bool __stdcall operator ==(const string &s1, const string &s2) { return s1.compare(s2)==0; }
inline bool __stdcall operator ==(const sChar *s1, const string &s2) { return s2.compare(s1)==0; }
inline bool __stdcall operator ==(const string &s1, const sChar *s2) { return s1.compare(s2)==0; }
inline bool __stdcall operator !=(const string &s1, const string &s2) { return s1.compare(s2)!=0; }
inline bool __stdcall operator !=(const sChar *s1, const string &s2) { return s2.compare(s1)!=0; }
inline bool __stdcall operator !=(const string &s1, const sChar *s2) { return s1.compare(s2)!=0; }
inline bool __stdcall operator < (const string &s1, const string &s2) { return s1.compare(s2)<0; }
inline bool __stdcall operator < (const sChar *s1, const string &s2) { return s2.compare(s1)>0; }
inline bool __stdcall operator < (const string &s1, const sChar *s2) { return s1.compare(s2)<0; }
inline bool __stdcall operator <=(const string &s1, const string &s2) { return s1.compare(s2)<=0; }
inline bool __stdcall operator <=(const sChar *s1, const string &s2) { return s2.compare(s1)>=0; }
inline bool __stdcall operator <=(const string &s1, const sChar *s2) { return s1.compare(s2)<=0; }
inline bool __stdcall operator > (const string &s1, const string &s2) { return s1.compare(s2)>0; }
inline bool __stdcall operator > (const sChar *s1, const string &s2) { return s2.compare(s1)<0; }
inline bool __stdcall operator > (const string &s1, const sChar *s2) { return s1.compare(s2)>0; }
inline bool __stdcall operator >=(const string &s1, const string &s2) { return s1.compare(s2)>=0; }
inline bool __stdcall operator >=(const sChar *s1, const string &s2) { return s2.compare(s1)<=0; }
inline bool __stdcall operator >=(const string &s1, const sChar *s2) { return s1.compare(s2)>=0; }
// ---- helpers
class noncopyable
{
protected:
noncopyable() {}
~noncopyable() {}
private:
noncopyable(const noncopyable &);
const noncopyable &operator =(const noncopyable &);
};
template<class T> T minimum(T a, T b)
{
return (a<b)?a:b;
}
template<class T> T maximum(T a, T b)
{
return (a<b)?b:a;
}
template<class T> T clamp(T x, T min, T max)
{
return (x<min)?min:(x>max)?max:x;
}
};
#endif