DCCL v4
dccl::Bitset Class Reference

A variable size container of bits (subclassed from std::deque<bool>) with an optional hierarchy. Similar to set::bitset but can be resized at runtime and has the ability to have parent Bitsets that can give bits to their children. More...

#include <dccl/bitset.h>

Inheritance diagram for dccl::Bitset:

Public Member Functions

 Bitset (Bitset *parent=nullptr)
 Construct an empty Bitset. More...
 
 Bitset (size_type num_bits, unsigned long value=0, Bitset *parent=nullptr)
 Construct a Bitset of a certain initial size and value. More...
 
void get_more_bits (size_type num_bits)
 Retrieve more bits from the parent Bitset. More...
 
Bitsetoperator&= (const Bitset &rhs)
 Logical AND in place. More...
 
Bitsetoperator|= (const Bitset &rhs)
 Logical OR in place. More...
 
Bitsetoperator^= (const Bitset &rhs)
 Logical XOR in place. More...
 
Bitsetoperator<<= (size_type n)
 Left shift in place. More...
 
Bitsetoperator>>= (size_type n)
 Right shift in place. More...
 
Bitset operator<< (size_type n) const
 Left shift. More...
 
Bitset operator>> (size_type n) const
 Right shift. More...
 
Bitsetset (size_type n, bool val=true)
 Set a bit to a given value. More...
 
Bitsetset ()
 Set all bits true. More...
 
Bitsetreset (size_type n)
 Reset a bit (i.e. set it to false) More...
 
Bitsetreset ()
 Set all bits false. More...
 
Bitsetflip (size_type n)
 Flip (toggle) a bit. More...
 
Bitsetflip ()
 Flip (toggle) all bits. More...
 
bool test (size_type n) const
 Test a bit (return its value) More...
 
template<typename IntType >
void from (IntType value, size_type num_bits=std::numeric_limits< IntType >::digits)
 Sets value of the Bitset to the contents of an integer. More...
 
void from_ulong (unsigned long value, size_type num_bits=std::numeric_limits< unsigned long >::digits)
 Sets value of the Bitset to the contents of an unsigned long integer. Equivalent to from<unsigned long>()
 
template<typename IntType >
IntType to () const
 Returns the value of the Bitset as a integer. More...
 
unsigned long to_ulong () const
 Returns the value of the Bitset as an unsigned long integer. Equivalent to to<unsigned long>().
 
std::string to_string () const
 Returns the value of the Bitset as a printable string, where each bit is represented by '1' or '0'. The msb is written into the zero index of the string, so it is printed msb to lsb (as is standard for writing numbers).
 
std::string to_byte_string ()
 Returns the value of the Bitset to a byte string, where each character represents 8 bits of the Bitset. The string is used as a byte container, and is not intended to be printed. More...
 
size_t to_byte_string (char *buf, size_t max_len)
 Generate a byte string representation of the Bitset, where each character represents 8 bits of the Bitset. The string is used as a byte container, and is not intended to be printed. More...
 
void from_byte_string (const std::string &s)
 Sets the value of the Bitset to the contents of a byte string, where each character represents 8 bits of the Bitset. More...
 
template<typename CharIterator >
void from_byte_stream (CharIterator begin, CharIterator end)
 Sets the value of the Bitset to the contents of a byte string, where each character represents 8 bits of the Bitset. A string container the values where the least signficant byte in string[0] and the most significant byte in string[size()-1]. More...
 
Bitsetprepend (const Bitset &bits)
 Adds the bitset to the little end.
 
Bitsetappend (const Bitset &bits)
 Adds the bitset to the big end.
 

Detailed Description

A variable size container of bits (subclassed from std::deque<bool>) with an optional hierarchy. Similar to set::bitset but can be resized at runtime and has the ability to have parent Bitsets that can give bits to their children.

This is the class used within DCCL hold the encoded message as it is created. The front() of the deque represents the least significant bit (lsb) and the back() is the most significant bit (msb). DCCL messages are encoded and decoded starting with the lsb and ending at the msb. The hierarchy is used to represent parent bit pools from which the child can pull more bits from to decode. The top level Bitset represents the entire encoded message, whereas the children are the message fields.

Definition at line 42 of file bitset.h.

Constructor & Destructor Documentation

◆ Bitset() [1/2]

dccl::Bitset::Bitset ( Bitset parent = nullptr)
inlineexplicit

Construct an empty Bitset.

Parameters
parentPointer to a bitset that should be consider this Bitset's parent for calls to get_more_bits()

Definition at line 48 of file bitset.h.

◆ Bitset() [2/2]

dccl::Bitset::Bitset ( size_type  num_bits,
unsigned long  value = 0,
Bitset parent = nullptr 
)
inlineexplicit

Construct a Bitset of a certain initial size and value.

Parameters
num_bitsInitial size of this Bitset
valueInitial value of the bits in this Bitset
parentPointer to a bitset that should be consider this Bitset's parent for calls to get_more_bits()

Definition at line 55 of file bitset.h.

Member Function Documentation

◆ flip() [1/2]

Bitset& dccl::Bitset::flip ( )
inline

Flip (toggle) all bits.

Returns
A reference to the resulting Bitset

Definition at line 214 of file bitset.h.

◆ flip() [2/2]

Bitset& dccl::Bitset::flip ( size_type  n)
inline

Flip (toggle) a bit.

Parameters
nbit to flip
Returns
A reference to the resulting Bitset

Definition at line 209 of file bitset.h.

◆ from()

template<typename IntType >
void dccl::Bitset::from ( IntType  value,
size_type  num_bits = std::numeric_limits<IntType>::digits 
)
inline

Sets value of the Bitset to the contents of an integer.

Parameters
valueValue to give the Bitset.
num_bitsThe resulting size of the Bitset.

Definition at line 236 of file bitset.h.

◆ from_byte_stream()

template<typename CharIterator >
void dccl::Bitset::from_byte_stream ( CharIterator  begin,
CharIterator  end 
)
inline

Sets the value of the Bitset to the contents of a byte string, where each character represents 8 bits of the Bitset. A string container the values where the least signficant byte in string[0] and the most significant byte in string[size()-1].

Parameters
beginIterator pointing to the begining of the input buffer
endIterator pointing to the end of the input bufer

Definition at line 341 of file bitset.h.

◆ from_byte_string()

void dccl::Bitset::from_byte_string ( const std::string &  s)
inline

Sets the value of the Bitset to the contents of a byte string, where each character represents 8 bits of the Bitset.

Parameters
sA string container the values where the least signficant byte in string[0] and the most significant byte in string[size()-1]

Definition at line 335 of file bitset.h.

◆ get_more_bits()

void dccl::Bitset::get_more_bits ( size_type  num_bits)
inline

Retrieve more bits from the parent Bitset.

Get (and remove) bits from the little end of the parent bitset and add them to the big end of our bitset, (the parent will request from their parent if required).

Parameters
num_bitsNumber of bits to get.
Exceptions
ExceptionThe parent (and up the hierarchy, if applicable) do not have num_bits to give up.

Definition at line 420 of file bitset.h.

◆ operator&=()

Bitset& dccl::Bitset::operator&= ( const Bitset rhs)
inline

Logical AND in place.

Apply the result of a logical AND of this Bitset and another to this Bitset.

Parameters
rhsThe Bitset to perform the operation on with this Bitset.
Returns
A reference to the resulting Bitset
Exceptions
ExceptionThe size of the two Bitsets are not equal.

Definition at line 77 of file bitset.h.

◆ operator<<()

Bitset dccl::Bitset::operator<< ( size_type  n) const
inline

Left shift.

Same as operator<<=() but does not modify this Bitset.

Definition at line 153 of file bitset.h.

◆ operator<<=()

Bitset& dccl::Bitset::operator<<= ( size_type  n)
inline

Left shift in place.

Shifts the Bitset to the left and inserts false (0) to the new least significant bits and discards any bits that come off the most significant end. This operation does not change the size of the Bitset.

Parameters
nThe number of bits to shift. This is equivalent to multiplying the Bitset by 2^n if the Bitset can hold the result
Returns
A reference to the resulting Bitset

Definition at line 124 of file bitset.h.

◆ operator>>()

Bitset dccl::Bitset::operator>> ( size_type  n) const
inline

Right shift.

Same as operator>>=() but does not modify this Bitset.

Definition at line 163 of file bitset.h.

◆ operator>>=()

Bitset& dccl::Bitset::operator>>= ( size_type  n)
inline

Right shift in place.

Shifts the Bitset to the right and inserts false (0) to the new most significant bits and discards any bits that come off the least significant end. This operation does not change the size of the Bitset.

Parameters
nThe number of bits to shift. This is equivalent to dividing the Bitset by 2^n
Returns
A reference to the resulting Bitset

Definition at line 140 of file bitset.h.

◆ operator^=()

Bitset& dccl::Bitset::operator^= ( const Bitset rhs)
inline

Logical XOR in place.

Apply the result of a logical XOR of this Bitset and another to this Bitset.

Parameters
rhsThe Bitset to perform the operation on with this Bitset.
Returns
A reference to the resulting Bitset
Exceptions
ExceptionThe size of the two Bitsets are not equal.

Definition at line 107 of file bitset.h.

◆ operator|=()

Bitset& dccl::Bitset::operator|= ( const Bitset rhs)
inline

Logical OR in place.

Apply the result of a logical OR of this Bitset and another to this Bitset.

Parameters
rhsThe Bitset to perform the operation on with this Bitset.
Returns
A reference to the resulting Bitset
Exceptions
ExceptionThe size of the two Bitsets are not equal.

Definition at line 92 of file bitset.h.

◆ reset() [1/2]

Bitset& dccl::Bitset::reset ( )
inline

Set all bits false.

Returns
A reference to the resulting Bitset

Definition at line 199 of file bitset.h.

◆ reset() [2/2]

Bitset& dccl::Bitset::reset ( size_type  n)
inline

Reset a bit (i.e. set it to false)

Parameters
nbit to reset
Returns
A reference to the resulting Bitset

Definition at line 194 of file bitset.h.

◆ set() [1/2]

Bitset& dccl::Bitset::set ( )
inline

Set all bits true.

Returns
A reference to the resulting Bitset

Definition at line 184 of file bitset.h.

◆ set() [2/2]

Bitset& dccl::Bitset::set ( size_type  n,
bool  val = true 
)
inline

Set a bit to a given value.

Parameters
nbit to set
valvalue to set the bit to
Returns
A reference to the resulting Bitset

Definition at line 175 of file bitset.h.

◆ test()

bool dccl::Bitset::test ( size_type  n) const
inline

Test a bit (return its value)

Parameters
nbit to test
Returns
value of the bit

Definition at line 224 of file bitset.h.

◆ to()

template<typename IntType >
IntType dccl::Bitset::to ( ) const
inline

Returns the value of the Bitset as a integer.

Returns
Value of the bitset
Exceptions
ExceptionThe integer type cannot represent the current Bitset.

Definition at line 258 of file bitset.h.

◆ to_byte_string() [1/2]

std::string dccl::Bitset::to_byte_string ( )
inline

Returns the value of the Bitset to a byte string, where each character represents 8 bits of the Bitset. The string is used as a byte container, and is not intended to be printed.

Returns
A string containing the value of the Bitset, with the least signficant byte in string[0] and the most significant byte in string[size()-1]

Definition at line 297 of file bitset.h.

◆ to_byte_string() [2/2]

size_t dccl::Bitset::to_byte_string ( char *  buf,
size_t  max_len 
)
inline

Generate a byte string representation of the Bitset, where each character represents 8 bits of the Bitset. The string is used as a byte container, and is not intended to be printed.

Parameters
bufAn output string containing the value of the Bitset, with the least signficant byte in string[0] and the most significant byte in string[size()-1]
max_lenMaximum length of buf
Returns
number of bytes written to buf
Exceptions
std::length_errorif max_len < encoded length.

Definition at line 313 of file bitset.h.


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