CoDiPack  3.0.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

inlinestatic constexpr EnumBitset ALL ()
 Constructor for a bitset with all values flagged as true.
 
inlinebool any ()
 Conversion to boolean.
 
inlineEnumBitset & flip ()
 Flip all bits.
 
inlineEnumBitset & flip (Enum pos)
 Flip the bit for the enum.
 
inlineBitset getData () const
 Get the underlying bitset.
 
 inlineEnumBitset ()=default
 Constructor all false.
 
 inlineEnumBitset (Enum pos)
 Constructor which sets one bit to true.
 
inlinestatic constexpr EnumBitset NONE ()
 Constructor for a bitset with all values flagged as false.
 
inlinebool operator!= (Enum const &pos) const
 Not equal operation for a bitset and an enum.
 
inlinebool operator!= (EnumBitset const &o) const
 Not equal operation for two bitsets.
 
inlineEnumBitset & operator&= (Enum const &pos)
 And operation of the bitsets and and enum.
 
inlineEnumBitset & operator&= (EnumBitset const &o)
 And operation of two bitsets.
 
inlinebool operator== (Enum const &pos) const
 Equal operation for a bitset and an enum.
 
inlinebool operator== (EnumBitset const &o) const
 Equal operation for two bitsets.
 
inlineEnumBitset & operator|= (Enum const &pos)
 Or operation of the bitset and an enum.
 
inlineEnumBitset & operator|= (EnumBitset const &o)
 Or operation of two bitsets.
 
inlineEnumBitset & reset ()
 Reset all bits to false.
 
inlineEnumBitset & reset (Enum pos)
 Reset the bit for the enum to false.
 
inlineEnumBitset & set (Enum pos)
 Set the bit for the enum to true.
 
inlinebool test (Enum pos) const
 Test if the bit for the enum is set.
 

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,
};
using TestOptions = codi::EnumBitset<TestFlags>;
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: