Pyrogenesis trunk
SerializeTemplates.h
Go to the documentation of this file.
1/* Copyright (C) 2020 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef INCLUDED_SERIALIZETEMPLATES
19#define INCLUDED_SERIALIZETEMPLATES
20
21/**
22 * @file
23 * Helper templates definitions for serializing/deserializing common objects.
24 *
25 * Usage:
26 * You need to (partially) specialize SerializeHelper for your type.
27 * The optional SFINAE argument can be used to provide generic specializations
28 * via std::enable_if_t<T>.
29 * If both paths are common, you can templatize operator()'s first argument,
30 * but you will need to templatize the passed value to account for different value categories.
31 *
32 * See SerializedTypes.h for some examples.
33 *
34 */
35
38
39// SFINAE is just there to allow SFINAE-partial specializations.
40template <typename T, typename SFINAE = void>
42{
43 template<typename... Args>
44 void operator()(ISerializer& serialize, const char* name, T value, Args&&...);
45 template<typename... Args>
46 void operator()(IDeserializer& serialize, const char* name, T& value, Args&&...);
47};
48
49// This is the variant for an explicitly specified T (where what you pass is another type).
50template <typename T, typename S, typename... Args>
51void Serializer(S& serialize, const char* name, Args&&... args)
52{
53 SerializeHelper<std::remove_const_t<std::remove_reference_t<T>>>()(serialize, name, std::forward<Args>(args)...);
54}
55
56// This lets T be deduced from the argument.
57template <typename T, typename S, typename... Args>
58void Serializer(S& serialize, const char* name, T&& value, Args&&... args)
59{
60 SerializeHelper<std::remove_const_t<std::remove_reference_t<T>>>()(serialize, name, std::forward<T>(value), std::forward<Args>(args)...);
61}
62
63namespace Serialize
64{
65 template<typename S, class T>
66 using qualify = std::conditional_t<std::is_same_v<S, ISerializer&>, const T&, T&>;
67}
68
69#endif // INCLUDED_SERIALIZETEMPLATES
void Serializer(S &serialize, const char *name, Args &&... args)
Definition: SerializeTemplates.h:51
Deserialization interface; see serialization overview.
Definition: IDeserializer.h:35
Serialization interface; see serialization overview.
Definition: ISerializer.h:121
Definition: SerializeTemplates.h:64
std::conditional_t< std::is_same_v< S, ISerializer & >, const T &, T & > qualify
Definition: SerializeTemplates.h:66
#define T(string_literal)
Definition: secure_crt.cpp:77
Definition: SerializeTemplates.h:42
void operator()(IDeserializer &serialize, const char *name, T &value, Args &&...)
void operator()(ISerializer &serialize, const char *name, T value, Args &&...)