| 1 | // ==================================================================== // |
|---|
| 2 | // // |
|---|
| 3 | // File : arbtools.h // |
|---|
| 4 | // Purpose : small general purpose helper classes // |
|---|
| 5 | // // |
|---|
| 6 | // // |
|---|
| 7 | // Coded by Ralf Westram (coder@reallysoft.de) in August 2003 // |
|---|
| 8 | // Copyright Department of Microbiology (Technical University Munich) // |
|---|
| 9 | // // |
|---|
| 10 | // Visit our web site at: http://www.arb-home.de/ // |
|---|
| 11 | // // |
|---|
| 12 | // // |
|---|
| 13 | // ==================================================================== // |
|---|
| 14 | |
|---|
| 15 | #ifndef ARBTOOLS_H |
|---|
| 16 | #define ARBTOOLS_H |
|---|
| 17 | |
|---|
| 18 | #ifndef _GLIBCXX_NEW |
|---|
| 19 | #include <new> |
|---|
| 20 | #endif |
|---|
| 21 | #ifndef CXXFORWARD_H |
|---|
| 22 | #include <cxxforward.h> |
|---|
| 23 | #endif |
|---|
| 24 | #ifndef _GLIBCXX_CMATH |
|---|
| 25 | #include <cmath> |
|---|
| 26 | #endif |
|---|
| 27 | |
|---|
| 28 | // Base class for classes that may not be copied, neither via copy |
|---|
| 29 | // constructor or assignment operator. |
|---|
| 30 | // (Note: see also class RefPtr below) |
|---|
| 31 | |
|---|
| 32 | #ifdef Cxx11 |
|---|
| 33 | struct Noncopyable { |
|---|
| 34 | Noncopyable(const Noncopyable& other) = delete; |
|---|
| 35 | Noncopyable& operator= (const Noncopyable&) = delete; |
|---|
| 36 | Noncopyable() {} |
|---|
| 37 | }; |
|---|
| 38 | #else |
|---|
| 39 | class Noncopyable { |
|---|
| 40 | Noncopyable(const Noncopyable&); |
|---|
| 41 | Noncopyable& operator = (const Noncopyable&); |
|---|
| 42 | public: |
|---|
| 43 | Noncopyable() {} |
|---|
| 44 | }; |
|---|
| 45 | #endif |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | // helper macros to make inplace-reconstruction less obfuscated |
|---|
| 49 | #define INPLACE_RECONSTRUCT(type,this) \ |
|---|
| 50 | do { \ |
|---|
| 51 | (this)->~type(); \ |
|---|
| 52 | ::new(this) type(); \ |
|---|
| 53 | } while(0) |
|---|
| 54 | |
|---|
| 55 | #define INPLACE_COPY_RECONSTRUCT(type,this,other) \ |
|---|
| 56 | do { \ |
|---|
| 57 | (this)->~type(); \ |
|---|
| 58 | ::new(this) type(other); \ |
|---|
| 59 | } while(0) |
|---|
| 60 | |
|---|
| 61 | #define DECLARE_ASSIGNMENT_OPERATOR(T) \ |
|---|
| 62 | T& operator = (const T& other) { \ |
|---|
| 63 | INPLACE_COPY_RECONSTRUCT(T, this, other); \ |
|---|
| 64 | return *this; \ |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | // class to hold a pointer to an object owned by somebody else. |
|---|
| 69 | // (useful to avoid warnings about missing copy-ctor/op=) |
|---|
| 70 | template <typename T> class RefPtr { |
|---|
| 71 | T *ptr; |
|---|
| 72 | public: |
|---|
| 73 | RefPtr(T *ptr_) : ptr(ptr_) {} |
|---|
| 74 | RefPtr(const RefPtr<T>& other) : ptr(other.ptr) {} |
|---|
| 75 | DECLARE_ASSIGNMENT_OPERATOR(RefPtr<T>); |
|---|
| 76 | ~RefPtr() {} |
|---|
| 77 | |
|---|
| 78 | operator T*() const { return ptr; } |
|---|
| 79 | |
|---|
| 80 | const T *operator->() const { return ptr; } |
|---|
| 81 | T *operator->() { return ptr; } |
|---|
| 82 | |
|---|
| 83 | const T& operator*() const { return *ptr; } |
|---|
| 84 | T& operator*() { return *ptr; } |
|---|
| 85 | |
|---|
| 86 | T*& pointer_ref() { return ptr; } |
|---|
| 87 | }; |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | // generic below/above predicates |
|---|
| 91 | template<typename T> |
|---|
| 92 | class isBelow { |
|---|
| 93 | T t; |
|---|
| 94 | public: |
|---|
| 95 | isBelow(T t_) : t(t_) {} |
|---|
| 96 | bool operator()(T o) { return o<t; } |
|---|
| 97 | }; |
|---|
| 98 | |
|---|
| 99 | template<typename T> |
|---|
| 100 | class isAbove { |
|---|
| 101 | T t; |
|---|
| 102 | public: |
|---|
| 103 | isAbove(T t_) : t(t_) {} |
|---|
| 104 | bool operator()(T o) { return o>t; } |
|---|
| 105 | }; |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | // typedef iterator types |
|---|
| 109 | #define DEFINE_NAMED_ITERATORS(type,name) \ |
|---|
| 110 | typedef type::iterator name##Iter; \ |
|---|
| 111 | typedef type::const_iterator name##CIter; \ |
|---|
| 112 | typedef type::reverse_iterator name##RIter; \ |
|---|
| 113 | typedef type::const_reverse_iterator name##CRIter |
|---|
| 114 | |
|---|
| 115 | #define DEFINE_ITERATORS(type) DEFINE_NAMED_ITERATORS(type,type) |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | // locally modify a value, restore on destruction |
|---|
| 119 | template<typename T> |
|---|
| 120 | class LocallyModify : virtual Noncopyable { |
|---|
| 121 | T& var; |
|---|
| 122 | T prevValue; |
|---|
| 123 | public: |
|---|
| 124 | LocallyModify(T& var_, T localValue) : var(var_), prevValue(var) { var = localValue; } |
|---|
| 125 | ~LocallyModify() { var = prevValue; } |
|---|
| 126 | |
|---|
| 127 | T old_value() const { return prevValue; } |
|---|
| 128 | }; |
|---|
| 129 | |
|---|
| 130 | // disallow_type |
|---|
| 131 | // (useful to deny template instantiation with specific types) |
|---|
| 132 | template <typename T, typename U> struct different_types {}; |
|---|
| 133 | template <typename T> struct different_types<T,T>; |
|---|
| 134 | template <typename T, typename U> struct disallow_type { |
|---|
| 135 | // Usage example: |
|---|
| 136 | // disallow_type<T, AW_CL>::here(); |
|---|
| 137 | // where 'T' is a template parameter |
|---|
| 138 | |
|---|
| 139 | different_types<T,U> wrong_type_used; |
|---|
| 140 | static inline void here(){} |
|---|
| 141 | }; |
|---|
| 142 | |
|---|
| 143 | // StrictlyAliased_BasePtrRef allows to pass a 'DERIVED*&' |
|---|
| 144 | // to a function which expects a 'BASE*&' |
|---|
| 145 | // without breaking strict aliasing rules |
|---|
| 146 | template <typename DERIVED, typename BASE> |
|---|
| 147 | class StrictlyAliased_BasePtrRef : virtual Noncopyable { |
|---|
| 148 | DERIVED*& user_ptr; |
|---|
| 149 | BASE *forwarded_ptr; |
|---|
| 150 | |
|---|
| 151 | public: |
|---|
| 152 | StrictlyAliased_BasePtrRef(DERIVED*& ptr) |
|---|
| 153 | : user_ptr(ptr), |
|---|
| 154 | forwarded_ptr(ptr) |
|---|
| 155 | {} |
|---|
| 156 | ~StrictlyAliased_BasePtrRef() { |
|---|
| 157 | user_ptr = static_cast<DERIVED*>(forwarded_ptr); |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | BASE*& forward() { return forwarded_ptr; } |
|---|
| 161 | }; |
|---|
| 162 | |
|---|
| 163 | // NAN/INF checks |
|---|
| 164 | // replace c99 macros isnan, isnormal and isinf. isinf is broken for gcc 4.4.3; see ../CORE/arb_diff.cxx@isinf |
|---|
| 165 | |
|---|
| 166 | template <typename T> CONSTEXPR_INLINE bool is_nan(const T& n) { return n != n; } |
|---|
| 167 | template <typename T> CONSTEXPR_INLINE bool is_nan_or_inf(const T& n) { return is_nan(0*n); } |
|---|
| 168 | template <typename T> CONSTEXPR_INLINE bool is_normal(const T& n) { return n != 0 && !is_nan_or_inf(n); } |
|---|
| 169 | template <typename T> CONSTEXPR_INLINE bool is_inf(const T& n) { return !is_nan(n) && is_nan_or_inf(n); } |
|---|
| 170 | |
|---|
| 171 | CONSTEXPR_INLINE int double_diff_2_cmp(const double d) { |
|---|
| 172 | return d<0 ? -1 : (d>0 ? 1 : 0); |
|---|
| 173 | } |
|---|
| 174 | CONSTEXPR_INLINE int double_cmp(const double d1, const double d2) { |
|---|
| 175 | /*! returns <0 if d1<d2, >0 if d1>d2 (i.e. this function behaves like strcmp) */ |
|---|
| 176 | return double_diff_2_cmp(d1-d2); |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | template <typename NUM> |
|---|
| 180 | CONSTEXPR_INLINE int calc_digits(NUM val) { |
|---|
| 181 | /*! calculate output length of val (w/o sign) */ |
|---|
| 182 | return val ? log10(val)+1 : 1; |
|---|
| 183 | } |
|---|
| 184 | template <typename NUM> |
|---|
| 185 | CONSTEXPR_INLINE int calc_signed_digits(NUM val) { |
|---|
| 186 | /*! calculate output length of val (with sign) */ |
|---|
| 187 | return val<0 ? calc_digits(-val)+1 : calc_digits(val); |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | #else |
|---|
| 191 | #error arbtools.h included twice |
|---|
| 192 | #endif // ARBTOOLS_H |
|---|
| 193 | |
|---|