Typed flags
Type-safe and human-readable set of bool flags
List of all members
typed_flags< Args > Class Template Reference

#include <typed_flags.hpp>

Public Member Functions

Creation
 typed_flags ()=default
 
template<typename... T>
 typed_flags (flag< T >...flags) noexcept
 
 typed_flags (unsigned long long data) noexcept
 
template<class CharT >
 typed_flags (const CharT *str, typename std::basic_string< CharT >::size_type n=std::basic_string< CharT >::npos, CharT zero=CharT('0'), CharT one=CharT('1'))
 
Element access
template<typename T >
bool test () const noexcept
 
template<typename... T>
bool none () const noexcept
 
template<typename... T>
bool any () const noexcept
 
template<typename... T>
bool all () const noexcept
 
template<typename... T>
void get (flag< T > &...flags) const noexcept
 
Modifiers
template<typename... T>
void set (flag< T >...flags) noexcept
 
template<typename... T>
void set (bool value=true) noexcept
 
template<typename... T>
void reset () noexcept
 
template<typename... T>
void flip () noexcept
 
Conversions
template<typename T >
to_integral () const noexcept
 
template<class CharT = char, class Traits = std::char_traits<CharT>, class Allocator = std::allocator<CharT>>
auto to_string (CharT zero=CharT('0'), CharT one=CharT('1')) const
 
Logical member operators
bool operator== (this_type const &other) const noexcept
 
bool operator!= (this_type const &other) const noexcept
 
Bitwise member operators
this_typeoperator&= (this_type const &other) noexcept
 
this_typeoperator|= (this_type const &other) noexcept
 
this_typeoperator^= (this_type const &other) noexcept
 
this_type operator~ () const noexcept
 

Related Functions

(Note that these are not member functions.)

Bitwise non-member operators
template<typename... Args>
typed_flags< Args... > operator& (typed_flags< Args... > const &lhs, typed_flags< Args... > const &rhs)
 
template<typename... Args>
typed_flags< Args... > operator| (typed_flags< Args... > const &lhs, typed_flags< Args... > const &rhs)
 
template<typename... Args>
typed_flags< Args... > operator^ (typed_flags< Args... > const &lhs, typed_flags< Args... > const &rhs)
 

Capacity

static constexpr size_t length = sizeof...(Args)
 
constexpr size_t size () const noexcept
 
template<typename T >
static constexpr size_t index () noexcept
 

Detailed Description

template<typename... Args>
class typed_flags< Args >

Type-safe flag container.

Templated frontend to raw bit storage. Allows type safe bit manipulations translating user defined types to corresponding indexes.

Parameters
Args...user defined types.
Note
Types can be incomplete.

Constructor & Destructor Documentation

template<typename... Args>
typed_flags< Args >::typed_flags ( )
default

Sets all flags to zero.

template<typename... Args>
template<typename... T>
typed_flags< Args >::typed_flags ( flag< T >...  flags)
inlineexplicitnoexcept

Sets concrete flags to corresponding values.

Parameters
flag<T>...flag values.
template<typename... Args>
typed_flags< Args >::typed_flags ( unsigned long long  data)
inlineexplicitnoexcept

Loads flag values from integral number.
Least significant bit corresponds to the first parameter of the class template. Remaining flags are initialized to zeros.

Parameters
datasource integral number.
template<typename... Args>
template<class CharT >
typed_flags< Args >::typed_flags ( const CharT *  str,
typename std::basic_string< CharT >::size_type  n = std::basic_string<CharT>::npos,
CharT  zero = CharT('0'),
CharT  one = CharT('1') 
)
inlineexplicit

Loads flag values from characters.
Rightmost character corresponds to the first parameter of the class template. Remaining flags are initialized to zeros.

Parameters
strstring used to initialize flags.
nnumber of characters to read from string (optional).
zerocharacter representing unset flag (optional).
onecharacter representing set flag (optional).
Exceptions
std::invalid_argumentif character is neither zero nor one.

Member Function Documentation

template<typename... Args>
template<typename... T>
bool typed_flags< Args >::all ( ) const
inlinenoexcept

Checks that every specified flag is set.

Parameters
T...flag types.
Returns
true if every specified flag is set, false otherwise.
Note
Invoking all() without template parameters checks all flags are equal to one.
template<typename... Args>
template<typename... T>
bool typed_flags< Args >::any ( ) const
inlinenoexcept

Checks that at least one of specified flags is set.

Parameters
T...flag types.
Returns
true if one of specified flags is set, false otherwise.
Note
Invoking any() without template parameters checks at least one of all flags is set.
template<typename... Args>
template<typename... T>
void typed_flags< Args >::flip ( )
inlinenoexcept

Reverts specified flags i.e. zeros becomes ones and vice versa.

Parameters
T...flag types.
template<typename... Args>
template<typename... T>
void typed_flags< Args >::get ( flag< T > &...  flags) const
inlinenoexcept

Extracts values of specified flags to variables.

Parameters
flag<T>&...flag variables to store result.
template<typename... Args>
template<typename T >
static constexpr size_t typed_flags< Args >::index ( )
inlinestaticnoexcept

Get index of specified flag.

Parameters
Tflag type.
Returns
size_t
template<typename... Args>
template<typename... T>
bool typed_flags< Args >::none ( ) const
inlinenoexcept

Checks that every specified flag is unset.

Parameters
T...flag types.
Returns
true if every specified flag is unset, false otherwise.
Note
Invoking none() without template parameters checks all flags are equal to zero.
template<typename... Args>
template<typename... T>
void typed_flags< Args >::reset ( )
inlinenoexcept

Unsets specified flags.

Parameters
T...flag types.
template<typename... Args>
template<typename... T>
void typed_flags< Args >::set ( flag< T >...  flags)
inlinenoexcept

Sets specified flags from flag variables.

Parameters
flag<T>...flag variables to load from.
template<typename... Args>
template<typename... T>
void typed_flags< Args >::set ( bool  value = true)
inlinenoexcept

Changes specified flags.

Parameters
T...flag types.
valuesets flags to this value.
template<typename... Args>
constexpr size_t typed_flags< Args >::size ( ) const
inlinenoexcept

Get the number of flags.

Returns
typed_flags<Args...>::length
template<typename... Args>
template<typename T >
bool typed_flags< Args >::test ( ) const
inlinenoexcept

Returns the value of the specified flag.

Parameters
Tflag type.
Returns
true if the flag is set, false otherwise.
template<typename... Args>
template<typename T >
T typed_flags< Args >::to_integral ( ) const
noexcept

Converts flags to integral number.
Least significant bit corresponds to the first parameter of the class template.

Parameters
Ttarget integral type.
Returns
integral number of type T.
Note
If type T can't hold all flags static assertion fails.
template<typename... Args>
template<class CharT = char, class Traits = std::char_traits<CharT>, class Allocator = std::allocator<CharT>>
auto typed_flags< Args >::to_string ( CharT  zero = CharT('0'),
CharT  one = CharT('1') 
) const

Converts flags to standard string.
Rightmost character corresponds to the first parameter of the class template.

Parameters
zerocharacter representing unset flag (optional).
onecharacter representing set flag (optional).
Returns
std::basic_string<CharT, Traits, Allocator>

Member Data Documentation

template<typename... Args>
constexpr size_t typed_flags< Args >::length = sizeof...(Args)
static

Number of flags i.e. template parameters.