--- rxvt-unicode/src/rxvtutil.h 2004/08/15 00:37:04 1.1 +++ rxvt-unicode/src/rxvtutil.h 2012/05/25 07:38:05 1.54 @@ -1,29 +1,78 @@ #ifndef RXVT_UTIL_H #define RXVT_UTIL_H -extern class byteorder { - static unsigned int e; // at least 32 bits -public: - byteorder (); - - static bool big_endian () { return e == 0x11223344; }; - static bool network () { return e == 0x11223344; }; - static bool little_endian () { return e == 0x44332211; }; - static bool vax () { return e == 0x44332211; }; -} byteorder; +#include +#include +#include "ecb.h" +#include "estl.h" + +// increases code size unless -fno-enforce-eh-specs +#if __GNUC__ +# define NOTHROW +# define THROW(x) +#else +# define NOTHROW throw() +# define THROW(x) throw x +#endif -template static inline T min (T a, U b) { return a < b ? a : b; } -template static inline T max (T a, U b) { return a > b ? a : b; } +// various utility functions +template static inline void min_it (T &a, U b) { a = a < (T)b ? a : (T)b; } +template static inline void max_it (T &a, U b) { a = a > (T)b ? a : (T)b; } + +template static inline T clamp (T v, U a, V b) { return v < (T)a ? a : v >(T)b ? b : v; } +template static inline void clamp_it (T &v, U a, V b) { v = v < (T)a ? a : v >(T)b ? b : v; } + +template static inline T squared_diff (T a, T b) { return (a - b) * (a - b); } + +// linear interpolation +template +static inline +T lerp (T a, U b, P p) +{ + return (long(a) * long(100 - p) + long(b) * long(p) + 50) / 100; +} -#include "simplevec.h" +// return a very temporary (and never deallocated) buffer. keep small. +void *rxvt_temp_buf (int len); template -struct vector : simplevec -{ }; +static inline T * +rxvt_temp_buf (int len) +{ + return (T *)rxvt_temp_buf (len * sizeof (T)); +} + +// in range including end +#define IN_RANGE_INC(val,beg,end) \ + ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) + +// in range excluding end +#define IN_RANGE_EXC(val,beg,end) \ + ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) + +// for m >= -n, ensure remainder lies between 0..n-1 +#define MOD(m,n) (((m) + (n)) % (n)) + +// makes dynamically allocated objects zero-initialised +struct zero_initialized +{ + void *operator new (size_t s); + void operator delete (void *p, size_t s); +}; + +struct stringvec : simplevec +{ + ~stringvec () + { + for (char **c = begin (); c != end (); c++) + free (*c); + } +}; #if 0 template -struct rxvt_vec : simplevec { +struct rxvt_vec : simplevec +{ typedef T *iterator; void push_back (T d) { simplevec::push_back ((void *)d); } @@ -37,17 +86,9 @@ }; #endif -template -I find (I first, I last, const T& value) -{ - while (first != last && *first != value) - ++first; - - return first; -} - template -struct auto_ptr { +struct auto_ptr +{ T *p; auto_ptr () : p (0) { } @@ -68,16 +109,16 @@ ~auto_ptr () { - delete p; + free (p); } - // void because it makes sense in our context - void operator = (T *a) + void reset (T *a) { - delete p; + free (p); p = a; } + // void because it makes sense in our context void operator = (auto_ptr &a) { *this = a.p; @@ -106,13 +147,5 @@ typedef auto_ptr auto_str; -struct stringvec : simplevec -{ - ~stringvec () - { - for (char **c = begin (); c != end (); c++) - delete [] *c; - } -}; #endif