LArSoft  v06_85_00
Liquid Argon Software toolkit - http://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 <iosfwd> // std::ostream
19 #include <string>
20 #include <exception>
21 
22 namespace util {
23 
24  namespace flags {
25 
39  template
40  <unsigned int NFlags, typename Storage = details::smallestUInt_t<NFlags>>
41  class FlagSet: public BitMask<Storage> {
42 
43  static_assert(details::computePages<Storage>(NFlags) <= 1,
44  "Too many flags for this storage type.");
45 
46  public:
48 
50 
51  using Base_t = Mask_t;
52 
54  using Bits_t = typename Mask_t::Bits_t;
55 
57  using FlagIndex_t = typename Mask_t::FlagIndex_t;
58 
60  using Flag_t = typename Mask_t::Flag_t;
61 
62 
65 
67  using Exception = typename Mask_t::Exception;
68 
71 
74 
76 
77 
78  FlagSet() = default;
79  FlagSet(This_t const&) = default;
80  FlagSet(This_t&&) = default;
81  FlagSet& operator= (This_t const&) = default;
82  FlagSet& operator= (This_t&&) = default;
83 
85  // This is effectively a copy constructor due to the definition of Mask_t.
86  constexpr FlagSet(Mask_t const& from): Base_t(from) {}
87 
88 
89  using Base_t::Base_t; // inherit the rest of the constructors
90 
91 
92 
95 
97  constexpr Mask_t const& mask() const { return *this; }
98 
107  constexpr bool isFlag(FlagIndex_t flagIndex) const;
108 
123  constexpr bool isFlag(Flag_t flag) const;
124 
125 
139  bool test(Flag_t flag) const;
140 
154  bool test(FlagIndex_t flagIndex) const;
155 
157 
158 
160  template <typename Stream>
161  void dump(Stream&& out) const
162  { mask().dump(std::forward<Stream>(out), size()); }
163 
164 
166  static constexpr size_t size()
167  { return NFlags; }
168 
169 
181  template <typename... Args>
182  static constexpr Mask_t createMask(Args... args)
183  { return Mask_t::create(args...); }
184 
185  private:
186 
188  bool testImpl(Flag_t flag) const;
189 
190  }; // class FlagSet<>
191 
192 
194  template <unsigned int NBits, typename Storage>
195  std::ostream& operator<<
196  (std::ostream& out, FlagSet<NBits, Storage> const& flags)
197  { flags.dump(out); return out; }
198 
199  } // namespace flags
200 
201 } // namespace util
202 
203 
204 //------------------------------------------------------------------------------
205 //--- template implementation
206 //---
207 
208 #include "FlagSet.tcc"
209 
210 //------------------------------------------------------------------------------
211 
212 
213 #endif // LARDATAOBJ_UTILITIES_FLAGSET_H
typename Bits_t::Flag_t Flag_t
Type identifying a single flag.
Definition: BitMask.h:434
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:17
errors::OutOfRange OutOfRangeError
Out-of-range flag index.
Definition: BitMask.h:443
Class holding flags.
errors::Exception Exception
Generic BitMask exception.
Definition: BitMask.h:440
Base class for exceptions thrown by flag-related utilities.
Definition: BitMask.h:375
errors::FlagNotDefined FlagNotDefinedError
Flag not defined.
Definition: BitMask.h:446
util::flags::Bits_t< Storage_t > Bits_t
Set of bits.
Definition: BitMask.h:431
A class containing a set of flags.
Definition: FlagSet.h:41
constexpr Mask_t const & mask() const
Returns all the flags in the form of a mask.
Definition: FlagSet.h:97
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:429
A class containing a set of flags.
Definition: BitMask.h:420
static constexpr Mask_t createMask(Args...args)
Creates a new BitMask.
Definition: FlagSet.h:182
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:86
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:161
Exception thrown to convey that an invalid flag index was used.
Definition: BitMask.h:390
bool testImpl(Flag_t flag) const
Implementation detail of test()
Exception thrown to convey that an undefined flag index was tested.
Definition: BitMask.h:383
BitMask< Storage > Mask_t
Type of bit mask for this flag set.
Definition: FlagSet.h:49
static constexpr size_t size()
Returns the number of flags the set supports.
Definition: FlagSet.h:166