Last active
February 3, 2019 17:25
-
-
Save falrnd/d9d8c54a214f96c23305ca36d10987b5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# include <Siv3D.hpp> // OpenSiv3D v0.3.1 | |
class IFormatable { | |
public: | |
virtual operator String() const = 0; | |
}; | |
class SubA : public IFormatable { | |
public: | |
int a; | |
SubA() : a(0) {} | |
SubA(int a) : a(a) {} | |
template<class Archive> | |
void SIV3D_SERIALIZE(Archive& ar) { | |
ar(a); | |
} | |
operator String() const override { | |
return Format(U"SubA{", a, U'}'); | |
} | |
}; | |
class SubB : public IFormatable { | |
public: | |
String n; | |
SubB() : n(U"null") {} | |
SubB(const String& n) : n(n) {} | |
template<class Archive> | |
void SIV3D_SERIALIZE(Archive& ar) { | |
ar(n); | |
} | |
operator String() const override { | |
return Format(U"SubB{", n, U'}'); | |
} | |
}; | |
CEREAL_REGISTER_TYPE(SubA); | |
CEREAL_REGISTER_TYPE(SubB); | |
CEREAL_REGISTER_POLYMORPHIC_RELATION(IFormatable, SubA); | |
CEREAL_REGISTER_POLYMORPHIC_RELATION(IFormatable, SubB); | |
namespace s3d { | |
template<class T> | |
void Formatter(FormatData& f, const std::shared_ptr<T>& v) { | |
Formatter(f, v->operator String()); | |
} | |
} | |
void Main() { | |
using type = std::shared_ptr<IFormatable>; | |
{ | |
Array<type>v; | |
v << std::make_shared<SubA>(114514) | |
<< std::make_shared<SubB>(U"BABYLON"); | |
Serializer<BinaryWriter>(U"test")(v); | |
Logger << v; | |
} | |
{ | |
Array<type>v; | |
Deserializer<BinaryReader>(U"test")(v); | |
Logger << v; | |
} | |
Logger << SubA(1919); | |
Logger << SubB(U"GO is GOD"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# include <Siv3D.hpp> // OpenSiv3D v0.3.1 | |
class IFormatable { | |
public: | |
virtual void format(FormatData&) const = 0; | |
}; | |
using shared_IFormatable = std::shared_ptr<IFormatable>; | |
template<> | |
void s3d::Formatter(FormatData& f, const shared_IFormatable& v) { | |
v->format(f); | |
} | |
class SubA : public IFormatable { | |
public: | |
int a; | |
SubA() : a(0) {} | |
SubA(int a) : a(a) {} | |
template<class Archive> | |
void SIV3D_SERIALIZE(Archive& ar) { | |
ar(a); | |
} | |
void format(FormatData& f) const override { | |
f.string.append(U"SubA{").append(ToString(a))<<U'}'; | |
} | |
}; | |
class SubB : public IFormatable { | |
public: | |
String n; | |
SubB() : n(U"null") {} | |
SubB(const String& n) : n(n) {} | |
template<class Archive> | |
void SIV3D_SERIALIZE(Archive& ar) { | |
ar(n); | |
} | |
void format(FormatData& f) const override { | |
f.string.append(U"SubB{").append(n)<<U'}'; | |
} | |
}; | |
CEREAL_REGISTER_TYPE(SubA); | |
CEREAL_REGISTER_TYPE(SubB); | |
CEREAL_REGISTER_POLYMORPHIC_RELATION(IFormatable, SubA); | |
CEREAL_REGISTER_POLYMORPHIC_RELATION(IFormatable, SubB); | |
void Main() { | |
using type = shared_IFormatable; | |
{ | |
Array<type>v; | |
v << std::make_shared<SubA>(114514) | |
<< std::make_shared<SubB>(U"BABYLON"); | |
Logger << v; | |
Serializer<BinaryWriter>(U"test")(v); | |
} | |
{ | |
Array<type>v; | |
Deserializer<BinaryReader>(U"test")(v); | |
Logger << v; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment