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