2

I'm looking for a C++ API to conveniently an efficiently serialize/unserialize (user-defined) types of arbitrary bit-size into some structure (preferrably template class) containing a set of bits. I believe boost::dynamic_bitset is good start but it doesn't contain functions/operators for appending builtin-types such uint8_t, uint16_t etc.

I want this to work something like in the following example

boost::dynamic_bitset bs;
bs.append((uint8_t)123);
bs.append((uint16_t)12345, big_endian);

Stream operators should be supported aswell with a state-dependent endian-behaviour:

bs << (uint8_t)(123);
bs << little_endian;
bs << (uint16_t)(12345); // serialized as little_endian
bs << big_endian;

I also know of Boost.Serialization and Boost.Endian (in Boost Sandbox). I would like something like that extended to the bit-level.

I believe boost::dynamic_bitset's member functions

void append(Block block);

and

template <typename BlockInputIterator>
void append(BlockInputIterator first, BlockInputIterator last);

are too low level for most users. I guess it should be reused in these new append functions. To optimize even more expression templates could be used to group consecutive serializations into larger (byte-sized) blocks before appending them to the dynamic_bitset.

Nordlöw
  • 11,838
  • 10
  • 52
  • 99

1 Answers1

1

I'm not aware of any library, but it isn't that hard to write a BitBufferReader/Writer yourself and it will be completely to your requirements.

stefaanv
  • 14,072
  • 2
  • 31
  • 53
  • Is `BitBufferReader/Writer` some class you instantiate `boost::dynamic_bitset` with? – Nordlöw Apr 13 '12 at 11:33
  • 1
    I would use an ordinary container (vector/array) with a byte-offset and a bit-offset, but you can use what you find most appropriate. – stefaanv Apr 13 '12 at 11:57