ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtutil.h
Revision: 1.62
Committed: Wed Nov 12 12:12:02 2014 UTC (9 years, 6 months ago) by sf-exg
Content type: text/plain
Branch: MAIN
Changes since 1.61: +0 -5 lines
Log Message:
Make new/delete compatible with C++11 and change new to use rxvt_malloc.

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.58 #include "emman.h"
10    
11 root 1.24 // increases code size unless -fno-enforce-eh-specs
12     #if __GNUC__
13     # define NOTHROW
14     # define THROW(x)
15     #else
16     # define NOTHROW throw()
17     # define THROW(x) throw x
18     #endif
19    
20 root 1.16 // various utility functions
21 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; }
22     template<typename T, typename U> static inline void max_it (T &a, U b) { a = a > (T)b ? a : (T)b; }
23 root 1.13
24     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; }
25     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; }
26    
27 sf-exg 1.51 template<typename T> static inline T squared_diff (T a, T b) { return (a - b) * (a - b); }
28 root 1.25
29 root 1.21 // linear interpolation
30     template<typename T, typename U, typename P>
31 root 1.55 static inline T
32     lerp (T a, U b, P p)
33 root 1.21 {
34 root 1.26 return (long(a) * long(100 - p) + long(b) * long(p) + 50) / 100;
35 root 1.21 }
36    
37 ayin 1.32 // return a very temporary (and never deallocated) buffer. keep small.
38     void *rxvt_temp_buf (int len);
39    
40     template<typename T>
41     static inline T *
42     rxvt_temp_buf (int len)
43     {
44     return (T *)rxvt_temp_buf (len * sizeof (T));
45     }
46    
47 root 1.11 // in range including end
48     #define IN_RANGE_INC(val,beg,end) \
49 root 1.9 ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg))
50 root 1.1
51 root 1.11 // in range excluding end
52     #define IN_RANGE_EXC(val,beg,end) \
53     ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg))
54    
55 ayin 1.36 // for m >= -n, ensure remainder lies between 0..n-1
56     #define MOD(m,n) (((m) + (n)) % (n))
57    
58 root 1.16 // makes dynamically allocated objects zero-initialised
59 root 1.38 struct zero_initialized
60     {
61 root 1.4 void *operator new (size_t s);
62     void operator delete (void *p, size_t s);
63     };
64    
65 ayin 1.32 struct stringvec : simplevec<char *>
66     {
67     ~stringvec ()
68     {
69     for (char **c = begin (); c != end (); c++)
70     free (*c);
71     }
72     };
73    
74 root 1.34 #if 0
75 root 1.1 template<typename T>
76 root 1.38 struct rxvt_vec : simplevec<void *>
77     {
78 root 1.1 typedef T *iterator;
79    
80     void push_back (T d) { simplevec<void *>::push_back ((void *)d); }
81     T pop_back () { return (T*)simplevec<void *>::pop_back (); }
82     void erase (int i) { erase (begin () + i); }
83     void erase (iterator i) { simplevec<void *>::erase ((void **)i); }
84     iterator begin () const { return (iterator)simplevec<void *>::begin (); }
85     iterator end () const { return (iterator)simplevec<void *>::end (); }
86     T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); }
87     const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); }
88     };
89 root 1.34 #endif
90 root 1.1
91 sf-exg 1.61 typedef estl::scoped_array<char> auto_str;
92 root 1.20
93 root 1.1 #endif
94