source: tags/arb-6.0-rc1/TEMPLATES/arbtools.h

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