OpenMS  2.8.0
FlagSet.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2021.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Chris Bielow $
32 // $Authors: Chris Bielow $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
37 #include <OpenMS/CONCEPT/Macros.h> // for OPENMS_PRECONDITION
38 
39 namespace OpenMS
40 {
52  template<class ENUM>
53  class FlagSet
54  {
55  public:
58  : value_(0)
59  {}
60 
62  explicit FlagSet(const ENUM& en)
63  : value_(getPow_(en))
64  {
65  }
66 
67  FlagSet(const FlagSet& stat) = default;
68 
70  FlagSet& operator=(const FlagSet& stat) = default;
71 
73  FlagSet& operator=(const ENUM& en) = delete;
74 
75 
77  ~FlagSet() = default;
78 
80  bool operator==(const FlagSet& stat) const
81  {
82  return (value_ == stat.value_);
83  }
84 
86  FlagSet operator&(const ENUM& en) const
87  {
88  FlagSet s(*this) ;
89  s &= en;
90  return s;
91  }
92 
94  FlagSet operator&(const FlagSet& rhs) const
95  {
96  FlagSet s(*this);
97  s &= rhs;
98  return s;
99  }
100 
102  FlagSet& operator&=(const ENUM& en)
103  {
104  value_ &= getPow_(en);
105  return *this;
106  }
107 
110  {
111  value_ &= rhs.value_;
112  return *this;
113  }
114 
116  FlagSet operator|(const ENUM& en) const
117  {
118  FlagSet s(*this);
119  s.value_ |= getPow_(en);
120  return s;
121  }
123  FlagSet operator|(const FlagSet& rhs) const
124  {
125  FlagSet s(*this);
126  s.value_ |= rhs.value_;
127  return s;
128  }
129 
131  FlagSet& operator|=(const ENUM& en)
132  {
133  value_ |= getPow_(en);
134  return *this;
135  }
136 
139  {
140  value_ |= rhs.value_;
141  return *this;
142  }
143 
145  FlagSet operator+(const ENUM& en) const
146  {
147  return *this | en;
148  }
149 
151  FlagSet operator+(const FlagSet& en) const
152  {
153  return *this | en;
154  }
156  FlagSet& operator+=(const ENUM& rhs)
157  {
158  return *this |= rhs;
159  }
160 
163  {
164  return *this |= rhs;
165  }
166 
169  {
170  FlagSet r(*this);
171  r -= rhs;
172  return r;
173  }
174 
177  {
178  auto overlap = value_ & rhs.value_;
179  value_ ^= overlap; // disable bits which overlap with rhs using XOR
180  return *this;
181  }
182 
184  FlagSet operator-(const ENUM& rhs)
185  {
186  FlagSet r(*this);
187  r -= rhs;
188  return r;
189  }
190 
192  FlagSet& operator-=(const ENUM& rhs)
193  {
194  auto overlap = value_ & FlagSet(rhs).value_;
195  value_ ^= overlap; // disable bits which overlap with rhs using XOR
196  return *this;
197  }
198 
202  bool isSuperSetOf(const FlagSet& required) const
203  {
204  return ((*this | required) == *this);
205  }
206 
210  bool isSuperSetOf(const ENUM& required) const
211  {
212  return ((*this | required) == *this);
213  }
214 
216  bool empty() const
217  {
218  return value_ == 0;
219  }
220 
222  UInt64 value() const
223  {
224  return value_;
225  }
226 
227  private:
229  UInt64 getPow_(const ENUM& en) const
230  {
231  OPENMS_PRECONDITION((int)en >= 0, "Enum value is too small!")
232  OPENMS_PRECONDITION((int)en <= 63, "Enum value is too large!")
233  return UInt64(1) << UInt64(en);
234  }
236  };
237 }
Stores and handles combinations of enum values, e.g. a set of flags as bits flipped in an UInt64.
Definition: FlagSet.h:54
FlagSet operator-(const ENUM &rhs)
remove flag in rhs from this
Definition: FlagSet.h:184
FlagSet & operator+=(const FlagSet &rhs)
bitwise OR= (same as |=)
Definition: FlagSet.h:162
UInt64 getPow_(const ENUM &en) const
computes pow(2, r)
Definition: FlagSet.h:229
FlagSet operator+(const ENUM &en) const
bitwise OR (same as |)
Definition: FlagSet.h:145
UInt64 value_
Definition: FlagSet.h:235
FlagSet & operator-=(const ENUM &rhs)
remove flag in rhs from this
Definition: FlagSet.h:192
bool operator==(const FlagSet &stat) const
Equality.
Definition: FlagSet.h:80
FlagSet & operator=(const FlagSet &stat)=default
Assignment.
bool isSuperSetOf(const ENUM &required) const
Check if this FlagSet has the bit for required.
Definition: FlagSet.h:210
FlagSet operator|(const ENUM &en) const
bitwise OR
Definition: FlagSet.h:116
FlagSet operator&(const FlagSet &rhs) const
bitwise AND
Definition: FlagSet.h:94
FlagSet & operator|=(const ENUM &en)
bitwise OR=
Definition: FlagSet.h:131
FlagSet operator|(const FlagSet &rhs) const
bitwise OR
Definition: FlagSet.h:123
UInt64 value() const
internal representation (mostly for illustrative purposes)
Definition: FlagSet.h:222
FlagSet operator+(const FlagSet &en) const
bitwise OR (same as |)
Definition: FlagSet.h:151
bool isSuperSetOf(const FlagSet &required) const
Check if this FlagSet has at least the active bits of another required FlagSet.
Definition: FlagSet.h:202
bool empty() const
checks if any bit is set
Definition: FlagSet.h:216
FlagSet & operator|=(const FlagSet &rhs)
bitwise OR=
Definition: FlagSet.h:138
FlagSet & operator&=(const FlagSet &rhs)
bitwise AND=
Definition: FlagSet.h:109
FlagSet()
Constructors.
Definition: FlagSet.h:57
FlagSet operator-(const FlagSet &rhs)
remove all flags set in rhs from this
Definition: FlagSet.h:168
FlagSet & operator&=(const ENUM &en)
bitwise AND=
Definition: FlagSet.h:102
FlagSet(const ENUM &en)
C'tor from Enum.
Definition: FlagSet.h:62
FlagSet(const FlagSet &stat)=default
FlagSet & operator=(const ENUM &en)=delete
no Assignment from Enum (would allow implicit conversion)
FlagSet operator&(const ENUM &en) const
bitwise AND
Definition: FlagSet.h:86
FlagSet & operator+=(const ENUM &rhs)
bitwise OR= (same as |=)
Definition: FlagSet.h:156
~FlagSet()=default
Destructor (default)
FlagSet & operator-=(const FlagSet &rhs)
remove all flags set in rhs from this
Definition: FlagSet.h:176
OPENMS_UINT64_TYPE UInt64
Unsigned integer type (64bit)
Definition: Types.h:77
#define OPENMS_PRECONDITION(condition, message)
Precondition macro.
Definition: openms/include/OpenMS/CONCEPT/Macros.h:120
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47