File: IDeserializer.h

package info (click to toggle)
0ad 0~r11863-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 30,560 kB
  • sloc: cpp: 201,230; ansic: 28,387; sh: 10,593; perl: 4,847; python: 2,240; makefile: 658; java: 412; xml: 243; sql: 40
file content (96 lines) | stat: -rw-r--r-- 3,978 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* Copyright (C) 2011 Wildfire Games.
 * This file is part of 0 A.D.
 *
 * 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
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * 0 A.D. is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef INCLUDED_IDESERIALIZER
#define INCLUDED_IDESERIALIZER

#include "maths/Fixed.h"
#include "ps/Errors.h"
#include "scriptinterface/ScriptTypes.h"

ERROR_GROUP(Deserialize);
ERROR_TYPE(Deserialize, OutOfBounds);
ERROR_TYPE(Deserialize, InvalidCharInString);
ERROR_TYPE(Deserialize, ReadFailed);
ERROR_TYPE(Deserialize, ScriptError);

/**
 * Deserialization interface; see \ref serialization "serialization overview".
 */
class IDeserializer
{
public:
	virtual ~IDeserializer();

	virtual void NumberU8(const char* name, uint8_t& out, uint8_t lower, uint8_t upper);
	virtual void NumberI8(const char* name, int8_t& out, int8_t lower, int8_t upper);
	virtual void NumberU16(const char* name, uint16_t& out, uint16_t lower, uint16_t upper);
	virtual void NumberI16(const char* name, int16_t& out, int16_t lower, int16_t upper);
	virtual void NumberU32(const char* name, uint32_t& out, uint32_t lower, uint32_t upper);
	virtual void NumberI32(const char* name, int32_t& out, int32_t lower, int32_t upper);
	virtual void NumberU8_Unbounded(const char* name, uint8_t& out);
	virtual void NumberI8_Unbounded(const char* name, int8_t& out);
	virtual void NumberU16_Unbounded(const char* name, uint16_t& out);
	virtual void NumberI16_Unbounded(const char* name, int16_t& out);
	virtual void NumberU32_Unbounded(const char* name, uint32_t& out);
	virtual void NumberI32_Unbounded(const char* name, int32_t& out);
	virtual void NumberFloat_Unbounded(const char* name, float& out);
	virtual void NumberDouble_Unbounded(const char* name, double& out);
	virtual void NumberFixed_Unbounded(const char* name, fixed& out);
	virtual void Bool(const char* name, bool& out);
	virtual void StringASCII(const char* name, std::string& out, uint32_t minlength, uint32_t maxlength);
	virtual void String(const char* name, std::wstring& out, uint32_t minlength, uint32_t maxlength);

	/// Deserialize a jsval, replacing 'out'
	virtual void ScriptVal(const char* name, jsval& out) = 0;
	virtual void ScriptVal(const char* name, CScriptVal& out) = 0;
	virtual void ScriptVal(const char* name, CScriptValRooted& out) = 0;

	/// Deserialize an object jsval, appending properties to object 'obj'
	virtual void ScriptObjectAppend(const char* name, jsval& obj) = 0;

	/// Deserialize a JSString
	virtual void ScriptString(const char* name, JSString*& out) = 0;

	virtual void RawBytes(const char* name, u8* data, size_t len);

	// Features for simulation-state serialisation:
	virtual int GetVersion() const;

	/**
	 * Returns a stream which can be used to deserialize data directly.
	 * (This is particularly useful for chaining multiple deserializers
	 * together.)
	 */
	virtual std::istream& GetStream() = 0;

	/**
	 * Throws an exception if the stream definitely cannot provide the required
	 * number of bytes.
	 * (It might be conservative and *not* throw an exception in some cases where
	 * the stream actually can't provide the required bytes.)
	 * (This should be used when allocating memory based on data in the
	 * stream, e.g. reading strings, to avoid dangerously large allocations
	 * when the data is invalid.)
	 */
	virtual void RequireBytesInStream(size_t numBytes) = 0;

protected:
	virtual void Get(const char* name, u8* data, size_t len) = 0;
};

#endif // INCLUDED_IDESERIALIZER