LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
util::flags Namespace Reference

Classes and functions to manage bit masks and flags. More...

Namespaces

 details
 
 errors
 Namespace enclosing BitMask exceptions.
 

Classes

class  BitMask
 A class containing a set of flags. More...
 
struct  BitMaskFromValuesTag
 Type for constructor tag from values. More...
 
struct  Bits_t
 Type identifying a set of bits. More...
 
struct  Flag_t
 Type identifying a flag. Operations are implemented as free functions. More...
 
class  FlagSet
 A class containing a set of flags. More...
 

Typedefs

using Index_t = unsigned int
 Type to denote the index of the flag. More...
 

Functions

template<typename Storage >
std::ostream & operator<< (std::ostream &out, Flag_t< Storage > flag)
 Output of a flag into a stream (prints its index). More...
 
template<typename Storage >
std::string to_string (Flag_t< Storage > const flag)
 Convert a flag into a stream (shows its index). More...
 
template<typename Stream , typename Storage >
Stream & operator<< (Stream &&out, BitMask< Storage > const &mask)
 Output of a bit mask into a stream. More...
 
template<typename Storage >
constexpr BitMask< Storage > makeMask (Bits_t< Storage > bits)
 Constructs a mask from bits. More...
 
template<unsigned int NBits, typename Storage >
std::ostream & operator<< (std::ostream &out, FlagSet< NBits, Storage > const &flags)
 Output of a flag set into a stream. More...
 
Comparison operators for flags (based on the index)
template<typename Storage >
constexpr bool operator== (Index_t left, Flag_t< Storage > right)
 
template<typename Storage >
constexpr bool operator== (Flag_t< Storage > left, Index_t right)
 
template<typename Storage >
constexpr bool operator!= (Index_t left, Flag_t< Storage > right)
 
template<typename Storage >
constexpr bool operator!= (Flag_t< Storage > left, Index_t right)
 
template<typename Storage >
constexpr bool operator< (Index_t left, Flag_t< Storage > right)
 
template<typename Storage >
constexpr bool operator< (Flag_t< Storage > left, Index_t right)
 
template<typename Storage >
constexpr bool operator> (Index_t left, Flag_t< Storage > right)
 
template<typename Storage >
constexpr bool operator> (Flag_t< Storage > left, Index_t right)
 
template<typename Storage >
constexpr bool operator<= (Index_t left, Flag_t< Storage > right)
 
template<typename Storage >
constexpr bool operator<= (Flag_t< Storage > left, Index_t right)
 
template<typename Storage >
constexpr bool operator>= (Index_t left, Flag_t< Storage > right)
 
template<typename Storage >
constexpr bool operator>= (Flag_t< Storage > left, Index_t right)
 
Flag and bit operations.

Any bitwise operation with a flag returns a Bits_t.

template<typename Storage >
constexpr Bits_t< Storage > operator| (Bits_t< Storage > left, Bits_t< Storage > right)
 Returns a new Bits_t with all the bits from both arguments set. More...
 
template<typename Storage >
constexpr Bits_t< Storage > operator| (Bits_t< Storage > left, typename Bits_t< Storage >::Flag_t right)
 Returns a new Bits_t with all the bits from both arguments set. More...
 
template<typename Storage >
constexpr Bits_t< Storage > operator| (typename Bits_t< Storage >::Flag_t left, Bits_t< Storage > right)
 Returns a new Bits_t with all the bits from both arguments set. More...
 
template<typename Storage >
constexpr Bits_t< Storage > operator| (Flag_t< Storage > left, Flag_t< Storage > right)
 Returns a new Bits_t with all the bits from both arguments set. More...
 
template<typename Storage >
constexpr Bits_t< Storage > operator+ (Bits_t< Storage > left, Bits_t< Storage > right)
 Returns a new Bits_t with all the bits from both arguments set. More...
 
template<typename Storage >
constexpr Bits_t< Storage > operator+ (Bits_t< Storage > left, typename Bits_t< Storage >::Flag_t right)
 Returns a new Bits_t with all the bits from both arguments set. More...
 
template<typename Storage >
constexpr Bits_t< Storage > operator+ (typename Bits_t< Storage >::Flag_t left, Bits_t< Storage > right)
 Returns a new Bits_t with all the bits from both arguments set. More...
 
template<typename Storage >
constexpr Bits_t< Storage > operator+ (Flag_t< Storage > left, Flag_t< Storage > right)
 Returns a new Bits_t with all the bits from both arguments set. More...
 
Flag and mask management

The operations use the first operand as the starting point for the result. The binary operations in this group are:

  • bit-wise OR (equivalent to combineWithMask())
  • bit-wise AND (equivalent to intersectWithMask())
  • addition: the flags of the right operand that are defined are copied into the result
  • subtraction: the flags of the right operand that are set are unset in the result

The unary operations are:

  • bitwise negation (equivalent to negateMask())
  • unary plus sign: no operation (bit it converts the operand to Mask_t)
  • unary minus sign is not defined
template<typename Storage >
constexpr BitMask< Storage > operator| (BitMask< Storage > left, BitMask< Storage > right)
 
template<typename Storage >
constexpr BitMask< Storage > operator| (BitMask< Storage > left, typename BitMask< Storage >::Bits_t right)
 
template<typename Storage >
constexpr BitMask< Storage > operator| (typename BitMask< Storage >::Bits_t left, BitMask< Storage > right)
 
template<typename Storage >
constexpr BitMask< Storage > operator& (BitMask< Storage > left, BitMask< Storage > right)
 
template<typename Storage >
constexpr BitMask< Storage > operator& (BitMask< Storage > left, typename BitMask< Storage >::Bits_t right)
 
template<typename Storage >
constexpr BitMask< Storage > operator& (typename BitMask< Storage >::Bits_t left, BitMask< Storage > right)
 
template<typename Storage >
constexpr BitMask< Storage > operator& (Bits_t< Storage > left, Bits_t< Storage > right)
 
template<typename Storage >
constexpr BitMask< Storage > operator+ (BitMask< Storage > baseMask, BitMask< Storage > mask)
 
template<typename Storage >
constexpr BitMask< Storage > operator+ (BitMask< Storage > baseMask, typename BitMask< Storage >::Bits_t bits)
 
template<typename Storage >
constexpr BitMask< Storage > operator+ (typename BitMask< Storage >::Bits_t baseBits, BitMask< Storage > mask)
 
template<typename Storage >
constexpr BitMask< Storage > operator- (BitMask< Storage > baseMask, BitMask< Storage > mask)
 
template<typename Storage >
constexpr BitMask< Storage > operator- (BitMask< Storage > baseMask, typename BitMask< Storage >::Bits_t bits)
 
template<typename Storage >
constexpr BitMask< Storage > operator- (typename BitMask< Storage >::Bits_t baseBits, BitMask< Storage > mask)
 
template<typename Storage >
constexpr BitMask< Storage > operator- (Bits_t< Storage > baseBits, Bits_t< Storage > bits)
 
template<typename Storage >
constexpr BitMask< Storage > operator+ (BitMask< Storage > mask)
 Returns a copy of the mask. More...
 
template<typename Storage >
constexpr BitMask< Storage > operator+ (Bits_t< Storage > bits)
 Returns a mask with the specified bits set. More...
 
template<typename Storage >
constexpr BitMask< Storage > operator- (Bits_t< Storage > bits)
 Returns a mask M = -B so that A + M is equivalent to A - B. More...
 
template<typename Storage >
constexpr BitMask< Storage > operator- (Flag_t< Storage > flag)
 Returns a mask M = -B so that A + M is equivalent to A - B. More...
 
template<typename Storage >
constexpr BitMask< Storage > operator~ (BitMask< Storage > mask)
 Returns a bit set which unsets the specified bits. More...
 
template<typename Storage >
constexpr BitMask< Storage > Set (Flag_t< Storage > flag)
 Returns a bit mask which sets the specified flag. More...
 
template<typename Storage >
constexpr BitMask< Storage > Unset (Flag_t< Storage > flag)
 Returns a bit mask which unsets the specified flag. More...
 

Variables

constexpr BitMaskFromValuesTag maskFromValues {}
 Value useful for BitMask constructors from value. More...
 

Detailed Description

Classes and functions to manage bit masks and flags.

In this namespace a "hierarchy" of classes are defined:

  • Flag_t: a single bit; it can be initialized with the bit index, but it is stored as a bit mask. A flag has two states: set and unset.
  • Bits_t: a set of flags (represented as a bit mask).
  • BitMask: a set of flags which can have one of three states each: set, unset or undefined.
  • FlagSet: a set of flags (like BitMask), with the knowledge of a total number of supported flags.

The first three classes manage exactly how many bits they have storage for. FlagSet instead has also the notion of how many flags are actually supported. A number of flag-wise operations are defined. The result of an operation between two classes might be a class of a superior level in the hierarchy. In this sense, the lower objects should be intended as shortcut representations of Bits_t. More specifically:

Typedef Documentation

using util::flags::Index_t = typedef unsigned int

Type to denote the index of the flag.

Definition at line 59 of file BitMask.h.

Function Documentation

template<typename Storage >
constexpr BitMask<Storage> util::flags::makeMask ( Bits_t< Storage >  bits)
template<typename Storage >
constexpr bool util::flags::operator!= ( Index_t  left,
Flag_t< Storage >  right 
)

Definition at line 138 of file BitMask.h.

References util::flags::Flag_t< Storage >::index().

139  {
140  return left != right.index();
141  }
constexpr auto const & right(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:102
constexpr auto const & left(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:94
template<typename Storage >
constexpr bool util::flags::operator!= ( Flag_t< Storage >  left,
Index_t  right 
)

Definition at line 143 of file BitMask.h.

References util::flags::Flag_t< Storage >::index(), and art::right().

144  {
145  return left.index() != right;
146  }
constexpr auto const & right(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:102
constexpr auto const & left(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:94
template<typename Storage >
constexpr BitMask<Storage> util::flags::operator& ( BitMask< Storage >  left,
BitMask< Storage >  right 
)

Returns a mask which intersects two of them.

See also
BitMask<Storage>::intersectWithMask()

Referenced by operator<<().

template<typename Storage >
constexpr BitMask<Storage> util::flags::operator& ( BitMask< Storage >  left,
typename BitMask< Storage >::Bits_t  right 
)

Returns a mask which intersects two of them.

See also
BitMask<Storage>::intersectWithMask()
template<typename Storage >
constexpr BitMask<Storage> util::flags::operator& ( typename BitMask< Storage >::Bits_t  left,
BitMask< Storage >  right 
)

Returns a mask which intersects two of them.

See also
BitMask<Storage>::intersectWithMask()
template<typename Storage >
constexpr BitMask<Storage> util::flags::operator& ( Bits_t< Storage >  left,
Bits_t< Storage >  right 
)

Returns a mask which intersects two of them.

See also
BitMask<Storage>::intersectWithMask()
template<typename Storage >
constexpr Bits_t<Storage> util::flags::operator+ ( Bits_t< Storage >  left,
Bits_t< Storage >  right 
)

Returns a new Bits_t with all the bits from both arguments set.

Referenced by operator<<(), and util::flags::Bits_t< Storage_t >::unsetBits().

template<typename Storage >
constexpr Bits_t<Storage> util::flags::operator+ ( Bits_t< Storage >  left,
typename Bits_t< Storage >::Flag_t  right 
)

Returns a new Bits_t with all the bits from both arguments set.

template<typename Storage >
constexpr Bits_t<Storage> util::flags::operator+ ( typename Bits_t< Storage >::Flag_t  left,
Bits_t< Storage >  right 
)

Returns a new Bits_t with all the bits from both arguments set.

template<typename Storage >
constexpr Bits_t<Storage> util::flags::operator+ ( Flag_t< Storage >  left,
Flag_t< Storage >  right 
)

Returns a new Bits_t with all the bits from both arguments set.

template<typename Storage >
constexpr BitMask<Storage> util::flags::operator+ ( BitMask< Storage >  baseMask,
BitMask< Storage >  mask 
)

Returns a mask which merges two of them.

See also
BitMask<Storage>::mergeIntoMask()
template<typename Storage >
constexpr BitMask<Storage> util::flags::operator+ ( BitMask< Storage >  baseMask,
typename BitMask< Storage >::Bits_t  bits 
)

Returns a mask which merges two of them

See also
BitMask<Storage>::mergeIntoMask()
template<typename Storage >
constexpr BitMask<Storage> util::flags::operator+ ( typename BitMask< Storage >::Bits_t  baseBits,
BitMask< Storage >  mask 
)

Returns a mask which merges two of them

See also
BitMask<Storage>::mergeIntoMask()
template<typename Storage >
constexpr BitMask<Storage> util::flags::operator+ ( BitMask< Storage >  mask)

Returns a copy of the mask.

template<typename Storage >
constexpr BitMask<Storage> util::flags::operator+ ( Bits_t< Storage >  bits)

Returns a mask with the specified bits set.

template<typename Storage >
constexpr BitMask<Storage> util::flags::operator- ( BitMask< Storage >  baseMask,
BitMask< Storage >  mask 
)

Returns a mask set which defines and unsets the bits set in the mask.

See also
BitMask<Storage>::unsetMask()

Referenced by operator<<().

template<typename Storage >
constexpr BitMask<Storage> util::flags::operator- ( BitMask< Storage >  baseMask,
typename BitMask< Storage >::Bits_t  bits 
)

Returns a mask set which defines and unsets the specified bits.

See also
BitMask<Storage>::unsetMask()
template<typename Storage >
constexpr BitMask<Storage> util::flags::operator- ( typename BitMask< Storage >::Bits_t  baseBits,
BitMask< Storage >  mask 
)

Returns a mask set which defines and unsets the bits set in the mask.

See also
BitMask<Storage>::unsetMask()
template<typename Storage >
constexpr BitMask<Storage> util::flags::operator- ( Bits_t< Storage >  baseBits,
Bits_t< Storage >  bits 
)

Returns a mask which defines and unsets the specified bits.

See also
BitMask<Storage>::unsetMask()
template<typename Storage >
constexpr BitMask<Storage> util::flags::operator- ( Bits_t< Storage >  bits)

Returns a mask M = -B so that A + M is equivalent to A - B.

template<typename Storage >
constexpr BitMask<Storage> util::flags::operator- ( Flag_t< Storage >  flag)

Returns a mask M = -B so that A + M is equivalent to A - B.

template<typename Storage >
constexpr bool util::flags::operator< ( Index_t  left,
Flag_t< Storage >  right 
)

Definition at line 149 of file BitMask.h.

References art::left(), and art::right().

150  {
151  return left < right.index();
152  }
constexpr auto const & right(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:102
constexpr auto const & left(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:94
template<typename Storage >
constexpr bool util::flags::operator< ( Flag_t< Storage >  left,
Index_t  right 
)

Definition at line 154 of file BitMask.h.

References art::right().

155  {
156  return left.index() < right;
157  }
constexpr auto const & right(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:102
constexpr auto const & left(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:94
template<unsigned int NBits, typename Storage >
std::ostream& util::flags::operator<< ( std::ostream &  out,
FlagSet< NBits, Storage > const &  flags 
)

Output of a flag set into a stream.

Definition at line 186 of file FlagSet.h.

187  {
188  flags.dump(out);
189  return out;
190  }
template<typename Storage >
std::ostream& util::flags::operator<< ( std::ostream &  out,
Flag_t< Storage >  flag 
)

Output of a flag into a stream (prints its index).

Definition at line 196 of file BitMask.h.

197  {
198  out << '[' << flag.index() << ']';
199  return out;
200  }
template<typename Stream , typename Storage >
Stream& util::flags::operator<< ( Stream &&  out,
BitMask< Storage > const &  mask 
)

Output of a bit mask into a stream.

Definition at line 1187 of file BitMask.h.

References makeMask(), operator&(), operator+(), operator-(), operator|(), operator~(), Set(), and Unset().

1188  {
1189  mask.dump(std::forward<Stream>(out));
1190  return out;
1191  }
template<typename Storage >
constexpr bool util::flags::operator<= ( Index_t  left,
Flag_t< Storage >  right 
)

Definition at line 171 of file BitMask.h.

References art::right().

172  {
173  return left <= right.index();
174  }
constexpr auto const & right(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:102
constexpr auto const & left(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:94
template<typename Storage >
constexpr bool util::flags::operator<= ( Flag_t< Storage >  left,
Index_t  right 
)

Definition at line 176 of file BitMask.h.

References art::right().

177  {
178  return left.index() <= right;
179  }
constexpr auto const & right(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:102
constexpr auto const & left(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:94
template<typename Storage >
constexpr bool util::flags::operator== ( Index_t  left,
Flag_t< Storage >  right 
)

Definition at line 127 of file BitMask.h.

References util::flags::Flag_t< Storage >::index().

128  {
129  return left == right.index();
130  }
constexpr auto const & right(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:102
constexpr auto const & left(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:94
template<typename Storage >
constexpr bool util::flags::operator== ( Flag_t< Storage >  left,
Index_t  right 
)

Definition at line 132 of file BitMask.h.

References util::flags::Flag_t< Storage >::index(), and art::right().

133  {
134  return left.index() == right;
135  }
constexpr auto const & right(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:102
constexpr auto const & left(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:94
template<typename Storage >
constexpr bool util::flags::operator> ( Index_t  left,
Flag_t< Storage >  right 
)

Definition at line 160 of file BitMask.h.

References util::flags::Flag_t< Storage >::index().

161  {
162  return left > right.index();
163  }
constexpr auto const & right(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:102
constexpr auto const & left(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:94
template<typename Storage >
constexpr bool util::flags::operator> ( Flag_t< Storage >  left,
Index_t  right 
)

Definition at line 165 of file BitMask.h.

References util::flags::Flag_t< Storage >::index(), and art::right().

166  {
167  return left.index() > right;
168  }
constexpr auto const & right(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:102
constexpr auto const & left(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:94
template<typename Storage >
constexpr bool util::flags::operator>= ( Index_t  left,
Flag_t< Storage >  right 
)

Definition at line 182 of file BitMask.h.

References util::flags::Flag_t< Storage >::index().

183  {
184  return left >= right.index();
185  }
constexpr auto const & right(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:102
constexpr auto const & left(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:94
template<typename Storage >
constexpr bool util::flags::operator>= ( Flag_t< Storage >  left,
Index_t  right 
)

Definition at line 187 of file BitMask.h.

References util::flags::Flag_t< Storage >::index(), and art::right().

188  {
189  return left.index() >= right;
190  }
constexpr auto const & right(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:102
constexpr auto const & left(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:94
template<typename Storage >
constexpr Bits_t<Storage> util::flags::operator| ( Bits_t< Storage >  left,
Bits_t< Storage >  right 
)

Returns a new Bits_t with all the bits from both arguments set.

Referenced by operator<<(), and util::flags::Bits_t< Storage_t >::unsetBits().

template<typename Storage >
constexpr Bits_t<Storage> util::flags::operator| ( Bits_t< Storage >  left,
typename Bits_t< Storage >::Flag_t  right 
)

Returns a new Bits_t with all the bits from both arguments set.

template<typename Storage >
constexpr Bits_t<Storage> util::flags::operator| ( typename Bits_t< Storage >::Flag_t  left,
Bits_t< Storage >  right 
)

Returns a new Bits_t with all the bits from both arguments set.

template<typename Storage >
constexpr Bits_t<Storage> util::flags::operator| ( Flag_t< Storage >  left,
Flag_t< Storage >  right 
)

Returns a new Bits_t with all the bits from both arguments set.

template<typename Storage >
constexpr BitMask<Storage> util::flags::operator| ( BitMask< Storage >  left,
BitMask< Storage >  right 
)

Returns a mask which combines two of them.

See also
BitMask<Storage>::combineWithMask()
template<typename Storage >
constexpr BitMask<Storage> util::flags::operator| ( BitMask< Storage >  left,
typename BitMask< Storage >::Bits_t  right 
)

Returns a mask which merges two of them.

See also
BitMask<Storage>::combineWithMask()
template<typename Storage >
constexpr BitMask<Storage> util::flags::operator| ( typename BitMask< Storage >::Bits_t  left,
BitMask< Storage >  right 
)

Returns a mask which merges two of them.

See also
BitMask<Storage>::combineWithMask()
template<typename Storage >
constexpr BitMask<Storage> util::flags::operator~ ( BitMask< Storage >  mask)

Returns a bit set which unsets the specified bits.

Referenced by operator<<().

template<typename Storage >
constexpr BitMask<Storage> util::flags::Set ( Flag_t< Storage >  flag)

Returns a bit mask which sets the specified flag.

Referenced by operator<<().

template<typename Storage >
std::string util::flags::to_string ( Flag_t< Storage > const  flag)

Convert a flag into a stream (shows its index).

Definition at line 204 of file BitMask.h.

References util::flags::Flag_t< Storage >::index().

205  {
206  return std::to_string(flag.index());
207  }
std::string to_string(Flag_t< Storage > const flag)
Convert a flag into a stream (shows its index).
Definition: BitMask.h:204
template<typename Storage >
constexpr BitMask<Storage> util::flags::Unset ( Flag_t< Storage >  flag)

Returns a bit mask which unsets the specified flag.

Referenced by operator<<().

Variable Documentation

constexpr BitMaskFromValuesTag util::flags::maskFromValues {}

Value useful for BitMask constructors from value.

Definition at line 391 of file BitMask.h.