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.28 by ayin, Tue Jun 26 10:46:28 2007 UTC vs.
Revision 1.60 by sf-exg, Tue Oct 28 09:05:33 2014 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 <new>
4#include <cstdlib> 5#include <stdlib.h>
5#include <cstring> 6#include <string.h>
7#include "ecb.h"
8#include "estl.h"
6 9
7using namespace std; 10#include "emman.h"
8
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)
13
14#define HAVE_GCC_BUILTINS (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ == 4))
15
16#ifndef __attribute__
17# if __GNUC__
18# if (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || (__GNUC__ < 2)
19# define __attribute__(x)
20# endif
21# endif
22# define __attribute__(x)
23#endif
24
25#define NORETURN __attribute__ ((noreturn))
26#define UNUSED __attribute__ ((unused))
27#define CONST __attribute__ ((const))
28 11
29// increases code size unless -fno-enforce-eh-specs 12// increases code size unless -fno-enforce-eh-specs
30#if __GNUC__ 13#if __GNUC__
31# define NOTHROW 14# define NOTHROW
32# define THROW(x) 15# define THROW(x)
33#else 16#else
34# define NOTHROW throw() 17# define NOTHROW throw()
35# define THROW(x) throw x 18# define THROW(x) throw x
36#endif 19#endif
37 20
38extern class byteorder {
39 static unsigned int e; // at least 32 bits
40public:
41 byteorder ();
42
43 static bool big_endian () { return e == 0x11223344; };
44 static bool network () { return e == 0x11223344; };
45 static bool little_endian () { return e == 0x44332211; };
46 static bool vax () { return e == 0x44332211; };
47} byteorder;
48
49// various utility functions 21// various utility functions
50template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; }
51template<typename T, typename U> static inline void min_it (T &a, U b) { a = a < (T)b ? a : (T)b; } 22template<typename T, typename U> static inline void min_it (T &a, U b) { a = a < (T)b ? a : (T)b; }
52template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; }
53template<typename T, typename U> static inline void max_it (T &a, U b) { a = a > (T)b ? a : (T)b; } 23template<typename T, typename U> static inline void max_it (T &a, U b) { a = a > (T)b ? a : (T)b; }
54 24
55template<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; } 25template<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; }
56template<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; } 26template<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; }
57 27
58template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; }
59
60template<typename T> static inline T squared_diff (T a, T b) { return (a-b)*(a-b); } 28template<typename T> static inline T squared_diff (T a, T b) { return (a - b) * (a - b); }
61 29
62// linear interpolation 30// linear interpolation
63template<typename T, typename U, typename P> 31template<typename T, typename U, typename P>
64static inline 32static inline T
65T lerp (T a, U b, P p) 33lerp (T a, U b, P p)
66{ 34{
67 return (long(a) * long(100 - p) + long(b) * long(p) + 50) / 100; 35 return (long(a) * long(100 - p) + long(b) * long(p) + 50) / 100;
68} 36}
69 37
70// some bit functions, xft fuck me plenty 38// return a very temporary (and never deallocated) buffer. keep small.
71#if HAVE_GCC_BUILTINS 39void *rxvt_temp_buf (int len);
72static inline int ctz (unsigned int x) CONST { return __builtin_ctz (x); } 40
73static inline int popcount (unsigned int x) CONST { return __builtin_popcount (x); } 41template<typename T>
74#else 42static inline T *
75// count trailing zero bits and count # of one bits 43rxvt_temp_buf (int len)
76int ctz (unsigned int x) CONST; 44{
77int popcount (unsigned int x) CONST; 45 return (T *)rxvt_temp_buf (len * sizeof (T));
78#endif 46}
79 47
80// in range including end 48// in range including end
81#define IN_RANGE_INC(val,beg,end) \ 49#define IN_RANGE_INC(val,beg,end) \
82 ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) 50 ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg))
83 51
84// in range excluding end 52// in range excluding end
85#define IN_RANGE_EXC(val,beg,end) \ 53#define IN_RANGE_EXC(val,beg,end) \
86 ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) 54 ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg))
87 55
56// for m >= -n, ensure remainder lies between 0..n-1
57#define MOD(m,n) (((m) + (n)) % (n))
58
88// makes dynamically allocated objects zero-initialised 59// makes dynamically allocated objects zero-initialised
89struct zero_initialized { 60struct zero_initialized
61{
90 void *operator new (size_t s); 62 void *operator new (size_t s);
91 void operator delete (void *p, size_t s); 63 void operator delete (void *p, size_t s);
92}; 64};
93 65
94/* simplevec taken (and heavily modified), from: 66// alas new/delete cannot be specified as inline in C++11 (see 17.6.4.6)
95 * 67void *operator new (size_t s) throw (std::bad_alloc);
96 * MICO --- a free CORBA implementation 68void operator delete (void *p) throw ();
97 * Copyright (C) 1997-98 Kay Roemer & Arno Puder
98 */
99template<class T>
100struct simplevec {
101 typedef T* iterator;
102 typedef const T* const_iterator;
103 typedef unsigned long size_type;
104 69
105private: 70struct stringvec : simplevec<char *>
106 size_type _last, _size; 71{
107 T *_buf; 72 ~stringvec ()
108
109public:
110 const_iterator begin () const
111 { 73 {
112 return &_buf[0]; 74 for (char **c = begin (); c != end (); c++)
75 free (*c);
113 } 76 }
114 iterator begin ()
115 {
116 return &_buf[0];
117 }
118 const_iterator end () const
119 {
120 return &_buf[_last];
121 }
122 iterator end ()
123 {
124 return &_buf[_last];
125 }
126 size_type capacity () const
127 {
128 return _size;
129 }
130 size_type size () const
131 {
132 return _last;
133 }
134
135private:
136 static T *alloc (size_type n)
137 {
138 return (T *)::operator new ((size_t) (n * sizeof (T)));
139 }
140 static void dealloc (T *buf)
141 {
142 if (buf)
143 ::operator delete (buf);
144 }
145
146 void reserve (iterator where, size_type n)
147 {
148 if (_last + n <= _size) {
149 memmove (where+n, where, (end ()-where)*sizeof (T));
150 } else {
151 size_type sz = _last+n;
152 sz = (_size == 0) ? max (sz, 5) : max (sz, 2*_size);
153 T *nbuf = alloc (sz);
154 if (_buf) {
155 memcpy (nbuf, begin (), (where-begin ())*sizeof (T));
156 memcpy (nbuf + (where-begin ()) + n, where,
157 (end ()-where)*sizeof (T));
158 dealloc (_buf);
159 }
160 _buf = nbuf;
161 _size = sz;
162 }
163 }
164
165public:
166 void reserve (size_type sz)
167 {
168 if (_size < sz) {
169 sz = (_size == 0) ? max (sz, 5) : max (sz, 2*_size);
170 T *nbuf = alloc (sz);
171 if (_buf) {
172 memcpy (nbuf, begin (), size ()*sizeof (T));
173 dealloc (_buf);
174 }
175 _buf = nbuf;
176 _size = sz;
177 }
178 }
179 simplevec ()
180 : _last(0), _size(0), _buf(0)
181 {
182 }
183 simplevec (size_type n, const T& t = T ())
184 : _last(0), _size(0), _buf(0)
185 {
186 insert (begin (), n, t);
187 }
188 simplevec (const_iterator first, const_iterator last)
189 : _last(0), _size(0), _buf(0)
190 {
191 insert (begin (), first, last);
192 }
193 simplevec (const simplevec<T> &v)
194 : _last(0), _size(0), _buf(0)
195 {
196 reserve (v._last);
197 memcpy (_buf, v.begin (), v.size ()*sizeof (T));
198 _last = v._last;
199 }
200 simplevec<T> &operator= (const simplevec<T> &v)
201 {
202 if (this != &v) {
203 _last = 0;
204 reserve (v._last);
205 memcpy (_buf, v.begin (), v.size ()*sizeof (T));
206 _last = v._last;
207 }
208 return *this;
209 }
210 ~simplevec ()
211 {
212 dealloc (_buf);
213 }
214 const T &front () const
215 {
216 //ministl_assert (size () > 0);
217 return _buf[0];
218 }
219 T &front ()
220 {
221 //ministl_assert (size () > 0);
222 return _buf[0];
223 }
224 const T &back () const
225 {
226 //ministl_assert (size () > 0);
227 return _buf[_last-1];
228 }
229 T &back ()
230 {
231 //ministl_assert (size () > 0);
232 return _buf[_last-1];
233 }
234 bool empty () const
235 {
236 return _last == 0;
237 }
238 void clear ()
239 {
240 _last = 0;
241 }
242 void push_back (const T &t)
243 {
244 reserve (_last+1);
245 *end () = t;
246 ++_last;
247 }
248 void push_back (T &t)
249 {
250 reserve (_last+1);
251 *end () = t;
252 ++_last;
253 }
254 void pop_back ()
255 {
256 //ministl_assert (size () > 0);
257 --_last;
258 }
259 const T &operator[] (size_type idx) const
260 {
261 //ministl_assert (idx < size ());
262 return _buf[idx];
263 }
264 T &operator[] (size_type idx)
265 {
266 //ministl_assert (idx < size ());
267 return _buf[idx];
268 }
269 iterator insert (iterator pos, const T &t)
270 {
271 //ministl_assert (pos <= end ());
272 long at = pos - begin ();
273 reserve (pos, 1);
274 pos = begin ()+at;
275 *pos = t;
276 ++_last;
277 return pos;
278 }
279 iterator insert (iterator pos, const_iterator first, const_iterator last)
280 {
281 //ministl_assert (pos <= end ());
282 long n = last - first;
283 long at = pos - begin ();
284 if (n > 0) {
285 reserve (pos, n);
286 pos = begin ()+at;
287 memcpy (pos, first, (last-first)*sizeof (T));
288 _last += n;
289 }
290 return pos;
291 }
292 iterator insert (iterator pos, size_type n, const T &t)
293 {
294 //ministl_assert (pos <= end ());
295 long at = pos - begin ();
296 if (n > 0) {
297 reserve (pos, n);
298 pos = begin ()+at;
299 for (int i = 0; i < n; ++i)
300 pos[i] = t;
301 _last += n;
302 }
303 return pos;
304 }
305 void erase (iterator first, iterator last)
306 {
307 if (last != first) {
308 memmove (first, last, (end () - last) * sizeof (T));
309 _last -= last - first;
310 }
311 }
312 void erase (iterator pos)
313 {
314 if (pos != end ()) {
315 memmove (pos, pos+1, (end () - (pos+1)) * sizeof (T));
316 --_last;
317 }
318 }
319 void swap (simplevec<T> &t)
320 {
321 ::swap(_last, t._last);
322 ::swap(_size, t._size);
323 ::swap(_buf, t._buf);
324 }
325}; 77};
326
327template<class T>
328bool operator== (const simplevec<T> &v1, const simplevec<T> &v2)
329{
330 if (v1.size () != v2.size ())
331 return false;
332 return !v1.size () || !memcmp (&v1[0], &v2[0], v1.size ()*sizeof (T));
333}
334
335template<class T>
336bool operator< (const simplevec<T> &v1, const simplevec<T> &v2)
337{
338 unsigned long minlast = min (v1.size (), v2.size ());
339 for (unsigned long i = 0; i < minlast; ++i) {
340 if (v1[i] < v2[i])
341 return true;
342 if (v2[i] < v1[i])
343 return false;
344 }
345 return v1.size () < v2.size ();
346}
347
348
349template<typename T>
350struct vector : simplevec<T>
351{ };
352 78
353#if 0 79#if 0
354template<typename T> 80template<typename T>
355struct rxvt_vec : simplevec<void *> { 81struct rxvt_vec : simplevec<void *>
82{
356 typedef T *iterator; 83 typedef T *iterator;
357 84
358 void push_back (T d) { simplevec<void *>::push_back ((void *)d); } 85 void push_back (T d) { simplevec<void *>::push_back ((void *)d); }
359 T pop_back () { return (T*)simplevec<void *>::pop_back (); } 86 T pop_back () { return (T*)simplevec<void *>::pop_back (); }
360 void erase (int i) { erase (begin () + i); } 87 void erase (int i) { erase (begin () + i); }
364 T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); } 91 T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); }
365 const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); } 92 const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); }
366}; 93};
367#endif 94#endif
368 95
369template <typename I, typename T> 96template<typename T>
370I find (I first, I last, const T& value) 97struct auto_ptr
371{ 98{
372 while (first != last && *first != value)
373 ++first;
374
375 return first;
376}
377
378template<typename T>
379struct auto_ptr {
380 T *p; 99 T *p;
381 100
382 auto_ptr () : p (0) { } 101 auto_ptr () : p (0) { }
102
103 explicit
383 auto_ptr (T *a) : p (a) { } 104 auto_ptr (T *a) : p (a) { }
384 105
385 auto_ptr (auto_ptr<T> &a) 106 auto_ptr (auto_ptr &a)
386 { 107 {
387 p = a.p; 108 p = a.p;
388 a.p = 0; 109 a.p = 0;
389 } 110 }
390 111
398 ~auto_ptr () 119 ~auto_ptr ()
399 { 120 {
400 delete p; 121 delete p;
401 } 122 }
402 123
403 // void because it makes sense in our context 124 void reset (T *a)
404 void operator = (T *a)
405 { 125 {
406 delete p; 126 delete p;
407 p = a; 127 p = a;
408 } 128 }
409 129
130 // void because it makes sense in our context
410 void operator = (auto_ptr &a) 131 void operator =(auto_ptr &a)
411 { 132 {
412 *this = a.p; 133 reset (a.release ());
413 a.p = 0;
414 } 134 }
415 135
416 template<typename A> 136 template<typename A>
417 void operator = (auto_ptr<A> &a) 137 void operator =(auto_ptr<A> &a)
418 { 138 {
419 *this = a.p; 139 reset (a.release ());
420 a.p = 0;
421 } 140 }
422 141
142 T *operator ->() const { return p; }
423 operator T * () const { return p; } 143 T &operator *() const { return *p; }
424 144
145 operator T *() { return p; }
425 T *operator -> () const { return p; } 146 T *get () const { return p; }
426 T &operator * () const { return *p; }
427 147
428 T *get () 148 T *release()
429 { 149 {
430 T *r = p; 150 T *r = p;
431 p = 0; 151 p = 0;
432 return r; 152 return r;
433 } 153 }
434}; 154};
435 155
436typedef auto_ptr<char> auto_str; 156typedef auto_ptr<char> auto_str;
437 157
438struct stringvec : simplevec<char *>
439{
440 ~stringvec ()
441 {
442 for (char **c = begin (); c != end (); c++)
443 free (*c);
444 }
445};
446
447// return a very temporary (and never deallocated) buffer. keep small.
448void *rxvt_temp_buf (int len);
449
450template<typename T>
451static inline T *
452rxvt_temp_buf (int len)
453{
454 return (T *)rxvt_temp_buf (len * sizeof (T));
455}
456
457#endif 158#endif
458 159

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines