简体   繁体   English

序列化模板化子类

[英]Serialize Templatized Sub Class

Thank you very much for your time I really appreciate it 非常感谢您的宝贵时间,我非常感谢

There is a templatized subclass that needs to be serialized with the Cereal Serialization Library, the baseclass is empty, it only exists so we can have a vector of shared_ptr to the base class allowing it to hold multiple types of the templatized subclass, effectively allowing vector storage of multiple variable types. 有一个需要使用Cereal序列化库进行序列化的模板化子类,该基类为空,仅存在,因此我们可以向基类提供一个shared_ptr向量,以使其可以容纳多种类型的模板化子类,从而有效地允许向量存储多种变量类型。

class NetVar_ {};
template <class VARTYPE> class NetVar : public NetVar_
{
public:
    NetVar(VARTYPE Value)
    {
        Var = Value;
    }

    template <class Archive> void serialize(Archive & archive)
    {
        archive(Var);
    }

private:
    VARTYPE Var;
};

The following vector of the baseclass is pushed a few of the subclasses: 基类的以下向量被推入了一些子类:

std::vector<std::shared_ptr<NetVar_>> PacketData;
PacketData.push_back(std::make_shared<NetVar<int>>(32));
PacketData.push_back(std::make_shared<NetVar<int>>(32));
PacketData.push_back(std::make_shared<NetVar<std::string>>('test'));

Finally, the vector is serialized and sent off to a remote machine for processing: 最后,向量被序列化并发送到远程机器进行处理:

std::ostringstream SData;
{
    cereal::PortableBinaryOutputArchive Archive(SData);
    Archive(PacketData);
    //SData is sent to remote machine here through networking library.
}

I must be missing a key piece to the puzzle because when I deserialize the data the program throws an exception, if I debug the values of the output variables are either blank or large negative numbers which leads me to believe the baseclass and or subclass is not getting serialized properly. 我必须错过这个难题的关键部分,因为当我对数据进行反序列化时,程序会引发异常,如果我调试输出变量的值是空白或较大的负数,这会使我相信基类和或子类不是正确序列化。

The code has been simplified down to only expose the issue, for more information about the complete idea you can refer to this question here. 该代码已简化为仅暴露问题,有关完整思想的更多信息,您可以在此处参考此问题。

The following Cereal headers are being included: 以下谷物头包括在内:

#include <cereal\archives\portable_binary.hpp>
#include <cereal\types\vector.hpp>
#include <cereal\types\memory.hpp>
#include <cereal\types\string.hpp>

I'm sure I'll need more as I start to add more types of data into the baseclass. 在开始向基类中添加更多类型的数据时,我肯定会需要更多。

If someone has any idea what's going on here I would greatly appreciate it. 如果有人知道这里发生了什么,我将不胜感激。

Thank you again for your time. 再次感谢您的宝贵时间。

You do not have the choice here, polymorphism need virtuality when you have only access to a base class interface. 您在这里没有选择权,当您只能访问基类接口时,多态性需要虚拟性。 it prevents also Archive to be a template type. 它也阻止Archive成为模板类型。

I imagine cereal is doing some SFINAE to test the existence of the serialize method and have a default behavior if not found. 我想象谷物正在做一些SFINAE来测试序列化方法的存在,如果未找到,则具有默认行为。 That would be the case here as you do not have compilation error. 在这里就是这种情况,因为您没有编译错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM