source: tags/ms_r16q3/TEMPLATES/arbtools.h

Last change on this file was 15082, checked in by westram, 8 years ago
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.9 KB
Line 
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
33struct Noncopyable {
34    Noncopyable(const Noncopyable& other) = delete;
35    Noncopyable& operator= (const Noncopyable&) = delete;
36    Noncopyable() {}
37};
38#else
39class Noncopyable {
40    Noncopyable(const Noncopyable&);
41    Noncopyable& operator = (const Noncopyable&);
42public:
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=)
70template <typename T> class RefPtr {
71    T *ptr;
72public:
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
87
88// generic below/above predicates
89template<typename T>
90class isBelow {
91    T t;
92public:
93    isBelow(T t_) : t(t_) {}
94    bool operator()(T o) { return o<t; }
95};
96
97template<typename T>
98class isAbove {
99    T t;
100public:
101    isAbove(T t_) : t(t_) {}
102    bool operator()(T o) { return o>t; }
103};
104
105
106// typedef iterator types
107#define DEFINE_NAMED_ITERATORS(type,name)               \
108    typedef type::iterator name##Iter;                  \
109    typedef type::const_iterator name##CIter;           \
110    typedef type::reverse_iterator name##RIter;         \
111    typedef type::const_reverse_iterator name##CRIter
112
113#define DEFINE_ITERATORS(type) DEFINE_NAMED_ITERATORS(type,type)
114
115
116// locally modify a value, restore on destruction
117template<typename T>
118class LocallyModify : virtual Noncopyable {
119    T& var;
120    T  prevValue;
121public:
122    LocallyModify(T& var_, T localValue) : var(var_), prevValue(var) { var = localValue; }
123    ~LocallyModify() { var = prevValue; }
124
125    T old_value() const { return prevValue; }
126};
127
128// disallow_type
129// (useful to deny template instantiation with specific types)
130template <typename T, typename U> struct different_types {};
131template <typename T> struct different_types<T,T>;
132template <typename T, typename U> struct disallow_type {
133    // Usage example:
134    //     disallow_type<T, AW_CL>::here();
135    // where 'T' is a template parameter
136
137    different_types<T,U> wrong_type_used;
138    static inline void here(){}
139};
140
141// StrictlyAliased_BasePtrRef allows to pass a 'DERIVED*&'
142// to a function which expects a 'BASE*&'
143// without breaking strict aliasing rules
144template <typename DERIVED, typename BASE>
145class StrictlyAliased_BasePtrRef : virtual Noncopyable {
146    DERIVED*&  user_ptr;
147    BASE      *forwarded_ptr;
148
149public:
150    StrictlyAliased_BasePtrRef(DERIVED*& ptr)
151        : user_ptr(ptr),
152          forwarded_ptr(ptr)
153    {}
154    ~StrictlyAliased_BasePtrRef() {
155        user_ptr = static_cast<DERIVED*>(forwarded_ptr);
156    }
157
158    BASE*& forward() { return forwarded_ptr; }
159};
160
161// NAN/INF checks
162// replace c99 macros isnan, isnormal and isinf. isinf is broken for gcc 4.4.3; see ../CORE/arb_diff.cxx@isinf
163
164template <typename T> inline bool is_nan(const T& n) { return n != n; }
165template <typename T> inline bool is_nan_or_inf(const T& n) { return is_nan(0*n); }
166template <typename T> inline bool is_normal(const T& n) { return n != 0 && !is_nan_or_inf(n); }
167template <typename T> inline bool is_inf(const T& n) { return !is_nan(n) && is_nan_or_inf(n); }
168
169inline int double_cmp(const double d1, const double d2) {
170    /*! returns <0 if d1<d2, >0 if d1>d2 (i.e. this function behaves like strcmp) */
171    double d = d1-d2;
172    return d<0 ? -1 : (d>0 ? 1 : 0);
173}
174
175template <typename NUM>
176inline int calc_digits(NUM val) {
177    /*! calculate output length of val (w/o sign) */
178    return val ? log10(val)+1 : 1;
179}
180template <typename NUM>
181inline int calc_signed_digits(NUM val) {
182    /*! calculate output length of val (with sign) */
183    return val<0 ? calc_digits(-val)+1 : calc_digits(val);
184}
185
186#else
187#error arbtools.h included twice
188#endif // ARBTOOLS_H
189
Note: See TracBrowser for help on using the repository browser.