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.52 by sf-exg, Thu May 24 13:08:08 2012 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 <stdlib.h>
5 static unsigned int e; // at least 32 bits 5#include <string.h>
6public: 6#include "ecb.h"
7 byteorder (); 7#include "estl.h"
8 8
9 static bool big_endian () { return e == 0x11223344; }; 9// increases code size unless -fno-enforce-eh-specs
10 static bool network () { return e == 0x11223344; }; 10#if __GNUC__
11 static bool little_endian () { return e == 0x44332211; }; 11# define NOTHROW
12 static bool vax () { return e == 0x44332211; }; 12# define THROW(x)
13} byteorder; 13#else
14# define NOTHROW throw()
15# define THROW(x) throw x
16#endif
14 17
15template<typename T, typename U> 18// various utility functions
16static inline T min (T a, U b) { return a < b ? a : (T)b; } 19template<typename T, typename U> static inline void min_it (T &a, U b) { a = a < (T)b ? a : (T)b; }
17template<typename T, typename U> 20template<typename T, typename U> static inline void max_it (T &a, U b) { a = a > (T)b ? a : (T)b; }
18static inline T max (T a, U b) { return a > b ? a : (T)b; }
19 21
20#include "simplevec.h" 22template<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; }
23template<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
25template<typename T> static inline T squared_diff (T a, T b) { return (a - b) * (a - b); }
26
27// linear interpolation
28template<typename T, typename U, typename P>
29static inline
30T lerp (T a, U b, P p)
31{
32 return (long(a) * long(100 - p) + long(b) * long(p) + 50) / 100;
33}
34
35// return a very temporary (and never deallocated) buffer. keep small.
36void *rxvt_temp_buf (int len);
21 37
22template<typename T> 38template<typename T>
39static inline T *
40rxvt_temp_buf (int len)
41{
42 return (T *)rxvt_temp_buf (len * sizeof (T));
43}
44
45// in range including end
46#define IN_RANGE_INC(val,beg,end) \
47 ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg))
48
49// 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// for m >= -n, ensure remainder lies between 0..n-1
54#define MOD(m,n) (((m) + (n)) % (n))
55
56// makes dynamically allocated objects zero-initialised
57struct zero_initialized
58{
59 void *operator new (size_t s);
60 void operator delete (void *p, size_t s);
61};
62
23struct vector : simplevec<T> 63struct stringvec : simplevec<char *>
24{ }; 64{
65 ~stringvec ()
66 {
67 for (char **c = begin (); c != end (); c++)
68 free (*c);
69 }
70};
25 71
26#if 0 72#if 0
27template<typename T> 73template<typename T>
28struct rxvt_vec : simplevec<void *> { 74struct rxvt_vec : simplevec<void *>
75{
29 typedef T *iterator; 76 typedef T *iterator;
30 77
31 void push_back (T d) { simplevec<void *>::push_back ((void *)d); } 78 void push_back (T d) { simplevec<void *>::push_back ((void *)d); }
32 T pop_back () { return (T*)simplevec<void *>::pop_back (); } 79 T pop_back () { return (T*)simplevec<void *>::pop_back (); }
33 void erase (int i) { erase (begin () + i); } 80 void erase (int i) { erase (begin () + i); }
37 T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); } 84 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])); } 85 const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); }
39}; 86};
40#endif 87#endif
41 88
42template <typename I, typename T> 89template<typename T>
43I find (I first, I last, const T& value) 90struct auto_ptr
44{ 91{
45 while (first != last && *first != value)
46 ++first;
47
48 return first;
49}
50
51template<typename T>
52struct auto_ptr {
53 T *p; 92 T *p;
54 93
55 auto_ptr () : p (0) { } 94 auto_ptr () : p (0) { }
56 auto_ptr (T *a) : p (a) { } 95 auto_ptr (T *a) : p (a) { }
57 96
68 a.p = 0; 107 a.p = 0;
69 } 108 }
70 109
71 ~auto_ptr () 110 ~auto_ptr ()
72 { 111 {
73 delete p; 112 free (p);
113 }
114
115 void reset (T *a)
116 {
117 free (p);
118 p = a;
74 } 119 }
75 120
76 // void because it makes sense in our context 121 // void because it makes sense in our context
77 void operator = (T *a)
78 {
79 delete p;
80 p = a;
81 }
82
83 void operator = (auto_ptr &a) 122 void operator = (auto_ptr &a)
84 { 123 {
85 *this = a.p; 124 *this = a.p;
86 a.p = 0; 125 a.p = 0;
87 } 126 }
106 } 145 }
107}; 146};
108 147
109typedef auto_ptr<char> auto_str; 148typedef auto_ptr<char> auto_str;
110 149
111struct stringvec : simplevec<char *>
112{
113 ~stringvec ()
114 {
115 for (char **c = begin (); c != end (); c++)
116 delete [] *c;
117 }
118};
119#endif 150#endif
120 151

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines