3

私はシリアルライブラリを使用しており、カスタム コンストラクターを使用して派生型の shared_ptr インスタンスを取得して、アーカイブに正しくフィードしようとしています。私は次のドキュメントに注ぎ込みましたが、明らかに何かが欠けています。

私の階層のクラスは次のとおりです。

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/types/memory.hpp、cereal/types/map.hpp、cereal/types/string.hpp、および cereal/types/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>          ]

textures.h ヘッダー ファイルの完全なリンクは、こちらのペーストビンにありますが、関連するセクションと思われるものを含めました。

4

1 に答える 1