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.1 by root, Sun Aug 15 00:37:04 2004 UTC vs.
Revision 1.59 by sf-exg, Thu Oct 23 22:39:40 2014 UTC

1#ifndef RXVT_UTIL_H 1#ifndef RXVT_UTIL_H
2#define RXVT_UTIL_H 2#define RXVT_UTIL_H
3 3
4extern class byteorder { 4#include <new>
5 static unsigned int e; // at least 32 bits 5#include <stdlib.h>
6public: 6#include <string.h>
7 byteorder (); 7#include "ecb.h"
8#include "estl.h"
8 9
9 static bool big_endian () { return e == 0x11223344; }; 10#include "emman.h"
10 static bool network () { return e == 0x11223344; };
11 static bool little_endian () { return e == 0x44332211; };
12 static bool vax () { return e == 0x44332211; };
13} byteorder;
14 11
15template<typename T, typename U> static inline T min (T a, U b) { return a < b ? a : b; } 12// increases code size unless -fno-enforce-eh-specs
16template<typename T, typename U> static inline T max (T a, U b) { return a > b ? a : b; } 13#if __GNUC__
14# define NOTHROW
15# define THROW(x)
16#else
17# define NOTHROW throw()
18# define THROW(x) throw x
19#endif
17 20
18#include "simplevec.h" 21// various utility functions
22template<typename T, typename U> static inline void min_it (T &a, U b) { a = a < (T)b ? a : (T)b; }
23template<typename T, typename U> static inline void max_it (T &a, U b) { a = a > (T)b ? a : (T)b; }
24
25template<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; }
26template<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; }
27
28template<typename T> static inline T squared_diff (T a, T b) { return (a - b) * (a - b); }
29
30// linear interpolation
31template<typename T, typename U, typename P>
32static inline T
33lerp (T a, U b, P p)
34{
35 return (long(a) * long(100 - p) + long(b) * long(p) + 50) / 100;
36}
37
38// return a very temporary (and never deallocated) buffer. keep small.
39void *rxvt_temp_buf (int len);
19 40
20template<typename T> 41template<typename T>
42static inline T *
43rxvt_temp_buf (int len)
44{
45 return (T *)rxvt_temp_buf (len * sizeof (T));
46}
47
48// in range including end
49#define IN_RANGE_INC(val,beg,end) \
50 ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg))
51
52// in range excluding end
53#define IN_RANGE_EXC(val,beg,end) \
54 ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg))
55
56// for m >= -n, ensure remainder lies between 0..n-1
57#define MOD(m,n) (((m) + (n)) % (n))
58
59// makes dynamically allocated objects zero-initialised
60struct zero_initialized
61{
62 void *operator new (size_t s);
63 void operator delete (void *p, size_t s);
64};
65
21struct vector : simplevec<T> 66struct stringvec : simplevec<char *>
22{ }; 67{
68 ~stringvec ()
69 {
70 for (char **c = begin (); c != end (); c++)
71 free (*c);
72 }
73};
23 74
24#if 0 75#if 0
25template<typename T> 76template<typename T>
26struct rxvt_vec : simplevec<void *> { 77struct rxvt_vec : simplevec<void *>
78{
27 typedef T *iterator; 79 typedef T *iterator;
28 80
29 void push_back (T d) { simplevec<void *>::push_back ((void *)d); } 81 void push_back (T d) { simplevec<void *>::push_back ((void *)d); }
30 T pop_back () { return (T*)simplevec<void *>::pop_back (); } 82 T pop_back () { return (T*)simplevec<void *>::pop_back (); }
31 void erase (int i) { erase (begin () + i); } 83 void erase (int i) { erase (begin () + i); }
35 T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); } 87 T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); }
36 const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); } 88 const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); }
37}; 89};
38#endif 90#endif
39 91
40template <typename I, typename T> 92inline void *
41I find (I first, I last, const T& value) 93operator new (size_t size) throw (std::bad_alloc)
42{ 94{
43 while (first != last && *first != value) 95 // TODO: use rxvt_malloc
44 ++first; 96 return malloc (size);
97}
45 98
46 return first; 99inline void
100operator delete (void *p) throw ()
101{
102 free (p);
47} 103}
48 104
49template<typename T> 105template<typename T>
50struct auto_ptr { 106struct auto_ptr
107{
51 T *p; 108 T *p;
52 109
53 auto_ptr () : p (0) { } 110 auto_ptr () : p (0) { }
111
112 explicit
54 auto_ptr (T *a) : p (a) { } 113 auto_ptr (T *a) : p (a) { }
55 114
56 auto_ptr (auto_ptr<T> &a) 115 auto_ptr (auto_ptr &a)
57 { 116 {
58 p = a.p; 117 p = a.p;
59 a.p = 0; 118 a.p = 0;
60 } 119 }
61 120
69 ~auto_ptr () 128 ~auto_ptr ()
70 { 129 {
71 delete p; 130 delete p;
72 } 131 }
73 132
74 // void because it makes sense in our context 133 void reset (T *a)
75 void operator = (T *a)
76 { 134 {
77 delete p; 135 delete p;
78 p = a; 136 p = a;
79 } 137 }
80 138
139 // void because it makes sense in our context
81 void operator = (auto_ptr &a) 140 void operator =(auto_ptr &a)
82 { 141 {
83 *this = a.p; 142 reset (a.release ());
84 a.p = 0;
85 } 143 }
86 144
87 template<typename A> 145 template<typename A>
88 void operator = (auto_ptr<A> &a) 146 void operator =(auto_ptr<A> &a)
89 { 147 {
90 *this = a.p; 148 reset (a.release ());
91 a.p = 0;
92 } 149 }
93 150
151 T *operator ->() const { return p; }
94 operator T * () const { return p; } 152 T &operator *() const { return *p; }
95 153
154 operator T *() { return p; }
96 T *operator -> () const { return p; } 155 T *get () const { return p; }
97 T &operator * () const { return *p; }
98 156
99 T *get () 157 T *release()
100 { 158 {
101 T *r = p; 159 T *r = p;
102 p = 0; 160 p = 0;
103 return r; 161 return r;
104 } 162 }
105}; 163};
106 164
107typedef auto_ptr<char> auto_str; 165typedef auto_ptr<char> auto_str;
108 166
109struct stringvec : simplevec<char *>
110{
111 ~stringvec ()
112 {
113 for (char **c = begin (); c != end (); c++)
114 delete [] *c;
115 }
116};
117#endif 167#endif
118 168

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines