Last active
August 29, 2015 13:57
-
-
Save AzothAmmo/9699962 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 <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