ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/util.h
(Generate patch)

Comparing deliantra/server/include/util.h (file contents):
Revision 1.12 by root, Tue Sep 12 21:10:32 2006 UTC vs.
Revision 1.25 by root, Sat Dec 30 10:16:10 2006 UTC

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>
18
19// use a gcc extension for auto declarations until ISO C++ sanctifies them
20#define AUTODECL(var,expr) typeof(expr) var = (expr)
13 21
14// makes dynamically allocated objects zero-initialised 22// makes dynamically allocated objects zero-initialised
15struct zero_initialised 23struct zero_initialised
16{ 24{
17 void *operator new (size_t s, void *p) 25 void *operator new (size_t s, void *p)
39 { 47 {
40 g_slice_free1 (s, p); 48 g_slice_free1 (s, p);
41 } 49 }
42}; 50};
43 51
52void *salloc_ (int n) throw (std::bad_alloc);
53void *salloc_ (int n, void *src) throw (std::bad_alloc);
54
44// strictly the same as g_slice_alloc, but never returns 0 55// strictly the same as g_slice_alloc, but never returns 0
45void *alloc (int s) throw (std::bad_alloc); 56template<typename T>
57inline T *salloc (int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T)); }
58
59// also copies src into the new area, like "memdup"
60// if src is 0, clears the memory
61template<typename T>
62inline T *salloc (int n, T *src) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), (void *)src); }
63
64// clears the memory
65template<typename T>
66inline T *salloc0(int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), 0); }
67
46// for symmetry 68// for symmetry
47inline void dealloc (void *p, int s) throw () 69template<typename T>
70inline void sfree (T *ptr, int n = 1) throw ()
48{ 71{
49 g_slice_free1 (s, p); 72 g_slice_free1 (n * sizeof (T), (void *)ptr);
50} 73}
51 74
52// a STL-compatible allocator that uses g_slice 75// a STL-compatible allocator that uses g_slice
53// boy, this is verbose 76// boy, this is verbose
54template<typename Tp> 77template<typename Tp>
78 pointer address (reference x) const { return &x; } 101 pointer address (reference x) const { return &x; }
79 const_pointer address (const_reference x) const { return &x; } 102 const_pointer address (const_reference x) const { return &x; }
80 103
81 pointer allocate (size_type n, const_pointer = 0) 104 pointer allocate (size_type n, const_pointer = 0)
82 { 105 {
83 return static_cast<pointer>(alloc (n * sizeof (Tp))); 106 return salloc<Tp> (n);
84 } 107 }
85 108
86 void deallocate (pointer p, size_type n) 109 void deallocate (pointer p, size_type n)
87 { 110 {
88 dealloc (static_cast<void *>(p), n); 111 sfree<Tp> (p, n);
89 } 112 }
90 113
91 size_type max_size ()const throw () 114 size_type max_size ()const throw ()
92 { 115 {
93 return size_t (-1) / sizeof (Tp); 116 return size_t (-1) / sizeof (Tp);
100 123
101 void destroy (pointer p) 124 void destroy (pointer p)
102 { 125 {
103 p->~Tp (); 126 p->~Tp ();
104 } 127 }
105};
106
107struct refcounted
108{
109 mutable int refcnt;
110 refcounted () : refcnt (0) { }
111 void refcnt_inc () { ++refcnt; }
112 void refcnt_dec () { --refcnt;
113 if (refcnt < 0)abort();}//D
114}; 128};
115 129
116template<class T> 130template<class T>
117struct refptr 131struct refptr
118{ 132{
141 T &operator * () const { return *p; } 155 T &operator * () const { return *p; }
142 T *operator ->() const { return p; } 156 T *operator ->() const { return p; }
143 157
144 operator T *() const { return p; } 158 operator T *() const { return p; }
145}; 159};
160
161typedef refptr<maptile> maptile_ptr;
162typedef refptr<object> object_ptr;
163typedef refptr<archetype> arch_ptr;
164typedef refptr<client> client_ptr;
165typedef refptr<player> player_ptr;
146 166
147struct str_hash 167struct str_hash
148{ 168{
149 std::size_t operator ()(const char *s) const 169 std::size_t operator ()(const char *s) const
150 { 170 {
176 { 196 {
177 return !strcmp (a, b); 197 return !strcmp (a, b);
178 } 198 }
179}; 199};
180 200
181#include <vector>
182
183template<class obj> 201template<class obj>
184struct unordered_vector : std::vector<obj, slice_allocator<obj> > 202struct unordered_vector : std::vector<obj, slice_allocator<obj> >
185{ 203{
186 typedef typename unordered_vector::iterator iterator; 204 typedef typename unordered_vector::iterator iterator;
187 205
213inline void assign (char (&dst)[N], const char *src) 231inline void assign (char (&dst)[N], const char *src)
214{ 232{
215 assign ((char *)&dst, src, N); 233 assign ((char *)&dst, src, N);
216} 234}
217 235
236typedef double tstamp;
237
238// return current time as timestampe
239tstamp now ();
240
241int similar_direction (int a, int b);
242
218#endif 243#endif
219 244

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines