Skip to content

Instantly share code, notes, and snippets.

@Enhex
Last active August 25, 2017 13:27
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 Enhex/4d42ef13dda1681eaac7ae6ead25669c to your computer and use it in GitHub Desktop.
Save Enhex/4d42ef13dda1681eaac7ae6ead25669c to your computer and use it in GitHub Desktop.
/*
The MIT License (MIT)
Copyright (c) 2017 Yehonatan Ballas
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <assert.h>
#include <fstream>
#include <iostream>
#include <type_traits>
using namespace std;
void n() {
cout << '\n';
}
// read type
template<typename T>
void read(ifstream& f, T& value) {
f.read((char*)(&value), sizeof(value));
}
// write type
template<typename T>
void write(ofstream& f, T& value) {
f.write((char*)(&value), sizeof(value));
}
// Read or write. Useful for defining a single serialize() specialization for both read and write.
template<typename T>
void read_or_write(ifstream& f, T& value) {
read(f, value);
}
template<typename T>
void read_or_write(ofstream& f, T& value) {
write(f, value);
}
/* serialize variadic template
TODO:
- output format selection
- use type traits
*/
template<typename Stream, typename T>
void serialize(Stream& f, T& value)
{
serialize(f, value);
}
template<typename Stream, typename T, typename ...Ts>
void serialize(Stream& f, T& value, Ts&... args)
{
serialize(f, value);
serialize(f, args...);
}
//automatically provide default serialization implementation for arithmetic & array types
template<typename T, typename = enable_if_t<is_arithmetic_v<T> || is_array_v<T>> >
void serialize(ofstream& f, T& value) {
write(f, value);
}
template<typename T, typename = enable_if_t<is_arithmetic_v<T> || is_array_v<T>> >
void serialize(ifstream& f, T& value) {
read(f, value);
}
// Example for serializing a POD type (without references/pointers since memory addresses change)
struct Vector3 {
float x, y, z;
void print() const {
cout <<
"x: " << x << '\n' <<
"x: " << y << '\n' <<
"y: " << z << '\n';
}
};
template<typename Stream>
void serialize(Stream& f, Vector3& value) {
read_or_write(f, value);
}
// example class
struct A
{
int x = 0;
float y = 0;
char z[3];
Vector3 vec3;
void print() const {
cout
<< "x: " << x << '\n'
<< "x: " << y << '\n'
<< "z: " << z << '\n'
<< "vec3:\n";
vec3.print();
}
};
//TODO alternatively can be a member function so we don't have to pass A and we can access members directly. Requires using SFINAE to check if the method is available.
template<typename Stream>
void serialize(Stream& f, A& value) {
// choose which members to serialize
serialize(f, value.x, value.y, value.z, value.vec3); // members' types' already have serialization implemented
/*TODO
can provide a wrapper around the Stream type that overloads operator() so we can write concisely:
f(value.x, value.y);
*/
}
namespace test
{
void serialization()
{
A a;
a.x = 5;
a.y = 7.5;
a.z[0] = 'a';
a.z[1] = 'b';
a.z[2] = '\0';
a.vec3 = {1,2,3};
a.print();
n();
// serialize to file
{
ofstream f("test", ofstream::binary);
serialize(f, a);
}
a.x = 0;
a.y = 0;
a.z[0] = '0';
a.z[1] = '0';
a.vec3 = { 0,0,0 };
// read back from file
{
ifstream f("test", ios::binary);
serialize(f, a);
}
a.print();
n();
assert(a.x == 5);
assert(a.y == 7.5);
assert(a.z[0] == 'a');
assert(a.z[1] == 'b');
assert(a.z[2] == '\0');
assert(a.vec3.x == 1);
assert(a.vec3.y == 2);
assert(a.vec3.z == 3);
}
}
int main()
{
cout <<
"Serialization test\n"
"==================\n";
test::serialization();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment