--- rxvt-unicode/src/rxvtutil.h 2008/07/21 16:51:31 1.37 +++ rxvt-unicode/src/rxvtutil.h 2014/10/28 09:05:33 1.60 @@ -1,35 +1,13 @@ #ifndef RXVT_UTIL_H #define RXVT_UTIL_H -#include -#include +#include +#include +#include +#include "ecb.h" +#include "estl.h" -using namespace std; - -#define PP_CONCAT_(a, b) a ## b -#define PP_CONCAT(a, b) PP_CONCAT_(a, b) -#define PP_STRINGIFY_(a) #a -#define PP_STRINGIFY(a) PP_STRINGIFY_(a) - -#define HAVE_GCC_BUILTINS (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ == 4)) - -#if __GNUC__ >= 4 -# define rxvt_attribute(x) __attribute__(x) -# define expect(expr,value) __builtin_expect ((expr),(value)) -#else -# define rxvt_attribute(x) -# define expect(expr,value) (expr) -#endif - -// put into ifs if you are very sure that the expression -// is mostly true or mosty false. note that these return -// booleans, not the expression. -#define expect_false(expr) expect ((expr) != 0, 0) -#define expect_true(expr) expect ((expr) != 0, 1) - -#define NORETURN rxvt_attribute ((noreturn)) -#define UNUSED rxvt_attribute ((unused)) -#define CONST rxvt_attribute ((const)) +#include "emman.h" // increases code size unless -fno-enforce-eh-specs #if __GNUC__ @@ -40,47 +18,23 @@ # define THROW(x) throw x #endif -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; - // various utility functions -template static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } -template static inline void min_it (T &a, U b) { a = a < (T)b ? a : (T)b; } -template static inline T max (T a, U b) { return 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 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 void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } - -template static inline T squared_diff (T a, T b) { return (a-b)*(a-b); } +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) +static inline T +lerp (T a, U b, P p) { return (long(a) * long(100 - p) + long(b) * long(p) + 50) / 100; } -template -I find (I first, I last, const T& value) -{ - while (first != last && *first != value) - ++first; - - return first; -} - // return a very temporary (and never deallocated) buffer. keep small. void *rxvt_temp_buf (int len); @@ -91,16 +45,6 @@ return (T *)rxvt_temp_buf (len * sizeof (T)); } -// some bit functions, xft fuck me plenty -#if HAVE_GCC_BUILTINS -static inline int ctz (unsigned int x) { return __builtin_ctz (x); } -static inline int popcount (unsigned int x) { return __builtin_popcount (x); } -#else -// count trailing zero bits and count # of one bits -int ctz (unsigned int x) CONST; -int popcount (unsigned int x) CONST; -#endif - // in range including end #define IN_RANGE_INC(val,beg,end) \ ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) @@ -113,270 +57,15 @@ #define MOD(m,n) (((m) + (n)) % (n)) // makes dynamically allocated objects zero-initialised -struct zero_initialized { +struct zero_initialized +{ void *operator new (size_t s); void operator delete (void *p, size_t s); }; -/* simplevec taken (and heavily modified), from: - * - * MICO --- a free CORBA implementation - * Copyright (C) 1997-98 Kay Roemer & Arno Puder - */ -template -struct simplevec { - typedef T* iterator; - typedef const T* const_iterator; - typedef unsigned long size_type; - -private: - size_type _last, _size; - T *_buf; - -public: - const_iterator begin () const - { - return &_buf[0]; - } - iterator begin () - { - return &_buf[0]; - } - const_iterator end () const - { - return &_buf[_last]; - } - iterator end () - { - return &_buf[_last]; - } - size_type capacity () const - { - return _size; - } - size_type size () const - { - return _last; - } - -private: - static T *alloc (size_type n) - { - return (T *)::operator new ((size_t) (n * sizeof (T))); - } - static void dealloc (T *buf) - { - if (buf) - ::operator delete (buf); - } - - void reserve (iterator where, size_type n) - { - if (_last + n <= _size) { - memmove (where+n, where, (end ()-where)*sizeof (T)); - } else { - size_type sz = _last+n; - sz = (_size == 0) ? max (sz, 5) : max (sz, 2*_size); - T *nbuf = alloc (sz); - if (_buf) { - memcpy (nbuf, begin (), (where-begin ())*sizeof (T)); - memcpy (nbuf + (where-begin ()) + n, where, - (end ()-where)*sizeof (T)); - dealloc (_buf); - } - _buf = nbuf; - _size = sz; - } - } - -public: - void reserve (size_type sz) - { - if (_size < sz) { - sz = (_size == 0) ? max (sz, 5) : max (sz, 2*_size); - T *nbuf = alloc (sz); - if (_buf) { - memcpy (nbuf, begin (), size ()*sizeof (T)); - dealloc (_buf); - } - _buf = nbuf; - _size = sz; - } - } - simplevec () - : _last(0), _size(0), _buf(0) - { - } - simplevec (size_type n, const T& t = T ()) - : _last(0), _size(0), _buf(0) - { - insert (begin (), n, t); - } - simplevec (const_iterator first, const_iterator last) - : _last(0), _size(0), _buf(0) - { - insert (begin (), first, last); - } - simplevec (const simplevec &v) - : _last(0), _size(0), _buf(0) - { - reserve (v._last); - memcpy (_buf, v.begin (), v.size ()*sizeof (T)); - _last = v._last; - } - simplevec &operator= (const simplevec &v) - { - if (this != &v) { - _last = 0; - reserve (v._last); - memcpy (_buf, v.begin (), v.size ()*sizeof (T)); - _last = v._last; - } - return *this; - } - ~simplevec () - { - dealloc (_buf); - } - const T &front () const - { - //ministl_assert (size () > 0); - return _buf[0]; - } - T &front () - { - //ministl_assert (size () > 0); - return _buf[0]; - } - const T &back () const - { - //ministl_assert (size () > 0); - return _buf[_last-1]; - } - T &back () - { - //ministl_assert (size () > 0); - return _buf[_last-1]; - } - bool empty () const - { - return _last == 0; - } - void clear () - { - _last = 0; - } - void push_back (const T &t) - { - reserve (_last+1); - *end () = t; - ++_last; - } - void push_back (T &t) - { - reserve (_last+1); - *end () = t; - ++_last; - } - void pop_back () - { - //ministl_assert (size () > 0); - --_last; - } - const T &operator[] (size_type idx) const - { - //ministl_assert (idx < size ()); - return _buf[idx]; - } - T &operator[] (size_type idx) - { - //ministl_assert (idx < size ()); - return _buf[idx]; - } - iterator insert (iterator pos, const T &t) - { - //ministl_assert (pos <= end ()); - long at = pos - begin (); - reserve (pos, 1); - pos = begin ()+at; - *pos = t; - ++_last; - return pos; - } - iterator insert (iterator pos, const_iterator first, const_iterator last) - { - //ministl_assert (pos <= end ()); - long n = last - first; - long at = pos - begin (); - if (n > 0) { - reserve (pos, n); - pos = begin ()+at; - memcpy (pos, first, (last-first)*sizeof (T)); - _last += n; - } - return pos; - } - iterator insert (iterator pos, size_type n, const T &t) - { - //ministl_assert (pos <= end ()); - long at = pos - begin (); - if (n > 0) { - reserve (pos, n); - pos = begin ()+at; - for (int i = 0; i < n; ++i) - pos[i] = t; - _last += n; - } - return pos; - } - void erase (iterator first, iterator last) - { - if (last != first) { - memmove (first, last, (end () - last) * sizeof (T)); - _last -= last - first; - } - } - void erase (iterator pos) - { - if (pos != end ()) { - memmove (pos, pos+1, (end () - (pos+1)) * sizeof (T)); - --_last; - } - } - void swap (simplevec &t) - { - ::swap(_last, t._last); - ::swap(_size, t._size); - ::swap(_buf, t._buf); - } -}; - -template -bool operator== (const simplevec &v1, const simplevec &v2) -{ - if (v1.size () != v2.size ()) - return false; - return !v1.size () || !memcmp (&v1[0], &v2[0], v1.size ()*sizeof (T)); -} - -template -bool operator< (const simplevec &v1, const simplevec &v2) -{ - unsigned long minlast = min (v1.size (), v2.size ()); - for (unsigned long i = 0; i < minlast; ++i) { - if (v1[i] < v2[i]) - return true; - if (v2[i] < v1[i]) - return false; - } - return v1.size () < v2.size (); -} - - -template -struct vector : simplevec -{ -}; +// alas new/delete cannot be specified as inline in C++11 (see 17.6.4.6) +void *operator new (size_t s) throw (std::bad_alloc); +void operator delete (void *p) throw (); struct stringvec : simplevec { @@ -389,7 +78,8 @@ #if 0 template -struct rxvt_vec : simplevec { +struct rxvt_vec : simplevec +{ typedef T *iterator; void push_back (T d) { simplevec::push_back ((void *)d); } @@ -404,13 +94,16 @@ #endif template -struct auto_ptr { +struct auto_ptr +{ T *p; - auto_ptr () : p (0) { } + auto_ptr () : p (0) { } + + explicit auto_ptr (T *a) : p (a) { } - auto_ptr (auto_ptr &a) + auto_ptr (auto_ptr &a) { p = a.p; a.p = 0; @@ -428,32 +121,31 @@ delete p; } - // void because it makes sense in our context - void operator = (T *a) + void reset (T *a) { delete p; p = a; } - void operator = (auto_ptr &a) + // void because it makes sense in our context + void operator =(auto_ptr &a) { - *this = a.p; - a.p = 0; + reset (a.release ()); } template - void operator = (auto_ptr &a) + void operator =(auto_ptr &a) { - *this = a.p; - a.p = 0; + reset (a.release ()); } - operator T * () const { return p; } + T *operator ->() const { return p; } + T &operator *() const { return *p; } - T *operator -> () const { return p; } - T &operator * () const { return *p; } + operator T *() { return p; } + T *get () const { return p; } - T *get () + T *release() { T *r = p; p = 0;