C++ 我应该在处理序列化时硬编码变量大小吗?C++;
我有一个简单的程序,它可以进行一些预计算,并将结构序列化为二进制文件,然后由另一个程序加载。当这个程序在我的机器上运行时,如果依赖sizeof()函数,会不会导致问题?我知道一些处理器之间可能存在大小不一的情况,这就是为什么我考虑对它们进行硬编码的原因,因为要加载的文件仅在我的计算机上创建。当我们希望通过网络传输数据时,我们应该牢记以下几个目标:C++ 我应该在处理序列化时硬编码变量大小吗?C++;,c++,serialization,C++,Serialization,我有一个简单的程序,它可以进行一些预计算,并将结构序列化为二进制文件,然后由另一个程序加载。当这个程序在我的机器上运行时,如果依赖sizeof()函数,会不会导致问题?我知道一些处理器之间可能存在大小不一的情况,这就是为什么我考虑对它们进行硬编码的原因,因为要加载的文件仅在我的计算机上创建。当我们希望通过网络传输数据时,我们应该牢记以下几个目标: 可移植性—在任何主机上以任何语言、任何处理器体系结构读取和写入数据的能力 速度-网络传输比内存访问慢几个数量级。我们发送和接收的数据越少,应用程序的响
#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));
}
#包括
#包括
#包括
#包括
#包括
#包括
名称空间notstd{
使用byte=std::uint8\u t;
}
模板结构未签名\u版本;
模板结构无符号_版本{using type=std::uint16_t;};
模板结构无符号_版本{using type=std::uint32_t;};
模板结构无符号_版本{using type=std::uint64_t;};
使用unsigned_version_t=typename unsigned_version::type的模板;
模板结构签名\u版本;
模板结构签名的_版本{using type=std::int16_t;};
模板结构签名\u版本{using type=std::int32\u t;};
模板结构签名\u版本{using type=std::int64\u t;};
使用签名版本的模板\u t=typename签名版本::type;
模板
自动锯齿形(有符号输入)->无符号版本
{
使用单词类型=无符号版本;
constexpr自动位计数=标准::数值限制::数字;
自动结果=字类型((输入>(位计数-1));
返回结果;
}
模板
自动解压(无符号输入)->有符号版本
{
自动负片=输入&1;
自动累计=(输入>>1);
如果(否定)
accum=~accum;
自动结果=有符号的版本;
std::memcpy(std::addressof(结果)、std::addressof(累计)、sizeof(结果));
返回结果;
}
模板
自动序列化(签名输入,输出)-输出
{
使用notstd::byte;
自动换档器=Z字形(输入);
bool last_byte=false;
做
{
如果(换档杆<128)
最后一个字节=真;
自动值=字节(移位器和0x7f);
如果(不是最后一个字节)val |=字节(0x80);
*iter++=val;
换档杆>>=7;
}while(不是最后一个字节);
返回iter;
}
模板
自动反序列化(InIter和iter、Sentinel last)->SignedInt
{
使用notstd::byte;
使用累计类型=无符号版本;
自动累计=累计类型(0);
int-shift=0;
while(iter!=最后一次)
{
自动值=字节(*iter++);
自动移位器=(accum_type(val)&0x7f)您希望尽可能避免使用幻数。此外,一般来说,当您序列化为二进制时,您希望它是“可移植的”二进制,而不是特定机器的二进制。为什么不使用cstdint中的类型?也不要忘记序列化的另一个问题是结构中数据字段的对齐。如果不知道存在这样的问题,将研究这一问题,不仅是大小问题,还有操作系统的持久性问题。(输入>>(位计数-1))
将符号传播到所有位,但是最低位。这是有意的吗?@MaximeGroushkin是的。这是符号位到所有位的算术移位。本质上与if(x<0)x=~x;相同。
感谢您介绍我这一点。这是教我一些东西的投票。