Ignition Common

API Reference

4.1.0
FlagSet< T, LastElement, ExcludeLast > Class Template Reference

Set of flags defined by a C++11 enum class. More...

#include <FlagSet.hh>

Public Member Functions

 FlagSet ()=default
 Create an empty FlagSet (no flags set). More...
 
 FlagSet (const T &_val)
 Construct a FlagSet with the given flag set and all other unset. More...
 
 FlagSet (const std::initializer_list< T > &_list)
 Construct a FlagSet with the given flags set and all other unset. More...
 
bool All () const
 Test whether all flags are set. More...
 
bool Any () const
 Test whether any flag is set. More...
 
std::size_t Count () const
 Return the number of flags set to true. More...
 
FlagSetFlip ()
 Set all flags to their negation. More...
 
FlagSetFlip (const T &_val)
 Negate the given flag. More...
 
size_t Hash () const
 Compute hash of the FlagSet. More...
 
bool None () const
 Test whether no flag is set. More...
 
FlagSet operator & (const T &_val) const
 Return a FlagSet with only the given flag set (or even this one unset if it wasn't set in this FlagSet before). More...
 
FlagSet operator & (const FlagSet &_val) const
 Return a bit AND of this FlagSet and the argument. More...
 
FlagSetoperator &= (const T &_val) noexcept
 Return a FlagSet with only the given flag set (or even this one unset if it wasn't set in this FlagSet before). More...
 
FlagSetoperator &= (const FlagSet &_o) noexcept
 Return a bit AND of this FlagSet and the argument. More...
 
 operator bool () const
 Return true if at least one flag is set to true. More...
 
bool operator!= (const FlagSet &_o) const
 Test FlagSet inequality. More...
 
bool operator== (const FlagSet &_o) const
 Test FlagSet equality. More...
 
constexpr bool operator[] (const T &_val) const
 Return whether the given flag is set. More...
 
FlagSet operator| (const T &_val) const
 Return a FlagSet with the given flag set to true. More...
 
FlagSet operator| (const FlagSet &_val) const
 Return a bit OR of this FlagSet and the argument. More...
 
FlagSetoperator|= (const T &_val) noexcept
 Set the given flag to true in this FlagSet. More...
 
FlagSetoperator|= (const FlagSet &_o) noexcept
 Return a bit OR of this FlagSet and the argument. More...
 
FlagSet operator~ () const
 Return a negation of this FlagSet. More...
 
FlagSetReset ()
 Set all flags to false. More...
 
FlagSetReset (const T &_val)
 Set the given flag to false. More...
 
FlagSetSet ()
 Set all flags to true. More...
 
FlagSetSet (const T &_val, bool _value=true)
 Set the given flag to the specified value. More...
 
std::size_t Size () const
 Return the total number of flags represented by this FlagSet. More...
 
std::string String () const
 Return a string describing this FlagSet. More...
 

Static Public Member Functions

static FlagSet AllSet ()
 Retrurn a FlagSet with all flags set to true. More...
 
static FlagSet NoneSet ()
 Retrurn a FlagSet with all flags set to false. More...
 

Static Public Attributes

static constexpr size_t numElements
 Number of elements of the bitset. More...
 

Friends

std::ostreamoperator<< (std::ostream &_stream, const FlagSet &_self)
 Operator for outputting to std::ostream. More...
 

Detailed Description

template<typename T, T LastElement = T::_, bool ExcludeLast = true>
class ignition::common::FlagSet< T, LastElement, ExcludeLast >

Set of flags defined by a C++11 enum class.

Note
If the enum's last element is '_' (just underscore), the convenience signature can be used specifying just the enum type (the underscore will be automatically used as LastElement, and will be excluded from the list of valid values).
FlagSet only works for enums that do not contain negative values.
The underlying bitset representing this FlagSet will have as many bits as is the size_t value of LastElement (+- 1). Be aware of that when calling Count(), Size() etc. Also be aware of that when creating a FlagSet of an enum that would contain very high numbers in the underlying representation.
Template Parameters
TThe enum class type. The underlying type of the enum has to be convertible to size_t.
LastElementLast element of the enum. If the enum's last element is '_' (just underscore), it can be deduced automatically.
ExcludeLastIf true, LastElement is considered as an element beyond all valid elements and will not be treated as a valid value of the FlagSet. If false, the last element is treated as a valid value.

Constructor & Destructor Documentation

◆ FlagSet() [1/3]

◆ FlagSet() [2/3]

FlagSet ( const T &  _val)
inline

Construct a FlagSet with the given flag set and all other unset.

Parameters
[in]_valThe flag to set.
Note
This is intentionally not an explicit constructor so that values of the enum can be implicitly converted to a FlagSet with just the one value set.

References bitset< Bits >::set().

◆ FlagSet() [3/3]

FlagSet ( const std::initializer_list< T > &  _list)
inlineexplicit

Construct a FlagSet with the given flags set and all other unset.

Parameters
[in]_listThe list of flags to set.

References bitset< Bits >::set().

Member Function Documentation

◆ All()

bool All ( ) const
inline

Test whether all flags are set.

Returns
True if at all flags are set to true.

References bitset< Bits >::all().

◆ AllSet()

static FlagSet AllSet ( )
inlinestatic

Retrurn a FlagSet with all flags set to true.

References FlagSet< T, LastElement, ExcludeLast >::FlagSet().

◆ Any()

bool Any ( ) const
inline

Test whether any flag is set.

Returns
True if at least one flag is set to true.

References bitset< Bits >::any().

◆ Count()

std::size_t Count ( ) const
inline

Return the number of flags set to true.

Returns
The number of true flags in this FlagSet.
See also
Size()

References bitset< Bits >::count().

◆ Flip() [1/2]

FlagSet& Flip ( )
inline

Set all flags to their negation.

Returns
This.

References bitset< Bits >::flip().

◆ Flip() [2/2]

FlagSet& Flip ( const T &  _val)
inline

Negate the given flag.

Parameters
[in]_valThe flag to negate.
Returns
This.

References bitset< Bits >::flip().

◆ Hash()

size_t Hash ( ) const
inline

Compute hash of the FlagSet.

Returns
The hash.

◆ None()

bool None ( ) const
inline

Test whether no flag is set.

Returns
True if at no flag is set to true.

References bitset< Bits >::none().

◆ NoneSet()

static FlagSet NoneSet ( )
inlinestatic

Retrurn a FlagSet with all flags set to false.

References FlagSet< T, LastElement, ExcludeLast >::FlagSet().

◆ operator &() [1/2]

FlagSet operator& ( const T &  _val) const
inline

Return a FlagSet with only the given flag set (or even this one unset if it wasn't set in this FlagSet before).

Parameters
[in]_valThe flag to set.
Returns
The new FlagSet.
Note
The resulting bitset can contain at most 1 bit set to true.

References bitset< Bits >::count().

◆ operator &() [2/2]

FlagSet operator& ( const FlagSet< T, LastElement, ExcludeLast > &  _val) const
inline

Return a bit AND of this FlagSet and the argument.

Parameters
[in]_oThe other FlagSet.
Returns
The new FlagSet.

◆ operator &=() [1/2]

FlagSet& operator&= ( const T &  _val)
inlinenoexcept

Return a FlagSet with only the given flag set (or even this one unset if it wasn't set in this FlagSet before).

Parameters
[in]_valThe flag to set.
Returns
This.

References bitset< Bits >::reset(), bitset< Bits >::set(), and bitset< Bits >::test().

◆ operator &=() [2/2]

FlagSet& operator&= ( const FlagSet< T, LastElement, ExcludeLast > &  _o)
inlinenoexcept

Return a bit AND of this FlagSet and the argument.

Parameters
[in]_oThe other FlagSet.
Returns
This.

◆ operator bool()

operator bool ( ) const
inlineexplicit

Return true if at least one flag is set to true.

Returns
Whether at least one flag is set.

References bitset< Bits >::any().

◆ operator!=()

bool operator!= ( const FlagSet< T, LastElement, ExcludeLast > &  _o) const
inline

Test FlagSet inequality.

Parameters
[in]_oThe other FlagSet.
Returns
Whether the FlagSets represent different sets of flags.

◆ operator==()

bool operator== ( const FlagSet< T, LastElement, ExcludeLast > &  _o) const
inline

Test FlagSet equality.

Parameters
[in]_oThe other FlagSet.
Returns
Whether the FlagSets represent the same set of flags.

◆ operator[]()

constexpr bool operator[] ( const T &  _val) const
inline

Return whether the given flag is set.

Parameters
[in]_valThe flag to test.
Returns
Whether the flag is set.
Note
This only works for enums whose underlying type is unsigned.

◆ operator|() [1/2]

FlagSet operator| ( const T &  _val) const
inline

Return a FlagSet with the given flag set to true.

Parameters
[in]_valThe flag to set.
Returns
The new FlagSet.
Note
The resulting bitset contains at least 1 bit set to true.

References bitset< Bits >::count().

◆ operator|() [2/2]

FlagSet operator| ( const FlagSet< T, LastElement, ExcludeLast > &  _val) const
inline

Return a bit OR of this FlagSet and the argument.

Parameters
[in]_oThe other FlagSet.
Returns
The new FlagSet.

◆ operator|=() [1/2]

FlagSet& operator|= ( const T &  _val)
inlinenoexcept

Set the given flag to true in this FlagSet.

Parameters
[in]_valThe flag to set.
Returns
This.

References bitset< Bits >::set().

◆ operator|=() [2/2]

FlagSet& operator|= ( const FlagSet< T, LastElement, ExcludeLast > &  _o)
inlinenoexcept

Return a bit OR of this FlagSet and the argument.

Parameters
[in]_oThe other FlagSet.
Returns
This.

◆ operator~()

FlagSet operator~ ( ) const
inline

Return a negation of this FlagSet.

Returns
The negated FlagSet.

References bitset< Bits >::flip().

◆ Reset() [1/2]

FlagSet& Reset ( )
inline

Set all flags to false.

Returns
This.

References bitset< Bits >::reset().

◆ Reset() [2/2]

FlagSet& Reset ( const T &  _val)
inline

Set the given flag to false.

Parameters
[in]_valThe flag to set.
Returns
This.

References bitset< Bits >::reset().

◆ Set() [1/2]

FlagSet& Set ( )
inline

Set all flags to true.

Returns
This.

References bitset< Bits >::set().

◆ Set() [2/2]

FlagSet& Set ( const T &  _val,
bool  _value = true 
)
inline

Set the given flag to the specified value.

Parameters
[in]_valThe flag to set.
[in]_valueThe value to set.
Returns
This.

References bitset< Bits >::set().

◆ Size()

std::size_t Size ( ) const
inline

Return the total number of flags represented by this FlagSet.

Note
This corresponds to the value of the '_' element of the enum.
Returns
The number of flags in this FlagSet.
See also
Count()

References bitset< Bits >::size().

◆ String()

std::string String ( ) const
inline

Return a string describing this FlagSet.

Returns
The string.

References bitset< Bits >::to_string().

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream _stream,
const FlagSet< T, LastElement, ExcludeLast > &  _self 
)
friend

Operator for outputting to std::ostream.

Parameters
[in,out]_streamThe stream to write to.
[in]_selfThe FlagSet to write.
Returns
_stream with the contents of the given FlagSet written.

Member Data Documentation

◆ numElements

constexpr size_t numElements
static
Initial value:
= static_cast<size_t>(
static_cast<UnderlyingType>(LastElement) +
static_cast<UnderlyingType>(1 - ExcludeLast))

Number of elements of the bitset.


The documentation for this class was generated from the following file: