Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/simulation2/serialization/SerializeTemplates.h
/* Copyright (C) 2016 Wildfire Games. | /* Copyright (C) 2019 Wildfire Games. | ||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 0 A.D. is free software: you can redistribute it and/or modify | ||||
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
Show All 9 Lines | |||||
#define INCLUDED_SERIALIZETEMPLATES | #define INCLUDED_SERIALIZETEMPLATES | ||||
/** | /** | ||||
* @file | * @file | ||||
* Helper templates for serializing/deserializing common objects. | * Helper templates for serializing/deserializing common objects. | ||||
*/ | */ | ||||
#include "simulation2/components/ICmpPathfinder.h" | #include "simulation2/components/ICmpPathfinder.h" | ||||
#include "simulation2/serialization/IDeserializer.h" | |||||
#include "simulation2/serialization/ISerializer.h" | |||||
#include <boost/unordered_map.hpp> | |||||
#include <utility> | |||||
template<typename ELEM> | template<typename ELEM> | ||||
struct SerializeVector | struct SerializeVector | ||||
{ | { | ||||
template<typename T> | template<typename T> | ||||
void operator()(ISerializer& serialize, const char* name, std::vector<T>& value) | void operator()(ISerializer& serialize, const char* name, std::vector<T>& value) | ||||
{ | { | ||||
size_t len = value.size(); | size_t len = value.size(); | ||||
▲ Show 20 Lines • Show All 60 Lines • ▼ Show 20 Lines | for (size_t i = 0; i < len;) | ||||
T el; | T el; | ||||
ELEM()(deserialize, name, el); | ELEM()(deserialize, name, el); | ||||
i += count; | i += count; | ||||
value.insert(value.end(), count, el); | value.insert(value.end(), count, el); | ||||
} | } | ||||
} | } | ||||
}; | }; | ||||
template<typename ELEM> | |||||
struct SerializeSet | |||||
{ | |||||
template<typename T> | |||||
void operator()(ISerializer& serialize, const char* name, const std::set<T>& value) | |||||
{ | |||||
serialize.NumberU32_Unbounded("size", static_cast<u32>(value.size())); | |||||
for (const T& elem : value) | |||||
ELEM()(serialize, name, elem); | |||||
} | |||||
template<typename T> | |||||
void operator()(IDeserializer& deserialize, const char* name, std::set<T>& value) | |||||
{ | |||||
value.clear(); | |||||
u32 size; | |||||
deserialize.NumberU32_Unbounded("size", size); | |||||
for (size_t i = 0; i < size; ++i) | |||||
{ | |||||
T el; | |||||
ELEM()(deserialize, name, el); | |||||
value.emplace(std::move(el)); | |||||
} | |||||
} | |||||
}; | |||||
template<typename KS, typename VS> | template<typename KS, typename VS> | ||||
struct SerializeMap | struct SerializeMap | ||||
{ | { | ||||
template<typename K, typename V> | template<typename K, typename V> | ||||
void operator()(ISerializer& serialize, const char* UNUSED(name), std::map<K, V>& value) | void operator()(ISerializer& serialize, const char* UNUSED(name), std::map<K, V>& value) | ||||
{ | { | ||||
size_t len = value.size(); | size_t len = value.size(); | ||||
serialize.NumberU32_Unbounded("length", (u32)len); | serialize.NumberU32_Unbounded("length", (u32)len); | ||||
Show All 25 Lines | void operator()(IDeserializer& deserialize, const char* UNUSED(name), M& value) | ||||
u32 len; | u32 len; | ||||
deserialize.NumberU32_Unbounded("length", len); | deserialize.NumberU32_Unbounded("length", len); | ||||
for (size_t i = 0; i < len; ++i) | for (size_t i = 0; i < len; ++i) | ||||
{ | { | ||||
K k; | K k; | ||||
V v; | V v; | ||||
KS()(deserialize, "key", k); | KS()(deserialize, "key", k); | ||||
VS()(deserialize, "value", v); | VS()(deserialize, "value", v); | ||||
value.insert(std::make_pair(k, v)); | value.emplace(std::move(k), std::move(v)); | ||||
} | } | ||||
} | } | ||||
template<typename M, typename C> | template<typename M, typename C> | ||||
void operator()(IDeserializer& deserialize, const char* UNUSED(name), M& value, C& context) | void operator()(IDeserializer& deserialize, const char* UNUSED(name), M& value, C& context) | ||||
{ | { | ||||
typedef typename M::key_type K; | typedef typename M::key_type K; | ||||
typedef typename M::value_type::second_type V; // M::data_type gives errors with gcc | typedef typename M::value_type::second_type V; // M::data_type gives errors with gcc | ||||
value.clear(); | value.clear(); | ||||
u32 len; | u32 len; | ||||
deserialize.NumberU32_Unbounded("length", len); | deserialize.NumberU32_Unbounded("length", len); | ||||
for (size_t i = 0; i < len; ++i) | for (size_t i = 0; i < len; ++i) | ||||
{ | { | ||||
K k; | K k; | ||||
V v; | V v; | ||||
KS()(deserialize, "key", k); | KS()(deserialize, "key", k); | ||||
VS()(deserialize, "value", v, context); | VS()(deserialize, "value", v, context); | ||||
value.insert(std::make_pair(k, v)); | value.emplace(std::move(k), std::move(v)); | ||||
} | } | ||||
} | } | ||||
}; | }; | ||||
// We have to order the map before serializing to make things consistent | // We have to order the map before serializing to make things consistent | ||||
template<typename KS, typename VS> | template<typename KS, typename VS> | ||||
struct SerializeUnorderedMap | struct SerializeUnorderedMap | ||||
{ | { | ||||
Show All 18 Lines | struct SerializeU8_Enum | ||||
{ | { | ||||
serialize.NumberU8(name, value, 0, max); | serialize.NumberU8(name, value, 0, max); | ||||
} | } | ||||
void operator()(IDeserializer& deserialize, const char* name, T& value) | void operator()(IDeserializer& deserialize, const char* name, T& value) | ||||
{ | { | ||||
u8 val; | u8 val; | ||||
deserialize.NumberU8(name, val, 0, max); | deserialize.NumberU8(name, val, 0, max); | ||||
value = (T)val; | value = static_cast<T>(val); | ||||
} | } | ||||
}; | }; | ||||
struct SerializeU8_Unbounded | struct SerializeU8_Unbounded | ||||
{ | { | ||||
void operator()(ISerializer& serialize, const char* name, u8 value) | void operator()(ISerializer& serialize, const char* name, u8 value) | ||||
{ | { | ||||
serialize.NumberU8_Unbounded(name, value); | serialize.NumberU8_Unbounded(name, value); | ||||
▲ Show 20 Lines • Show All 107 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator