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.3 by root, Sat Aug 21 05:32:00 2004 UTC vs.
Revision 1.64 by root, Tue Jun 25 03:35:01 2019 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:
7 byteorder ();
8 6
9 static bool big_endian () { return e == 0x11223344; }; 7#define ECB_NO_THREADS 1
10 static bool network () { return e == 0x11223344; }; 8#include "ecb.h"
11 static bool little_endian () { return e == 0x44332211; };
12 static bool vax () { return e == 0x44332211; };
13} byteorder;
14 9
15template<typename T, typename U> 10#include "estl.h"
16static inline T min (T a, U b) { return a < b ? a : (T)b; }
17template<typename T, typename U>
18static inline T max (T a, U b) { return a > b ? a : (T)b; }
19 11
20#include "simplevec.h" 12#include "emman.h"
13
14// increases code size unless -fno-enforce-eh-specs
15#if __GNUC__
16# define NOTHROW
17# define THROW(x)
18#else
19# define NOTHROW throw()
20# define THROW(x) throw x
21#endif
22
23// various utility functions
24template<typename T, typename U> static inline void min_it (T &a, U b) { a = a < (T)b ? a : (T)b; }
25template<typename T, typename U> static inline void max_it (T &a, U b) { a = a > (T)b ? a : (T)b; }
26
27template<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; }
28template<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; }
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
23struct vector : simplevec<T> 66struct stringvec : simplevec<char *>
24{ }; 67{
68 ~stringvec ()
69 {
70 for (char **c = begin (); c != end (); c++)
71 free (*c);
72 }
73};
25 74
26#if 0 75#if 0
27template<typename T> 76template<typename T>
28struct rxvt_vec : simplevec<void *> { 77struct rxvt_vec : simplevec<void *>
78{
29 typedef T *iterator; 79 typedef T *iterator;
30 80
31 void push_back (T d) { simplevec<void *>::push_back ((void *)d); } 81 void push_back (T d) { simplevec<void *>::push_back ((void *)d); }
32 T pop_back () { return (T*)simplevec<void *>::pop_back (); } 82 T pop_back () { return (T*)simplevec<void *>::pop_back (); }
33 void erase (int i) { erase (begin () + i); } 83 void erase (int i) { erase (begin () + i); }
37 T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); } 87 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])); } 88 const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); }
39}; 89};
40#endif 90#endif
41 91
42template <typename I, typename T> 92typedef estl::scoped_array<char> auto_str;
43I find (I first, I last, const T& value)
44{
45 while (first != last && *first != value)
46 ++first;
47
48 return first;
49}
50
51template<typename T>
52struct auto_ptr {
53 T *p;
54
55 auto_ptr () : p (0) { }
56 auto_ptr (T *a) : p (a) { }
57
58 auto_ptr (auto_ptr<T> &a)
59 {
60 p = a.p;
61 a.p = 0;
62 }
63
64 template<typename A>
65 auto_ptr (auto_ptr<A> &a)
66 {
67 p = a.p;
68 a.p = 0;
69 }
70
71 ~auto_ptr ()
72 {
73 delete p;
74 }
75
76 // 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)
84 {
85 *this = a.p;
86 a.p = 0;
87 }
88
89 template<typename A>
90 void operator = (auto_ptr<A> &a)
91 {
92 *this = a.p;
93 a.p = 0;
94 }
95
96 operator T * () const { return p; }
97
98 T *operator -> () const { return p; }
99 T &operator * () const { return *p; }
100
101 T *get ()
102 {
103 T *r = p;
104 p = 0;
105 return r;
106 }
107};
108
109typedef auto_ptr<char> auto_str;
110
111struct stringvec : simplevec<char *>
112{
113 ~stringvec ()
114 {
115 for (char **c = begin (); c != end (); c++)
116 delete [] *c;
117 }
118};
119
120struct zero_initialized {
121 void *operator new (size_t s);
122 void operator delete (void *p, size_t s);
123};
124 93
125#endif 94#endif
126 95

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines