c++ - 在处理序列化时我应该硬编码变量大小吗? C++

标签 c++ serialization

我有一个简单的程序,可以进行一些预先计算并将结构序列化为二进制文件,然后由另一个程序加载该文件,虽然这在我的机器上有效,但如果我依赖 sizeof() 函数,它会导致问题吗?我知道可变大小在某些处理器之间可能会有所不同,这就是我考虑对它们进行硬编码的原因,因为要加载的文件仅在我的计算机上创建。

最佳答案

当我们想要通过网络传输数据时,我们应该牢记几个目标:

  1. 可移植性 - 能够以任何语言、使用任何处理器架构在任何主机上读取和写入数据。

  2. 速度 - 网络传输比内存访问慢几个数量级。我们发送和接收的数据越少,我们的应用程序的响应速度就越快。

用有符号整数实现此目的的一种流行方法(对于其他类型还有其他技术)称为之字形编码

它用于谷歌的 Protocol Buffer 和许多其他数据传输模式。

Zig-zag 编码的优点是传输的字节数取决于您正在传输的数字的大小,而不是二进制位数。大多数数字很小。因此,传输一个小负数的所有前导 1 是没有意义的。它们可以被暗示。

这里是 zig-zag 编码的实现,适用于 16、32 和 64 位的整数。

随意拉伸(stretch)。

请注意,无符号整数不需要进行锯齿形编码,字符串很容易 - 使用可变长度整数编码长度,后跟 N 字节字符串数据。

#include <cstddef>
#include <cstdint>
#include <cassert>
#include <limits>
#include <memory>
#include <cstring>

namespace notstd {
    using byte = std::uint8_t;
}

template<class SignedInt> struct unsigned_version;
template<> struct unsigned_version<std::int16_t> { using type = std::uint16_t; };
template<> struct unsigned_version<std::int32_t> { using type = std::uint32_t; };
template<> struct unsigned_version<std::int64_t> { using type = std::uint64_t; };
template<class SignedInt> using unsigned_version_t = typename unsigned_version<SignedInt>::type;

template<class UnSignedInt> struct signed_version;
template<> struct signed_version<std::uint16_t> { using type = std::int16_t; };
template<> struct signed_version<std::uint32_t> { using type = std::int32_t; };
template<> struct signed_version<std::uint64_t> { using type = std::int64_t; };
template<class UnSignedInt> using signed_version_t = typename signed_version<UnSignedInt>::type;

template<class SignedInt>
auto zig_zag(SignedInt input) -> unsigned_version_t<SignedInt>
{
    using word_type = unsigned_version_t<SignedInt>;
    constexpr auto bit_count = std::numeric_limits<word_type>::digits;
    auto result = word_type((input << 1) ^ (input >> (bit_count - 1)));
    return result;
}

template<class UnsignedInt>
auto unzig_zag(UnsignedInt input) -> signed_version_t<UnsignedInt>
{
    auto negative = input & 1;
    auto accum = (input >> 1);
    if (negative)
        accum = ~accum;
    auto result = signed_version_t<UnsignedInt>();
    std::memcpy(std::addressof(result), std::addressof(accum), sizeof(result));
    return result;
}

template<class SignedInt, class OutIter>
auto serialise(SignedInt input, OutIter iter) -> OutIter
{
    using notstd::byte;

    auto shifter = zig_zag(input);

    bool last_byte = false;
    do
    {
        if (shifter < 128)
            last_byte = true;
        auto val = byte(shifter & 0x7f);
        if (not last_byte) val |= byte(0x80);
        *iter++ = val;
        shifter >>= 7;
    } while (not last_byte);

    return iter;
}

template<class SignedInt, class InIter, class Sentinel>
auto deserialise(InIter& iter, Sentinel last) -> SignedInt
{
    using notstd::byte;

    using accum_type = unsigned_version_t<SignedInt>;
    auto accum = accum_type(0);
    int shift = 0;
    while (iter != last)
    {
        auto val = byte(*iter++);
        auto shifter = (accum_type(val) & 0x7f) << shift;
        accum |= shifter;
        if ((val & byte(0x80)) == byte(0))
        {
            break;
        }
        shift += 7;
    }
    return unzig_zag(accum);
}


#include <vector>
#include <iterator>


int main()
{
    using notstd::byte;
    auto buffer = std::vector<byte>();

    std::int32_t i = 16;
    auto iz = zig_zag(i);
    auto iuz = unzig_zag(iz);
    assert(i == iuz);

    i = -16;
    iz = zig_zag(i);
    iuz = unzig_zag(iz);
    assert(i == iuz);

    auto i1 = std::int16_t(3);
    auto i2 = std::int32_t(8736);
    auto i3 = std::int64_t(-7333738);

    auto iout = serialise(i1, back_inserter(buffer));
    iout = serialise(i2, iout);
    iout = serialise(i3, iout);


    auto iin = begin(buffer);
    auto o1 = deserialise<decltype(i1)>(iin, end(buffer));
    auto o2 = deserialise<decltype(i2)>(iin, end(buffer));
    auto o3 = deserialise<decltype(i3)>(iin, end(buffer));

    assert(i1 == o1);
    assert(i2 == o2);
    assert(i3 == o3);
    assert(iin == end(buffer));
}

关于c++ - 在处理序列化时我应该硬编码变量大小吗? C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46304708/

相关文章:

带有枚举的 C++ Visual Studio 调试器错误

c++ - 找到两个给定单词和字典之间的最短单词阶梯

c++ - C++ 标准对于 move 事件对象存储有何规定?

java - Java 中如何序列化属性?

java - 创建 SpecificDatumWriter<T> 时出现 NullPointerException

c++ - -Wconversion warning while using operator <<= on unsigned char

c++ - 不能长疙瘩

php - 如果输入复选框,如何更新序列化数组

c# - 具有动态元素名称的序列化和通用列表

swift - 有没有办法对闭包进行编码和解码?