4.6.0
Freundlich's C++ toolkit
|
A statically sized bitfield.
Consider the states "a person can be hungry and tired, not hungry and tired, hungry and not tired or not hungry and not tired". Being hungry and being tired are orthogonal states which can be implemented using the bit representation of an enumeration:
This has a multitude of problems: It abuses enums, it is tiresome to write and it is limited by the number of bits the biggest integer type can hold. To fix this, a bitfield is indexed by natural numbers instead of powers of two and can hold an arbitrary (but fixed) number of bits.
A bitfield is used together with an enum type that fulfils the requirements of fcppt.enum.
Here's a small example:
As you can see, you can treat a bitfield like an integral type – it has bitwise operator&, operator|
and so on. But you can also treat it like a std::map<Enum,bool>
.
Classes | |
struct | fcppt::container::bitfield::default_internal_type |
Tells a bitfield to choose an internal type. More... | |
struct | fcppt::container::bitfield::hash< Bitfield > |
A hash function for bitfields. More... | |
class | fcppt::container::bitfield::object< ElementType, InternalType > |
A statically sized bitfield. More... | |
Typedefs | |
template<typename NumElements , typename InternalType > | |
using | fcppt::container::bitfield::array |
Meta function to retrieve the internal storage type used by fcppt::container::bitfield::object. | |
Functions | |
template<typename ElementType , typename InternalType > | |
bool | fcppt::container::bitfield::operator== (fcppt::container::bitfield::object< ElementType, InternalType > const &_left, fcppt::container::bitfield::object< ElementType, InternalType > const &_right) |
Compares two bitfields for equality. | |
template<typename ElementType , typename InternalType > | |
bool | fcppt::container::bitfield::operator!= (fcppt::container::bitfield::object< ElementType, InternalType > const &_left, fcppt::container::bitfield::object< ElementType, InternalType > const &_right) |
Compares two bitfields for inequality. | |
template<typename Result , typename Function > | |
Result | fcppt::container::bitfield::init (Function const &_function) |
Initialize a bitfield using a function. | |
template<typename ElementType , typename InternalType > | |
fcppt::container::bitfield::object< ElementType, InternalType > & | fcppt::container::bitfield::operator|= (fcppt::container::bitfield::object< ElementType, InternalType > &_left, ElementType const _index) |
Set a bit to true. | |
template<typename ElementType , typename InternalType > | |
fcppt::container::bitfield::object< ElementType, InternalType > & | fcppt::container::bitfield::operator|= (fcppt::container::bitfield::object< ElementType, InternalType > &_left, fcppt::container::bitfield::object< ElementType, InternalType > const &_right) |
Do a bit-wise "or" for all bits. | |
template<typename ElementType , typename InternalType > | |
fcppt::container::bitfield::object< ElementType, InternalType > & | fcppt::container::bitfield::operator&= (fcppt::container::bitfield::object< ElementType, InternalType > &_left, fcppt::container::bitfield::object< ElementType, InternalType > const &_right) |
Do a bit-wise "and" for all bits. | |
template<typename ElementType , typename InternalType > | |
fcppt::container::bitfield::object< ElementType, InternalType > & | fcppt::container::bitfield::operator^= (fcppt::container::bitfield::object< ElementType, InternalType > &_left, fcppt::container::bitfield::object< ElementType, InternalType > const &_right) |
Do a bit-wise "xor" for all bits. | |
template<typename ElementType , typename InternalType > | |
fcppt::container::bitfield::object< ElementType, InternalType > | fcppt::container::bitfield::operator~ (fcppt::container::bitfield::object< ElementType, InternalType > _field) |
Do a bit-wise "not" for all bits (inverts all bits). | |
template<typename ElementType , typename InternalType > | |
fcppt::container::bitfield::value_type | fcppt::container::bitfield::operator& (fcppt::container::bitfield::object< ElementType, InternalType > const &_field, ElementType const _index) |
Checks if the specified bit is set. | |
template<typename ElementType , typename InternalType > | |
fcppt::container::bitfield::object< ElementType, InternalType > | fcppt::container::bitfield::operator| (fcppt::container::bitfield::object< ElementType, InternalType > _field, ElementType const _index) |
Set the specified bit to true. | |
template<typename ElementType , typename InternalType > | |
fcppt::container::bitfield::object< ElementType, InternalType > | fcppt::container::bitfield::operator| (fcppt::container::bitfield::object< ElementType, InternalType > _left, fcppt::container::bitfield::object< ElementType, InternalType > const &_right) |
Do a bit-wise "or" for all bits. | |
template<typename ElementType , typename InternalType > | |
fcppt::container::bitfield::object< ElementType, InternalType > | fcppt::container::bitfield::operator& (fcppt::container::bitfield::object< ElementType, InternalType > _left, fcppt::container::bitfield::object< ElementType, InternalType > const &_right) |
Do a bit-wise "and" for all bits. | |
template<typename ElementType , typename InternalType > | |
fcppt::container::bitfield::object< ElementType, InternalType > | fcppt::container::bitfield::operator^ (fcppt::container::bitfield::object< ElementType, InternalType > _left, fcppt::container::bitfield::object< ElementType, InternalType > const &_right) |
Do a bit-wise "xor" for all bits. | |
template<typename Ch , typename Traits , typename ElementType , typename InternalType > | |
std::basic_ostream< Ch, Traits > & | fcppt::container::bitfield::operator<< (std::basic_ostream< Ch, Traits > &_stream, fcppt::container::bitfield::object< ElementType, InternalType > const &_bitfield) |
Outputs a bitfield. | |
template<typename ElementType , typename InternalType > requires (fcppt::container::bitfield::object<ElementType, InternalType>::array_size::value == 1U) | |
fcppt::container::bitfield::object< ElementType, InternalType >::internal_type | fcppt::container::bitfield::underlying_value (fcppt::container::bitfield::object< ElementType, InternalType > const &_bitfield) |
Returns the underlying value of a bitfield. | |
using fcppt::container::bitfield::array |
Meta function to retrieve the internal storage type used by fcppt::container::bitfield::object.
This is currently just an array of as many InternalTypes
values as are necessary to hold NumElements
bits.
Result fcppt::container::bitfield::init | ( | Function const & | _function | ) |
Initialize a bitfield using a function.
Every bit of Result with index e
is set to _function(e)
.
Result | Must be a bitfield. |
Function | Must be callable as bool (Result::element_type) . |
bool fcppt::container::bitfield::operator!= | ( | fcppt::container::bitfield::object< ElementType, InternalType > const & | _left, |
fcppt::container::bitfield::object< ElementType, InternalType > const & | _right ) |
Compares two bitfields for inequality.
fcppt::container::bitfield::object< ElementType, InternalType > fcppt::container::bitfield::operator& | ( | fcppt::container::bitfield::object< ElementType, InternalType > | _left, |
fcppt::container::bitfield::object< ElementType, InternalType > const & | _right ) |
Do a bit-wise "and" for all bits.
fcppt::container::bitfield::value_type fcppt::container::bitfield::operator& | ( | fcppt::container::bitfield::object< ElementType, InternalType > const & | _field, |
ElementType const | _index ) |
Checks if the specified bit is set.
fcppt::container::bitfield::object< ElementType, InternalType > & fcppt::container::bitfield::operator&= | ( | fcppt::container::bitfield::object< ElementType, InternalType > & | _left, |
fcppt::container::bitfield::object< ElementType, InternalType > const & | _right ) |
Do a bit-wise "and" for all bits.
std::basic_ostream< Ch, Traits > & fcppt::container::bitfield::operator<< | ( | std::basic_ostream< Ch, Traits > & | _stream, |
fcppt::container::bitfield::object< ElementType, InternalType > const & | _bitfield ) |
Outputs a bitfield.
bool fcppt::container::bitfield::operator== | ( | fcppt::container::bitfield::object< ElementType, InternalType > const & | _left, |
fcppt::container::bitfield::object< ElementType, InternalType > const & | _right ) |
Compares two bitfields for equality.
fcppt::container::bitfield::object< ElementType, InternalType > fcppt::container::bitfield::operator^ | ( | fcppt::container::bitfield::object< ElementType, InternalType > | _left, |
fcppt::container::bitfield::object< ElementType, InternalType > const & | _right ) |
Do a bit-wise "xor" for all bits.
fcppt::container::bitfield::object< ElementType, InternalType > & fcppt::container::bitfield::operator^= | ( | fcppt::container::bitfield::object< ElementType, InternalType > & | _left, |
fcppt::container::bitfield::object< ElementType, InternalType > const & | _right ) |
Do a bit-wise "xor" for all bits.
fcppt::container::bitfield::object< ElementType, InternalType > fcppt::container::bitfield::operator| | ( | fcppt::container::bitfield::object< ElementType, InternalType > | _field, |
ElementType const | _index ) |
Set the specified bit to true.
fcppt::container::bitfield::object< ElementType, InternalType > fcppt::container::bitfield::operator| | ( | fcppt::container::bitfield::object< ElementType, InternalType > | _left, |
fcppt::container::bitfield::object< ElementType, InternalType > const & | _right ) |
Do a bit-wise "or" for all bits.
fcppt::container::bitfield::object< ElementType, InternalType > & fcppt::container::bitfield::operator|= | ( | fcppt::container::bitfield::object< ElementType, InternalType > & | _left, |
ElementType const | _index ) |
Set a bit to true.
fcppt::container::bitfield::object< ElementType, InternalType > & fcppt::container::bitfield::operator|= | ( | fcppt::container::bitfield::object< ElementType, InternalType > & | _left, |
fcppt::container::bitfield::object< ElementType, InternalType > const & | _right ) |
Do a bit-wise "or" for all bits.
fcppt::container::bitfield::object< ElementType, InternalType > fcppt::container::bitfield::operator~ | ( | fcppt::container::bitfield::object< ElementType, InternalType > | _field | ) |
Do a bit-wise "not" for all bits (inverts all bits).
fcppt::container::bitfield::object< ElementType, InternalType >::internal_type fcppt::container::bitfield::underlying_value | ( | fcppt::container::bitfield::object< ElementType, InternalType > const & | _bitfield | ) |
Returns the underlying value of a bitfield.
Returns the underlying value of _bitfield. This function can only be called if the bitfield consists of exactly one underlying value (which means that _bitfield::array_size::value == 1U
).