… | |
… | |
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 | void *salloc (int size, void *src) throw (std::bad_alloc); |
60 | // if src is 0, clears the memory |
|
|
61 | template<typename T> |
|
|
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); } |
|
|
67 | |
51 | // for symmetry |
68 | // for symmetry |
|
|
69 | template<typename T> |
52 | inline void sfree (void *ptr, int size) throw () |
70 | inline void sfree (T *ptr, int n = 1) throw () |
53 | { |
71 | { |
54 | g_slice_free1 (size, ptr); |
72 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
55 | } |
73 | } |
56 | |
74 | |
57 | // a STL-compatible allocator that uses g_slice |
75 | // a STL-compatible allocator that uses g_slice |
58 | // boy, this is verbose |
76 | // boy, this is verbose |
59 | template<typename Tp> |
77 | template<typename Tp> |
… | |
… | |
83 | pointer address (reference x) const { return &x; } |
101 | pointer address (reference x) const { return &x; } |
84 | const_pointer address (const_reference x) const { return &x; } |
102 | const_pointer address (const_reference x) const { return &x; } |
85 | |
103 | |
86 | pointer allocate (size_type n, const_pointer = 0) |
104 | pointer allocate (size_type n, const_pointer = 0) |
87 | { |
105 | { |
88 | return static_cast<pointer>(salloc (n * sizeof (Tp))); |
106 | return salloc<Tp> (n); |
89 | } |
107 | } |
90 | |
108 | |
91 | void deallocate (pointer p, size_type n) |
109 | void deallocate (pointer p, size_type n) |
92 | { |
110 | { |
93 | sfree (static_cast<void *>(p), n * sizeof (Tp)); |
111 | sfree<Tp> (p, n); |
94 | } |
112 | } |
95 | |
113 | |
96 | size_type max_size ()const throw () |
114 | size_type max_size ()const throw () |
97 | { |
115 | { |
98 | return size_t (-1) / sizeof (Tp); |
116 | return size_t (-1) / sizeof (Tp); |
… | |
… | |
105 | |
123 | |
106 | void destroy (pointer p) |
124 | void destroy (pointer p) |
107 | { |
125 | { |
108 | p->~Tp (); |
126 | p->~Tp (); |
109 | } |
127 | } |
110 | }; |
|
|
111 | |
|
|
112 | struct refcounted |
|
|
113 | { |
|
|
114 | refcounted () : refcnt (0) { } |
|
|
115 | // virtual ~refcounted (); |
|
|
116 | void refcnt_inc () { ++refcnt; } |
|
|
117 | void refcnt_dec () { --refcnt; } |
|
|
118 | bool dead () { return refcnt == 0; } |
|
|
119 | mutable int refcnt; |
|
|
120 | #if 0 |
|
|
121 | private: |
|
|
122 | static refcounted *rc_first; |
|
|
123 | refcounted *rc_next; |
|
|
124 | #endif |
|
|
125 | }; |
128 | }; |
126 | |
129 | |
127 | template<class T> |
130 | template<class T> |
128 | struct refptr |
131 | struct refptr |
129 | { |
132 | { |
… | |
… | |
152 | T &operator * () const { return *p; } |
155 | T &operator * () const { return *p; } |
153 | T *operator ->() const { return p; } |
156 | T *operator ->() const { return p; } |
154 | |
157 | |
155 | operator T *() const { return p; } |
158 | operator T *() const { return p; } |
156 | }; |
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; |
157 | |
166 | |
158 | struct str_hash |
167 | struct str_hash |
159 | { |
168 | { |
160 | std::size_t operator ()(const char *s) const |
169 | std::size_t operator ()(const char *s) const |
161 | { |
170 | { |
… | |
… | |
187 | { |
196 | { |
188 | return !strcmp (a, b); |
197 | return !strcmp (a, b); |
189 | } |
198 | } |
190 | }; |
199 | }; |
191 | |
200 | |
192 | #include <vector> |
|
|
193 | |
|
|
194 | template<class obj> |
201 | template<class obj> |
195 | struct unordered_vector : std::vector<obj, slice_allocator<obj> > |
202 | struct unordered_vector : std::vector<obj, slice_allocator<obj> > |
196 | { |
203 | { |
197 | typedef typename unordered_vector::iterator iterator; |
204 | typedef typename unordered_vector::iterator iterator; |
198 | |
205 | |
… | |
… | |
229 | typedef double tstamp; |
236 | typedef double tstamp; |
230 | |
237 | |
231 | // return current time as timestampe |
238 | // return current time as timestampe |
232 | tstamp now (); |
239 | tstamp now (); |
233 | |
240 | |
|
|
241 | int similar_direction (int a, int b); |
|
|
242 | |
234 | #endif |
243 | #endif |
235 | |
244 | |