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.63 by root, Sun Dec 14 04:52:10 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 <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#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 10
15template<typename T, typename U> static inline T min (T a, U b) { return a < b ? a : b; } 11// 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; } 12#if __GNUC__
13# define NOTHROW
14# define THROW(x)
15#else
16# define NOTHROW throw()
17# define THROW(x) throw x
18#endif
17 19
18#include "simplevec.h" 20// various utility functions
21template<typename T, typename U> static inline void min_it (T &a, U b) { a = a < (T)b ? a : (T)b; }
22template<typename T, typename U> static inline void max_it (T &a, U b) { a = a > (T)b ? a : (T)b; }
23
24template<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; }
25template<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// linear interpolation
28template<typename T, typename U, typename P>
29static inline T
30lerp (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);
19 37
20template<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
21struct vector : simplevec<T> 63struct stringvec : simplevec<char *>
22{ }; 64{
65 ~stringvec ()
66 {
67 for (char **c = begin (); c != end (); c++)
68 free (*c);
69 }
70};
23 71
24#if 0 72#if 0
25template<typename T> 73template<typename T>
26struct rxvt_vec : simplevec<void *> { 74struct rxvt_vec : simplevec<void *>
75{
27 typedef T *iterator; 76 typedef T *iterator;
28 77
29 void push_back (T d) { simplevec<void *>::push_back ((void *)d); } 78 void push_back (T d) { simplevec<void *>::push_back ((void *)d); }
30 T pop_back () { return (T*)simplevec<void *>::pop_back (); } 79 T pop_back () { return (T*)simplevec<void *>::pop_back (); }
31 void erase (int i) { erase (begin () + i); } 80 void erase (int i) { erase (begin () + i); }
35 T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); } 84 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])); } 85 const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); }
37}; 86};
38#endif 87#endif
39 88
40template <typename I, typename T> 89typedef estl::scoped_array<char> auto_str;
41I find (I first, I last, const T& value)
42{
43 while (first != last && *first != value)
44 ++first;
45 90
46 return first;
47}
48
49template<typename T>
50struct auto_ptr {
51 T *p;
52
53 auto_ptr () : p (0) { }
54 auto_ptr (T *a) : p (a) { }
55
56 auto_ptr (auto_ptr<T> &a)
57 {
58 p = a.p;
59 a.p = 0;
60 }
61
62 template<typename A>
63 auto_ptr (auto_ptr<A> &a)
64 {
65 p = a.p;
66 a.p = 0;
67 }
68
69 ~auto_ptr ()
70 {
71 delete p;
72 }
73
74 // void because it makes sense in our context
75 void operator = (T *a)
76 {
77 delete p;
78 p = a;
79 }
80
81 void operator = (auto_ptr &a)
82 {
83 *this = a.p;
84 a.p = 0;
85 }
86
87 template<typename A>
88 void operator = (auto_ptr<A> &a)
89 {
90 *this = a.p;
91 a.p = 0;
92 }
93
94 operator T * () const { return p; }
95
96 T *operator -> () const { return p; }
97 T &operator * () const { return *p; }
98
99 T *get ()
100 {
101 T *r = p;
102 p = 0;
103 return r;
104 }
105};
106
107typedef auto_ptr<char> auto_str;
108
109struct stringvec : simplevec<char *>
110{
111 ~stringvec ()
112 {
113 for (char **c = begin (); c != end (); c++)
114 delete [] *c;
115 }
116};
117#endif 91#endif
118 92

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines