--- rxvt-unicode/src/rxvtutil.h 2004/08/21 05:32:00 1.3 +++ rxvt-unicode/src/rxvtutil.h 2014/11/12 12:12:02 1.62 @@ -1,126 +1,94 @@ #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; - -template -static inline T min (T a, U b) { return a < b ? a : (T)b; } -template -static inline T max (T a, U b) { return a > b ? a : (T)b; } - -#include "simplevec.h" - -template -struct vector : simplevec -{ }; - -#if 0 -template -struct rxvt_vec : simplevec { - typedef T *iterator; - - void push_back (T d) { simplevec::push_back ((void *)d); } - T pop_back () { return (T*)simplevec::pop_back (); } - void erase (int i) { erase (begin () + i); } - void erase (iterator i) { simplevec::erase ((void **)i); } - iterator begin () const { return (iterator)simplevec::begin (); } - iterator end () const { return (iterator)simplevec::end (); } - T &operator [] (int i) { return * (T *) (& ((* (simplevec *)this)[i])); } - const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec *)this)[i])); } -}; +#include +#include +#include "ecb.h" +#include "estl.h" + +#include "emman.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 -I find (I first, I last, const T& value) +// 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) { - while (first != last && *first != value) - ++first; - - return first; + return (long(a) * long(100 - p) + long(b) * long(p) + 50) / 100; } -template -struct auto_ptr { - T *p; - - auto_ptr () : p (0) { } - auto_ptr (T *a) : p (a) { } - - auto_ptr (auto_ptr &a) - { - p = a.p; - a.p = 0; - } - - template - auto_ptr (auto_ptr &a) - { - p = a.p; - a.p = 0; - } - - ~auto_ptr () - { - delete p; - } - - // void because it makes sense in our context - void operator = (T *a) - { - delete p; - p = a; - } +// return a very temporary (and never deallocated) buffer. keep small. +void *rxvt_temp_buf (int len); - void operator = (auto_ptr &a) - { - *this = a.p; - a.p = 0; - } - - template - void operator = (auto_ptr &a) - { - *this = a.p; - a.p = 0; - } +template +static inline T * +rxvt_temp_buf (int len) +{ + return (T *)rxvt_temp_buf (len * sizeof (T)); +} - operator T * () const { return p; } +// 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)) - T *operator -> () const { return p; } - T &operator * () const { return *p; } +// for m >= -n, ensure remainder lies between 0..n-1 +#define MOD(m,n) (((m) + (n)) % (n)) - T *get () - { - T *r = p; - p = 0; - return r; - } +// makes dynamically allocated objects zero-initialised +struct zero_initialized +{ + void *operator new (size_t s); + void operator delete (void *p, size_t s); }; -typedef auto_ptr auto_str; - struct stringvec : simplevec { ~stringvec () { for (char **c = begin (); c != end (); c++) - delete [] *c; + free (*c); } }; -struct zero_initialized { - void *operator new (size_t s); - void operator delete (void *p, size_t s); +#if 0 +template +struct rxvt_vec : simplevec +{ + typedef T *iterator; + + void push_back (T d) { simplevec::push_back ((void *)d); } + T pop_back () { return (T*)simplevec::pop_back (); } + void erase (int i) { erase (begin () + i); } + void erase (iterator i) { simplevec::erase ((void **)i); } + iterator begin () const { return (iterator)simplevec::begin (); } + iterator end () const { return (iterator)simplevec::end (); } + T &operator [] (int i) { return * (T *) (& ((* (simplevec *)this)[i])); } + const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec *)this)[i])); } }; +#endif + +typedef estl::scoped_array auto_str; #endif