ad67d5c71b8ba9b8fe048f4752d26ac5b68a8934
bitserial
Big-endian, zero-padding, zero-allocation bit-field serialization framework for C++20
Features
- Single-declaration schema: Define struct and serialization schema simultaneously (X-Macro style)
- Compile-time expansion: No runtime reflection overhead
- Big-endian MSB-first: No padding between fields, bit-accurate packing
- Rich type support:
bool, signed/unsigned integers,float32,float64- Automatic sign-extension for signed integers when reading
- Composable: Nested structs, fixed-length arrays
- Multiple IO formats: Bytes, span, hex
- Zero-allocation: Writes directly to user-provided
std::span
Requirements
- C++20 compiler
- GCC 10+, Clang 11+, MSVC 19.29+ (with
/Zc:preprocessor)
- GCC 10+, Clang 11+, MSVC 19.29+ (with
Quick Start
#include "bitserial.hpp"
// 1. Define packet schema
#define MY_PACKET_FIELDS(BSF) \
BSF##_F (std::uint8_t, version, 4) \
BSF##_F (std::uint8_t, flags, 4) \
BSF##_F (std::int16_t, temp) \
BSF##_F (bool, ok) \
BSF##_F (float, voltage) \
BSF##_ARR(std::uint8_t, mac, 6)
BITSERIAL_DEFINE(MyPacket, MY_PACKET_FIELDS);
// 2. Serialize
MyPacket pkt{
.version = 0xA,
.flags = 0x5,
.temp = -42,
.ok = true,
.voltage = 3.7f,
.mac = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66},
};
auto bytes = bitserial::to_bytes(pkt);
auto hex = bitserial::to_hex(pkt); // "A5..."
// 3. Deserialize
auto pkt2 = bitserial::from_bytes<MyPacket>(bytes);
auto pkt3 = bitserial::from_hex<MyPacket>(hex);
Field Types
| Macro | Description | Example |
|---|---|---|
BSF_F(T, name) |
Scalar with natural bit width | BSF_F(std::uint16_t, x) → 16 bits |
BSF_F(T, name, bits) |
Scalar with custom bit width | BSF_F(std::uint8_t, x, 4) → 4 bits |
BSF_NEST(T, name) |
Nested struct | BSF_NEST(Header, hdr) |
BSF_ARR(T, name, len) |
Fixed array with natural element width | BSF_ARR(std::uint8_t, mac, 6) |
BSF_ARR(T, name, len, ebits) |
Fixed array with custom element bits | BSF_ARR(std::int16_t, d, 4, 10) |
Compile-time Size Computation
static_assert(bitserial::bit_size_v<MyPacket> == 124);
static_assert(bitserial::byte_size_v<MyPacket> == 16); // (124 + 7) / 8
User-provided Buffer (Zero-allocation)
std::vector<std::uint8_t> buf(bitserial::byte_size_v<MyPacket>);
bitserial::to_bytes(pkt, std::span<std::uint8_t>(buf));
Error Handling
// Throws std::out_of_range if buffer is too small
// Throws std::invalid_argument for invalid hex characters
Real-world Example: IPv4 Header
#define IPV4_FIELDS(BSF) \
BSF##_F(std::uint8_t, version, 4) \
BSF##_F(std::uint8_t, ihl, 4) \
BSF##_F(std::uint8_t, tos) \
BSF##_F(std::uint16_t, total_len) \
BSF##_F(std::uint16_t, id) \
BSF##_F(std::uint8_t, flags, 3) \
BSF##_F(std::uint16_t, frag_off, 13) \
BSF##_F(std::uint8_t, ttl) \
BSF##_F(std::uint8_t, proto) \
BSF##_F(std::uint16_t, checksum)
BITSERIAL_DEFINE(IPv4Head, IPV4_FIELDS);
static_assert(bitserial::byte_size_v<IPv4Head> == 12);
Split Definition (Add Methods)
#define SENSOR_FIELDS(BSF) \
BSF##_F(std::uint16_t, id) \
BSF##_F(std::int32_t, value)
BITSERIAL_BEGIN(SensorData, SENSOR_FIELDS)
bool is_valid() const { return value != 0; }
BITSERIAL_END(SensorData, SENSOR_FIELDS);
License
MIT
Languages
C++
91.6%
CMake
3.4%
Makefile
2.7%
Lua
2.3%