… | |
… | |
6 | #else |
6 | #else |
7 | # define is_constant(c) 0 |
7 | # define is_constant(c) 0 |
8 | #endif |
8 | #endif |
9 | |
9 | |
10 | #include <cstddef> |
10 | #include <cstddef> |
|
|
11 | #include <new> |
|
|
12 | #include <vector> |
11 | |
13 | |
12 | #include <glib.h> |
14 | #include <glib.h> |
|
|
15 | |
|
|
16 | #include <shstr.h> |
|
|
17 | #include <traits.h> |
13 | |
18 | |
14 | // use a gcc extension for auto declarations until ISO C++ sanctifies them |
19 | // use a gcc extension for auto declarations until ISO C++ sanctifies them |
15 | #define AUTODECL(var,expr) typeof(expr) var = (expr) |
20 | #define AUTODECL(var,expr) typeof(expr) var = (expr) |
16 | |
21 | |
17 | // makes dynamically allocated objects zero-initialised |
22 | // makes dynamically allocated objects zero-initialised |
… | |
… | |
42 | { |
47 | { |
43 | g_slice_free1 (s, p); |
48 | g_slice_free1 (s, p); |
44 | } |
49 | } |
45 | }; |
50 | }; |
46 | |
51 | |
|
|
52 | void *salloc_ (int n) throw (std::bad_alloc); |
|
|
53 | void *salloc_ (int n, void *src) throw (std::bad_alloc); |
|
|
54 | |
47 | // strictly the same as g_slice_alloc, but never returns 0 |
55 | // strictly the same as g_slice_alloc, but never returns 0 |
48 | void *salloc (int size) throw (std::bad_alloc); |
56 | template<typename T> |
|
|
57 | inline T *salloc (int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T)); } |
|
|
58 | |
49 | // also copies src into the new area, like "memdup" |
59 | // also copies src into the new area, like "memdup" |
50 | // if src is 0, clears the memory |
60 | // if src is 0, clears the memory |
51 | void *salloc (int size, void *src) throw (std::bad_alloc); |
|
|
52 | |
|
|
53 | // and as a template |
|
|
54 | template<typename T> |
61 | template<typename T> |
55 | inline T *salloc (int size) throw (std::bad_alloc) { return (T *)salloc (size * sizeof (T)); } |
|
|
56 | template<typename T> |
|
|
57 | inline T *salloc (int size, T *src) throw (std::bad_alloc) { return (T *)salloc (size * sizeof (T), (void *)src); } |
62 | inline T *salloc (int n, T *src) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), (void *)src); } |
|
|
63 | |
|
|
64 | // clears the memory |
|
|
65 | template<typename T> |
|
|
66 | inline T *salloc0(int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), 0); } |
58 | |
67 | |
59 | // for symmetry |
68 | // for symmetry |
60 | template<typename T> |
69 | template<typename T> |
61 | inline void sfree (T *ptr, int size) throw () |
70 | inline void sfree (T *ptr, int n = 1) throw () |
62 | { |
71 | { |
63 | g_slice_free1 (size * sizeof (T), (void *)ptr); |
72 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
64 | } |
73 | } |
65 | |
74 | |
66 | // a STL-compatible allocator that uses g_slice |
75 | // a STL-compatible allocator that uses g_slice |
67 | // boy, this is verbose |
76 | // boy, this is verbose |
68 | template<typename Tp> |
77 | template<typename Tp> |
… | |
… | |
97 | return salloc<Tp> (n); |
106 | return salloc<Tp> (n); |
98 | } |
107 | } |
99 | |
108 | |
100 | void deallocate (pointer p, size_type n) |
109 | void deallocate (pointer p, size_type n) |
101 | { |
110 | { |
102 | sfree (p, n); |
111 | sfree<Tp> (p, n); |
103 | } |
112 | } |
104 | |
113 | |
105 | size_type max_size ()const throw () |
114 | size_type max_size ()const throw () |
106 | { |
115 | { |
107 | return size_t (-1) / sizeof (Tp); |
116 | return size_t (-1) / sizeof (Tp); |
… | |
… | |
114 | |
123 | |
115 | void destroy (pointer p) |
124 | void destroy (pointer p) |
116 | { |
125 | { |
117 | p->~Tp (); |
126 | p->~Tp (); |
118 | } |
127 | } |
119 | }; |
|
|
120 | |
|
|
121 | struct refcounted |
|
|
122 | { |
|
|
123 | refcounted () : refcnt (0) { } |
|
|
124 | // virtual ~refcounted (); |
|
|
125 | void refcnt_inc () { ++refcnt; } |
|
|
126 | void refcnt_dec () { --refcnt; } |
|
|
127 | bool dead () { return refcnt == 0; } |
|
|
128 | mutable int refcnt; |
|
|
129 | #if 0 |
|
|
130 | private: |
|
|
131 | static refcounted *rc_first; |
|
|
132 | refcounted *rc_next; |
|
|
133 | #endif |
|
|
134 | }; |
128 | }; |
135 | |
129 | |
136 | template<class T> |
130 | template<class T> |
137 | struct refptr |
131 | struct refptr |
138 | { |
132 | { |
… | |
… | |
161 | T &operator * () const { return *p; } |
155 | T &operator * () const { return *p; } |
162 | T *operator ->() const { return p; } |
156 | T *operator ->() const { return p; } |
163 | |
157 | |
164 | operator T *() const { return p; } |
158 | operator T *() const { return p; } |
165 | }; |
159 | }; |
|
|
160 | |
|
|
161 | typedef refptr<maptile> maptile_ptr; |
|
|
162 | typedef refptr<object> object_ptr; |
|
|
163 | typedef refptr<archetype> arch_ptr; |
|
|
164 | typedef refptr<client> client_ptr; |
|
|
165 | typedef refptr<player> player_ptr; |
166 | |
166 | |
167 | struct str_hash |
167 | struct str_hash |
168 | { |
168 | { |
169 | std::size_t operator ()(const char *s) const |
169 | std::size_t operator ()(const char *s) const |
170 | { |
170 | { |
… | |
… | |
196 | { |
196 | { |
197 | return !strcmp (a, b); |
197 | return !strcmp (a, b); |
198 | } |
198 | } |
199 | }; |
199 | }; |
200 | |
200 | |
201 | #include <vector> |
|
|
202 | |
|
|
203 | template<class obj> |
201 | template<class obj> |
204 | struct unordered_vector : std::vector<obj, slice_allocator<obj> > |
202 | struct unordered_vector : std::vector<obj, slice_allocator<obj> > |
205 | { |
203 | { |
206 | typedef typename unordered_vector::iterator iterator; |
204 | typedef typename unordered_vector::iterator iterator; |
207 | |
205 | |
… | |
… | |
238 | typedef double tstamp; |
236 | typedef double tstamp; |
239 | |
237 | |
240 | // return current time as timestampe |
238 | // return current time as timestampe |
241 | tstamp now (); |
239 | tstamp now (); |
242 | |
240 | |
|
|
241 | int similar_direction (int a, int b); |
|
|
242 | |
243 | #endif |
243 | #endif |
244 | |
244 | |