/* * SerializerReflection.cpp, part of VCMI engine * * Authors: listed in file AUTHORS in main folder * * License: GNU General Public License v2.0 or later * Full text of license available in license.txt file, in main folder * */ #include "StdInc.h" #include "SerializerReflection.h" #include "BinaryDeserializer.h" #include "BinarySerializer.h" #include "RegisterTypes.h" VCMI_LIB_NAMESPACE_BEGIN template class SerializerReflection final : public ISerializerReflection { public: Serializeable * createPtr(BinaryDeserializer &ar, IGameCallback * cb) const override { return ClassObjectCreator::invoke(cb); } void loadPtr(BinaryDeserializer &ar, IGameCallback * cb, Serializeable * data) const override { auto * realPtr = dynamic_cast(data); realPtr->serialize(ar); } void savePtr(BinarySerializer &s, const Serializeable *data) const override { const Type *ptr = dynamic_cast(data); const_cast(ptr)->serialize(s); } }; template class SerializerCompatibility : public ISerializerReflection { public: Serializeable * createPtr(BinaryDeserializer &ar, IGameCallback * cb) const override { return ClassObjectCreator::invoke(cb); } void savePtr(BinarySerializer &s, const Serializeable *data) const override { throw std::runtime_error("Illegal call to savePtr - this type should not be used for serialization!"); } }; class SerializerCompatibilityBonusingBuilding final : public SerializerCompatibility { void loadPtr(BinaryDeserializer &ar, IGameCallback * cb, Serializeable * data) const override { auto * realPtr = dynamic_cast(data); realPtr->serialize(ar); } }; class SerializerCompatibilityArtifactsAltar final : public SerializerCompatibility { void loadPtr(BinaryDeserializer &ar, IGameCallback * cb, Serializeable * data) const override { auto * realPtr = dynamic_cast(data); realPtr->serializeArtifactsAltar(ar); } }; template void CSerializationApplier::registerType(uint16_t ID) { assert(!apps.count(ID)); apps[ID].reset(new SerializerReflection); } CSerializationApplier::CSerializationApplier() { registerTypes(*this); apps[54].reset(new SerializerCompatibilityBonusingBuilding); apps[55].reset(new SerializerCompatibilityBonusingBuilding); apps[81].reset(new SerializerCompatibilityArtifactsAltar); } CSerializationApplier & CSerializationApplier::getInstance() { static CSerializationApplier registry; return registry; } VCMI_LIB_NAMESPACE_END