Skip to content

Instantly share code, notes, and snippets.

@tilarids tilarids/.gitignore
Last active Jul 7, 2016

Embed
What would you like to do?
Simple serialize implementation for discussion
main
main.dSYM/

Simple serialize implementation for discussion

#pragma once
#include "serialize.h"
// should be serializable.
class A: public Serializable {
public:
A(int x = 0, int y = 0): x_(x), y_(y) {}
virtual ~A() {}
virtual void Serialize(std::ostream* out) const {
serialize_int(x_, out);
serialize_int(y_, out);
}
virtual void Deserialize(std::istream* in) {
deserialize_int(in, &x_);
deserialize_int(in, &y_);
}
private:
int x_;
int y_;
};
#pragma once
#include "a.h"
// should be serializable.
class B : public A {
public:
B(int x = 0, int y = 0, double z = 0.0): A(x, y), z_(z) {}
virtual ~B() {}
virtual void Serialize(std::ostream* out) const {
A::Serialize(out);
serialize_double(z_, out);
}
virtual void Deserialize(std::istream* in) {
A::Deserialize(in);
deserialize_double(in, &z_);
}
private:
double z_;
};
#pragma once
#include <vector>
#include "b.h"
#include "serialize.h"
// should be serializable.
class C : public Serializable {
public:
explicit C(int n = 0) {
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);
}
}
virtual void Serialize(std::ostream* out) const {
serialize_vector(v_b_, out);
serialize_vector(v_, out);
}
virtual void Deserialize(std::istream* in) {
deserialize_vector(in, &v_b_);
deserialize_vector(in, &v_);
}
private:
std::vector<B> v_b_;
std::vector<int> v_;
};
#include <string>
#include <fstream>
#include "c.h"
void main_serialize(const std::string& filename, const C& in) {
std::ofstream fout(filename);
in.Serialize(&fout);
}
C main_deserialize(const std::string& filename) {
C out;
std::ifstream fin(filename);
out.Deserialize(&fin);
return out;
}
int main() {
C test1(3);
main_serialize("/tmp/test1.bin", test1);
C test1_new = main_deserialize("/tmp/test1.bin");
main_serialize("/tmp/test1_.bin", test1);
return 0;
}
main: main.cpp a.h b.h c.h serialize.h
g++ -g -O0 -o main -std=c++11 main.cpp
#pragma once
#include <vector>
#include <istream>
#include <ostream>
class Serializable {
public:
virtual ~Serializable() {}
virtual void Serialize(std::ostream* out) const = 0;
virtual void Deserialize(std::istream* in) = 0;
};
void serialize_int(int in, std::ostream* out) {
*out << in << "\n";
}
void deserialize_int(std::istream* in, int* out) {
*in >> *out;
in->get();
}
void serialize_double(double in, std::ostream* out) {
*out << in << "\n";
}
void deserialize_double(std::istream* in, double* out) {
*in >> *out;
in->get();
}
namespace detail {
void serialize_helper(const Serializable& in, std::ostream* out) {
in.Serialize(out);
}
void serialize_helper(const int& in, std::ostream* out) {
serialize_int(in, out);
}
void serialize_helper(const double& in, std::ostream* out) {
serialize_double(in, out);
}
template <typename T>
void serialize_helper(const std::vector<T>& in, std::ostream* out) {
serialize_vector(in, out);
}
void deserialize_helper(std::istream* in, Serializable* out) {
out->Deserialize(in);
}
void deserialize_helper(std::istream* in, int* out) {
deserialize_int(in, out);
}
void deserialize_helper(std::istream* in, double* out) {
deserialize_double(in, out);
}
template <typename T>
void deserialize_helper(std::istream* in, std::vector<T>* out) {
deserialize_vector(in, out);
}
} // namespace detail
template <typename T>
void serialize_vector(const std::vector<T>& in, std::ostream* out) {
serialize_int(in.size(), out);
for (const auto& x : in) {
detail::serialize_helper(x, out);
}
}
template <typename T>
void deserialize_vector(std::istream* in, std::vector<T>* out) {
int size;
deserialize_int(in, &size);
out->resize(size);
for (int i = 0; i < size; ++i) {
detail::deserialize_helper(in, &((*out)[i]));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.