将任何数据类型序列化为向量< uint8_t> - 使用reinterpret_cast? [英] serialize any data type as vector<uint8_t> - use reinterpret_cast?

查看:1491
本文介绍了将任何数据类型序列化为向量< uint8_t> - 使用reinterpret_cast?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有在搜索中发现任何与搜索直接相关的内容,因此请原谅,如果这是重复的。

I didnt find anything directly related in searching, so please forgive if this is a duplicate.

我想要做的是通过网络连接序列化数据。我的方法是转换我需要传递到一个 std :: vector< uint8_t> ,并在接收端将数据解包到适当的变量中。我的方法看起来像这样:

What I am looking to do is serialize data across a network connection. My approach is to convert everything I need to transfer to a std::vector< uint8_t > and on the receiving side unpack the data into the appropriate variables. My approach looks like this:

template <typename T>
inline void pack (std::vector< uint8_t >& dst, T& data) {
    uint8_t * src = static_cast < uint8_t* >(static_cast < void * >(&data));
    dst.insert (dst.end (), src, src + sizeof (T));
}   

template <typename T>
inline void unpack (vector <uint8_t >& src, int index, T& data) {
    copy (&src[index], &src[index + sizeof (T)], &data);
}

我使用的是

vector< uint8_t > buffer;
uint32_t foo = 103, bar = 443;
pack (buff, foo);
pack (buff, bar);

// And on the receive side
uint32_t a = 0, b = 0;
size_t offset = 0;
unpack (buffer, offset, a);
offset += sizeof (a);
unpack (buffer, offset, b);

我关心的是

uint8_t * src = static_cast< uint8_t *>(static_cast< void *>(& data));

as reinterpret_cast )。有没有更好的方法来完成这个没有双重铸造?

line (which I understand to do the same as reinterpret_cast). Is there a better way to accomplish this without the double cast?

我天真的方法是只使用 static_cast< uint8_t *>(& data)失败。我过去曾被告知 reinterpret_cast 是坏的。所以我想避免它(或者我现在的构造)如果可能。

My naive approach was to just use static_cast< uint8_t* >(&data) which failed. I've been told in the past that reinterpret_cast is bad. So I'd like to avoid it (or the construct I have currently) if possible.

当然,总是有 uint8_t * src = (uint8_t *)(& data)

建议?

推荐答案

我的建议是忽略所有的人告诉你 reinterpret_cast 是坏的。他们告诉你这是坏的,因为它通常不是一个好的习惯,采取一种类型的内存映射,假装它是另一种类型。但在这种情况下,这正是你想做的,因为你的整个目的是传输内存映射作为一系列字节。

My suggestion is to ignore all the people telling you that reinterpret_cast is bad. They tell you it is bad, because it's generally not a good practice to take the memory map of one type and pretend that it's another type. But in this case, that is exactly what you want to do, as your entire purpose is to transmit the memory map as a series of bytes.

使用双 - static_cast ,因为它完全详细说明了你正在采取一种类型,有意假装它是其他的事实。这种情况正是 reinterpret_cast 的用法,并且躲避使用它与一个void指针中介只是遮掩你的意思没有好处。

It is far better than using a double-static_cast, as it fully details the fact that you are taking one type and purposefully pretending that it is something else. This situation is exactly what reinterpret_cast is for, and dodging using it with a void pointer intermediary is simply obscuring your meaning with no benefit.

此外,我确定你知道这一点,但注意T中的指针。

Also, I'm sure that you're aware of this, but watch for pointers in T.

这篇关于将任何数据类型序列化为向量&lt; uint8_t&gt; - 使用reinterpret_cast?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆