libUTL++
FlagsMI.h
1 #pragma once
2 
4 
5 #include <libutl/Object.h>
6 
8 
9 UTL_NS_BEGIN;
10 
12 // FlagsMI /////////////////////////////////////////////////////////////////////////////////////////
14 
23 
25 class FlagsMI
26 {
27 public:
30  {
31  _flags = 0;
32  }
33 
35  virtual ~FlagsMI()
36  {
37  }
38 
40  void
41  copyFlags(const FlagsMI& rhs)
42  {
43  _flags = rhs._flags;
44  }
45 
52  void
53  copyFlags(const FlagsMI& rhs, uint_t lsb, uint_t msb)
54  {
55  copyFlags(rhs._flags, lsb, msb);
56  }
57 
64  void
65  copyFlags(uint64_t flags, uint_t lsb, uint_t msb)
66  {
67  ASSERTD(lsb <= msb);
68  ASSERTD(msb <= 63);
69  uint64_t mask;
70  if (msb == 63)
71  mask = uint64_t_max;
72  else
73  mask = ((uint64_t)1 << (msb + 1)) - 1;
74  if (lsb > 0) mask &= ~(((uint64_t)1 << lsb) - 1);
75  _flags = (_flags & ~mask) | (flags & mask);
76  }
77 
78  void
79  serializeFlags(Stream& stream, uint_t io, uint_t mode)
80  {
81  utl::serialize(_flags, stream, io, mode);
82  }
83 
89  bool
90  getFlag(uint_t flagNum) const
91  {
92  ASSERTD(flagNum < 64);
93  uint64_t mask = ((uint64_t)1 << flagNum);
94  return ((_flags & mask) != 0);
95  }
96 
102  void
103  setFlag(uint_t flagNum, bool val)
104  {
105  ASSERTD(flagNum < 64);
106  uint64_t mask = ((uint64_t)1 << flagNum);
107  if (val)
108  _flags |= mask;
109  else
110  _flags &= ~mask;
111  }
112 
119  uint64_t
121  {
122  return (_flags & mask) >> shift;
123  }
124 
131  void
133  {
134  _flags &= ~mask;
135  _flags |= (num << shift);
136  }
137 
139  uint64_t
140  getFlags() const
141  {
142  return _flags;
143  }
144 
146  void
148  {
149  _flags = flags;
150  }
151 
152 protected:
153  uint64_t _flags;
154 };
155 
157 // FlagGuard ///////////////////////////////////////////////////////////////////////////////////////
159 
169 
172 {
173 public:
180  FlagGuard(FlagsMI* object, uint_t flagNum, bool val)
181  {
182  // save the original value and toggle the flag
183  _object = object;
184  _flagNum = flagNum;
185  _value = object->getFlag(flagNum);
186  object->setFlag(flagNum, val);
187  }
188 
191  {
192  ASSERTD(_object != nullptr);
193  _object->setFlag(_flagNum, _value);
194  }
195 
196 private:
197  FlagsMI* _object;
198  uint_t _flagNum;
199  bool _value;
200 };
201 
203 
204 UTL_NS_END;
uint64_t getFlagsNumber(uint64_t mask, uint64_t shift=0)
Get a multi-bit value in the flags data (which is stored as one 64-bit integer).
Definition: FlagsMI.h:120
void serialize(bool &b, Stream &stream, uint_t io, uint_t mode=ser_default)
Serialize a boolean.
FlagGuard(FlagsMI *object, uint_t flagNum, bool val)
Constructor.
Definition: FlagsMI.h:180
void setFlag(uint_t flagNum, bool val)
Set a user-defined flag.
Definition: FlagsMI.h:103
void copyFlags(uint64_t flags, uint_t lsb, uint_t msb)
Copy (some of) the given flags.
Definition: FlagsMI.h:65
FlagsMI()
Constructor.
Definition: FlagsMI.h:29
void setFlagsNumber(uint64_t mask, uint64_t shift, uint64_t num)
Set a multi-bit value in the flags data (which is stored as one 64-bit integer).
Definition: FlagsMI.h:132
~FlagGuard()
Restore the previous value of the flag.
Definition: FlagsMI.h:190
uint64_t getFlags() const
Get the flags.
Definition: FlagsMI.h:140
Mix-in to provide 64-bits for space-efficient storage of up to 64 boolean flags.
Definition: FlagsMI.h:25
Flag guard.
Definition: FlagsMI.h:171
virtual ~FlagsMI()
Destructor.
Definition: FlagsMI.h:35
unsigned long uint64_t
Unsigned 64-bit integer.
Definition: types.h:154
void copyFlags(const FlagsMI &rhs, uint_t lsb, uint_t msb)
Copy (some of) the given flags.
Definition: FlagsMI.h:53
unsigned int uint_t
Unsigned integer.
Definition: types.h:59
Stream I/O abstraction.
Definition: Stream.h:68
const uint64_t uint64_t_max
Maximum uint64_t value.
void copyFlags(const FlagsMI &rhs)
Copy the given flags.
Definition: FlagsMI.h:41
void setFlags(uint64_t flags)
Set the flags.
Definition: FlagsMI.h:147
bool getFlag(uint_t flagNum) const
Get a user-defined flag.
Definition: FlagsMI.h:90
#define ASSERTD
Do an assertion in DEBUG mode only.