LCOV - code coverage report
Current view: top level - source/simulation2/serialization - SerializeTemplates.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 3 3 100.0 %
Date: 2023-01-19 00:18:29 Functions: 16 89 18.0 %

          Line data    Source code
       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             : 
      36             : #include "simulation2/serialization/ISerializer.h"
      37             : #include "simulation2/serialization/IDeserializer.h"
      38             : 
      39             : // SFINAE is just there to allow SFINAE-partial specializations.
      40             : template <typename T, typename SFINAE = void>
      41             : struct SerializeHelper
      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).
      50             : template <typename T, typename S, typename... Args>
      51             : void 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.
      57             : template <typename T, typename S, typename... Args>
      58          56 : void Serializer(S& serialize, const char* name, T&& value, Args&&... args)
      59             : {
      60          56 :     SerializeHelper<std::remove_const_t<std::remove_reference_t<T>>>()(serialize, name, std::forward<T>(value), std::forward<Args>(args)...);
      61          56 : }
      62             : 
      63             : namespace 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

Generated by: LCOV version 1.13