ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtutil.h
Revision: 1.4
Committed: Thu Sep 2 07:44:40 2004 UTC (19 years, 9 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-4_4, rel-4_6, rel-4_7, rel-4_2, rel-4_3, rel-4_1, rel-4_0
Changes since 1.3: +255 -6 lines
Log Message:
*** empty log message ***

File Contents

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