Gazebo Common

API Reference

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

Static Public Member Functions

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

Static Public Attributes

static constexpr size_t numElements
 Number of elements of the bitset.
 

Detailed Description

template<typename T, T LastElement = T::_, bool ExcludeLast = true>
class gz::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]

template<typename T , T LastElement = T::_, bool ExcludeLast = true>
FlagSet ( )
default

◆ FlagSet() [2/3]

template<typename T , T LastElement = T::_, bool ExcludeLast = true>
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]

template<typename T , T LastElement = T::_, bool ExcludeLast = true>
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()

template<typename T , T LastElement = T::_, bool ExcludeLast = true>
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()

template<typename T , T LastElement = T::_, bool ExcludeLast = true>
static FlagSet AllSet ( )
inlinestatic

◆ Any()

template<typename T , T LastElement = T::_, bool ExcludeLast = true>
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()

template<typename T , T LastElement = T::_, bool ExcludeLast = true>
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]

template<typename T , T LastElement = T::_, bool ExcludeLast = true>
FlagSet & Flip ( )
inline

Set all flags to their negation.

Returns
This.

References bitset< Bits >::flip().

◆ Flip() [2/2]

template<typename T , T LastElement = T::_, bool ExcludeLast = true>
FlagSet & Flip ( const T &  _val)
inline

Negate the given flag.

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

References bitset< Bits >::flip().

◆ Hash()

template<typename T , T LastElement = T::_, bool ExcludeLast = true>
size_t Hash ( ) const
inline

Compute hash of the FlagSet.

Returns
The hash.

◆ None()

template<typename T , T LastElement = T::_, bool ExcludeLast = true>
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()

template<typename T , T LastElement = T::_, bool ExcludeLast = true>
static FlagSet NoneSet ( )
inlinestatic

Retrurn a FlagSet with all flags set to false.

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

◆ operator bool()

template<typename T , T LastElement = T::_, bool ExcludeLast = true>
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!=()

template<typename T , T LastElement = T::_, bool ExcludeLast = true>
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&() [1/2]

template<typename T , T LastElement = T::_, bool ExcludeLast = true>
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&() [2/2]

template<typename T , T LastElement = T::_, bool ExcludeLast = true>
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&=() [1/2]

template<typename T , T LastElement = T::_, bool ExcludeLast = true>
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&=() [2/2]

template<typename T , T LastElement = T::_, bool ExcludeLast = true>
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==()

template<typename T , T LastElement = T::_, bool ExcludeLast = true>
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[]()

template<typename T , T LastElement = T::_, bool ExcludeLast = true>
constexpr bool operator[] ( const T &  _val) const
inlineconstexpr

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]

template<typename T , T LastElement = T::_, bool ExcludeLast = true>
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|() [2/2]

template<typename T , T LastElement = T::_, bool ExcludeLast = true>
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|=() [1/2]

template<typename T , T LastElement = T::_, bool ExcludeLast = true>
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|=() [2/2]

template<typename T , T LastElement = T::_, bool ExcludeLast = true>
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~()

template<typename T , T LastElement = T::_, bool ExcludeLast = true>
FlagSet operator~ ( ) const
inline

Return a negation of this FlagSet.

Returns
The negated FlagSet.

References bitset< Bits >::flip().

◆ Reset() [1/2]

template<typename T , T LastElement = T::_, bool ExcludeLast = true>
FlagSet & Reset ( )
inline

Set all flags to false.

Returns
This.

References bitset< Bits >::reset().

◆ Reset() [2/2]

template<typename T , T LastElement = T::_, bool ExcludeLast = true>
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]

template<typename T , T LastElement = T::_, bool ExcludeLast = true>
FlagSet & Set ( )
inline

Set all flags to true.

Returns
This.

References bitset< Bits >::set().

Referenced by FlagSet< T, LastElement, ExcludeLast >::AllSet().

◆ Set() [2/2]

template<typename T , T LastElement = T::_, bool ExcludeLast = true>
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()

template<typename T , T LastElement = T::_, bool ExcludeLast = true>
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()

template<typename T , T LastElement = T::_, bool ExcludeLast = true>
std::string String ( ) const
inline

Return a string describing this FlagSet.

Returns
The string.

References bitset< Bits >::to_string().

Member Data Documentation

◆ numElements

template<typename T , T LastElement = T::_, bool ExcludeLast = true>
constexpr size_t numElements
staticconstexpr
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: