按成员序列化成员 [英] Serialization member by member

查看:54
本文介绍了按成员序列化成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了一个 template序列化器 适用于T 类型的任何可简单复制 对象,只需序列化sizeof(T) 字节.

I've implemented a template<typename T> Serializer that works on any trivially copyable object of type T, just serializing sizeof(T) bytes.

然后我为其他类型的兴趣实现了一些(部分)专业化,例如 std::vectorstd::bacis_string>.对于其他类型,我会触发 static_assert(is_trivially_copyable::type, "Unsupported type");.

Then I've implemented a couple of (partial) specializations for other types of interest, like std::vector<T> and std::bacis_string<T>. For other types, I trigger a static_assert(is_trivially_copyable<T>::type, "Unsupported type");.

这不是我想要的,因为我想避免序列化,例如带有裸指针的类型,例如:

This is not what I want, because I want to avoid serializing, for example, types with naked pointers, like:

struct C_style_vector{
    size_t size;
    int* ptr;
};

对于这种类型,我假设用户将定义一个特别的专业.相反,就目前而言,我的 Serializer 不适用于这样的类型:

For such kind of types, I assume that the user will define an ad hoc specialization. Conversely, as far as now my Serializer doesn't work with a type like this:

struct Simple_type{
    double d;
    std::vector<int> v;
};

即使 Simple_type 的每个成员都可以被我的类序列化.

even though every member of Simple_type is serializable by my class.

那么,我如何使用裸指针捕获类型?我如何告诉我的序列化程序序列化仅由可序列化成员组成的类型,一个成员序列化它?

So, how can I catch types with naked pointers? And how can I tell my serializer to serialize types composed only by serializable members, serializing it member by member?

推荐答案

其实并不简单,在 C++ 中是做不到的,没有一些用户的补充,因为 C++ 中没有反射.

It is not actually simple and cannot be done in C++, without some users additions, since there is no reflection in C++.

你可以使用 boost::fusion 之类的东西,但在这种情况下用户应该使用融合序列.我认为最好的方法是使用 boost::serialization,用户必须为自己的类型提供 serialize/deserialize 函数.

You can use something like boost::fusion, but user should use fusion sequences in this case. The best way is that uses boost::serialization I think, user MUST provide serialize/deserialize functions for own types.

融合示例.

template<bool Value, typename Next, typename Last>
struct is_serializable_impl
{
private:
   static const bool cvalue = !boost::is_pointer<
   typename boost::remove_reference<
   typename boost::fusion::result_of::deref<Next>::type>::type>::value;
public:
   static const bool value = Value && is_serializable_impl<
   cvalue, typename boost::fusion::result_of::next<Next>::type, Last>::value;
};

template<bool Value, typename Last>
struct is_serializable_impl<Value, Last, Last>
{
   static const bool value = Value;
};

template<typename T>
struct is_serializable :
is_serializable_impl<true, typename boost::fusion::result_of::begin<T>::type,
   typename boost::fusion::result_of::end<T>::type>
{
};

template<typename T, typename = void>
struct serializer;

template<typename T>
struct serializer<T,
typename boost::enable_if<typename 
boost::fusion::traits::is_sequence<T>::type>::type>
{
   static_assert(is_serializable<T>::value, "Not serializable");
};

实例

这篇关于按成员序列化成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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