CoDiPack  2.2.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
codi::EnumBitset< T_Enum > Struct Template Reference

A bitset with enum items as flags. More...

#include <enumBitset.hpp>

Inheritance diagram for codi::EnumBitset< T_Enum >:

Public Types

using Enum = T_Enum
 See EnumBitset.
 

Public Member Functions

bool any ()
 Conversion to boolean.
 
 EnumBitset ()=default
 Constructor all false.
 
 EnumBitset (Enum pos)
 Constructor which sets one bit to true.
 
EnumBitsetflip ()
 Flip all bits.
 
EnumBitsetflip (Enum pos)
 Flip the bit for the enum.
 
Bitset getData () const
 Get the underlying bitset.
 
bool operator!= (Enum const &pos) const
 Not equal operation for a bitset and an enum.
 
bool operator!= (EnumBitset const &o) const
 Not equal operation for two bitsets.
 
EnumBitsetoperator&= (Enum const &pos)
 And operation of the bitsets and and enum.
 
EnumBitsetoperator&= (EnumBitset const &o)
 And operation of two bitsets.
 
bool operator== (Enum const &pos) const
 Equal operation for a bitset and an enum.
 
bool operator== (EnumBitset const &o) const
 Equal operation for two bitsets.
 
EnumBitsetoperator|= (Enum const &pos)
 Or operation of the bitset and an enum.
 
EnumBitsetoperator|= (EnumBitset const &o)
 Or operation of two bitsets.
 
EnumBitsetreset ()
 Reset all bits to false.
 
EnumBitsetreset (Enum pos)
 Reset the bit for the enum to false.
 
EnumBitsetset (Enum pos)
 Set the bit for the enum to true.
 
bool test (Enum pos) const
 Test if the bit for the enum is set.
 

Static Public Member Functions

static constexpr EnumBitset ALL ()
 Constructor for a bitset with all values flagged as true.
 
static constexpr EnumBitset NONE ()
 Constructor for a bitset with all values flagged as false.
 

Detailed Description

template<typename T_Enum>
struct codi::EnumBitset< T_Enum >

A bitset with enum items as flags.

The bitset implementation allows to use an enum class as flags without causing compiler warnings or having to cast the enum elements to integer types.

The enum needs to have a 'MaxElement' item and all elements need to be positive. An example definition is:

enum class TestFlags {
Bit1 = 0,
Bit2,
Bit3,
Bit4,
Bit5,
MaxElement,
};
CODI_INLINE TestOptions operator|(TestFlags a, TestFlags b) {
return TestOptions(a) | b;
}

where the overloaded operator | is required to create a bitset out of two flags. An example use case is:

TestOptions options = TestFlags::Bit1 | TestFlags::Bit3 | TestFlags::Bit5;
std::cout << "Flags: " << options << std::endl;
std::cout << "Flag1: " << options.test(TestFlags::Bit1) << std::endl;
std::cout << "Flag2: " << options.test(TestFlags::Bit2) << std::endl;
std::cout << "Flag3: " << options.test(TestFlags::Bit3) << std::endl;
std::cout << "Flag4: " << options.test(TestFlags::Bit4) << std::endl;
std::cout << "Flag5: " << options.test(TestFlags::Bit5) << std::endl;
Template Parameters
T_EnumAn enumeration class. It has to have the element 'MaxElement' and only positive values.

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