Flag.h 4.35 KB
Newer Older
1
2
3
4
// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
5
// ==  http://www.amdis-fem.org                                              ==
6
7
// ==                                                                        ==
// ============================================================================
8
9
10
11
12
13
14
15
16
17
18
19
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology 
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

/** \file Flagh.h */

#ifndef AMDIS_FLAG_H
#define AMDIS_FLAG_H

#include "Global.h"

namespace AMDiS {

  /** \ingroup Common
   * \brief
   * The Flag class encapsulates flags which represents simple information.
   * Used e.g. while mesh traversal to specify, which elements should be
   * visited.
   */
  class Flag
  {	
  public:
Thomas Witkowski's avatar
Thomas Witkowski committed
39
    /// Constructs a unset Flag
40
    inline Flag() : flags(0) {}
41

Thomas Witkowski's avatar
Thomas Witkowski committed
42
    /// Constructs a Flag initialized by f
43
    inline Flag(const unsigned long f) : flags(f) {}
44

Thomas Witkowski's avatar
Thomas Witkowski committed
45
    /// Copy constructor
46
    inline Flag(const Flag& f) : flags(f.flags) {}
47

Thomas Witkowski's avatar
Thomas Witkowski committed
48
    /// Destructor
49
    inline ~Flag() {}
50

Thomas Witkowski's avatar
Thomas Witkowski committed
51
    /// Compares two Flags
52
53
    inline bool operator==(const Flag& f) const 
    {
Thomas Witkowski's avatar
Thomas Witkowski committed
54
      return (flags == f.flags);
55
    }
56

Thomas Witkowski's avatar
Thomas Witkowski committed
57
    /// Compares two Flags
58
59
    bool operator!=(const Flag& f) const 
    {
Thomas Witkowski's avatar
Thomas Witkowski committed
60
      return !(f == *this);
61
    }
62

Thomas Witkowski's avatar
Thomas Witkowski committed
63
    /// Assignment operator
64
65
    inline Flag& operator=(const Flag& f) 
    {
Thomas Witkowski's avatar
Thomas Witkowski committed
66
67
      if (this != &f) 
	flags = f.flags; 
68
      return *this;
69
    }
70

Thomas Witkowski's avatar
Thomas Witkowski committed
71
    /// Typecast
72
73
    inline operator bool() const 
    { 
Thomas Witkowski's avatar
Thomas Witkowski committed
74
      return isAnySet(); 
75
    }
76

Thomas Witkowski's avatar
Thomas Witkowski committed
77
    /// Set \ref flags 
78
79
    inline void setFlags(const unsigned long f) 
    { 
Thomas Witkowski's avatar
Thomas Witkowski committed
80
      flags = f; 
81
    }
82

Thomas Witkowski's avatar
Thomas Witkowski committed
83
    /// Set \ref flags
84
85
    inline void setFlags(const Flag& f) 
    { 
Thomas Witkowski's avatar
Thomas Witkowski committed
86
      flags = f.flags; 
87
    }
88

Thomas Witkowski's avatar
Thomas Witkowski committed
89
    /// Sets \ref flags to \ref flags | f
90
91
    inline void setFlag(const unsigned long f) 
    { 
Thomas Witkowski's avatar
Thomas Witkowski committed
92
      flags |= f; 
93
    }
94

Thomas Witkowski's avatar
Thomas Witkowski committed
95
    /// Sets \ref flags to \ref flags | f.flags
96
97
    inline void setFlag(const Flag& f) 
    { 
Thomas Witkowski's avatar
Thomas Witkowski committed
98
      flags |= f.flags; 
99
    }
100

Thomas Witkowski's avatar
Thomas Witkowski committed
101
    /// Sets \ref flags to \ref flags & ~f
102
103
    inline void unsetFlag(const unsigned long f) 
    { 
Thomas Witkowski's avatar
Thomas Witkowski committed
104
      flags &= ~f; 
105
    }
106

Thomas Witkowski's avatar
Thomas Witkowski committed
107
    /// Sets \ref flags to \ref flags & ~f.flags
108
109
    inline void unsetFlag(const Flag& f) 
    { 
Thomas Witkowski's avatar
Thomas Witkowski committed
110
      flags &= ~f.flags; 
111
    }
112

113
    inline unsigned long getFlags() const 
114
    { 
Thomas Witkowski's avatar
Thomas Witkowski committed
115
      return flags; 
116
    }
117

Thomas Witkowski's avatar
Thomas Witkowski committed
118
    /// Returns \ref flags | f.flags
119
120
    inline Flag operator+(const Flag& f) const 
    {
121
122
123
      Flag r(flags); 
      r.setFlag(f); 
      return r;
124
    }
125

Thomas Witkowski's avatar
Thomas Witkowski committed
126
    /// Returns \ref flags & ~f.flags
127
128
    inline Flag operator-(const Flag& f) const 
    {
129
130
131
      Flag r(flags); 
      r.unsetFlag(f); 
      return r;
132
    }
133

Thomas Witkowski's avatar
Thomas Witkowski committed
134
    /// Returns \ref flags | f.flags
135
136
    inline Flag operator|(const Flag& f) const 
    {
137
138
139
      Flag r(flags); 
      r.setFlag(f); 
      return r;
140
    }
141

Thomas Witkowski's avatar
Thomas Witkowski committed
142
    /// Returns \ref flags & f.flags
143
144
    inline Flag operator&(const Flag& f) const 
    {
145
      Flag r(flags);
Thomas Witkowski's avatar
Thomas Witkowski committed
146
      r.flags &= f.flags; 
147
      return r;
148
    }
149

Thomas Witkowski's avatar
Thomas Witkowski committed
150
    /// Sets \ref flags to \ref flags &= f.flags
151
152
    inline Flag operator&=(const Flag& f) 
    {
153
154
      flags &= f.flags;
      return *this;
155
    }
156

Thomas Witkowski's avatar
Thomas Witkowski committed
157
    /// Returns \ref flags ^ f.flags
158
159
    inline Flag operator^(const Flag& f) const 
    {
160
      Flag r(flags);
Thomas Witkowski's avatar
Thomas Witkowski committed
161
      r.flags ^= f.flags;
162
      return r;
163
    }
164

Thomas Witkowski's avatar
Thomas Witkowski committed
165
    /// Sets \ref flags to \ref flags & f.flags
166
167
168
    inline Flag& operator|=(const Flag& f) 
    {
      if (this != &f)
Thomas Witkowski's avatar
Thomas Witkowski committed
169
	flags |= f.flags;
170
      return *this;
171
    }
172

Thomas Witkowski's avatar
Thomas Witkowski committed
173
    /// Returns ~\ref flags
174
175
    inline Flag operator~() const 
    { 
176
177
178
      Flag r;
      r.flags = ~flags; 
      return r;
179
    }
180

Thomas Witkowski's avatar
Thomas Witkowski committed
181
    /// Checks whether all set bits of f.flags are set in \ref flags too.
182
183
    inline bool isSet(const Flag& f) const 
    {
184
      return ((flags&f.flags) == f.flags);
185
    }
186

Thomas Witkowski's avatar
Thomas Witkowski committed
187
    /// Returns !\ref isSet(f)
188
189
    inline bool isUnset(const Flag& f) const 
    { 
Thomas Witkowski's avatar
Thomas Witkowski committed
190
      return !isSet(f); 
191
    }
192

Thomas Witkowski's avatar
Thomas Witkowski committed
193
    /// Returns true if \ref flags != 0
194
195
    inline bool isAnySet() const 
    { 
Thomas Witkowski's avatar
Thomas Witkowski committed
196
      return (flags != 0); 
197
    }
198
199

  protected:	
Thomas Witkowski's avatar
Thomas Witkowski committed
200
    /// Internal flag representation
201
202
203
204
205
206
207
208
    unsigned long  flags;
  };

}

#endif  // AMDIS_FLAG_H