-
-
Save AzothAmmo/a56b63b225a0886eddc2 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 "A.hpp" | |
#include <cereal/types/polymorphic.hpp> | |
#include <cereal/archives/json.hpp> | |
struct Cat : Animal | |
{ | |
int x; | |
template <class Archive> | |
void serialize( Archive & ar ) | |
{ | |
ar( x ); | |
} | |
std::string name() { return "cat"; } | |
}; | |
std::unique_ptr<Animal> makeAnimal(std::string const & animalType) | |
{ | |
if( animalType == "cat" ) | |
{ | |
return std::unique_ptr<Animal>( new Cat() ); | |
} | |
} | |
CEREAL_REGISTER_TYPE(Cat); |
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
#pragma once | |
#include <string> | |
#include <memory> | |
struct Animal | |
{ | |
virtual std::string name() = 0; | |
}; | |
std::unique_ptr<Animal> makeAnimal(std::string const & animalType); |
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
{ | |
"value0": { | |
"polymorphic_id": 2147483649, | |
"polymorphic_name": "Cat", | |
"ptr_wrapper": { | |
"valid": 1, | |
"data": { | |
"value0": 0 | |
} | |
} | |
} | |
} |
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 "A.hpp" | |
#include <cereal/archives/json.hpp> | |
#include <cereal/types/polymorphic.hpp> | |
#include <iostream> | |
int main() | |
{ | |
auto a = makeAnimal("cat"); | |
{ | |
cereal::JSONOutputArchive oar(std::cout); | |
oar( a ); | |
} | |
} |
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
all: | |
g++ -std=c++11 -I${CEREAL_INCLUDE} -c A.cpp | |
ar rvs A.a A.o | |
g++ -std=c++11 -I${CEREAL_INCLUDE} main.cpp A.a -o main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment