ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtutil.h
Revision: 1.55
Committed: Fri May 25 07:48:03 2012 UTC (12 years ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.54: +28 -11 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.55 #include <new>
5 sf-exg 1.47 #include <stdlib.h>
6     #include <string.h>
7 sf-exg 1.45 #include "ecb.h"
8 sf-exg 1.49 #include "estl.h"
9 root 1.4
10 root 1.24 // increases code size unless -fno-enforce-eh-specs
11     #if __GNUC__
12     # define NOTHROW
13     # define THROW(x)
14     #else
15     # define NOTHROW throw()
16     # define THROW(x) throw x
17     #endif
18    
19 root 1.16 // various utility functions
20 sf-exg 1.51 template<typename T, typename U> static inline void min_it (T &a, U b) { a = a < (T)b ? a : (T)b; }
21     template<typename T, typename U> static inline void max_it (T &a, U b) { a = a > (T)b ? a : (T)b; }
22 root 1.13
23     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; }
24     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; }
25    
26 sf-exg 1.51 template<typename T> static inline T squared_diff (T a, T b) { return (a - b) * (a - b); }
27 root 1.25
28 root 1.21 // linear interpolation
29     template<typename T, typename U, typename P>
30 root 1.55 static inline T
31     lerp (T a, U b, P p)
32 root 1.21 {
33 root 1.26 return (long(a) * long(100 - p) + long(b) * long(p) + 50) / 100;
34 root 1.21 }
35    
36 ayin 1.32 // return a very temporary (and never deallocated) buffer. keep small.
37     void *rxvt_temp_buf (int len);
38    
39     template<typename T>
40     static inline T *
41     rxvt_temp_buf (int len)
42     {
43     return (T *)rxvt_temp_buf (len * sizeof (T));
44     }
45    
46 root 1.11 // in range including end
47     #define IN_RANGE_INC(val,beg,end) \
48 root 1.9 ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg))
49 root 1.1
50 root 1.11 // in range excluding end
51     #define IN_RANGE_EXC(val,beg,end) \
52     ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg))
53    
54 ayin 1.36 // for m >= -n, ensure remainder lies between 0..n-1
55     #define MOD(m,n) (((m) + (n)) % (n))
56    
57 root 1.16 // makes dynamically allocated objects zero-initialised
58 root 1.38 struct zero_initialized
59     {
60 root 1.4 void *operator new (size_t s);
61     void operator delete (void *p, size_t s);
62     };
63    
64 ayin 1.32 struct stringvec : simplevec<char *>
65     {
66     ~stringvec ()
67     {
68     for (char **c = begin (); c != end (); c++)
69     free (*c);
70     }
71     };
72    
73 root 1.34 #if 0
74 root 1.1 template<typename T>
75 root 1.38 struct rxvt_vec : simplevec<void *>
76     {
77 root 1.1 typedef T *iterator;
78    
79     void push_back (T d) { simplevec<void *>::push_back ((void *)d); }
80     T pop_back () { return (T*)simplevec<void *>::pop_back (); }
81     void erase (int i) { erase (begin () + i); }
82     void erase (iterator i) { simplevec<void *>::erase ((void **)i); }
83     iterator begin () const { return (iterator)simplevec<void *>::begin (); }
84     iterator end () const { return (iterator)simplevec<void *>::end (); }
85     T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); }
86     const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); }
87     };
88 root 1.34 #endif
89 root 1.1
90 root 1.55 inline void *
91     operator new (size_t size)
92     {
93     // TODO: use rxvt_malloc
94     return malloc (size);
95     }
96    
97     inline void
98     operator delete (void *p)
99     {
100     free (p);
101     }
102    
103 root 1.1 template<typename T>
104 root 1.38 struct auto_ptr
105     {
106 root 1.1 T *p;
107    
108 root 1.55 auto_ptr () : p (0) { }
109    
110     explicit
111 root 1.1 auto_ptr (T *a) : p (a) { }
112    
113     auto_ptr (auto_ptr<T> &a)
114     {
115     p = a.p;
116     a.p = 0;
117     }
118    
119     template<typename A>
120     auto_ptr (auto_ptr<A> &a)
121     {
122     p = a.p;
123     a.p = 0;
124     }
125    
126     ~auto_ptr ()
127     {
128 root 1.55 delete p;
129 root 1.1 }
130    
131 sf-exg 1.54 void reset (T *a)
132 root 1.1 {
133 root 1.55 delete p;
134 root 1.1 p = a;
135     }
136    
137 sf-exg 1.54 // void because it makes sense in our context
138 root 1.55 void operator =(auto_ptr &a)
139 root 1.1 {
140     *this = a.p;
141     a.p = 0;
142     }
143    
144     template<typename A>
145 root 1.55 void operator =(auto_ptr<A> &a)
146 root 1.1 {
147     *this = a.p;
148     a.p = 0;
149     }
150    
151 root 1.55 T *operator ->() const { return p; }
152     T &operator *() const { return *p; }
153 root 1.1
154 root 1.55 operator T *() { return p; }
155     T *get () const { return p; }
156 root 1.1
157 root 1.55 T *release()
158 root 1.1 {
159     T *r = p;
160     p = 0;
161     return r;
162     }
163     };
164    
165     typedef auto_ptr<char> auto_str;
166 root 1.20
167 root 1.1 #endif
168