… | |
… | |
13 | void *operator new (size_t s, void *); |
13 | void *operator new (size_t s, void *); |
14 | void *operator new (size_t s); |
14 | void *operator new (size_t s); |
15 | void *operator new [] (size_t s); |
15 | void *operator new [] (size_t s); |
16 | void operator delete (void *p, size_t s); |
16 | void operator delete (void *p, size_t s); |
17 | void operator delete [] (void *p, size_t s); |
17 | void operator delete [] (void *p, size_t s); |
|
|
18 | }; |
|
|
19 | |
|
|
20 | struct refcounted |
|
|
21 | { |
|
|
22 | mutable int refcnt; |
|
|
23 | refcounted () : refcnt (0) { } |
|
|
24 | void refcnt_inc () { ++refcnt; } |
|
|
25 | void refcnt_dec () { --refcnt; |
|
|
26 | if (refcnt < 0)abort();}//D |
|
|
27 | }; |
|
|
28 | |
|
|
29 | template<class T> |
|
|
30 | struct refptr |
|
|
31 | { |
|
|
32 | T *p; |
|
|
33 | |
|
|
34 | refptr () : p(0) { } |
|
|
35 | refptr (const refptr<T> &p) : p(p.p) { if (p) p->refcnt_inc (); } |
|
|
36 | refptr (T *p) : p(p) { if (p) p->refcnt_inc (); } |
|
|
37 | ~refptr () { if (p) p->refcnt_dec (); } |
|
|
38 | |
|
|
39 | const refptr<T> &operator =(T *o) |
|
|
40 | { |
|
|
41 | if (p) p->refcnt_dec (); |
|
|
42 | p = o; |
|
|
43 | if (p) p->refcnt_inc (); |
|
|
44 | |
|
|
45 | return *this; |
|
|
46 | } |
|
|
47 | |
|
|
48 | const refptr<T> &operator =(const refptr<T> o) |
|
|
49 | { |
|
|
50 | *this = o.p; |
|
|
51 | return *this; |
|
|
52 | } |
|
|
53 | |
|
|
54 | T &operator * () const { return *p; } |
|
|
55 | T *operator ->() const { return p; } |
|
|
56 | |
|
|
57 | operator T *() const { return p; } |
18 | }; |
58 | }; |
19 | |
59 | |
20 | struct str_hash |
60 | struct str_hash |
21 | { |
61 | { |
22 | std::size_t operator ()(const char *s) const |
62 | std::size_t operator ()(const char *s) const |
… | |
… | |
70 | { |
110 | { |
71 | erase ((unsigned int )(i - this->begin ())); |
111 | erase ((unsigned int )(i - this->begin ())); |
72 | } |
112 | } |
73 | }; |
113 | }; |
74 | |
114 | |
|
|
115 | template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
|
|
116 | template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
|
|
117 | template<typename T, typename U, typename V> static inline T clamp (T v, U a, V b) { return v < (T)a ? a : v >(T)b ? b : v; } |
|
|
118 | |
|
|
119 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
|
|
120 | |
|
|
121 | // basically does what strncpy should do |
|
|
122 | template<int N> |
|
|
123 | inline void assign (char (&dst)[N], const char *src) |
|
|
124 | { |
|
|
125 | // should be optimised at some point, maybe should also add "..." |
|
|
126 | // when buffer is too small. |
|
|
127 | snprintf (dst, N, "%s", src); |
|
|
128 | } |
|
|
129 | |
75 | #endif |
130 | #endif |
76 | |
131 | |