MySQL 9.1.0
Source Code Documentation
stdx::flags< E > Class Template Reference

a type-safe flags type. More...

#include <flags.h>

Public Types

using enum_type = std::decay_t< E >
 underlying integer type of the enum_type. More...
 
using underlying_type = std::underlying_type_t< enum_type >
 implementation type of the underlying type. More...
 
using impl_type = std::make_unsigned_t< underlying_type >
 
using size_type = std::size_t
 

Public Member Functions

constexpr flags ()=default
 default constructor. More...
 
constexpr flags (const flags &other)=default
 copy constructor. More...
 
constexpr flags (flags &&other)=default
 move constructor More...
 
constexpr flagsoperator= (const flags &other)=default
 copy assignment More...
 
constexpr flagsoperator= (flags &&other)=default
 move assignment More...
 
constexpr flags (enum_type v)
 converting constructor from enum_type. More...
 
constexpr operator bool () const noexcept
 check if any bit is set. More...
 
constexpr bool operator! () const noexcept
 check if no bit is set. More...
 
constexpr flags operator~ () const noexcept
 negation of all bits. More...
 
constexpr flagsoperator|= (const flags &other) noexcept
 bit-or. More...
 
constexpr flagsoperator&= (const flags &other) noexcept
 bit-and. More...
 
constexpr flagsoperator^= (const flags &other) noexcept
 bit-xor. More...
 
constexpr void underlying_value (underlying_type v) noexcept
 set underlying value. More...
 
constexpr underlying_type underlying_value () const noexcept
 get underlying value. More...
 
constexpr size_type count () const noexcept
 count bits set to true. More...
 
constexpr size_type size () const noexcept
 returns number of bits the flag-type can hold. More...
 
constexpr void reset ()
 reset all flags to 0. More...
 

Private Member Functions

constexpr flags (impl_type v) noexcept
 

Private Attributes

impl_type v_ {}
 

Friends

constexpr friend flags operator& (flags a, flags b) noexcept
 bit-and. More...
 
constexpr friend flags operator| (flags a, flags b) noexcept
 bit-or. More...
 
constexpr friend flags operator^ (flags a, flags b) noexcept
 bit-xor. More...
 

Detailed Description

template<class E>
class stdx::flags< E >

a type-safe flags type.

abstract

Using flags in the in C++ isn't very ergonimic:

  1. using plain C-enum's isn't typesafe
  2. using std::bitset requires bit-positions to set/reset/test them
  3. scoped-enums (enum class) doesn't have operaters

For ease of use it should be possible to have a flags-type which allows the type-safe operations:

  • flags = flags & flag
  • flags &= flag
  • flags = flags | flag
  • flags |= flag
  • flags = flags ^ flag
  • flags ^= flag
  • flags = ~flag
  • flag | flag -> flags
  • flag & flag -> flags
  • flag ^ flag -> flags

example

// underlying scoped-enum
enum class somebits {
bit0 = 1 << 0,
bit1 = 1 << 1,
};
// activate stdx::flags support for the scoped-enum
namespace stdx {
template<>
struct is_flags<somebits> : std::true_type {}
}
{ // default-construct + assignment
someflags = somebits::bit0 | somebits::bit1;
// get the underlying bitvalue.
std::cerr << someflags.underlying_value() << "\n";
}
{ // direct initialization from enum
stdx::flags<somebits> someflags{somebits::bit0 | somebits::bit1};
}
{ // testing if bit set
stdx::flags<somebits> someflags{somebits::bit0 | somebits::bit1};
if (someflags & somebits::bit1) {} // true
}
{ // setting raw value
someflags.underlying_value(3);
if (someflags & somebits::bit0) {} // true
if (someflags & somebits::bit1) {} // true
}
a type-safe flags type.
Definition: flags.h:115
constexpr void underlying_value(underlying_type v) noexcept
set underlying value.
Definition: flags.h:237
Definition: bit.h:32
Template Parameters
Ea scoped enum that is tagged with stdx::is_flags.

Member Typedef Documentation

◆ enum_type

template<class E >
using stdx::flags< E >::enum_type = std::decay_t<E>

underlying integer type of the enum_type.

◆ impl_type

template<class E >
using stdx::flags< E >::impl_type = std::make_unsigned_t<underlying_type>

◆ size_type

template<class E >
using stdx::flags< E >::size_type = std::size_t

◆ underlying_type

template<class E >
using stdx::flags< E >::underlying_type = std::underlying_type_t<enum_type>

implementation type of the underlying type.

Constructor & Destructor Documentation

◆ flags() [1/5]

template<class E >
constexpr stdx::flags< E >::flags ( )
constexprdefault

default constructor.

initializes underlying_value to 0 (no-bits-set).

◆ flags() [2/5]

template<class E >
constexpr stdx::flags< E >::flags ( const flags< E > &  other)
constexprdefault

copy constructor.

◆ flags() [3/5]

template<class E >
constexpr stdx::flags< E >::flags ( flags< E > &&  other)
constexprdefault

move constructor

◆ flags() [4/5]

template<class E >
constexpr stdx::flags< E >::flags ( enum_type  v)
inlineconstexpr

converting constructor from enum_type.

◆ flags() [5/5]

template<class E >
constexpr stdx::flags< E >::flags ( impl_type  v)
inlineexplicitconstexprprivatenoexcept

Member Function Documentation

◆ count()

template<class E >
constexpr size_type stdx::flags< E >::count ( ) const
inlineconstexprnoexcept

count bits set to true.

See also
size()
Returns
bits set to true.

◆ operator bool()

template<class E >
constexpr stdx::flags< E >::operator bool ( ) const
inlineconstexprnoexcept

check if any bit is set.

Return values
trueat least one bit is set.
falseno bit is set.

◆ operator!()

template<class E >
constexpr bool stdx::flags< E >::operator! ( ) const
inlineconstexprnoexcept

check if no bit is set.

Return values
trueno bit is set.
falseat least one bit is set.

◆ operator&=()

template<class E >
constexpr flags & stdx::flags< E >::operator&= ( const flags< E > &  other)
inlineconstexprnoexcept

bit-and.

flags &= flags.

◆ operator=() [1/2]

template<class E >
constexpr flags & stdx::flags< E >::operator= ( const flags< E > &  other)
constexprdefault

copy assignment

◆ operator=() [2/2]

template<class E >
constexpr flags & stdx::flags< E >::operator= ( flags< E > &&  other)
constexprdefault

move assignment

◆ operator^=()

template<class E >
constexpr flags & stdx::flags< E >::operator^= ( const flags< E > &  other)
inlineconstexprnoexcept

bit-xor.

flag ^= flag

◆ operator|=()

template<class E >
constexpr flags & stdx::flags< E >::operator|= ( const flags< E > &  other)
inlineconstexprnoexcept

bit-or.

flags |= flags.

◆ operator~()

template<class E >
constexpr flags stdx::flags< E >::operator~ ( ) const
inlineconstexprnoexcept

negation of all bits.

~flags -> flags.

◆ reset()

template<class E >
constexpr void stdx::flags< E >::reset ( )
inlineconstexpr

reset all flags to 0.

◆ size()

template<class E >
constexpr size_type stdx::flags< E >::size ( ) const
inlineconstexprnoexcept

returns number of bits the flag-type can hold.

See also
count()
Returns
number of bits the flag-type can hold.

◆ underlying_value() [1/2]

template<class E >
constexpr underlying_type stdx::flags< E >::underlying_value ( ) const
inlineconstexprnoexcept

get underlying value.

Returns
underlying value.

◆ underlying_value() [2/2]

template<class E >
constexpr void stdx::flags< E >::underlying_value ( underlying_type  v)
inlineconstexprnoexcept

set underlying value.

Parameters
vunderlying value to set.

Friends And Related Function Documentation

◆ operator&

template<class E >
constexpr friend flags operator& ( flags< E >  a,
flags< E >  b 
)
friend

bit-and.

flags & flags -> flags

◆ operator^

template<class E >
constexpr friend flags operator^ ( flags< E >  a,
flags< E >  b 
)
friend

bit-xor.

flags ^ flags -> flags

◆ operator|

template<class E >
constexpr friend flags operator| ( flags< E >  a,
flags< E >  b 
)
friend

bit-or.

flags | flags -> flags

Member Data Documentation

◆ v_

template<class E >
impl_type stdx::flags< E >::v_ {}
private

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