Skip to content

Instantly share code, notes, and snippets.

@tilarids
Last active July 7, 2016 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tilarids/cc64ab35a09eda717ee48b99b8c79266 to your computer and use it in GitHub Desktop.
Save tilarids/cc64ab35a09eda717ee48b99b8c79266 to your computer and use it in GitHub Desktop.
Testbed for C++ serialization
main
main.dSYM/
#pragma once
// should be serializable.
class A {
public:
A(int x, int y): x_(x), y_(y) {}
virtual ~A() {}
private:
int x_;
int y_;
};
#pragma once
#include "a.h"
// should be serializable.
class B : public A {
public:
B(int x, int y, double z): A(x, y), z_(z) {}
virtual ~B() {}
private:
double z_;
};
#pragma once
#include <vector>
#include "b.h"
// should be serializable.
class C {
public:
explicit C(int n) {
v_b_.reserve(n);
v_.reserve(n);
for (int i = 0; i < n; ++i) {
v_b_.emplace_back(i, i + 1, i * 42);
v_.push_back(i);
}
}
private:
std::vector<B> v_b_;
std::vector<int> v_;
};
#include <string>
#include "c.h"
void serialize(const std::string& filename, C) {
// todo
}
C deserialize(const std::string& filename) {
// todo
return C(0);
}
int main() {
C test1(3);
C test2(5);
serialize("test1.bin", test1);
serialize("test2.bin", test2);
C test1_new = deserialize("test1.bin");
C test2_new = deserialize("test2.bin");
return 0;
}
main: main.cpp a.h b.h c.h
g++ -g -O0 -o main -std=c++11 main.cpp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment