source: tags/svn.1.5.4/TEMPLATES/arbtools.h

Last change on this file was 8420, checked in by westram, 12 years ago
  • added LocallyModify::old_value()
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.0 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
22//  Base class for classes that may not be copied, neither via copy
23//  constructor or assignment operator.
24
25class Noncopyable {
26    Noncopyable(const Noncopyable&);
27    Noncopyable& operator=(const Noncopyable&);
28public:
29    Noncopyable() {}
30};
31
32// helper macros to make inplace-reconstruction less obfuscated
33#define INPLACE_RECONSTRUCT(type,this)          \
34    do {                                        \
35        (this)->~type();                        \
36        ::new(this) type();                     \
37    } while(0)
38
39#define INPLACE_COPY_RECONSTRUCT(type,this,other)       \
40    do {                                                \
41        (this)->~type();                                \
42        ::new(this) type(other);                        \
43    } while(0)
44
45#define DECLARE_ASSIGNMENT_OPERATOR(T)                  \
46    T& operator = (const T& other) {                    \
47        INPLACE_COPY_RECONSTRUCT(T, this, other);       \
48        return *this;                                   \
49    }
50
51
52template<typename T>
53class isBelow {
54    T t;
55public:
56    isBelow(T t_) : t(t_) {}
57    bool operator()(T o) { return o<t; }
58};
59
60template<typename T>
61class isAbove {
62    T t;
63public:
64    isAbove(T t_) : t(t_) {}
65    bool operator()(T o) { return o>t; }
66};
67
68#define DEFINE_NAMED_ITERATORS(type,name)               \
69    typedef type::iterator name##Iter;                  \
70    typedef type::const_iterator name##CIter;           \
71    typedef type::reverse_iterator name##RIter;         \
72    typedef type::const_reverse_iterator name##CRIter
73
74#define DEFINE_ITERATORS(type) DEFINE_NAMED_ITERATORS(type,type)
75
76template<typename T>
77class LocallyModify {
78    T& var;
79    T  prevValue;
80public:
81    LocallyModify(T& var_, T localValue) : var(var_), prevValue(var) { var = localValue; }
82    ~LocallyModify() { var = prevValue; }
83
84    T old_value() const { return prevValue; }
85};
86
87#else
88#error arbtools.h included twice
89#endif // ARBTOOLS_H
90
Note: See TracBrowser for help on using the repository browser.