LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
FlagSet.h
Go to the documentation of this file.
1 
10 #ifndef LARDATAOBJ_UTILITIES_FLAGSET_H
11 #define LARDATAOBJ_UTILITIES_FLAGSET_H
12 
13 // LArSoft headers
14 
16 
17 // C/C++ standard library
18 #include <exception>
19 #include <iosfwd> // std::ostream
20 #include <string>
21 
22 namespace util {
23 
24  namespace flags {
25 
39  template <unsigned int NFlags, typename Storage = details::smallestUInt_t<NFlags>>
40  class FlagSet : public BitMask<Storage> {
41 
42  static_assert(details::computePages<Storage>(NFlags) <= 1,
43  "Too many flags for this storage type.");
44 
45  public:
47 
49 
50  using Base_t = Mask_t;
51 
53  using Bits_t = typename Mask_t::Bits_t;
54 
56  using FlagIndex_t = typename Mask_t::FlagIndex_t;
57 
59  using Flag_t = typename Mask_t::Flag_t;
60 
63 
65  using Exception = typename Mask_t::Exception;
66 
69 
72 
74 
75  FlagSet() = default;
76  FlagSet(This_t const&) = default;
77  FlagSet(This_t&&) = default;
78  FlagSet& operator=(This_t const&) = default;
79  FlagSet& operator=(This_t&&) = default;
80 
82  // This is effectively a copy constructor due to the definition of Mask_t.
83  constexpr FlagSet(Mask_t const& from) : Base_t(from) {}
84 
85  using Base_t::Base_t; // inherit the rest of the constructors
86 
89 
91  constexpr Mask_t const& mask() const { return *this; }
92 
101  constexpr bool isFlag(FlagIndex_t flagIndex) const;
102 
117  constexpr bool isFlag(Flag_t flag) const;
118 
132  bool test(Flag_t flag) const;
133 
147  bool test(FlagIndex_t flagIndex) const;
148 
150 
152  template <typename Stream>
153  void dump(Stream&& out) const
154  {
155  mask().dump(std::forward<Stream>(out), size());
156  }
157 
159  static constexpr size_t size() { return NFlags; }
160 
172  template <typename... Args>
173  static constexpr Mask_t createMask(Args... args)
174  {
175  return Mask_t::create(args...);
176  }
177 
178  private:
180  bool testImpl(Flag_t flag) const;
181 
182  }; // class FlagSet<>
183 
185  template <unsigned int NBits, typename Storage>
186  std::ostream& operator<<(std::ostream& out, FlagSet<NBits, Storage> const& flags)
187  {
188  flags.dump(out);
189  return out;
190  }
191 
192  } // namespace flags
193 
194 } // namespace util
195 
196 //------------------------------------------------------------------------------
197 //--- template implementation
198 //---
199 
200 #include "FlagSet.tcc"
201 
202 //------------------------------------------------------------------------------
203 
204 #endif // LARDATAOBJ_UTILITIES_FLAGSET_H
typename Bits_t::Flag_t Flag_t
Type identifying a single flag.
Definition: BitMask.h:420
void dump(Stream &&out, unsigned int nBits) const
Prints into the specified stream the least nBits significant bits.
Namespace for general, non-LArSoft-specific utilities.
Definition: PIDAAlg.h:26
errors::OutOfRange OutOfRangeError
Out-of-range flag index.
Definition: BitMask.h:429
Class holding flags.
errors::Exception Exception
Generic BitMask exception.
Definition: BitMask.h:426
Base class for exceptions thrown by flag-related utilities.
Definition: BitMask.h:368
errors::FlagNotDefined FlagNotDefinedError
Flag not defined.
Definition: BitMask.h:432
util::flags::Bits_t< Storage_t > Bits_t
Set of bits.
Definition: BitMask.h:417
A class containing a set of flags.
Definition: FlagSet.h:40
constexpr Mask_t const & mask() const
Returns all the flags in the form of a mask.
Definition: FlagSet.h:91
static constexpr Mask_t create(Args...args)
Creates a new BitMask.
util::flags::Index_t FlagIndex_t
Type of index of flag.
Definition: BitMask.h:415
A class containing a set of flags.
Definition: BitMask.h:407
static constexpr Mask_t createMask(Args...args)
Creates a new BitMask.
Definition: FlagSet.h:173
bool test(Flag_t flag) const
Returns if the specified flag is set.
FlagSet & operator=(This_t const &)=default
constexpr FlagSet(Mask_t const &from)
Constructor: copy the specified mask.
Definition: FlagSet.h:83
constexpr bool isFlag(FlagIndex_t flagIndex) const
Returns whether the flag index is valid.
void dump(Stream &&out) const
Dumps on screen only the "official" flags (see size()).
Definition: FlagSet.h:153
Exception thrown to convey that an invalid flag index was used.
Definition: BitMask.h:380
bool testImpl(Flag_t flag) const
Implementation detail of test()
Exception thrown to convey that an undefined flag index was tested.
Definition: BitMask.h:375
BitMask< Storage > Mask_t
Type of bit mask for this flag set.
Definition: FlagSet.h:48
static constexpr size_t size()
Returns the number of flags the set supports.
Definition: FlagSet.h:159