ARB
arbtools.h
Go to the documentation of this file.
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:
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) {}
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 
89  const T* plain_pointer() const { return ptr; }
90 };
91 
92 
93 // generic below/above predicates
94 template<typename T>
95 class isBelow {
96  T t;
97 public:
98  isBelow(T t_) : t(t_) {}
99  bool operator()(T o) { return o<t; }
100 };
101 
102 template<typename T>
103 class isAbove {
104  T t;
105 public:
106  isAbove(T t_) : t(t_) {}
107  bool operator()(T o) { return o>t; }
108 };
109 
110 
111 // typedef iterator types
112 #define DEFINE_NAMED_ITERATORS(type,name) \
113  typedef type::iterator name##Iter; \
114  typedef type::const_iterator name##CIter; \
115  typedef type::reverse_iterator name##RIter; \
116  typedef type::const_reverse_iterator name##CRIter
117 
118 #define DEFINE_ITERATORS(type) DEFINE_NAMED_ITERATORS(type,type)
119 
120 // macro to cast custom function signatures into generic signatures
121 // (allowing to suppress gcc 8.1++ warnings provided by -Wcast-function-type)
122 // sync with ../AISC_COM/C/aisc_global.h@AISC_CASTSIG
123 #define CASTSIG(sig,cb) ((sig)((void*)(cb)))
124 
125 // locally modify a value, restore on destruction
126 template<typename T>
127 class LocallyModify : virtual Noncopyable {
128  T& var;
129  T prevValue;
130 public:
131  // common pitfall: LocallyModify<int>(variable);
132  // wanted: LocallyModify<int> NAME(variable);
133 
134  LocallyModify(T& var_, T localValue) : var(var_), prevValue(var) { var = localValue; } // protect + change
135  explicit LocallyModify(T& var_) : var(var_), prevValue(var) {} // protect only
136  ~LocallyModify() { var = prevValue; }
137 
138  T old_value() const { return prevValue; }
139 };
140 
141 // disallow_type
142 // (useful to deny template instantiation with specific types)
143 template <typename T, typename U> struct different_types {};
144 template <typename T> struct different_types<T,T>;
145 template <typename T, typename U> struct disallow_type {
146  // Usage example:
147  // disallow_type<T, AW_CL>::here();
148  // where 'T' is a template parameter
149 
151  static inline void here(){}
152 };
153 
154 // StrictlyAliased_BasePtrRef allows to pass a 'DERIVED*&'
155 // to a function which expects a 'BASE*&'
156 // without breaking strict aliasing rules
157 template <typename DERIVED, typename BASE>
159  DERIVED*& user_ptr;
160  BASE *forwarded_ptr;
161 
162 public:
164  : user_ptr(ptr),
165  forwarded_ptr(ptr)
166  {}
168  user_ptr = static_cast<DERIVED*>(forwarded_ptr);
169  }
170 
171  BASE*& forward() { return forwarded_ptr; }
172 };
173 
174 // NAN/INF checks
175 // replace c99 macros isnan, isnormal and isinf. isinf is broken for gcc 4.4.3; see ../CORE/arb_diff.cxx@isinf
176 
177 template <typename T> CONSTEXPR_INLINE bool is_nan(const T& n) { return n != n; }
178 template <typename T> CONSTEXPR_INLINE bool is_nan_or_inf(const T& n) { return is_nan(0*n); }
179 template <typename T> CONSTEXPR_INLINE bool is_normal(const T& n) { return n != 0 && !is_nan_or_inf(n); }
180 template <typename T> CONSTEXPR_INLINE bool is_inf(const T& n) { return !is_nan(n) && is_nan_or_inf(n); }
181 
182 CONSTEXPR_INLINE int double_diff_2_cmp(const double d) {
183  return d<0 ? -1 : (d>0 ? 1 : 0);
184 }
185 CONSTEXPR_INLINE int double_cmp(const double d1, const double d2) {
187  return double_diff_2_cmp(d1-d2);
188 }
189 CONSTEXPR_INLINE int float_diff_2_cmp(const float f) {
190  return f<0 ? -1 : (f>0 ? 1 : 0);
191 }
192 CONSTEXPR_INLINE int float_cmp(const float f1, const float f2) {
194  return float_diff_2_cmp(f1-f2);
195 }
197  return L<0 ? -1 : (L>0 ? 1 : 0);
198 }
199 CONSTEXPR_INLINE int long_cmp(const long L1, const long L2) {
201  return long_diff_2_cmp(L1-L2);
202 }
203 
204 template <typename NUM>
207  return val ? log10(val)+1 : 1;
208 }
209 template <typename NUM>
212  return val<0 ? calc_digits(-val)+1 : calc_digits(val);
213 }
214 
215 #else
216 #error arbtools.h included twice
217 #endif // ARBTOOLS_H
218 
LocallyModify(T &var_)
Definition: arbtools.h:135
Noncopyable()
Definition: arbtools.h:43
CONSTEXPR_INLINE int calc_signed_digits(NUM val)
Definition: arbtools.h:210
CONSTEXPR_INLINE int calc_digits(NUM val)
Definition: arbtools.h:205
CONSTEXPR_INLINE bool is_nan(const T &n)
Definition: arbtools.h:177
CONSTEXPR_INLINE bool is_normal(const T &n)
Definition: arbtools.h:179
const T * plain_pointer() const
convert RefPtr to plain old pointer
Definition: arbtools.h:89
isAbove(T t_)
Definition: arbtools.h:106
T *& pointer_ref()
Definition: arbtools.h:86
CONSTEXPR_INLINE bool is_nan_or_inf(const T &n)
Definition: arbtools.h:178
CONSTEXPR_INLINE int long_diff_2_cmp(const long L)
Definition: arbtools.h:196
isBelow(T t_)
Definition: arbtools.h:98
RefPtr(T *ptr_)
Definition: arbtools.h:73
CONSTEXPR_INLINE bool is_inf(const T &n)
Definition: arbtools.h:180
bool operator()(T o)
Definition: arbtools.h:107
CONSTEXPR_INLINE int float_cmp(const float f1, const float f2)
Definition: arbtools.h:192
T * operator->()
Definition: arbtools.h:81
RefPtr(const RefPtr< T > &other)
Definition: arbtools.h:74
CONSTEXPR_INLINE int float_diff_2_cmp(const float f)
Definition: arbtools.h:189
#define CONSTEXPR_INLINE
Definition: cxxforward.h:111
const T & operator*() const
Definition: arbtools.h:83
T & operator*()
Definition: arbtools.h:84
DECLARE_ASSIGNMENT_OPERATOR(RefPtr< T >)
static void here()
Definition: arbtools.h:151
bool operator()(T o)
Definition: arbtools.h:99
StrictlyAliased_BasePtrRef(DERIVED *&ptr)
Definition: arbtools.h:163
const T * operator->() const
Definition: arbtools.h:80
~RefPtr()
Definition: arbtools.h:76
T old_value() const
Definition: arbtools.h:138
Definition: trnsprob.h:20
different_types< T, U > wrong_type_used
Definition: arbtools.h:150
CONSTEXPR_INLINE int double_cmp(const double d1, const double d2)
Definition: arbtools.h:185
LocallyModify(T &var_, T localValue)
Definition: arbtools.h:134
CONSTEXPR_INLINE int double_diff_2_cmp(const double d)
Definition: arbtools.h:182
CONSTEXPR_INLINE int long_cmp(const long L1, const long L2)
Definition: arbtools.h:199