test_graph.cpp 1.42 KB
Newer Older
Christoph Gerum's avatar
Christoph Gerum committed
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
#include <cereal/types/list.hpp>
#include <cereal/types/memory.hpp>
#include <cereal/archives/binary.hpp>
#include <cereal/archives/xml.hpp>
#include <cereal/archives/json.hpp>
#include <fstream>
#include <memory>

struct Node
{

  std::string name;
  std::list<std::weak_ptr<Node> > succ;

  Node(std::string n) : name(n), succ() {}
  
  template <class Archive>
  void serialize( Archive & ar )
  {
    ar( name, succ );
  }
};

struct Graph
{

  std::list<std::shared_ptr<Node> > nodes;
  
  template <class Archive>
  void serialize( Archive & ar )
  {
    ar( nodes );
  }
};

int main()
{
  Graph myGraph;

  auto n1 = std::make_shared<Node>("n1");
  auto n2 = std::make_shared<Node>("n2");
  auto n3 = std::make_shared<Node>("n3");
  auto n4 = std::make_shared<Node>("n4");

  myGraph.nodes.push_back(n1);
  myGraph.nodes.push_back(n2);
  myGraph.nodes.push_back(n3);
  myGraph.nodes.push_back(n4);

  n1->succ.push_back(n2);
  n1->succ.push_back(n3);
  
  n2->succ.push_back(n4);
  n3->succ.push_back(n4);
  n4->succ.push_back(n1);
  
  {
    std::ofstream os("graph.cereal", std::ios::binary);
    cereal::BinaryOutputArchive archive( os );
    archive( myGraph );
  }

  {
    std::ofstream os("graph.xml", std::ios::binary);
    cereal::XMLOutputArchive archive( os );
    archive( myGraph );
  }

  {
    std::ofstream os("graph.json", std::ios::binary);
    cereal::JSONOutputArchive archive( os );
    archive( myGraph );
  }
  
  return 0;
}