LCOV - code coverage report
Current view: top level - source/network - Serialization.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 2 2 100.0 %
Date: 2026-12-03 01:19:39 Functions: 0 0 -

          Line data    Source code
       1             : /* Copyright (C) 2026 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             : #include "lib/code_annotation.h"
      19             : #include "lib/code_generation.h"
      20             : 
      21             : #include <cstddef>
      22             : 
      23             : #ifndef INCLUDED_NETWORK_SERIALIZATION
      24             : #define INCLUDED_NETWORK_SERIALIZATION
      25             : 
      26             : #define Serialize_int_1(_pos, _val) \
      27             :     STMT( *((_pos)++) = static_cast<std::uint8_t>((_val)&0xff); )
      28             : 
      29             : #define Serialize_int_2(_pos, _val) STMT(\
      30             :     Serialize_int_1(_pos, (_val)>>8); \
      31             :     Serialize_int_1(_pos, (_val)); \
      32             : )
      33             : 
      34             : #define Serialize_int_3(_pos, _val) STMT(\
      35             :     Serialize_int_1(_pos, (_val)>>16); \
      36             :     Serialize_int_2(_pos, (_val)); \
      37             : )
      38             : 
      39             : #define Serialize_int_4(_pos, _val) STMT(\
      40             :     Serialize_int_1(_pos, (_val)>>24); \
      41             :     Serialize_int_3(_pos, (_val)); \
      42             : )
      43             : 
      44             : #define Serialize_int_8(_pos, _val) STMT(\
      45             :     Serialize_int_4(_pos, (_val)>>32); \
      46             :     Serialize_int_4(_pos, (_val)); \
      47             : )
      48             : 
      49             : #define __shift_de(_pos, _val) STMT( \
      50             :     (_val) <<= 8; \
      51             :     (_val) += *((_pos)++); )
      52             : 
      53             : #define Deserialize_int_1(_pos, _val) STMT(\
      54             :     (_val) = *((_pos)++); )
      55             : 
      56             : #define Deserialize_int_2(_pos, _val) STMT(\
      57             :     Deserialize_int_1(_pos, _val); \
      58             :     __shift_de(_pos, _val); )
      59             : 
      60             : #define Deserialize_int_3(_pos, _val) STMT(\
      61             :     Deserialize_int_2(_pos, _val); \
      62             :     __shift_de(_pos, _val); )
      63             : 
      64             : #define Deserialize_int_4(_pos, _val) STMT(\
      65             :     Deserialize_int_3(_pos, _val); \
      66             :     __shift_de(_pos, _val); )
      67             : 
      68             : #define Deserialize_int_8(_pos, _val) STMT(\
      69             :     uint32 _v1; uint32 _v2; \
      70             :     Deserialize_int_4(_pos, _v1); \
      71             :     Deserialize_int_4(_pos, _v2); \
      72             :     _val = _v1; \
      73             :     _val <<= 32;  /* janwas: careful! (uint32 << 32) = 0 */ \
      74             :     _val |= _v2; )
      75             : 
      76             : /**
      77             :  * An interface for serializable objects. For a serializable object to be usable
      78             :  * as a network message field, it must have a constructor without arguments.
      79             :  */
      80           6 : class ISerializable
      81             : {
      82             : public:
      83           1 :     virtual ~ISerializable() {}
      84             : 
      85             :     /**
      86             :      * Return the length of the serialized form of this object
      87             :      */
      88             :     virtual size_t GetSerializedLength() const = 0;
      89             :     /**
      90             :      * Serialize the object into the passed buffer.
      91             :      *
      92             :      * @return a pointer to the location in the buffer right after the
      93             :      * serialized object
      94             :      */
      95             :     virtual std::uint8_t *Serialize(std::uint8_t *buffer) const = 0;
      96             :     /**
      97             :      * Deserialize the object (i.e. read in data from the buffer and initialize
      98             :      * the object's fields). Note that it is up to the deserializer to detect
      99             :      * errors in data format.
     100             :      *
     101             :      * @param buffer A pointer pointing to the start of the serialized data.
     102             :      * @param end A pointer to the end of the message.
     103             :      *
     104             :      * @returns a pointer to the location in the buffer right after the
     105             :      * serialized object, or NULL if there was a data format error
     106             :      */
     107             :     virtual const std::uint8_t *Deserialize(const std::uint8_t *buffer, const std::uint8_t *end) = 0;
     108             : };
     109             : 
     110             : #endif

Generated by: LCOV version 1.16