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.2 by root, Sun Aug 15 07:20:16 2004 UTC vs.
Revision 1.60 by sf-exg, Tue Oct 28 09:05:33 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> 12// increases code size unless -fno-enforce-eh-specs
16static inline T min (T a, U b) { return a < b ? a : (T)b; } 13#if __GNUC__
17template<typename T, typename U> 14# define NOTHROW
18static inline T max (T a, U b) { return a > b ? a : (T)b; } 15# define THROW(x)
16#else
17# define NOTHROW throw()
18# define THROW(x) throw x
19#endif
19 20
20#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);
21 40
22template<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
66// alas new/delete cannot be specified as inline in C++11 (see 17.6.4.6)
67void *operator new (size_t s) throw (std::bad_alloc);
68void operator delete (void *p) throw ();
69
23struct vector : simplevec<T> 70struct stringvec : simplevec<char *>
24{ }; 71{
72 ~stringvec ()
73 {
74 for (char **c = begin (); c != end (); c++)
75 free (*c);
76 }
77};
25 78
26#if 0 79#if 0
27template<typename T> 80template<typename T>
28struct rxvt_vec : simplevec<void *> { 81struct rxvt_vec : simplevec<void *>
82{
29 typedef T *iterator; 83 typedef T *iterator;
30 84
31 void push_back (T d) { simplevec<void *>::push_back ((void *)d); } 85 void push_back (T d) { simplevec<void *>::push_back ((void *)d); }
32 T pop_back () { return (T*)simplevec<void *>::pop_back (); } 86 T pop_back () { return (T*)simplevec<void *>::pop_back (); }
33 void erase (int i) { erase (begin () + i); } 87 void erase (int i) { erase (begin () + i); }
37 T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); } 91 T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); }
38 const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); } 92 const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); }
39}; 93};
40#endif 94#endif
41 95
42template <typename I, typename T> 96template<typename T>
43I find (I first, I last, const T& value) 97struct auto_ptr
44{ 98{
45 while (first != last && *first != value)
46 ++first;
47
48 return first;
49}
50
51template<typename T>
52struct auto_ptr {
53 T *p; 99 T *p;
54 100
55 auto_ptr () : p (0) { } 101 auto_ptr () : p (0) { }
102
103 explicit
56 auto_ptr (T *a) : p (a) { } 104 auto_ptr (T *a) : p (a) { }
57 105
58 auto_ptr (auto_ptr<T> &a) 106 auto_ptr (auto_ptr &a)
59 { 107 {
60 p = a.p; 108 p = a.p;
61 a.p = 0; 109 a.p = 0;
62 } 110 }
63 111
71 ~auto_ptr () 119 ~auto_ptr ()
72 { 120 {
73 delete p; 121 delete p;
74 } 122 }
75 123
76 // void because it makes sense in our context 124 void reset (T *a)
77 void operator = (T *a)
78 { 125 {
79 delete p; 126 delete p;
80 p = a; 127 p = a;
81 } 128 }
82 129
130 // void because it makes sense in our context
83 void operator = (auto_ptr &a) 131 void operator =(auto_ptr &a)
84 { 132 {
85 *this = a.p; 133 reset (a.release ());
86 a.p = 0;
87 } 134 }
88 135
89 template<typename A> 136 template<typename A>
90 void operator = (auto_ptr<A> &a) 137 void operator =(auto_ptr<A> &a)
91 { 138 {
92 *this = a.p; 139 reset (a.release ());
93 a.p = 0;
94 } 140 }
95 141
142 T *operator ->() const { return p; }
96 operator T * () const { return p; } 143 T &operator *() const { return *p; }
97 144
145 operator T *() { return p; }
98 T *operator -> () const { return p; } 146 T *get () const { return p; }
99 T &operator * () const { return *p; }
100 147
101 T *get () 148 T *release()
102 { 149 {
103 T *r = p; 150 T *r = p;
104 p = 0; 151 p = 0;
105 return r; 152 return r;
106 } 153 }
107}; 154};
108 155
109typedef auto_ptr<char> auto_str; 156typedef auto_ptr<char> auto_str;
110 157
111struct stringvec : simplevec<char *>
112{
113 ~stringvec ()
114 {
115 for (char **c = begin (); c != end (); c++)
116 delete [] *c;
117 }
118};
119#endif 158#endif
120 159

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines