ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/util.h
Revision: 1.8
Committed: Mon Sep 11 23:33:30 2006 UTC (17 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.7: +6 -0 lines
Log Message:
- temporarily disabled shstr garbage collection
- use sint64 instead of uint64 in shop code
- implement fully recursive item iterator for object
- add some utility functions

File Contents

# User Rev Content
1 root 1.1 #ifndef UTIL_H__
2     #define UTIL_H__
3    
4 root 1.2 #if __GNUC__ >= 3
5     # define is_constant(c) __builtin_constant_p (c)
6     #else
7     # define is_constant(c) 0
8     #endif
9    
10 root 1.1 // makes dynamically allocated objects zero-initialised
11     struct zero_initialised
12     {
13 root 1.6 void *operator new (size_t s, void *);
14 root 1.1 void *operator new (size_t s);
15 root 1.3 void *operator new [] (size_t s);
16 root 1.1 void operator delete (void *p, size_t s);
17 root 1.3 void operator delete [] (void *p, size_t s);
18 root 1.1 };
19    
20 root 1.7 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; }
58     };
59    
60 root 1.4 struct str_hash
61     {
62     std::size_t operator ()(const char *s) const
63     {
64     unsigned long hash = 0;
65    
66     /* use the one-at-a-time hash function, which supposedly is
67     * better than the djb2-like one used by perl5.005, but
68     * certainly is better then the bug used here before.
69     * see http://burtleburtle.net/bob/hash/doobs.html
70     */
71     while (*s)
72     {
73     hash += *s++;
74     hash += hash << 10;
75     hash ^= hash >> 6;
76     }
77    
78     hash += hash << 3;
79     hash ^= hash >> 11;
80     hash += hash << 15;
81    
82     return hash;
83     }
84     };
85    
86     struct str_equal
87     {
88     bool operator ()(const char *a, const char *b) const
89     {
90     return !strcmp (a, b);
91     }
92     };
93    
94 root 1.6 #include <vector>
95    
96     template<class obj>
97     struct unordered_vector : std::vector<obj>
98     {
99     typedef typename std::vector<obj>::iterator iterator;
100    
101     void erase (unsigned int pos)
102     {
103     if (pos < this->size () - 1)
104     (*this)[pos] = (*this)[this->size () - 1];
105    
106     this->pop_back ();
107     }
108    
109     void erase (iterator i)
110     {
111     erase ((unsigned int )(i - this->begin ()));
112     }
113     };
114    
115 root 1.8 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 root 1.1 #endif
122