From 5cde53a7be38faa21647fc833e48dc13a49b209a Mon Sep 17 00:00:00 2001 From: DawnMagnet Date: Fri, 29 May 2026 15:50:22 +0800 Subject: [PATCH] Update comments and documentation for clarity and consistency across files --- bitserial_all.hpp | 121 +++++++++++++++----------------- include/bitserial/bitserial.hpp | 112 ++++++++++++++--------------- tests/bitserial_tests.cpp | 26 +++---- 3 files changed, 126 insertions(+), 133 deletions(-) diff --git a/bitserial_all.hpp b/bitserial_all.hpp index 30ce0dc..5d1447d 100644 --- a/bitserial_all.hpp +++ b/bitserial_all.hpp @@ -1,11 +1,10 @@ -// This file is generated by `xmake export_all`. +// This file is generated by combining header files. // Do not edit it directly; edit headers under include/bitserial instead. #pragma once // ============================================================================= // include/bitserial/detail/config.hpp // ============================================================================= - #if defined(_MSC_VER) && _MSC_VER < 1929 #error "bitserial requires MSVC 19.29 or newer for C++20 support." #endif @@ -55,11 +54,9 @@ #define BITSERIAL_DETAIL_CAN_BUILD_AVX512BW 1 #endif - // ============================================================================= // include/bitserial/bit_io.hpp // ============================================================================= - #include #include #include @@ -116,11 +113,9 @@ namespace bitserial { } // namespace bitserial - // ============================================================================= // include/bitserial/bulk.hpp // ============================================================================= - #include #include #include @@ -605,25 +600,24 @@ namespace bitserial { } // namespace bitserial - // ============================================================================= // include/bitserial/bitserial.hpp // ============================================================================= // ============================================================================= -// bitserial.hpp — 大端、无填充、零分配的位域序列化框架(C++20) -// 作者:DawnMagnet 时间:2026年4月27日 邮箱:axccjqh@qq.com +// bitserial.hpp — Big-endian, padding-free, zero-alloc bit-field serialization (C++20) +// Author: DawnMagnet Time: April 27, 2026 Email: axccjqh@qq.com // ----------------------------------------------------------------------------- -// 特性: -// * 单次声明同时生成 struct 与序列化 schema(X-Macro 风格) -// * 全编译期展开,无运行时反射开销 -// * 大端 MSB-first 位流,字段间无任何填充 -// * 自动支持 bool、有符号/无符号整数、float32、float64 -// - 位字段读出时按符号位扩展(符号整数) -// * 支持嵌套结构体、定长数组 -// * 提供 bytes / span / hex 三种 IO 形式 -// * 写入用户 std::span,不做内部分配 +// Features: +// * Single declaration generates both struct and serialization schema (X-Macro style) +// * Full compile-time expansion, no runtime reflection overhead +// * Big-endian MSB-first bitstream, no padding between fields +// * Automatic support for bool, signed/unsigned integers, float32, float64 +// - Bit fields are sign-extended on read (for signed integers) +// * Support for nested structures and fixed-length arrays +// * Provides three IO forms: bytes / span / hex +// * Writes to user std::span, no internal allocation // -// 编译要求:C++20(GCC 10+ / Clang 11+ / MSVC 19.29+ 且加 /Zc:preprocessor) +// Compilation requirement: C++20 (GCC 10+ / Clang 11+ / MSVC 19.29+ with /Zc:preprocessor) // ============================================================================= #include @@ -637,17 +631,17 @@ namespace bitserial { #include #include -// IEEE 754 仅在编译期校验一次:浮点序列化依赖标准布局 +// IEEE 754 is validated once at compile time: floating-point serialization depends on standard layout static_assert(sizeof(float) == 4, "bitserial requires 32-bit float"); static_assert(sizeof(double) == 8, "bitserial requires 64-bit double"); namespace bitserial { // ============================================================================= - // 字段描述符(由注册宏自动生成,用户一般不直接构造) + // Field descriptors (auto-generated by registration macros, users typically don't construct directly) // ============================================================================= - /// 标量字段:以 Bits 位打包成员 *MemPtr + /// Scalar field: pack member via *MemPtr using Bits bits template struct Field { static constexpr auto ptr = MemPtr; static constexpr std::size_t bits = Bits; @@ -655,14 +649,14 @@ namespace bitserial { static constexpr bool is_array = false; }; - /// 嵌套结构体字段:递归调用其 schema + /// Nested struct field: recursively invoke its schema template struct Nested { static constexpr auto ptr = MemPtr; static constexpr bool is_nested = true; static constexpr bool is_array = false; }; - /// 定长数组字段:每个元素占 ElemBits 位 + /// Fixed-length array field: each element uses ElemBits bits template struct ArrayField { static constexpr auto ptr = MemPtr; static constexpr std::size_t elem_bits = ElemBits; @@ -672,20 +666,20 @@ namespace bitserial { namespace detail { - /// 字段元组的尾哨兵,吸收宏展开末尾的逗号 + /// Tail sentinel for field tuple, absorbs trailing comma from macro expansion struct tuple_end { static constexpr bool is_nested = false; static constexpr bool is_array = false; static constexpr std::size_t bits = 0; }; - // 从成员指针 M C::* 萃取成员类型 + // Extract member type from member pointer M C::* template struct member_traits; template struct member_traits { using class_type = C; using member_type = M; }; - // 处理 const 修饰的成员指针(const 在 * 右侧) + // Handle const-qualified member pointer (const on right of *) template struct member_traits { using class_type = C; using member_type = M; @@ -694,20 +688,20 @@ namespace bitserial { using member_type_t = typename member_traits::member_type; // ============================================================================= - // 标量 <-> uint64 双向转换:完整处理 bool / 整数 / 浮点 + // Bidirectional scalar <-> uint64 conversion: complete handling of bool / integer / floating-point // ----------------------------------------------------------------------------- - // 规则: - // * bool → 0/1,1 bit 即可 - // * 无符号整型 → 直接转换 - // * 有符号整型 → 按位字段宽度做符号位扩展(read 时) - // * float → bit_cast,按 32 位定长打包 - // * double → bit_cast,按 64 位定长打包 + // Rules: + // * bool → 0/1, 1 bit suffices + // * Unsigned integer → direct conversion + // * Signed integer → sign-extend according to bit field width (on read) + // * float → bit_cast, pack as fixed 32-bit + // * double → bit_cast, pack as fixed 64-bit // - // 位字段宽度 (bits) 的合法性: + // Validity of bit field width (bits): // * bool: 1 - // * 整数: 1..sizeof(T)*8 - // * float32: 必须 == 32 - // * float64: 必须 == 64 + // * integer: 1..sizeof(T)*8 + // * float32: must be == 32 + // * float64: must be == 64 // ============================================================================= template @@ -715,7 +709,7 @@ namespace bitserial { std::is_same_v || std::is_integral_v || std::is_same_v || std::is_same_v; - /// 标量字段的「自然位宽」:用于用户省略位宽时的默认值 + /// "Natural bit width" of scalar field: default value when user omits bit width template consteval std::size_t natural_bits() noexcept { if constexpr (std::is_same_v) return 1; else if constexpr (std::is_integral_v) @@ -732,7 +726,7 @@ namespace bitserial { } } - /// 把标量编码为用于位流的 uint64 + /// Encode scalar as uint64 for bitstream template constexpr std::uint64_t encode_scalar(T v, std::size_t bits) noexcept { static_assert( @@ -747,7 +741,7 @@ namespace bitserial { (void)bits; return std::bit_cast(v); } else if constexpr (std::is_signed_v) { - // 有符号:截取低 bits 位(补码自然表示) + // Signed: truncate to low bits (two's complement natural representation) using U = std::make_unsigned_t; const auto u = static_cast(v); const std::uint64_t mask = @@ -758,7 +752,7 @@ namespace bitserial { } } - /// 把位流读出的 uint64 解码回标量 + /// Decode uint64 from bitstream back to scalar template constexpr T decode_scalar(std::uint64_t raw, std::size_t bits) noexcept { static_assert( @@ -771,7 +765,7 @@ namespace bitserial { } else if constexpr (std::is_same_v) { return std::bit_cast(raw); } else if constexpr (std::is_signed_v) { - // 符号扩展:若最高位(bits-1)为 1,则把高位补 1 + // Sign-extend: if MSB (bits-1) is 1, fill high bits with 1 if (bits < 64) { const std::uint64_t sign_bit = std::uint64_t{1} << (bits - 1); if (raw & sign_bit) { @@ -787,7 +781,7 @@ namespace bitserial { } } - /// 校验位宽合法性(编译期) + /// Validate bit width validity (compile-time) template consteval void validate_bits() noexcept { static_assert( @@ -812,7 +806,7 @@ namespace bitserial { } // namespace detail // ============================================================================= - // Schema 主模板:用户的注册宏会显式特化此模板 + // Main schema template: user registration macros explicitly specialize this // ============================================================================= template struct schema_of; @@ -825,7 +819,7 @@ namespace bitserial { inline constexpr bool has_schema_v = has_schema::value; // ============================================================================= - // 编译期位宽计算 + // Compile-time bit width calculation // ============================================================================= template consteval std::size_t bit_size() noexcept; @@ -864,7 +858,7 @@ namespace bitserial { inline constexpr std::size_t byte_size_v = (bit_size_v + 7) / 8; // ============================================================================= - // 字段读写 + // Field read/write // ============================================================================= template @@ -974,10 +968,10 @@ namespace bitserial { } // ============================================================================= - // 对外 API + // Public API // ============================================================================= - /// 写入用户缓冲(零分配),out 至少需要 byte_size_v 字节 + /// Write to user buffer (zero-alloc), out needs at least byte_size_v bytes template constexpr void to_bytes(const T &obj, std::span out) { if (out.size() < byte_size_v) @@ -986,7 +980,7 @@ namespace bitserial { to_bytes_impl(obj, out, pos); } - /// 序列化为定长 std::array + /// Serialize to fixed-size std::array template [[nodiscard]] constexpr std::array> to_bytes(const T &obj) { @@ -996,7 +990,7 @@ namespace bitserial { return buf; } - /// 反序列化 + /// Deserialize template [[nodiscard]] constexpr T from_bytes(std::span in) { if (in.size() < byte_size_v) @@ -1056,22 +1050,22 @@ namespace bitserial { } // namespace bitserial // ============================================================================= -// 注册宏部分 +// Registration macro section // ----------------------------------------------------------------------------- -// 字段表中可使用: -// BSF##_F (TYPE, NAME) -> 位宽 = 自然位宽(int/float/...) -// BSF##_F (TYPE, NAME, BITS) -> 位宽 = BITS(仅整型/bool 可定制) +// Can use in field table: +// BSF##_F (TYPE, NAME) -> bit width = natural bit width (int/float/...) +// BSF##_F (TYPE, NAME, BITS) -> bit width = BITS (only int/bool customizable) // BSF##_NEST(TYPE, NAME) -// BSF##_ARR (TYPE, NAME, LEN) -> 元素位宽 = 自然位宽 +// BSF##_ARR (TYPE, NAME, LEN) -> element bit width = natural bit width // BSF##_ARR (TYPE, NAME, LEN, EBITS) // ============================================================================= -// ---- 基础工具 --------------------------------------------------------------- +// ---- Basic utilities --------------------------------------------------------------- #define BSF_DETAIL_NTH_3(_1, _2, _3, N, ...) N #define BSF_DETAIL_NTH_4(_1, _2, _3, _4, N, ...) N -// ---- 第一遍:生成 struct 成员 ----------------------------------------------- +// ---- First pass: generate struct members ----------------------------------------------- #define BSF_DETAIL_MEMBER_F_2(TYPE, NAME) TYPE NAME; #define BSF_DETAIL_MEMBER_F_3(TYPE, NAME, BITS) TYPE NAME; @@ -1088,7 +1082,7 @@ namespace bitserial { __VA_ARGS__ \ ) -// ---- 第二遍:生成 schema 元组元素 ------------------------------------------- +// ---- Second pass: generate schema tuple elements ------------------------------------------- #define BSF_DETAIL_SCHEMA_F_2(TYPE, NAME) \ ::bitserial::Field< \ @@ -1114,15 +1108,15 @@ namespace bitserial { __VA_ARGS__ \ ) -// ---- 用户 API --------------------------------------------------------------- +// ---- User API --------------------------------------------------------------- #define BITSERIAL_EMIT_MEMBERS(FIELDS_MACRO) FIELDS_MACRO(BSF_DETAIL_MEMBER) #define BITSERIAL_EMIT_SCHEMA(FIELDS_MACRO) FIELDS_MACRO(BSF_DETAIL_SCHEMA) /** - * @brief 一行声明:同时生成 struct 与 schema_of 特化。 + * @brief Single-line declaration: simultaneously generate struct and schema_of specialization. * - * 用法: + * Usage: * * #define MY_FIELDS(BSF) \ * BSF##_F (std::uint8_t, ver, 4) \ @@ -1145,7 +1139,7 @@ namespace bitserial { ); \ } -/// 拆分版:BEGIN 开启结构体(可在中间追加成员/方法),END 闭合并生成 schema +/// Split version: BEGIN starts struct (can add members/methods in between), END closes and generates schema #define BITSERIAL_BEGIN(TYPE, FIELDS_MACRO) \ struct TYPE { \ BITSERIAL_EMIT_MEMBERS(FIELDS_MACRO) @@ -1159,4 +1153,3 @@ namespace bitserial { BITSERIAL_EMIT_SCHEMA(FIELDS_MACRO)::bitserial::detail::tuple_end{} \ ); \ } - diff --git a/include/bitserial/bitserial.hpp b/include/bitserial/bitserial.hpp index c22f009..64a8ec0 100644 --- a/include/bitserial/bitserial.hpp +++ b/include/bitserial/bitserial.hpp @@ -1,18 +1,18 @@ // ============================================================================= -// bitserial.hpp — 大端、无填充、零分配的位域序列化框架(C++20) -// 作者:DawnMagnet 时间:2026年4月27日 邮箱:axccjqh@qq.com +// bitserial.hpp — Big-endian, padding-free, zero-alloc bit-field serialization (C++20) +// Author: DawnMagnet Time: April 27, 2026 Email: axccjqh@qq.com // ----------------------------------------------------------------------------- -// 特性: -// * 单次声明同时生成 struct 与序列化 schema(X-Macro 风格) -// * 全编译期展开,无运行时反射开销 -// * 大端 MSB-first 位流,字段间无任何填充 -// * 自动支持 bool、有符号/无符号整数、float32、float64 -// - 位字段读出时按符号位扩展(符号整数) -// * 支持嵌套结构体、定长数组 -// * 提供 bytes / span / hex 三种 IO 形式 -// * 写入用户 std::span,不做内部分配 +// Features: +// * Single declaration generates both struct and serialization schema (X-Macro style) +// * Full compile-time expansion, no runtime reflection overhead +// * Big-endian MSB-first bitstream, no padding between fields +// * Automatic support for bool, signed/unsigned integers, float32, float64 +// - Bit fields are sign-extended on read (for signed integers) +// * Support for nested structures and fixed-length arrays +// * Provides three IO forms: bytes / span / hex +// * Writes to user std::span, no internal allocation // -// 编译要求:C++20(GCC 10+ / Clang 11+ / MSVC 19.29+ 且加 /Zc:preprocessor) +// Compilation requirement: C++20 (GCC 10+ / Clang 11+ / MSVC 19.29+ with /Zc:preprocessor) // ============================================================================= #pragma once @@ -28,17 +28,17 @@ #include #include -// IEEE 754 仅在编译期校验一次:浮点序列化依赖标准布局 +// IEEE 754 is validated once at compile time: floating-point serialization depends on standard layout static_assert(sizeof(float) == 4, "bitserial requires 32-bit float"); static_assert(sizeof(double) == 8, "bitserial requires 64-bit double"); namespace bitserial { // ============================================================================= - // 字段描述符(由注册宏自动生成,用户一般不直接构造) + // Field descriptors (auto-generated by registration macros, users typically don't construct directly) // ============================================================================= - /// 标量字段:以 Bits 位打包成员 *MemPtr + /// Scalar field: pack member via *MemPtr using Bits bits template struct Field { static constexpr auto ptr = MemPtr; static constexpr std::size_t bits = Bits; @@ -46,14 +46,14 @@ namespace bitserial { static constexpr bool is_array = false; }; - /// 嵌套结构体字段:递归调用其 schema + /// Nested struct field: recursively invoke its schema template struct Nested { static constexpr auto ptr = MemPtr; static constexpr bool is_nested = true; static constexpr bool is_array = false; }; - /// 定长数组字段:每个元素占 ElemBits 位 + /// Fixed-length array field: each element uses ElemBits bits template struct ArrayField { static constexpr auto ptr = MemPtr; static constexpr std::size_t elem_bits = ElemBits; @@ -63,20 +63,20 @@ namespace bitserial { namespace detail { - /// 字段元组的尾哨兵,吸收宏展开末尾的逗号 + /// Tail sentinel for field tuple, absorbs trailing comma from macro expansion struct tuple_end { static constexpr bool is_nested = false; static constexpr bool is_array = false; static constexpr std::size_t bits = 0; }; - // 从成员指针 M C::* 萃取成员类型 + // Extract member type from member pointer M C::* template struct member_traits; template struct member_traits { using class_type = C; using member_type = M; }; - // 处理 const 修饰的成员指针(const 在 * 右侧) + // Handle const-qualified member pointer (const on right of *) template struct member_traits { using class_type = C; using member_type = M; @@ -85,20 +85,20 @@ namespace bitserial { using member_type_t = typename member_traits::member_type; // ============================================================================= - // 标量 <-> uint64 双向转换:完整处理 bool / 整数 / 浮点 + // Bidirectional scalar <-> uint64 conversion: complete handling of bool / integer / floating-point // ----------------------------------------------------------------------------- - // 规则: - // * bool → 0/1,1 bit 即可 - // * 无符号整型 → 直接转换 - // * 有符号整型 → 按位字段宽度做符号位扩展(read 时) - // * float → bit_cast,按 32 位定长打包 - // * double → bit_cast,按 64 位定长打包 + // Rules: + // * bool → 0/1, 1 bit suffices + // * Unsigned integer → direct conversion + // * Signed integer → sign-extend according to bit field width (on read) + // * float → bit_cast, pack as fixed 32-bit + // * double → bit_cast, pack as fixed 64-bit // - // 位字段宽度 (bits) 的合法性: + // Validity of bit field width (bits): // * bool: 1 - // * 整数: 1..sizeof(T)*8 - // * float32: 必须 == 32 - // * float64: 必须 == 64 + // * integer: 1..sizeof(T)*8 + // * float32: must be == 32 + // * float64: must be == 64 // ============================================================================= template @@ -106,7 +106,7 @@ namespace bitserial { std::is_same_v || std::is_integral_v || std::is_same_v || std::is_same_v; - /// 标量字段的「自然位宽」:用于用户省略位宽时的默认值 + /// "Natural bit width" of scalar field: default value when user omits bit width template consteval std::size_t natural_bits() noexcept { if constexpr (std::is_same_v) return 1; else if constexpr (std::is_integral_v) @@ -123,7 +123,7 @@ namespace bitserial { } } - /// 把标量编码为用于位流的 uint64 + /// Encode scalar as uint64 for bitstream template constexpr std::uint64_t encode_scalar(T v, std::size_t bits) noexcept { static_assert( @@ -138,7 +138,7 @@ namespace bitserial { (void)bits; return std::bit_cast(v); } else if constexpr (std::is_signed_v) { - // 有符号:截取低 bits 位(补码自然表示) + // Signed: truncate to low bits (two's complement natural representation) using U = std::make_unsigned_t; const auto u = static_cast(v); const std::uint64_t mask = @@ -149,7 +149,7 @@ namespace bitserial { } } - /// 把位流读出的 uint64 解码回标量 + /// Decode uint64 from bitstream back to scalar template constexpr T decode_scalar(std::uint64_t raw, std::size_t bits) noexcept { static_assert( @@ -162,7 +162,7 @@ namespace bitserial { } else if constexpr (std::is_same_v) { return std::bit_cast(raw); } else if constexpr (std::is_signed_v) { - // 符号扩展:若最高位(bits-1)为 1,则把高位补 1 + // Sign-extend: if MSB (bits-1) is 1, fill high bits with 1 if (bits < 64) { const std::uint64_t sign_bit = std::uint64_t{1} << (bits - 1); if (raw & sign_bit) { @@ -178,7 +178,7 @@ namespace bitserial { } } - /// 校验位宽合法性(编译期) + /// Validate bit width validity (compile-time) template consteval void validate_bits() noexcept { static_assert( @@ -203,7 +203,7 @@ namespace bitserial { } // namespace detail // ============================================================================= - // Schema 主模板:用户的注册宏会显式特化此模板 + // Main schema template: user registration macros explicitly specialize this // ============================================================================= template struct schema_of; @@ -216,7 +216,7 @@ namespace bitserial { inline constexpr bool has_schema_v = has_schema::value; // ============================================================================= - // 编译期位宽计算 + // Compile-time bit width calculation // ============================================================================= template consteval std::size_t bit_size() noexcept; @@ -255,7 +255,7 @@ namespace bitserial { inline constexpr std::size_t byte_size_v = (bit_size_v + 7) / 8; // ============================================================================= - // 字段读写 + // Field read/write // ============================================================================= template @@ -365,10 +365,10 @@ namespace bitserial { } // ============================================================================= - // 对外 API + // Public API // ============================================================================= - /// 写入用户缓冲(零分配),out 至少需要 byte_size_v 字节 + /// Write to user buffer (zero-alloc), out needs at least byte_size_v bytes template constexpr void to_bytes(const T &obj, std::span out) { if (out.size() < byte_size_v) @@ -377,7 +377,7 @@ namespace bitserial { to_bytes_impl(obj, out, pos); } - /// 序列化为定长 std::array + /// Serialize to fixed-size std::array template [[nodiscard]] constexpr std::array> to_bytes(const T &obj) { @@ -387,7 +387,7 @@ namespace bitserial { return buf; } - /// 反序列化 + /// Deserialize template [[nodiscard]] constexpr T from_bytes(std::span in) { if (in.size() < byte_size_v) @@ -447,22 +447,22 @@ namespace bitserial { } // namespace bitserial // ============================================================================= -// 注册宏部分 +// Registration macro section // ----------------------------------------------------------------------------- -// 字段表中可使用: -// BSF##_F (TYPE, NAME) -> 位宽 = 自然位宽(int/float/...) -// BSF##_F (TYPE, NAME, BITS) -> 位宽 = BITS(仅整型/bool 可定制) +// Can use in field table: +// BSF##_F (TYPE, NAME) -> bit width = natural bit width (int/float/...) +// BSF##_F (TYPE, NAME, BITS) -> bit width = BITS (only int/bool customizable) // BSF##_NEST(TYPE, NAME) -// BSF##_ARR (TYPE, NAME, LEN) -> 元素位宽 = 自然位宽 +// BSF##_ARR (TYPE, NAME, LEN) -> element bit width = natural bit width // BSF##_ARR (TYPE, NAME, LEN, EBITS) // ============================================================================= -// ---- 基础工具 --------------------------------------------------------------- +// ---- Basic utilities --------------------------------------------------------------- #define BSF_DETAIL_NTH_3(_1, _2, _3, N, ...) N #define BSF_DETAIL_NTH_4(_1, _2, _3, _4, N, ...) N -// ---- 第一遍:生成 struct 成员 ----------------------------------------------- +// ---- First pass: generate struct members ----------------------------------------------- #define BSF_DETAIL_MEMBER_F_2(TYPE, NAME) TYPE NAME; #define BSF_DETAIL_MEMBER_F_3(TYPE, NAME, BITS) TYPE NAME; @@ -479,7 +479,7 @@ namespace bitserial { __VA_ARGS__ \ ) -// ---- 第二遍:生成 schema 元组元素 ------------------------------------------- +// ---- Second pass: generate schema tuple elements ------------------------------------------- #define BSF_DETAIL_SCHEMA_F_2(TYPE, NAME) \ ::bitserial::Field< \ @@ -505,15 +505,15 @@ namespace bitserial { __VA_ARGS__ \ ) -// ---- 用户 API --------------------------------------------------------------- +// ---- User API --------------------------------------------------------------- #define BITSERIAL_EMIT_MEMBERS(FIELDS_MACRO) FIELDS_MACRO(BSF_DETAIL_MEMBER) #define BITSERIAL_EMIT_SCHEMA(FIELDS_MACRO) FIELDS_MACRO(BSF_DETAIL_SCHEMA) /** - * @brief 一行声明:同时生成 struct 与 schema_of 特化。 + * @brief Single-line declaration: simultaneously generate struct and schema_of specialization. * - * 用法: + * Usage: * * #define MY_FIELDS(BSF) \ * BSF##_F (std::uint8_t, ver, 4) \ @@ -536,7 +536,7 @@ namespace bitserial { ); \ } -/// 拆分版:BEGIN 开启结构体(可在中间追加成员/方法),END 闭合并生成 schema +/// Split version: BEGIN starts struct (can add members/methods in between), END closes and generates schema #define BITSERIAL_BEGIN(TYPE, FIELDS_MACRO) \ struct TYPE { \ BITSERIAL_EMIT_MEMBERS(FIELDS_MACRO) diff --git a/tests/bitserial_tests.cpp b/tests/bitserial_tests.cpp index 704ce24..422496b 100644 --- a/tests/bitserial_tests.cpp +++ b/tests/bitserial_tests.cpp @@ -8,7 +8,7 @@ #include // ============================================================================= -// 1) 无符号整数 + 跨字节位宽 +// 1) Unsigned integers + cross-byte bit widths // ============================================================================= #define UINT_FIELDS(BSF) \ @@ -52,7 +52,7 @@ TEST_CASE("unsigned integers with cross-byte bit widths") { } // ============================================================================= -// 2) 有符号整数:正、负、边界、位宽截取与符号扩展 +// 2) Signed integers: positive, negative, boundary, bit width truncation and sign extension // ============================================================================= #define INT_FIELDS(BSF) \ @@ -148,7 +148,7 @@ TEST_CASE("signed integers: boundary values") { } // ============================================================================= -// 3) bool:单 bit 与多 bit +// 3) bool: single bit and multi-bit // ============================================================================= #define BOOL_FIELDS(BSF) \ @@ -175,7 +175,7 @@ TEST_CASE("bool fields: single and multi-bit") { } // ============================================================================= -// 4) float / double:含特殊值 +// 4) float / double: with special values // ============================================================================= #define FLOAT_FIELDS(BSF) \ @@ -233,7 +233,7 @@ TEST_CASE("float and double: special values (inf, nan, -0, subnormal)") { } // ============================================================================= -// 5) 嵌套结构体 +// 5) Nested structures // ============================================================================= #define INNER_FIELDS(BSF) \ @@ -267,7 +267,7 @@ TEST_CASE("nested structs") { } // ============================================================================= -// 6) 定长数组(自然位宽 / 自定义位宽) +// 6) Fixed-length arrays (natural bit width / custom bit width) // ============================================================================= #define ARR_NATURAL_FIELDS(BSF) \ @@ -329,7 +329,7 @@ TEST_CASE("arrays: packed with custom bit widths") { } // ============================================================================= -// 7) HEX 与 bytes 双向转换 +// 7) Bidirectional HEX and bytes conversion // ============================================================================= TEST_CASE("hex roundtrip: upper and lowercase") { @@ -352,7 +352,7 @@ TEST_CASE("hex roundtrip: upper and lowercase") { } // ============================================================================= -// 8) 字节序:手工验证 MSB-first 的位排布 +// 8) Byte order: manual verification of MSB-first bit layout // ============================================================================= TEST_CASE("byte order: MSB-first layout verification") { @@ -378,7 +378,7 @@ TEST_CASE("byte order: MSB-first layout verification") { } // ============================================================================= -// 9) 边界与异常 +// 9) Boundary and error handling // ============================================================================= TEST_CASE("error handling: buffer too small") { @@ -404,7 +404,7 @@ TEST_CASE("error handling: hex parsing errors") { } // ============================================================================= -// 10) 真实协议示例:IPv4 头部前 12 字节 +// 10) Real protocol example: first 12 bytes of IPv4 header // ============================================================================= #define IPV4_FIELDS(BSF) \ @@ -453,7 +453,7 @@ TEST_CASE("real protocol: IPv4 header layout") { } // ============================================================================= -// 11) 综合压力:所有标量类型 + 嵌套 + 数组 一次往返 +// 11) Comprehensive stress: all scalar types + nested + arrays round trip // ============================================================================= #define ALL_INNER_FIELDS(BSF) BSF##_F(std::int32_t, a) BSF##_F(double, b) @@ -535,7 +535,7 @@ TEST_CASE("comprehensive: all types combined stress test") { } // ============================================================================= -// 12) 用户提供 span 的零分配路径 +// 12) User-provided buffer: zero-allocation path // ============================================================================= TEST_CASE("user-provided buffer: zero-allocation path") { @@ -558,7 +558,7 @@ TEST_CASE("user-provided buffer: zero-allocation path") { } // ============================================================================= -// 13) constexpr 编译期序列化 +// 13) constexpr compile-time serialization // ============================================================================= constexpr UnsignedPkt make_pkt() {