ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtutil.h
Revision: 1.11
Committed: Tue Dec 20 19:30:59 2005 UTC (18 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.10: +6 -1 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #ifndef RXVT_UTIL_H
2 #define RXVT_UTIL_H
3
4 #include <cstring>
5
6 extern class byteorder {
7 static unsigned int e; // at least 32 bits
8 public:
9 byteorder ();
10
11 static bool big_endian () { return e == 0x11223344; };
12 static bool network () { return e == 0x11223344; };
13 static bool little_endian () { return e == 0x44332211; };
14 static bool vax () { return e == 0x44332211; };
15 } byteorder;
16
17 template<typename T, typename U>
18 static inline T min (T a, U b) { return a < (T)b ? a : (T)b; }
19 template<typename T, typename U>
20 static inline T max (T a, U b) { return a > (T)b ? a : (T)b; }
21 template<typename T>
22 static inline void swap (T& a, T& b) { T t=a; a=b; b=t; }
23
24 // in range including end
25 #define IN_RANGE_INC(val,beg,end) \
26 ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg))
27
28 // in range excluding end
29 #define IN_RANGE_EXC(val,beg,end) \
30 ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg))
31
32 struct zero_initialized {
33 void *operator new (size_t s);
34 void operator delete (void *p, size_t s);
35 };
36
37 /* simplevec taken (and heavily modified), from:
38 *
39 * MICO --- a free CORBA implementation
40 * Copyright (C) 1997-98 Kay Roemer & Arno Puder
41 */
42 template<class T>
43 struct simplevec {
44 typedef T* iterator;
45 typedef const T* const_iterator;
46 typedef unsigned long size_type;
47
48 private:
49 size_type _last, _size;
50 T *_buf;
51
52 public:
53 const_iterator begin () const
54 {
55 return &_buf[0];
56 }
57 iterator begin ()
58 {
59 return &_buf[0];
60 }
61 const_iterator end () const
62 {
63 return &_buf[_last];
64 }
65 iterator end ()
66 {
67 return &_buf[_last];
68 }
69 size_type capacity () const
70 {
71 return _size;
72 }
73 size_type size () const
74 {
75 return _last;
76 }
77
78 private:
79 static T *alloc (size_type n)
80 {
81 return (T *)::operator new ((size_t) (n * sizeof (T)));
82 }
83 static void dealloc (T *buf)
84 {
85 if (buf)
86 ::operator delete (buf);
87 }
88
89 void reserve (iterator where, size_type n)
90 {
91 if (_last + n <= _size) {
92 memmove (where+n, where, (end ()-where)*sizeof (T));
93 } else {
94 size_type sz = _last+n;
95 sz = (_size == 0) ? max (sz, 5) : max (sz, 2*_size);
96 T *nbuf = alloc (sz);
97 if (_buf) {
98 memcpy (nbuf, begin (), (where-begin ())*sizeof (T));
99 memcpy (nbuf + (where-begin ()) + n, where,
100 (end ()-where)*sizeof (T));
101 dealloc (_buf);
102 }
103 _buf = nbuf;
104 _size = sz;
105 }
106 }
107
108 public:
109 void reserve (size_type sz)
110 {
111 if (_size < sz) {
112 sz = (_size == 0) ? max (sz, 5) : max (sz, 2*_size);
113 T *nbuf = alloc (sz);
114 if (_buf) {
115 memcpy (nbuf, begin (), size ()*sizeof (T));
116 dealloc (_buf);
117 }
118 _buf = nbuf;
119 _size = sz;
120 }
121 }
122 simplevec ()
123 : _last(0), _size(0), _buf(0)
124 {
125 }
126 simplevec (size_type n, const T& t = T ())
127 : _last(0), _size(0), _buf(0)
128 {
129 insert (begin (), n, t);
130 }
131 simplevec (const_iterator first, const_iterator last)
132 : _last(0), _size(0), _buf(0)
133 {
134 insert (begin (), first, last);
135 }
136 simplevec (const simplevec<T> &v)
137 : _last(0), _size(0), _buf(0)
138 {
139 reserve (v._last);
140 memcpy (_buf, v.begin (), v.size ()*sizeof (T));
141 _last = v._last;
142 }
143 simplevec<T> &operator= (const simplevec<T> &v)
144 {
145 if (this != &v) {
146 _last = 0;
147 reserve (v._last);
148 memcpy (_buf, v.begin (), v.size ()*sizeof (T));
149 _last = v._last;
150 }
151 return *this;
152 }
153 ~simplevec ()
154 {
155 dealloc (_buf);
156 }
157 const T &front () const
158 {
159 //ministl_assert (size () > 0);
160 return _buf[0];
161 }
162 T &front ()
163 {
164 //ministl_assert (size () > 0);
165 return _buf[0];
166 }
167 const T &back () const
168 {
169 //ministl_assert (size () > 0);
170 return _buf[_last-1];
171 }
172 T &back ()
173 {
174 //ministl_assert (size () > 0);
175 return _buf[_last-1];
176 }
177 bool empty () const
178 {
179 return _last == 0;
180 }
181 void clear ()
182 {
183 _last = 0;
184 }
185 void push_back (const T &t)
186 {
187 reserve (_last+1);
188 *end () = t;
189 ++_last;
190 }
191 void push_back (T &t)
192 {
193 reserve (_last+1);
194 *end () = t;
195 ++_last;
196 }
197 void pop_back ()
198 {
199 //ministl_assert (size () > 0);
200 --_last;
201 }
202 const T &operator[] (size_type idx) const
203 {
204 //ministl_assert (idx < size ());
205 return _buf[idx];
206 }
207 T &operator[] (size_type idx)
208 {
209 //ministl_assert (idx < size ());
210 return _buf[idx];
211 }
212 iterator insert (iterator pos, const T &t)
213 {
214 //ministl_assert (pos <= end ());
215 long at = pos - begin ();
216 reserve (pos, 1);
217 pos = begin ()+at;
218 *pos = t;
219 ++_last;
220 return pos;
221 }
222 iterator insert (iterator pos, const_iterator first, const_iterator last)
223 {
224 //ministl_assert (pos <= end ());
225 long n = last - first;
226 long at = pos - begin ();
227 if (n > 0) {
228 reserve (pos, n);
229 pos = begin ()+at;
230 memcpy (pos, first, (last-first)*sizeof (T));
231 _last += n;
232 }
233 return pos;
234 }
235 iterator insert (iterator pos, size_type n, const T &t)
236 {
237 //ministl_assert (pos <= end ());
238 long at = pos - begin ();
239 if (n > 0) {
240 reserve (pos, n);
241 pos = begin ()+at;
242 for (int i = 0; i < n; ++i)
243 pos[i] = t;
244 _last += n;
245 }
246 return pos;
247 }
248 void erase (iterator first, iterator last)
249 {
250 if (last != first) {
251 memmove (first, last, (end ()-last)*sizeof (T));
252 _last -= last - first;
253 }
254 }
255 void erase (iterator pos)
256 {
257 if (pos != end ()) {
258 memmove (pos, pos+1, (end ()- (pos+1))*sizeof (T));
259 --_last;
260 }
261 }
262 void swap (simplevec<T> &t)
263 {
264 ::swap(_last, t._last);
265 ::swap(_size, t._size);
266 ::swap(_buf, t._buf);
267 }
268 };
269
270 template<class T>
271 bool operator== (const simplevec<T> &v1, const simplevec<T> &v2)
272 {
273 if (v1.size () != v2.size ())
274 return false;
275 return !v1.size () || !memcmp (&v1[0], &v2[0], v1.size ()*sizeof (T));
276 }
277
278 template<class T>
279 bool operator< (const simplevec<T> &v1, const simplevec<T> &v2)
280 {
281 unsigned long minlast = min (v1.size (), v2.size ());
282 for (unsigned long i = 0; i < minlast; ++i) {
283 if (v1[i] < v2[i])
284 return true;
285 if (v2[i] < v1[i])
286 return false;
287 }
288 return v1.size () < v2.size ();
289 }
290
291
292 template<typename T>
293 struct vector : simplevec<T>
294 { };
295
296 #if 0
297 template<typename T>
298 struct rxvt_vec : simplevec<void *> {
299 typedef T *iterator;
300
301 void push_back (T d) { simplevec<void *>::push_back ((void *)d); }
302 T pop_back () { return (T*)simplevec<void *>::pop_back (); }
303 void erase (int i) { erase (begin () + i); }
304 void erase (iterator i) { simplevec<void *>::erase ((void **)i); }
305 iterator begin () const { return (iterator)simplevec<void *>::begin (); }
306 iterator end () const { return (iterator)simplevec<void *>::end (); }
307 T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); }
308 const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); }
309 };
310 #endif
311
312 template <typename I, typename T>
313 I find (I first, I last, const T& value)
314 {
315 while (first != last && *first != value)
316 ++first;
317
318 return first;
319 }
320
321 template<typename T>
322 struct auto_ptr {
323 T *p;
324
325 auto_ptr () : p (0) { }
326 auto_ptr (T *a) : p (a) { }
327
328 auto_ptr (auto_ptr<T> &a)
329 {
330 p = a.p;
331 a.p = 0;
332 }
333
334 template<typename A>
335 auto_ptr (auto_ptr<A> &a)
336 {
337 p = a.p;
338 a.p = 0;
339 }
340
341 ~auto_ptr ()
342 {
343 delete p;
344 }
345
346 // void because it makes sense in our context
347 void operator = (T *a)
348 {
349 delete p;
350 p = a;
351 }
352
353 void operator = (auto_ptr &a)
354 {
355 *this = a.p;
356 a.p = 0;
357 }
358
359 template<typename A>
360 void operator = (auto_ptr<A> &a)
361 {
362 *this = a.p;
363 a.p = 0;
364 }
365
366 operator T * () const { return p; }
367
368 T *operator -> () const { return p; }
369 T &operator * () const { return *p; }
370
371 T *get ()
372 {
373 T *r = p;
374 p = 0;
375 return r;
376 }
377 };
378
379 typedef auto_ptr<char> auto_str;
380
381 struct stringvec : simplevec<char *>
382 {
383 ~stringvec ()
384 {
385 for (char **c = begin (); c != end (); c++)
386 delete [] *c;
387 }
388 };
389
390 #endif
391