Reputation: 12138
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
.
Upvotes: 2
Views: 381
Reputation: 14392
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.
Upvotes: 1