9 #define PP_CONCAT_(a, b) a ## b
10 #define PP_CONCAT(a, b) PP_CONCAT_(a, b)
11 #define PP_STRINGIFY_(a) #a
12 #define PP_STRINGIFY(a) PP_STRINGIFY_(a)
14 #define HAVE_GCC_BUILTINS (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ == 4))
17 # define rxvt_attribute(x) __attribute__(x)
18 # define expect(expr,value) __builtin_expect ((expr),(value))
20 # define rxvt_attribute(x)
21 # define expect(expr,value) (expr)
24 // put into ifs if you are very sure that the expression
25 // is mostly true or mosty false. note that these return
26 // booleans, not the expression.
27 #define expect_false(expr) expect ((expr) != 0, 0)
28 #define expect_true(expr) expect ((expr) != 0, 1)
30 #define NORETURN rxvt_attribute ((noreturn))
31 #define UNUSED rxvt_attribute ((unused))
32 #define CONST rxvt_attribute ((const))
34 // increases code size unless -fno-enforce-eh-specs
39 # define NOTHROW throw()
40 # define THROW(x) throw x
43 extern class byteorder {
44 static unsigned int e; // at least 32 bits
48 static bool big_endian () { return e == 0x11223344; };
49 static bool network () { return e == 0x11223344; };
50 static bool little_endian () { return e == 0x44332211; };
51 static bool vax () { return e == 0x44332211; };
54 // various utility functions
55 template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; }
56 template<typename T, typename U> static inline void min_it (T &a, U b) { a = a < (T)b ? a : (T)b; }
57 template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; }
58 template<typename T, typename U> static inline void max_it (T &a, U b) { a = a > (T)b ? a : (T)b; }
60 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; }
61 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; }
63 template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; }
65 template<typename T> static inline T squared_diff (T a, T b) { return (a-b)*(a-b); }
67 // linear interpolation
68 template<typename T, typename U, typename P>
70 T lerp (T a, U b, P p)
72 return (long(a) * long(100 - p) + long(b) * long(p) + 50) / 100;
75 template <typename I, typename T>
76 I find (I first, I last, const T& value)
78 while (first != last && *first != value)
84 // return a very temporary (and never deallocated) buffer. keep small.
85 void *rxvt_temp_buf (int len);
89 rxvt_temp_buf (int len)
91 return (T *)rxvt_temp_buf (len * sizeof (T));
94 // some bit functions, xft fuck me plenty
96 /* netbsd stupidly defines popcount itself and puts it into string.h */
97 static inline int rxvt_ctz (unsigned int x) { return __builtin_ctz (x); }
98 static inline int rxvt_popcount (unsigned int x) { return __builtin_popcount (x); }
100 // count trailing zero bits and count # of one bits
101 int rxvt_ctz (unsigned int x) CONST;
102 int rxvt_popcount (unsigned int x) CONST;
105 // in range including end
106 #define IN_RANGE_INC(val,beg,end) \
107 ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg))
109 // in range excluding end
110 #define IN_RANGE_EXC(val,beg,end) \
111 ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg))
113 // for m >= -n, ensure remainder lies between 0..n-1
114 #define MOD(m,n) (((m) + (n)) % (n))
116 // makes dynamically allocated objects zero-initialised
117 struct zero_initialized
119 void *operator new (size_t s);
120 void operator delete (void *p, size_t s);
123 /* simplevec taken (and heavily modified), from:
125 * MICO --- a free CORBA implementation
126 * Copyright (C) 1997-98 Kay Roemer & Arno Puder
132 typedef const T* const_iterator;
133 typedef unsigned long size_type;
136 size_type _last, _size;
140 const_iterator begin () const
148 const_iterator end () const
156 size_type capacity () const
160 size_type size () const
166 static T *alloc (size_type n)
168 return (T *)::operator new ((size_t) (n * sizeof (T)));
170 static void dealloc (T *buf)
173 ::operator delete (buf);
176 void reserve (iterator where, size_type n)
178 if (_last + n <= _size) {
179 memmove (where+n, where, (end ()-where)*sizeof (T));
181 size_type sz = _last+n;
182 sz = (_size == 0) ? max (sz, 5) : max (sz, 2*_size);
183 T *nbuf = alloc (sz);
185 memcpy (nbuf, begin (), (where-begin ())*sizeof (T));
186 memcpy (nbuf + (where-begin ()) + n, where,
187 (end ()-where)*sizeof (T));
196 void reserve (size_type sz)
199 sz = (_size == 0) ? max (sz, 5) : max (sz, 2*_size);
200 T *nbuf = alloc (sz);
202 memcpy (nbuf, begin (), size ()*sizeof (T));
210 : _last(0), _size(0), _buf(0)
213 simplevec (size_type n, const T& t = T ())
214 : _last(0), _size(0), _buf(0)
216 insert (begin (), n, t);
218 simplevec (const_iterator first, const_iterator last)
219 : _last(0), _size(0), _buf(0)
221 insert (begin (), first, last);
223 simplevec (const simplevec<T> &v)
224 : _last(0), _size(0), _buf(0)
227 memcpy (_buf, v.begin (), v.size ()*sizeof (T));
230 simplevec<T> &operator= (const simplevec<T> &v)
235 memcpy (_buf, v.begin (), v.size ()*sizeof (T));
244 const T &front () const
246 //ministl_assert (size () > 0);
251 //ministl_assert (size () > 0);
254 const T &back () const
256 //ministl_assert (size () > 0);
257 return _buf[_last-1];
261 //ministl_assert (size () > 0);
262 return _buf[_last-1];
272 void push_back (const T &t)
278 void push_back (T &t)
286 //ministl_assert (size () > 0);
289 const T &operator[] (size_type idx) const
291 //ministl_assert (idx < size ());
294 T &operator[] (size_type idx)
296 //ministl_assert (idx < size ());
299 iterator insert (iterator pos, const T &t)
301 //ministl_assert (pos <= end ());
302 long at = pos - begin ();
309 iterator insert (iterator pos, const_iterator first, const_iterator last)
311 //ministl_assert (pos <= end ());
312 long n = last - first;
313 long at = pos - begin ();
317 memcpy (pos, first, (last-first)*sizeof (T));
322 iterator insert (iterator pos, size_type n, const T &t)
324 //ministl_assert (pos <= end ());
325 long at = pos - begin ();
329 for (int i = 0; i < n; ++i)
335 void erase (iterator first, iterator last)
338 memmove (first, last, (end () - last) * sizeof (T));
339 _last -= last - first;
342 void erase (iterator pos)
345 memmove (pos, pos+1, (end () - (pos+1)) * sizeof (T));
349 void swap (simplevec<T> &t)
351 ::swap(_last, t._last);
352 ::swap(_size, t._size);
353 ::swap(_buf, t._buf);
358 bool operator== (const simplevec<T> &v1, const simplevec<T> &v2)
360 if (v1.size () != v2.size ())
362 return !v1.size () || !memcmp (&v1[0], &v2[0], v1.size ()*sizeof (T));
366 bool operator< (const simplevec<T> &v1, const simplevec<T> &v2)
368 unsigned long minlast = min (v1.size (), v2.size ());
369 for (unsigned long i = 0; i < minlast; ++i) {
375 return v1.size () < v2.size ();
380 struct vector : simplevec<T>
384 struct stringvec : simplevec<char *>
388 for (char **c = begin (); c != end (); c++)
395 struct rxvt_vec : simplevec<void *>
399 void push_back (T d) { simplevec<void *>::push_back ((void *)d); }
400 T pop_back () { return (T*)simplevec<void *>::pop_back (); }
401 void erase (int i) { erase (begin () + i); }
402 void erase (iterator i) { simplevec<void *>::erase ((void **)i); }
403 iterator begin () const { return (iterator)simplevec<void *>::begin (); }
404 iterator end () const { return (iterator)simplevec<void *>::end (); }
405 T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); }
406 const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); }
415 auto_ptr () : p (0) { }
416 auto_ptr (T *a) : p (a) { }
418 auto_ptr (auto_ptr<T> &a)
425 auto_ptr (auto_ptr<A> &a)
436 // void because it makes sense in our context
437 void operator = (T *a)
443 void operator = (auto_ptr &a)
450 void operator = (auto_ptr<A> &a)
456 operator T * () const { return p; }
458 T *operator -> () const { return p; }
459 T &operator * () const { return *p; }
469 typedef auto_ptr<char> auto_str;