Skip to content

Instantly share code, notes, and snippets.

@AzothAmmo

AzothAmmo/A.cpp Secret

Created July 12, 2013 19:11
Show Gist options
  • Save AzothAmmo/a56b63b225a0886eddc2 to your computer and use it in GitHub Desktop.
Save AzothAmmo/a56b63b225a0886eddc2 to your computer and use it in GitHub Desktop.
#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);
#pragma once
#include <string>
#include <memory>
struct Animal
{
virtual std::string name() = 0;
};
std::unique_ptr<Animal> makeAnimal(std::string const & animalType);
{
"value0": {
"polymorphic_id": 2147483649,
"polymorphic_name": "Cat",
"ptr_wrapper": {
"valid": 1,
"data": {
"value0": 0
}
}
}
}
#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 );
}
}
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