Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
StdSerializer.h
Go to the documentation of this file.
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#ifndef INCLUDED_STDSERIALIZER
19#define INCLUDED_STDSERIALIZER
20
21#include "lib/code_annotation.h"
23
24#include <cstring>
25#include <ostream>
26
27namespace Script { class Interface; }
28
29#define DEBUG_SERIALIZER_ANNOTATE 0 // annotate the stream to help debugging if you're reading the output in a hex editor
30
32{
34public:
35 CStdSerializerImpl(std::ostream& stream);
36
38 {
39 m_Stream.flush();
40 }
41
42 void Put([[maybe_unused]] const char* name, const std::uint8_t* data, size_t len)
43 {
44#if DEBUG_SERIALIZER_ANNOTATE
45 m_Stream.put('<');
46 m_Stream.write(name, strlen(name));
47 m_Stream.put('>');
48#endif
49 m_Stream.write((const char*)data, (std::streamsize)len);
50 }
51
52 std::ostream& GetStream()
53 {
54 return m_Stream;
55 }
56
57private:
58 std::ostream& m_Stream;
59};
60
61class CStdSerializer : public CBinarySerializer<CStdSerializerImpl>
62{
63public:
64 CStdSerializer(const Script::Interface& scriptInterface, std::ostream& stream);
65
66 virtual std::ostream& GetStream();
67};
68
69#endif // INCLUDED_STDSERIALIZER
Serialize to a binary stream.
Definition: BinarySerializer.h:129
Definition: StdSerializer.h:32
std::ostream & m_Stream
Definition: StdSerializer.h:58
void Put(const char *name, const std::uint8_t *data, size_t len)
Definition: StdSerializer.h:42
~CStdSerializerImpl()
Definition: StdSerializer.h:37
NONCOPYABLE(CStdSerializerImpl)
CStdSerializerImpl(std::ostream &stream)
Definition: StdSerializer.cpp:26
std::ostream & GetStream()
Definition: StdSerializer.h:52
Definition: StdSerializer.h:62
virtual std::ostream & GetStream()
Returns a stream which can be used to serialize data directly.
Definition: StdSerializer.cpp:36
CStdSerializer(const Script::Interface &scriptInterface, std::ostream &stream)
Definition: StdSerializer.cpp:31
Definition: Interface.h:93
Abstraction around a SpiderMonkey JSContext.
Definition: DapInterface.h:32