flags.hpp
Go to the documentation of this file.
1 
6 /*****************************************************************************
7  ** Ifdefs
8  *****************************************************************************/
9 
10 #ifndef ECL_UTILITIES_FLAGS_HPP_
11 #define ECL_UTILITIES_FLAGS_HPP_
12 
13 /*****************************************************************************
14  ** Includes
15  *****************************************************************************/
16 
17 /*****************************************************************************
18  ** Namespaces
19  *****************************************************************************/
20 
21 namespace ecl
22 {
23 
24 /*****************************************************************************
25  ** Classes
26  *****************************************************************************/
27 
28 enum Bits
29 {
30  Bit0 = 0x0000, Bit1 = 0x0001, Bit2 = 0x0002, Bit3 = 0x0004, Bit4 = 0x0008, Bit5 = 0x0010, Bit6 = 0x0020,
31  Bit7 = 0x0040, Bit8 = 0x0080, Bit9 = 0x0100, Bit10 = 0x0200, Bit11 = 0x0400, Bit12 = 0x0800, Bit13 = 0x1000,
32  Bit14 = 0x2000, Bit15 = 0x4000, Bit16 = 0x8000,
33 };
64 template<typename Enum>
65  class Flags
66  {
67  public:
68  /*********************
69  ** C&D
70  **********************/
71  Flags() : value(0) {};
72  Flags(const Flags<Enum> & other) : value(other.value) {};
73  Flags(Enum flag) : value(flag) {};
74  ~Flags() { }
75  ;
76 
77  /*********************
78  ** Utility
79  **********************/
80  void clear() { value = 0; }
84  bool testFlag(Enum flag) const
85  {
86  if ((value & flag) != 0)
87  {
88  return true;
89  }
90  else
91  {
92  if ( ( flag == 0 ) && ( value == 0 ) ) {
93  return true;
94  } else {
95  return false;
96  }
97  }
98  }
99  ;
100 
101  /*********************
102  ** Type compatibility
103  **********************/
104  operator int() const
105  {
106  return value;
107  }
108  ;
110  /******************************************
111  ** Return new instance
112  *******************************************/
117  Flags<Enum> operator&(int mask) const
118  {
119  Flags<Enum> f;
120  f.value = value & mask;
121  return f;
122  }
123  ;
128  Flags<Enum> operator&(Enum flag) const
129  {
130  Flags<Enum> f;
131  f.value = value & flag;
132  return f;
133  }
134  ;
139  Flags<Enum> operator|(Flags<Enum> other) const
140  {
141  Flags<Enum> f;
142  f.value = value | other.value;
143  return f;
144  }
145  ;
150  Flags<Enum> operator|(Enum flag) const
151  {
152  Flags<Enum> f;
153  f.value = value | flag;
154  return f;
155  }
156  ;
161  Flags<Enum> operator^(Flags<Enum> other) const
162  {
163  Flags<Enum> f;
164  f.value = value ^ other.value;
165  return f;
166  }
167  ;
172  Flags<Enum> operator^(Enum flag) const
173  {
174  Flags<Enum> f;
175  f.value = value ^ flag;
176  return f;
177  }
178  ;
183  Flags<Enum> operator~() const
184  {
185  Flags<Enum> f;
186  f.value = ~value;
187  return f;
188  }
189  ;
190 
191  /******************************************
192  ** Update current instance
193  *******************************************/
198  Flags<Enum> & operator=(const Flags<Enum> & other)
199  {
200  value = other.value;
201  return *this;
202  }
203  ;
208  Flags<Enum> & operator=(const Enum & flag)
209  {
210  value = static_cast<int>(flag);
211  return *this;
212  }
213  ;
218  Flags<Enum> & operator&=(int mask)
219  {
220  value &= mask;
221  return *this;
222  }
223  ;
228  Flags<Enum> & operator|=(Flags<Enum> other)
229  {
230  value |= other;
231  return *this;
232  }
233  ;
238  Flags<Enum> & operator|=(Enum flag)
239  {
240  value |= flag;
241  return *this;
242  }
243  ;
248  Flags<Enum> & operator^=(Flags<Enum> other)
249  {
250  value ^= other;
251  return *this;
252  }
253  ;
258  Flags<Enum> & operator^=(Enum flag)
259  {
260  value ^= flag;
261  return *this;
262  }
263  ;
264 
265  /*********************
266  ** Helper Methods
267  **********************/
272  friend inline Flags<Enum> operator|(Enum flag, Flags<Enum> flags)
273  {
274  return flags | flag;
275  }
276  private:
277  int value;
278  };
279 
280 } // namespace ecl
281 
282 #endif /* ECL_UTILITIES_FLAGS_HPP_ */
ecl::Bit10
@ Bit10
Definition: flags.hpp:45
ecl::Flags::operator~
Flags< Enum > operator~() const
Definition: flags.hpp:189
ecl::Bit1
@ Bit1
Definition: flags.hpp:44
ecl::Bit4
@ Bit4
Definition: flags.hpp:44
ecl::Bits
Bits
Definition: flags.hpp:34
ecl::Flags::operator=
Flags< Enum > & operator=(const Flags< Enum > &other)
Definition: flags.hpp:204
ecl::Bit12
@ Bit12
Definition: flags.hpp:45
ecl::Bit0
@ Bit0
Definition: flags.hpp:44
ecl::Bit8
@ Bit8
Definition: flags.hpp:45
ecl::Flags::value
int value
Definition: flags.hpp:283
ecl::Bit2
@ Bit2
Definition: flags.hpp:44
ecl::Flags::~Flags
~Flags()
Definition: flags.hpp:80
ecl::Bit13
@ Bit13
Definition: flags.hpp:45
ecl::Bit14
@ Bit14
Definition: flags.hpp:46
ecl::Flags::operator&=
Flags< Enum > & operator&=(int mask)
Definition: flags.hpp:224
ecl::Flags::testFlag
bool testFlag(Enum flag) const
Definition: flags.hpp:90
ecl::Flags::operator|=
Flags< Enum > & operator|=(Flags< Enum > other)
Definition: flags.hpp:234
ecl::Bit7
@ Bit7
Definition: flags.hpp:45
ecl::Bit15
@ Bit15
Definition: flags.hpp:46
ecl::Flags::operator|
Flags< Enum > operator|(Flags< Enum > other) const
Definition: flags.hpp:145
ecl::Flags::operator^
Flags< Enum > operator^(Flags< Enum > other) const
Definition: flags.hpp:167
ecl::Bit11
@ Bit11
Definition: flags.hpp:45
ecl::Flags
Convenience class for organising boolean flags.
Definition: flags.hpp:71
ecl::Bit6
@ Bit6
Definition: flags.hpp:44
ecl::Bit3
@ Bit3
Definition: flags.hpp:44
ecl::Bit16
@ Bit16
Definition: flags.hpp:46
ecl::Bit9
@ Bit9
Definition: flags.hpp:45
ecl::Bit5
@ Bit5
Definition: flags.hpp:44
ecl
Embedded control libraries.
ecl::Flags::operator^=
Flags< Enum > & operator^=(Flags< Enum > other)
Definition: flags.hpp:254
ecl::Flags::operator&
Flags< Enum > operator&(int mask) const
Definition: flags.hpp:123
ecl::Flags::Flags
Flags()
Definition: flags.hpp:77


ecl_utilities
Author(s): Daniel Stonier
autogenerated on Sun Aug 2 2020 03:12:13