ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtutil.h
(Generate patch)

Comparing rxvt-unicode/src/rxvtutil.h (file contents):
Revision 1.4 by root, Thu Sep 2 07:44:40 2004 UTC vs.
Revision 1.20 by root, Wed Jan 25 00:42:21 2006 UTC

1#ifndef RXVT_UTIL_H 1#ifndef RXVT_UTIL_H
2#define RXVT_UTIL_H 2#define RXVT_UTIL_H
3 3
4#include <cstdlib>
4#include <cstring> 5#include <cstring>
6
7#define PP_CONCAT_(a, b) a ## b
8#define PP_CONCAT(a, b) PP_CONCAT_(a, b)
9#define PP_STRINGIFY_(a) #a
10#define PP_STRINGIFY(a) PP_STRINGIFY_(a)
5 11
6extern class byteorder { 12extern class byteorder {
7 static unsigned int e; // at least 32 bits 13 static unsigned int e; // at least 32 bits
8public: 14public:
9 byteorder (); 15 byteorder ();
12 static bool network () { return e == 0x11223344; }; 18 static bool network () { return e == 0x11223344; };
13 static bool little_endian () { return e == 0x44332211; }; 19 static bool little_endian () { return e == 0x44332211; };
14 static bool vax () { return e == 0x44332211; }; 20 static bool vax () { return e == 0x44332211; };
15} byteorder; 21} byteorder;
16 22
17template<typename T, typename U> 23// various utility functions
18static inline T min (T a, U b) { return a < b ? a : (T)b; } 24template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; }
19template<typename T, typename U> 25template<typename T, typename U> static inline void min_it (T &a, U b) { a = a < (T)b ? a : (T)b; }
20static inline T max (T a, U b) { return a > b ? a : (T)b; } 26template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; }
27template<typename T, typename U> static inline void max_it (T &a, U b) { a = a > (T)b ? a : (T)b; }
21 28
29template<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; }
30template<typename T, typename U, typename V> static inline void clamp_it (T &v, U a, V b) { v = v < (T)a ? a : v >(T)b ? b : v; }
31
32template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; }
33
34// in range including end
35#define IN_RANGE_INC(val,beg,end) \
36 ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg))
37
38// in range excluding end
39#define IN_RANGE_EXC(val,beg,end) \
40 ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg))
41
42// makes dynamically allocated objects zero-initialised
22struct zero_initialized { 43struct zero_initialized {
23 void *operator new (size_t s); 44 void *operator new (size_t s);
24 void operator delete (void *p, size_t s); 45 void operator delete (void *p, size_t s);
25}; 46};
26 47
40 T *_buf; 61 T *_buf;
41 62
42public: 63public:
43 const_iterator begin () const 64 const_iterator begin () const
44 { 65 {
45 return &_buf[0]; 66 return &_buf[0];
46 } 67 }
47 iterator begin () 68 iterator begin ()
48 { 69 {
49 return &_buf[0]; 70 return &_buf[0];
50 } 71 }
51 const_iterator end () const 72 const_iterator end () const
52 { 73 {
53 return &_buf[_last]; 74 return &_buf[_last];
54 } 75 }
55 iterator end () 76 iterator end ()
56 { 77 {
57 return &_buf[_last]; 78 return &_buf[_last];
58 } 79 }
59 size_type capacity () const 80 size_type capacity () const
60 { 81 {
61 return _size; 82 return _size;
62 } 83 }
63 size_type size () const 84 size_type size () const
64 { 85 {
65 return _last; 86 return _last;
66 } 87 }
67 88
68private: 89private:
69 static T *alloc (size_type n) 90 static T *alloc (size_type n)
70 { 91 {
71 return (T *)::operator new ((size_t) (n * sizeof (T))); 92 return (T *)::operator new ((size_t) (n * sizeof (T)));
72 } 93 }
73 static void dealloc (T *buf) 94 static void dealloc (T *buf)
74 { 95 {
75 if (buf) 96 if (buf)
76 ::operator delete (buf); 97 ::operator delete (buf);
77 } 98 }
78 99
79 void reserve (iterator where, size_type n) 100 void reserve (iterator where, size_type n)
80 { 101 {
81 if (_last + n <= _size) { 102 if (_last + n <= _size) {
82 memmove (where+n, where, (end ()-where)*sizeof (T)); 103 memmove (where+n, where, (end ()-where)*sizeof (T));
83 } else { 104 } else {
84 size_type sz = _last+n; 105 size_type sz = _last+n;
85 sz = (_size == 0) ? max (sz, 5) : max (sz, 2*_size); 106 sz = (_size == 0) ? max (sz, 5) : max (sz, 2*_size);
86 T *nbuf = alloc (sz); 107 T *nbuf = alloc (sz);
87 if (_buf) { 108 if (_buf) {
88 memcpy (nbuf, begin (), (where-begin ())*sizeof (T)); 109 memcpy (nbuf, begin (), (where-begin ())*sizeof (T));
89 memcpy (nbuf + (where-begin ()) + n, where, 110 memcpy (nbuf + (where-begin ()) + n, where,
90 (end ()-where)*sizeof (T)); 111 (end ()-where)*sizeof (T));
91 dealloc (_buf); 112 dealloc (_buf);
92 } 113 }
93 _buf = nbuf; 114 _buf = nbuf;
94 _size = sz; 115 _size = sz;
95 } 116 }
96 } 117 }
97 118
98public: 119public:
99 void reserve (size_type sz) 120 void reserve (size_type sz)
100 { 121 {
101 if (_size < sz) { 122 if (_size < sz) {
102 sz = (_size == 0) ? max (sz, 5) : max (sz, 2*_size); 123 sz = (_size == 0) ? max (sz, 5) : max (sz, 2*_size);
103 T *nbuf = alloc (sz); 124 T *nbuf = alloc (sz);
104 if (_buf) { 125 if (_buf) {
105 memcpy (nbuf, begin (), size ()*sizeof (T)); 126 memcpy (nbuf, begin (), size ()*sizeof (T));
106 dealloc (_buf); 127 dealloc (_buf);
107 } 128 }
108 _buf = nbuf; 129 _buf = nbuf;
109 _size = sz; 130 _size = sz;
110 } 131 }
111 } 132 }
112 simplevec () 133 simplevec ()
113 : _last(0), _size(0), _buf(0) 134 : _last(0), _size(0), _buf(0)
114 { 135 {
115 } 136 }
116 simplevec (size_type n, const T& t = T ()) 137 simplevec (size_type n, const T& t = T ())
117 : _last(0), _size(0), _buf(0) 138 : _last(0), _size(0), _buf(0)
118 { 139 {
119 insert (begin (), n, t); 140 insert (begin (), n, t);
120 } 141 }
121 simplevec (const_iterator first, const_iterator last) 142 simplevec (const_iterator first, const_iterator last)
122 : _last(0), _size(0), _buf(0) 143 : _last(0), _size(0), _buf(0)
123 { 144 {
124 insert (begin (), first, last); 145 insert (begin (), first, last);
125 } 146 }
126 simplevec (const simplevec<T> &v) 147 simplevec (const simplevec<T> &v)
127 : _last(0), _size(0), _buf(0) 148 : _last(0), _size(0), _buf(0)
128 { 149 {
129 reserve (v._last); 150 reserve (v._last);
130 memcpy (_buf, v.begin (), v.size ()*sizeof (T)); 151 memcpy (_buf, v.begin (), v.size ()*sizeof (T));
131 _last = v._last; 152 _last = v._last;
132 } 153 }
133 simplevec<T> &operator= (const simplevec<T> &v) 154 simplevec<T> &operator= (const simplevec<T> &v)
134 { 155 {
135 if (this != &v) { 156 if (this != &v) {
136 _last = 0; 157 _last = 0;
137 reserve (v._last); 158 reserve (v._last);
138 memcpy (_buf, v.begin (), v.size ()*sizeof (T)); 159 memcpy (_buf, v.begin (), v.size ()*sizeof (T));
139 _last = v._last; 160 _last = v._last;
140 } 161 }
141 return *this; 162 return *this;
142 } 163 }
143 ~simplevec () 164 ~simplevec ()
144 { 165 {
145 dealloc (_buf); 166 dealloc (_buf);
146 } 167 }
147 const T &front () const 168 const T &front () const
148 { 169 {
149 //ministl_assert (size () > 0); 170 //ministl_assert (size () > 0);
150 return _buf[0]; 171 return _buf[0];
151 } 172 }
152 T &front () 173 T &front ()
153 { 174 {
154 //ministl_assert (size () > 0); 175 //ministl_assert (size () > 0);
155 return _buf[0]; 176 return _buf[0];
156 } 177 }
157 const T &back () const 178 const T &back () const
158 { 179 {
159 //ministl_assert (size () > 0); 180 //ministl_assert (size () > 0);
160 return _buf[_last-1]; 181 return _buf[_last-1];
161 } 182 }
162 T &back () 183 T &back ()
163 { 184 {
164 //ministl_assert (size () > 0); 185 //ministl_assert (size () > 0);
165 return _buf[_last-1]; 186 return _buf[_last-1];
166 } 187 }
167 bool empty () const 188 bool empty () const
168 { 189 {
169 return _last == 0; 190 return _last == 0;
170 } 191 }
171 void clear () 192 void clear ()
172 { 193 {
173 _last = 0; 194 _last = 0;
174 } 195 }
175 void push_back (const T &t) 196 void push_back (const T &t)
176 { 197 {
177 reserve (_last+1); 198 reserve (_last+1);
178 *end () = t; 199 *end () = t;
179 ++_last; 200 ++_last;
180 } 201 }
181 void push_back (T &t) 202 void push_back (T &t)
182 { 203 {
183 reserve (_last+1); 204 reserve (_last+1);
184 *end () = t; 205 *end () = t;
185 ++_last; 206 ++_last;
186 } 207 }
187 void pop_back () 208 void pop_back ()
188 { 209 {
189 //ministl_assert (size () > 0); 210 //ministl_assert (size () > 0);
190 --_last; 211 --_last;
191 } 212 }
192 const T &operator[] (size_type idx) const 213 const T &operator[] (size_type idx) const
193 { 214 {
194 //ministl_assert (idx < size ()); 215 //ministl_assert (idx < size ());
195 return _buf[idx]; 216 return _buf[idx];
196 } 217 }
197 T &operator[] (size_type idx) 218 T &operator[] (size_type idx)
198 { 219 {
199 //ministl_assert (idx < size ()); 220 //ministl_assert (idx < size ());
200 return _buf[idx]; 221 return _buf[idx];
201 } 222 }
202 iterator insert (iterator pos, const T &t) 223 iterator insert (iterator pos, const T &t)
203 { 224 {
204 //ministl_assert (pos <= end ()); 225 //ministl_assert (pos <= end ());
205 long at = pos - begin (); 226 long at = pos - begin ();
206 reserve (pos, 1); 227 reserve (pos, 1);
207 pos = begin ()+at; 228 pos = begin ()+at;
208 *pos = t; 229 *pos = t;
209 ++_last; 230 ++_last;
210 return pos; 231 return pos;
211 } 232 }
212 iterator insert (iterator pos, const_iterator first, const_iterator last) 233 iterator insert (iterator pos, const_iterator first, const_iterator last)
213 { 234 {
214 //ministl_assert (pos <= end ()); 235 //ministl_assert (pos <= end ());
215 long n = last - first; 236 long n = last - first;
216 long at = pos - begin (); 237 long at = pos - begin ();
217 if (n > 0) { 238 if (n > 0) {
218 reserve (pos, n); 239 reserve (pos, n);
219 pos = begin ()+at; 240 pos = begin ()+at;
220 memcpy (pos, first, (last-first)*sizeof (T)); 241 memcpy (pos, first, (last-first)*sizeof (T));
221 _last += n; 242 _last += n;
222 } 243 }
223 return pos; 244 return pos;
224 } 245 }
225 iterator insert (iterator pos, size_type n, const T &t) 246 iterator insert (iterator pos, size_type n, const T &t)
226 { 247 {
227 //ministl_assert (pos <= end ()); 248 //ministl_assert (pos <= end ());
228 long at = pos - begin (); 249 long at = pos - begin ();
229 if (n > 0) { 250 if (n > 0) {
230 reserve (pos, n); 251 reserve (pos, n);
231 pos = begin ()+at; 252 pos = begin ()+at;
232 for (int i = 0; i < n; ++i) 253 for (int i = 0; i < n; ++i)
233 pos[i] = t; 254 pos[i] = t;
234 _last += n; 255 _last += n;
235 } 256 }
236 return pos; 257 return pos;
237 } 258 }
238 void erase (iterator first, iterator last) 259 void erase (iterator first, iterator last)
239 { 260 {
240 if (last != first) { 261 if (last != first) {
241 memmove (first, last, (end ()-last)*sizeof (T)); 262 memmove (first, last, (end () - last) * sizeof (T));
242 _last -= last - first; 263 _last -= last - first;
243 } 264 }
244 } 265 }
245 void erase (iterator pos) 266 void erase (iterator pos)
246 { 267 {
247 if (pos != end ()) { 268 if (pos != end ()) {
248 memmove (pos, pos+1, (end ()- (pos+1))*sizeof (T)); 269 memmove (pos, pos+1, (end () - (pos+1)) * sizeof (T));
249 --_last; 270 --_last;
250 } 271 }
272 }
273 void swap (simplevec<T> &t)
274 {
275 ::swap(_last, t._last);
276 ::swap(_size, t._size);
277 ::swap(_buf, t._buf);
251 } 278 }
252}; 279};
253 280
254template<class T> 281template<class T>
255bool operator== (const simplevec<T> &v1, const simplevec<T> &v2) 282bool operator== (const simplevec<T> &v1, const simplevec<T> &v2)
256{ 283{
257 if (v1.size () != v2.size ()) 284 if (v1.size () != v2.size ())
258 return false; 285 return false;
259 return !v1.size () || !memcmp (&v1[0], &v2[0], v1.size ()*sizeof (T)); 286 return !v1.size () || !memcmp (&v1[0], &v2[0], v1.size ()*sizeof (T));
260} 287}
261 288
262template<class T> 289template<class T>
263bool operator< (const simplevec<T> &v1, const simplevec<T> &v2) 290bool operator< (const simplevec<T> &v1, const simplevec<T> &v2)
264{ 291{
265 unsigned long minlast = min (v1.size (), v2.size ()); 292 unsigned long minlast = min (v1.size (), v2.size ());
266 for (unsigned long i = 0; i < minlast; ++i) { 293 for (unsigned long i = 0; i < minlast; ++i) {
267 if (v1[i] < v2[i]) 294 if (v1[i] < v2[i])
268 return true; 295 return true;
269 if (v2[i] < v1[i]) 296 if (v2[i] < v1[i])
270 return false; 297 return false;
271 } 298 }
272 return v1.size () < v2.size (); 299 return v1.size () < v2.size ();
273} 300}
274 301
275 302
365struct stringvec : simplevec<char *> 392struct stringvec : simplevec<char *>
366{ 393{
367 ~stringvec () 394 ~stringvec ()
368 { 395 {
369 for (char **c = begin (); c != end (); c++) 396 for (char **c = begin (); c != end (); c++)
370 delete [] *c; 397 free (*c);
371 } 398 }
372}; 399};
373 400
401// return a very temporary (and never deallocated) buffer. keep small.
402void *rxvt_temp_buf (int len);
403
404template<typename T>
405inline T *
406rxvt_temp_buf (int len)
407{
408 return (T *)rxvt_temp_buf (len * sizeof (T));
409}
410
374#endif 411#endif
375 412

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines