158 lines
3.8 KiB
C++
158 lines
3.8 KiB
C++
#pragma once
|
|
#include "Defines.h"
|
|
#include <stdio.h>
|
|
#include <fstream>
|
|
#include <vector>
|
|
#include <map>
|
|
#include <string>
|
|
|
|
template<typename T, typename size_type = unsigned32>
|
|
struct Serializer
|
|
{
|
|
static void Serialize(const T& value, std::ostream& out)
|
|
{
|
|
out.write((char*)&value, sizeof(value));
|
|
}
|
|
|
|
static void Deserialize(T& value, std::istream& in)
|
|
{
|
|
in.read((char*)&value, sizeof(value));
|
|
}
|
|
};
|
|
|
|
template<typename size_type>
|
|
struct Serializer<std::string, size_type>
|
|
{
|
|
static void Serialize(const std::string& value, std::ostream& out)
|
|
{
|
|
size_type size = (size_type)value.size();
|
|
out.write((char*)&size, sizeof(size));
|
|
out.write((char*)&value[0], sizeof(char) * size);
|
|
}
|
|
|
|
static void Deserialize(std::string& value, std::istream& in)
|
|
{
|
|
size_type size;
|
|
in.read((char*)&size, sizeof(size));
|
|
std::vector<char> res(size);
|
|
in.read((char*)&res[0], sizeof(char) * size);
|
|
value = std::string(res.begin(), res.end());
|
|
}
|
|
};
|
|
|
|
template<typename T, typename size_type>
|
|
struct Serializer<std::vector<T>, size_type>
|
|
{
|
|
static void Serialize(const std::vector<T>& value, std::ostream& out)
|
|
{
|
|
size_type size = (size_type)value.size();
|
|
out.write((char*)&size, sizeof(size));
|
|
if (size > 0)
|
|
for (size_t i = 0; i < size; i++)
|
|
Serializer<T, size_type>::Serialize(value[i], out);
|
|
}
|
|
|
|
static void Deserialize(std::vector<T>& value, std::istream& in)
|
|
{
|
|
size_type size;
|
|
in.read((char*)&size, sizeof(size));
|
|
value.resize(size);
|
|
if (size > 0)
|
|
for (size_t i = 0; i < size; i++)
|
|
Serializer<T, size_type>::Deserialize(value[i], in);
|
|
}
|
|
};
|
|
|
|
template<typename size_type>
|
|
struct Serializer <std::vector<unsigned32>, size_type>
|
|
{
|
|
static void Serialize(const std::vector<unsigned32>& value, std::ostream& out)
|
|
{
|
|
size_type size = (size_type)value.size();
|
|
out.write((char*)&size, sizeof(size));
|
|
if (size > 0)
|
|
out.write((char*)&value[0], sizeof(unsigned32) * size);
|
|
}
|
|
|
|
static void Deserialize(std::vector<unsigned32>& value, std::istream& in)
|
|
{
|
|
size_type size;
|
|
in.read((char*)&size, sizeof(size));
|
|
value.resize(size);
|
|
if (size > 0)
|
|
in.read((char*)&value[0], sizeof(unsigned32) * size);
|
|
}
|
|
};
|
|
|
|
template<typename size_type>
|
|
struct Serializer <std::vector<unsigned8>, size_type>
|
|
{
|
|
static void Serialize(const std::vector<unsigned8>& value, std::ostream& out)
|
|
{
|
|
size_type size = (size_type)value.size();
|
|
out.write((char*)&size, sizeof(size));
|
|
if (size > 0)
|
|
out.write((char*)&value[0], sizeof(unsigned8) * size);
|
|
}
|
|
|
|
static void Deserialize(std::vector<unsigned8>& value, std::istream& in)
|
|
{
|
|
size_type size;
|
|
in.read((char*)&size, sizeof(size));
|
|
value.resize(size);
|
|
if (size > 0)
|
|
in.read((char*)&value[0], sizeof(unsigned8) * size);
|
|
}
|
|
};
|
|
|
|
template<typename T1, typename T2, typename size_type>
|
|
struct Serializer<std::map<T1, T2>, size_type>
|
|
{
|
|
static void Serialize(const std::map<T1, T2>& map, std::ostream& out)
|
|
{
|
|
// Write the additional properties to a file
|
|
size_type size = (size_type)map.size();
|
|
out.write((char*)&size, sizeof(size));
|
|
if (size > 0)
|
|
for (auto pair : map)
|
|
{
|
|
Serializer<T1, size_type>::Serialize(pair.first, out);
|
|
Serializer<T2, size_type>::Serialize(pair.second, out);
|
|
}
|
|
}
|
|
|
|
static void Deserialize(std::map<T1, T2>& map, std::istream& in)
|
|
{
|
|
map.clear();
|
|
|
|
size_type size = 0;
|
|
in.read((char*)&size, sizeof(size));
|
|
|
|
if (size > 0)
|
|
for (unsigned i = 0; i < size; i++)
|
|
{
|
|
T1 first;
|
|
Serializer<T1>::Deserialize(first, in);
|
|
|
|
T2 second;
|
|
Serializer<T2>::Deserialize(second, in);
|
|
map.insert(std::pair<T1, T2>(first, second));
|
|
}
|
|
}
|
|
};
|
|
|
|
template<typename T>
|
|
struct Serializer<T*>
|
|
{
|
|
static void Serialize(const T* source, size_t count, std::ostream& out)
|
|
{
|
|
if (count == 0) return;
|
|
out.write((char*)source, sizeof(T) * count);
|
|
}
|
|
|
|
static void Deserialize(const T* source, size_t count, std::istream& in)
|
|
{
|
|
if (count == 0) return;
|
|
in.read((char*)source, sizeof(T) * count);
|
|
}
|
|
}; |