OpenMS
FlagSet.h
Go to the documentation of this file.
1 // Copyright (c) 2002-present, The OpenMS Team -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Chris Bielow $
6 // $Authors: Chris Bielow $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
11 #include <OpenMS/CONCEPT/Macros.h> // for OPENMS_PRECONDITION
12 
13 namespace OpenMS
14 {
26  template<class ENUM>
27  class FlagSet
28  {
29  public:
32  : value_(0)
33  {}
34 
36  explicit FlagSet(const ENUM& en)
37  : value_(getPow_(en))
38  {
39  }
40 
41  FlagSet(const FlagSet& stat) = default;
42 
44  FlagSet& operator=(const FlagSet& stat) = default;
45 
47  FlagSet& operator=(const ENUM& en) = delete;
48 
49 
51  ~FlagSet() = default;
52 
54  bool operator==(const FlagSet& stat) const
55  {
56  return (value_ == stat.value_);
57  }
58 
60  FlagSet operator&(const ENUM& en) const
61  {
62  FlagSet s(*this) ;
63  s &= en;
64  return s;
65  }
66 
68  FlagSet operator&(const FlagSet& rhs) const
69  {
70  FlagSet s(*this);
71  s &= rhs;
72  return s;
73  }
74 
76  FlagSet& operator&=(const ENUM& en)
77  {
78  value_ &= getPow_(en);
79  return *this;
80  }
81 
83  FlagSet& operator&=(const FlagSet& rhs)
84  {
85  value_ &= rhs.value_;
86  return *this;
87  }
88 
90  FlagSet operator|(const ENUM& en) const
91  {
92  FlagSet s(*this);
93  s.value_ |= getPow_(en);
94  return s;
95  }
97  FlagSet operator|(const FlagSet& rhs) const
98  {
99  FlagSet s(*this);
100  s.value_ |= rhs.value_;
101  return s;
102  }
103 
105  FlagSet& operator|=(const ENUM& en)
106  {
107  value_ |= getPow_(en);
108  return *this;
109  }
110 
113  {
114  value_ |= rhs.value_;
115  return *this;
116  }
117 
119  FlagSet operator+(const ENUM& en) const
120  {
121  return *this | en;
122  }
123 
125  FlagSet operator+(const FlagSet& en) const
126  {
127  return *this | en;
128  }
130  FlagSet& operator+=(const ENUM& rhs)
131  {
132  return *this |= rhs;
133  }
134 
137  {
138  return *this |= rhs;
139  }
140 
143  {
144  FlagSet r(*this);
145  r -= rhs;
146  return r;
147  }
148 
151  {
152  auto overlap = value_ & rhs.value_;
153  value_ ^= overlap; // disable bits which overlap with rhs using XOR
154  return *this;
155  }
156 
158  FlagSet operator-(const ENUM& rhs)
159  {
160  FlagSet r(*this);
161  r -= rhs;
162  return r;
163  }
164 
166  FlagSet& operator-=(const ENUM& rhs)
167  {
168  auto overlap = value_ & FlagSet(rhs).value_;
169  value_ ^= overlap; // disable bits which overlap with rhs using XOR
170  return *this;
171  }
172 
176  bool isSuperSetOf(const FlagSet& required) const
177  {
178  return ((*this | required) == *this);
179  }
180 
184  bool isSuperSetOf(const ENUM& required) const
185  {
186  return ((*this | required) == *this);
187  }
188 
190  bool empty() const
191  {
192  return value_ == 0;
193  }
194 
196  UInt64 value() const
197  {
198  return value_;
199  }
200 
201  private:
203  UInt64 getPow_(const ENUM& en) const
204  {
205  OPENMS_PRECONDITION((int)en >= 0, "Enum value is too small!")
206  OPENMS_PRECONDITION((int)en <= 63, "Enum value is too large!")
207  return UInt64(1) << UInt64(en);
208  }
210  };
211 }
Stores and handles combinations of enum values, e.g. a set of flags as bits flipped in an UInt64.
Definition: FlagSet.h:28
FlagSet operator-(const ENUM &rhs)
remove flag in rhs from this
Definition: FlagSet.h:158
FlagSet & operator+=(const FlagSet &rhs)
bitwise OR= (same as |=)
Definition: FlagSet.h:136
UInt64 getPow_(const ENUM &en) const
computes pow(2, r)
Definition: FlagSet.h:203
FlagSet operator+(const ENUM &en) const
bitwise OR (same as |)
Definition: FlagSet.h:119
UInt64 value_
Definition: FlagSet.h:209
FlagSet & operator-=(const ENUM &rhs)
remove flag in rhs from this
Definition: FlagSet.h:166
bool operator==(const FlagSet &stat) const
Equality.
Definition: FlagSet.h:54
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:184
FlagSet operator|(const ENUM &en) const
bitwise OR
Definition: FlagSet.h:90
FlagSet operator&(const FlagSet &rhs) const
bitwise AND
Definition: FlagSet.h:68
FlagSet & operator|=(const ENUM &en)
bitwise OR=
Definition: FlagSet.h:105
FlagSet operator|(const FlagSet &rhs) const
bitwise OR
Definition: FlagSet.h:97
UInt64 value() const
internal representation (mostly for illustrative purposes)
Definition: FlagSet.h:196
FlagSet operator+(const FlagSet &en) const
bitwise OR (same as |)
Definition: FlagSet.h:125
bool isSuperSetOf(const FlagSet &required) const
Check if this FlagSet has at least the active bits of another required FlagSet.
Definition: FlagSet.h:176
bool empty() const
checks if any bit is set
Definition: FlagSet.h:190
FlagSet & operator|=(const FlagSet &rhs)
bitwise OR=
Definition: FlagSet.h:112
FlagSet & operator&=(const FlagSet &rhs)
bitwise AND=
Definition: FlagSet.h:83
FlagSet()
Constructors.
Definition: FlagSet.h:31
FlagSet operator-(const FlagSet &rhs)
remove all flags set in rhs from this
Definition: FlagSet.h:142
FlagSet & operator&=(const ENUM &en)
bitwise AND=
Definition: FlagSet.h:76
FlagSet(const ENUM &en)
C'tor from Enum.
Definition: FlagSet.h:36
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:60
FlagSet & operator+=(const ENUM &rhs)
bitwise OR= (same as |=)
Definition: FlagSet.h:130
~FlagSet()=default
Destructor (default)
FlagSet & operator-=(const FlagSet &rhs)
remove all flags set in rhs from this
Definition: FlagSet.h:150
uint64_t UInt64
Unsigned integer type (64bit)
Definition: Types.h:47
#define OPENMS_PRECONDITION(condition, message)
Precondition macro.
Definition: openms/include/OpenMS/CONCEPT/Macros.h:94
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19