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.2 by root, Sun Aug 15 07:20:16 2004 UTC vs.
Revision 1.18 by root, Tue Jan 17 16:06:48 2006 UTC

1#ifndef RXVT_UTIL_H 1#ifndef RXVT_UTIL_H
2#define 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)
3 10
4extern class byteorder { 11extern class byteorder {
5 static unsigned int e; // at least 32 bits 12 static unsigned int e; // at least 32 bits
6public: 13public:
7 byteorder (); 14 byteorder ();
10 static bool network () { return e == 0x11223344; }; 17 static bool network () { return e == 0x11223344; };
11 static bool little_endian () { return e == 0x44332211; }; 18 static bool little_endian () { return e == 0x44332211; };
12 static bool vax () { return e == 0x44332211; }; 19 static bool vax () { return e == 0x44332211; };
13} byteorder; 20} byteorder;
14 21
15template<typename T, typename U> 22// various utility functions
16static inline T min (T a, U b) { return a < b ? a : (T)b; } 23template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; }
17template<typename T, typename U> 24template<typename T, typename U> static inline void min_it (T &a, U b) { a = a < (T)b ? a : (T)b; }
18static inline T max (T a, U b) { return a > b ? a : (T)b; } 25template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; }
26template<typename T, typename U> static inline void max_it (T &a, U b) { a = a > (T)b ? a : (T)b; }
19 27
20#include "simplevec.h" 28template<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; }
29template<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
31template<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
42struct 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 */
52template<class T>
53struct simplevec {
54 typedef T* iterator;
55 typedef const T* const_iterator;
56 typedef unsigned long size_type;
57
58private:
59 size_type _last, _size;
60 T *_buf;
61
62public:
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
88private:
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
118public:
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
280template<class T>
281bool 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
288template<class T>
289bool 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
21 301
22template<typename T> 302template<typename T>
23struct vector : simplevec<T> 303struct vector : simplevec<T>
24{ }; 304{ };
25 305
114 { 394 {
115 for (char **c = begin (); c != end (); c++) 395 for (char **c = begin (); c != end (); c++)
116 delete [] *c; 396 delete [] *c;
117 } 397 }
118}; 398};
399
119#endif 400#endif
120 401

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines