source: branches/profile/TEMPLATES/arbtools.h

Last change on this file was 12080, checked in by westram, 10 years ago
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.5 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
25//  Base class for classes that may not be copied, neither via copy
26//  constructor or assignment operator.
27
28#ifdef Cxx11
29struct Noncopyable {
30    Noncopyable(const Noncopyable& other) = delete;
31    Noncopyable& operator= (const Noncopyable&) = delete;
32    Noncopyable() {}
33};
34#else
35class Noncopyable {
36    Noncopyable(const Noncopyable&);
37    Noncopyable& operator = (const Noncopyable&);
38public:
39    Noncopyable() {}
40};
41#endif
42
43
44// helper macros to make inplace-reconstruction less obfuscated
45#define INPLACE_RECONSTRUCT(type,this)          \
46    do {                                        \
47        (this)->~type();                        \
48        ::new(this) type();                     \
49    } while(0)
50
51#define INPLACE_COPY_RECONSTRUCT(type,this,other)       \
52    do {                                                \
53        (this)->~type();                                \
54        ::new(this) type(other);                        \
55    } while(0)
56
57#define DECLARE_ASSIGNMENT_OPERATOR(T)                  \
58    T& operator = (const T& other) {                    \
59        INPLACE_COPY_RECONSTRUCT(T, this, other);       \
60        return *this;                                   \
61    }
62
63
64// generic below/above predicates
65template<typename T>
66class isBelow {
67    T t;
68public:
69    isBelow(T t_) : t(t_) {}
70    bool operator()(T o) { return o<t; }
71};
72
73template<typename T>
74class isAbove {
75    T t;
76public:
77    isAbove(T t_) : t(t_) {}
78    bool operator()(T o) { return o>t; }
79};
80
81
82// typedef iterator types
83#define DEFINE_NAMED_ITERATORS(type,name)               \
84    typedef type::iterator name##Iter;                  \
85    typedef type::const_iterator name##CIter;           \
86    typedef type::reverse_iterator name##RIter;         \
87    typedef type::const_reverse_iterator name##CRIter
88
89#define DEFINE_ITERATORS(type) DEFINE_NAMED_ITERATORS(type,type)
90
91
92// locally modify a value, restore on destruction
93template<typename T>
94class LocallyModify : virtual Noncopyable {
95    T& var;
96    T  prevValue;
97public:
98    LocallyModify(T& var_, T localValue) : var(var_), prevValue(var) { var = localValue; }
99    ~LocallyModify() { var = prevValue; }
100
101    T old_value() const { return prevValue; }
102};
103
104
105// StrictlyAliased_BasePtrRef allows to pass a 'DERIVED*&'
106// to a function which expects a 'BASE*&'
107// without breaking strict aliasing rules
108template <typename DERIVED, typename BASE>
109class StrictlyAliased_BasePtrRef : virtual Noncopyable {
110    DERIVED*&  user_ptr;
111    BASE      *forwarded_ptr;
112
113public:
114    StrictlyAliased_BasePtrRef(DERIVED*& ptr)
115        : user_ptr(ptr),
116          forwarded_ptr(ptr)
117    {}
118    ~StrictlyAliased_BasePtrRef() {
119        user_ptr = static_cast<DERIVED*>(forwarded_ptr);
120    }
121
122    BASE*& forward() { return forwarded_ptr; }
123};
124
125// NAN/INF checks
126// replace c99 macros isnan, isnormal and isinf. isinf is broken for gcc 4.4.3; see ../CORE/arb_diff.cxx@isinf
127
128template <typename T> inline bool is_nan(const T& n) { return n != n; }
129template <typename T> inline bool is_nan_or_inf(const T& n) { return is_nan(0*n); }
130template <typename T> inline bool is_normal(const T& n) { return n != 0 && !is_nan_or_inf(n); }
131template <typename T> inline bool is_inf(const T& n) { return !is_nan(n) && is_nan_or_inf(n); }
132
133inline int double_cmp(const double d1, const double d2) {
134    /*! returns <0 if d1<d2, >0 if d1>d2 (i.e. this function behaves like strcmp) */
135    double d = d1-d2;
136    return d<0 ? -1 : (d>0 ? 1 : 0);
137}
138
139#else
140#error arbtools.h included twice
141#endif // ARBTOOLS_H
142
Note: See TracBrowser for help on using the repository browser.