Commit fa63364c authored by Christoph Gerum's avatar Christoph Gerum

Added implementations

parent e81c77b8
*~
# Prerequisites
*.d
......
[submodule "cereal"]
path = cereal
url = https://github.com/USCiLab/cereal.git
project(test_cereal)
cmake_minimum_required(VERSION 3.0)
include_directories(cereal/include)
add_executable(test_cereal test_cereal.cpp)
add_executable(test_graph test_graph.cpp)
Subproject commit bd839ccb47dfb5462331fede7c5f013ca8dc60cb
#include <cereal/types/unordered_map.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 MyRecord
{
private:
uint8_t x, y;
float z;
public:
template <class Archive>
void serialize( Archive & ar )
{
ar( x, y, z );
}
};
struct SomeData
{
int32_t id;
std::shared_ptr<std::unordered_map<uint32_t, MyRecord>> data;
template <class Archive>
void save( Archive & ar ) const
{
ar( data );
}
template <class Archive>
void load( Archive & ar )
{
static int32_t idGen = 0;
id = idGen++;
ar( data );
}
};
int main()
{
SomeData myData;
myData.data = std::make_shared<std::unordered_map<uint32_t, MyRecord> >();
MyRecord r1, r2, r3;
(*myData.data)[0] = r1;
(*myData.data)[1] = r2;
(*myData.data)[2] = r3;
{
std::ofstream os("out.cereal", std::ios::binary);
cereal::BinaryOutputArchive archive( os );
archive( myData );
}
{
std::ofstream os("out.xml", std::ios::binary);
cereal::XMLOutputArchive archive( os );
archive( myData );
}
{
std::ofstream os("out.json", std::ios::binary);
cereal::JSONOutputArchive archive( os );
archive( myData );
}
return 0;
}
#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;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment