Pyrogenesis trunk
IDeserializer.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_IDESERIALIZER
19#define INCLUDED_IDESERIALIZER
20
21#include "maths/Fixed.h"
22#include "ps/Errors.h"
24
25ERROR_GROUP(Deserialize);
26ERROR_TYPE(Deserialize, OutOfBounds);
27ERROR_TYPE(Deserialize, InvalidCharInString);
28ERROR_TYPE(Deserialize, ReadFailed);
29ERROR_TYPE(Deserialize, ScriptError);
30
31/**
32 * Deserialization interface; see \ref serialization "serialization overview".
33 */
35{
36public:
37 virtual ~IDeserializer();
38
39 virtual void NumberU8(const char* name, uint8_t& out, uint8_t lower, uint8_t upper);
40 virtual void NumberI8(const char* name, int8_t& out, int8_t lower, int8_t upper);
41 virtual void NumberU16(const char* name, uint16_t& out, uint16_t lower, uint16_t upper);
42 virtual void NumberI16(const char* name, int16_t& out, int16_t lower, int16_t upper);
43 virtual void NumberU32(const char* name, uint32_t& out, uint32_t lower, uint32_t upper);
44 virtual void NumberI32(const char* name, int32_t& out, int32_t lower, int32_t upper);
45 virtual void NumberU8_Unbounded(const char* name, uint8_t& out);
46 virtual void NumberI8_Unbounded(const char* name, int8_t& out);
47 virtual void NumberU16_Unbounded(const char* name, uint16_t& out);
48 virtual void NumberI16_Unbounded(const char* name, int16_t& out);
49 virtual void NumberU32_Unbounded(const char* name, uint32_t& out);
50 virtual void NumberI32_Unbounded(const char* name, int32_t& out);
51 virtual void NumberFloat_Unbounded(const char* name, float& out);
52 virtual void NumberDouble_Unbounded(const char* name, double& out);
53 virtual void NumberFixed_Unbounded(const char* name, fixed& out);
54 virtual void Bool(const char* name, bool& out);
55 virtual void StringASCII(const char* name, std::string& out, uint32_t minlength, uint32_t maxlength);
56 virtual void String(const char* name, std::wstring& out, uint32_t minlength, uint32_t maxlength);
57
58 /// Deserialize a JS::Value, replacing 'out'
59 virtual void ScriptVal(const char* name, JS::MutableHandleValue out) = 0;
60
61 /**
62 * Deserialize an object and assign its properties to objVal
63 * (Essentially equivalent to Object.assign(objVal, serialized))
64 */
65 virtual void ScriptObjectAssign(const char* name, JS::HandleValue objVal) = 0;
66
67 /// Deserialize a JSString
68 virtual void ScriptString(const char* name, JS::MutableHandleString out) = 0;
69
70 virtual void RawBytes(const char* name, u8* data, size_t len);
71
72 // Features for simulation-state serialisation:
73 virtual int GetVersion() const;
74
75 /**
76 * Returns a stream which can be used to deserialize data directly.
77 * (This is particularly useful for chaining multiple deserializers
78 * together.)
79 */
80 virtual std::istream& GetStream() = 0;
81
82 /**
83 * Throws an exception if the stream definitely cannot provide the required
84 * number of bytes.
85 * (It might be conservative and *not* throw an exception in some cases where
86 * the stream actually can't provide the required bytes.)
87 * (This should be used when allocating memory based on data in the
88 * stream, e.g. reading strings, to avoid dangerously large allocations
89 * when the data is invalid.)
90 */
91 virtual void RequireBytesInStream(size_t numBytes) = 0;
92
93protected:
94 virtual void Get(const char* name, u8* data, size_t len) = 0;
95};
96
97#endif // INCLUDED_IDESERIALIZER
ERROR_TYPE(Deserialize, OutOfBounds)
ERROR_GROUP(Deserialize)
A simple fixed-point number class.
Definition: Fixed.h:120
Deserialization interface; see serialization overview.
Definition: IDeserializer.h:35
virtual int GetVersion() const
Definition: IDeserializer.cpp:200
virtual void NumberU16(const char *name, uint16_t &out, uint16_t lower, uint16_t upper)
Definition: IDeserializer.cpp:52
virtual void NumberFloat_Unbounded(const char *name, float &out)
Definition: IDeserializer.cpp:138
virtual void ScriptString(const char *name, JS::MutableHandleString out)=0
Deserialize a JSString.
virtual void NumberU16_Unbounded(const char *name, uint16_t &out)
Definition: IDeserializer.cpp:110
virtual void NumberI16_Unbounded(const char *name, int16_t &out)
Definition: IDeserializer.cpp:117
virtual std::istream & GetStream()=0
Returns a stream which can be used to deserialize data directly.
virtual void String(const char *name, std::wstring &out, uint32_t minlength, uint32_t maxlength)
Definition: IDeserializer.cpp:176
virtual void Bool(const char *name, bool &out)
Definition: IDeserializer.cpp:155
virtual void NumberI8_Unbounded(const char *name, int8_t &out)
Definition: IDeserializer.cpp:105
virtual void NumberU8_Unbounded(const char *name, uint8_t &out)
Definition: IDeserializer.cpp:100
virtual void NumberFixed_Unbounded(const char *name, fixed &out)
Definition: IDeserializer.cpp:148
virtual void ScriptObjectAssign(const char *name, JS::HandleValue objVal)=0
Deserialize an object and assign its properties to objVal (Essentially equivalent to Object....
virtual void NumberU8(const char *name, uint8_t &out, uint8_t lower, uint8_t upper)
Definition: IDeserializer.cpp:30
virtual void NumberI32_Unbounded(const char *name, int32_t &out)
Definition: IDeserializer.cpp:131
virtual void StringASCII(const char *name, std::string &out, uint32_t minlength, uint32_t maxlength)
Definition: IDeserializer.cpp:162
virtual ~IDeserializer()
Definition: IDeserializer.cpp:26
virtual void NumberU32_Unbounded(const char *name, uint32_t &out)
Definition: IDeserializer.cpp:124
virtual void NumberI8(const char *name, int8_t &out, int8_t lower, int8_t upper)
Definition: IDeserializer.cpp:41
virtual void NumberI32(const char *name, int32_t &out, int32_t lower, int32_t upper)
Definition: IDeserializer.cpp:88
virtual void ScriptVal(const char *name, JS::MutableHandleValue out)=0
Deserialize a JS::Value, replacing 'out'.
virtual void NumberDouble_Unbounded(const char *name, double &out)
Definition: IDeserializer.cpp:143
virtual void Get(const char *name, u8 *data, size_t len)=0
virtual void RawBytes(const char *name, u8 *data, size_t len)
Definition: IDeserializer.cpp:195
virtual void NumberI16(const char *name, int16_t &out, int16_t lower, int16_t upper)
Definition: IDeserializer.cpp:64
virtual void RequireBytesInStream(size_t numBytes)=0
Throws an exception if the stream definitely cannot provide the required number of bytes.
virtual void NumberU32(const char *name, uint32_t &out, uint32_t lower, uint32_t upper)
Definition: IDeserializer.cpp:76
uint8_t u8
Definition: types.h:37
static void out(const wchar_t *fmt,...)
Definition: wdbg_sym.cpp:407
unsigned short uint16_t
Definition: wposix_types.h:52
unsigned int uint32_t
Definition: wposix_types.h:53
short int16_t
Definition: wposix_types.h:38
unsigned char uint8_t
Definition: wposix_types.h:51
signed char int8_t
Definition: wposix_types.h:37