ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtutil.h
Revision: 1.54
Committed: Fri May 25 07:38:05 2012 UTC (12 years ago) by sf-exg
Content type: text/plain
Branch: MAIN
Changes since 1.53: +2 -2 lines
Log Message:
Replace auto_ptr 'operator= (T *a)' with 'reset', again.

File Contents

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