我正在使用cereal库,并尝试使用自定义构造函数获取派生类型的shared_ptr实例,以将其正确地馈入存档。我倾倒了以下documentation,但显然缺少一些内容。

以下是我的继承结构中的类:

class TextureDefinition : public std::enable_shared_from_this<TextureDefinition> {
    template <class Archive>
    friend void serialize(Archive & archive, TextureDefinition & texture);
public:
    virtual ~TextureDefinition(){}
    std::shared_ptr<TextureHandle> makeHandle();
    std::shared_ptr<TextureHandle> makeHandle(const Point<int> &a_position, const Size<int> &a_size);

    void setOnReload(std::function< void (std::shared_ptr<TextureDefinition>) > a_onReload);
    void clearOnReload();

    GLuint textureId() const;
    std::string name() const;
    Size<int> size() const;

    //bookkeeping
    void reload();
    void cleanup();

protected:
    TextureDefinition(const std::string &a_name);

    std::string textureName;
    Size<int> textureSize;
    GLuint texture;

    std::vector< std::weak_ptr<TextureHandle> > handles;

private:
    virtual void reloadImplementation() = 0;
    virtual void cleanupImplementation(){}

    std::function< void (std::shared_ptr<TextureDefinition>) > onReload;
};

template <class Archive>
void serialize(Archive & archive, TextureDefinition &texture){
    archive(cereal::make_nvp("name", texture.name()), cereal::make_nvp("size", texture.size()));// , cereal::make_nvp("handles", handles)); //not saving handles right now to simplify testing.
}

class FileTextureDefinition : public TextureDefinition {
public:
    static std::shared_ptr<FileTextureDefinition> make(const std::string &a_filename, bool a_repeat = false){
        return std::shared_ptr<FileTextureDefinition>(new FileTextureDefinition(a_filename, a_repeat));
    }

    template <class Archive>
    static FileTextureDefinition * load_and_allocate(Archive &ar){
        return new FileTextureDefinition("", false);
    }

    template <class Archive>
    void serialize(Archive & archive){
        archive(cereal::base_class<TextureDefinition>(this), CEREAL_NVP(repeat));
        if(!handles.empty()){
            reload();
        }
    }
private:
    FileTextureDefinition(const std::string &a_filename, bool a_repeat):
        TextureDefinition(a_filename),
        repeat(a_repeat){
    }
    virtual void reloadImplementation();

    bool repeat;
};

这是我试图运行的测试代码:
void saveTest(){
    std::stringstream stream;
    {
        cereal::JSONOutputArchive archive(stream);
        auto testSave = MV::FileTextureDefinition::make("test.png");
        archive(cereal::make_nvp("test", testSave));
    }
    std::cout << stream.str() << std::endl;
}

我的“大小”类型确实正确序列化了。我还包括了 Cereal /类型/memory.hpp, Cereal /类型/map.hpp, Cereal /类型/string.hpp和 Cereal /类型/base_class.hpp,但是当我尝试对FileTextureDefinition运行上面的测试时,我得到了出现以下错误:
1>C:\git\external\cereal\include\cereal/cereal.hpp(432): error C2338: Trying to serialize an unserializable type with an output archive.
1>
1>  Types must either have a serialize function, or separate save/load functions (but not both).
1>  In addition, you may not mix versioned with non-versioned serialization functions.
1>  Serialize functions generally have the following signature:
1>
1>  template<class Archive>
1>    void serialize(Archive & ar)
1>    {
1>      ar( member1, member2, member3 );
1>    }
1>
1>
1>          C:\git\external\cereal\include\cereal/cereal.hpp(329) : see reference to function template instantiation 'cereal::JSONOutputArchive &cereal::OutputArchive<cereal::JSONOutputArchive,0>::processImpl<std::shared_ptr<MV::FileTextureDefinition>>(const T &)' being compiled
1>          with
1>          [
1>              T=std::shared_ptr<MV::FileTextureDefinition>
1>          ]
1>          C:\git\external\cereal\include\cereal/cereal.hpp(329) : see reference to function template instantiation 'cereal::JSONOutputArchive &cereal::OutputArchive<cereal::JSONOutputArchive,0>::processImpl<std::shared_ptr<MV::FileTextureDefinition>>(const T &)' being compiled
1>          with
1>          [
1>              T=std::shared_ptr<MV::FileTextureDefinition>
1>          ]
1>          C:\git\external\cereal\include\cereal/cereal.hpp(245) : see reference to function template instantiation 'void cereal::OutputArchive<cereal::JSONOutputArchive,0>::process<std::shared_ptr<MV::FileTextureDefinition>&>(T)' being compiled
1>          with
1>          [
1>              T=std::shared_ptr<MV::FileTextureDefinition> &
1>          ]
1>          C:\git\external\cereal\include\cereal/cereal.hpp(245) : see reference to function template instantiation 'void cereal::OutputArchive<cereal::JSONOutputArchive,0>::process<std::shared_ptr<MV::FileTextureDefinition>&>(T)' being compiled
1>          with
1>          [
1>              T=std::shared_ptr<MV::FileTextureDefinition> &
1>          ]
1>          C:\git\external\cereal\include\cereal/archives/json.hpp(711) : see reference to function template instantiation 'ArchiveType &cereal::OutputArchive<ArchiveType,0>::operator ()<_Ty&>(_Ty &)' being compiled
1>          with
1>          [
1>              ArchiveType=cereal::JSONOutputArchive
1>  ,            _Ty=std::shared_ptr<MV::FileTextureDefinition>
1>          ]
1>          C:\git\external\cereal\include\cereal/archives/json.hpp(711) : see reference to function template instantiation 'ArchiveType &cereal::OutputArchive<ArchiveType,0>::operator ()<_Ty&>(_Ty &)' being compiled
1>          with
1>          [
1>              ArchiveType=cereal::JSONOutputArchive
1>  ,            _Ty=std::shared_ptr<MV::FileTextureDefinition>
1>          ]
1>          C:\git\external\cereal\include\cereal/cereal.hpp(404) : see reference to function template instantiation 'void cereal::save<std::shared_ptr<MV::FileTextureDefinition>&>(cereal::JSONOutputArchive &,const cereal::NameValuePair<std::shared_ptr<MV::FileTextureDefinition> &> &)' being compiled
1>          C:\git\external\cereal\include\cereal/cereal.hpp(329) : see reference to function template instantiation 'cereal::JSONOutputArchive &cereal::OutputArchive<cereal::JSONOutputArchive,0>::processImpl<T>(const T &)' being compiled
1>          with
1>          [
1>              T=cereal::NameValuePair<std::shared_ptr<MV::FileTextureDefinition> &>
1>          ]
1>          C:\git\external\cereal\include\cereal/cereal.hpp(329) : see reference to function template instantiation 'cereal::JSONOutputArchive &cereal::OutputArchive<cereal::JSONOutputArchive,0>::processImpl<T>(const T &)' being compiled
1>          with
1>          [
1>              T=cereal::NameValuePair<std::shared_ptr<MV::FileTextureDefinition> &>
1>          ]
1>          C:\git\external\cereal\include\cereal/cereal.hpp(245) : see reference to function template instantiation 'void cereal::OutputArchive<cereal::JSONOutputArchive,0>::process<_Ty>(T &&)' being compiled
1>          with
1>          [
1>              _Ty=cereal::NameValuePair<std::shared_ptr<MV::FileTextureDefinition> &>
1>  ,            T=cereal::NameValuePair<std::shared_ptr<MV::FileTextureDefinition> &>
1>          ]
1>          C:\git\external\cereal\include\cereal/cereal.hpp(245) : see reference to function template instantiation 'void cereal::OutputArchive<cereal::JSONOutputArchive,0>::process<_Ty>(T &&)' being compiled
1>          with
1>          [
1>              _Ty=cereal::NameValuePair<std::shared_ptr<MV::FileTextureDefinition> &>
1>  ,            T=cereal::NameValuePair<std::shared_ptr<MV::FileTextureDefinition> &>
1>          ]
1>          c:\git\wargameclient\source\game.h(49) : see reference to function template instantiation 'ArchiveType &cereal::OutputArchive<ArchiveType,0>::operator ()<cereal::NameValuePair<std::shared_ptr<MV::FileTextureDefinition> &>>(cereal::NameValuePair<std::shared_ptr<MV::FileTextureDefinition> &> &&)' being compiled
1>          with
1>          [
1>              ArchiveType=cereal::JSONOutputArchive
1>          ]
1>          c:\git\wargameclient\source\game.h(49) : see reference to function template instantiation 'ArchiveType &cereal::OutputArchive<ArchiveType,0>::operator ()<cereal::NameValuePair<std::shared_ptr<MV::FileTextureDefinition> &>>(cereal::NameValuePair<std::shared_ptr<MV::FileTextureDefinition> &> &&)' being compiled
1>          with
1>          [
1>              ArchiveType=cereal::JSONOutputArchive
1>          ]

可以在pastebin上的here上找到textures.h头文件的完整链接,但是我已经包含了相关的部分。

最佳答案

啊哈!所以问题很简单。我习惯于自动处理多态,但是对于Cereal,您必须专门注册类型。通过为我的textures.cpp文件添加以下三行并包括“cereal / types / polymorphic.hpp”,为我解决了此问题:

CEREAL_REGISTER_TYPE(MV::FileTextureDefinition);
CEREAL_REGISTER_TYPE(MV::DynamicTextureDefinition);
CEREAL_REGISTER_TYPE(MV::SurfaceTextureDefinition);

关于c++ - C++ 11 Cereal 库无法序列化一类矿井,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20929553/

10-08 23:32