Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View stack overflow
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
#include <cereal/archives/json.hpp>
#include <cereal/types/polymorphic.hpp>
#include <cereal/types/string.hpp>
#include <cereal/types/vector.hpp>
#include <iostream>
#include <sstream>
#include <functional>
 
class Node
{
public:
Node() = default;
Node( std::string const & s ) :
_token( std::make_shared<std::string>(s) )
{ }
 
virtual ~Node ( ){ }
 
virtual std::size_t Hash ( )
{
std::hash<std::shared_ptr<std::string>> hash_fn;
return hash_fn(_token);
}
 
//private:
std::shared_ptr<std::string> _token;
friend cereal::access;
template <class Archive> void serialize ( Archive & archive )
{
archive( _token );
}
 
};
 
 
class Concept : public Node
{
public:
Concept() = default;
Concept( std::string const & s ) : Node(s) {}
 
~Concept( ) = default;
 
std::size_t Hash ( )
{
auto t = std::hash<std::string>()( "CONCEPT" );
return std::hash<std::size_t>()( t + this->Hash() );
}
 
protected:
friend cereal::access;
template <class Archive> void serialize( Archive & ar )
{
ar( cereal::base_class<Node>( this ) );
}
};
 
 
class Relation : public Node
{
public:
Relation() = default;
Relation( int x )
{
for( int i = 0; i < x; ++i )
edges.push_back( std::make_shared<Concept>(std::to_string(i)) );
}
 
~Relation( ) = default;
 
std::shared_ptr<Relation> Clone ( ) const
{
return std::make_shared<Relation>( *this );
}
 
//protected:
friend cereal::access;
std::vector<std::shared_ptr<Concept>> edges;
template <class Archive> void serialize( Archive & ar )
{
ar( cereal::base_class<Node>( this ) , CEREAL_NVP( edges ) );
}
};
 
CEREAL_REGISTER_TYPE(Concept)
CEREAL_REGISTER_TYPE(Relation)
 
int main()
{
std::stringstream ss;
{
cereal::JSONOutputArchive ar(ss);
std::shared_ptr<Node> n1 = std::make_shared<Node>();
std::shared_ptr<Node> n2 = std::make_shared<Concept>();
std::shared_ptr<Node> n3 = std::make_shared<Relation>(10);
 
ar( n1 );
ar( n2, n3 );
}
 
std::cout << ss.str() << std::endl;
 
{
cereal::JSONInputArchive ar(ss);
std::shared_ptr<Node> n1, n2, n3;
 
ar( n1, n2, n3 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.