/
writer.h
192 lines (163 loc) · 5.59 KB
/
writer.h
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* The Doomsday Engine Project -- libdeng2
*
* Copyright (c) 2004-2012 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* This program 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.
*
* This program 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 this program; if not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIBDENG2_WRITER_H
#define LIBDENG2_WRITER_H
#include "../libdeng2.h"
#include "../IByteArray"
#include "../ByteOrder"
#include <algorithm> // std::swap
namespace de {
class IWritable;
class String;
class Block;
class ByteArrayFile;
class FixedByteArray;
class IOStream;
/**
* Provides a protocol for writing data in a specific byte order into a byte
* array object (anything with a IByteArray interface). Defaults to
* little-endian byte order.
*
* @ingroup data
*/
class DENG2_PUBLIC Writer
{
public:
/// Seeking is not possible, e.g., when writing to a stream. @ingroup errors
DENG2_ERROR(SeekError);
public:
/**
* Constructs a new writer.
*
* @param destination Byte array to write to.
* @param byteOrder Byte order to use. The byte order defaults to little-endian byte order.
* @param offset Offset in @a destination where to start writing.
*/
Writer(IByteArray &destination, ByteOrder const &byteOrder = littleEndianByteOrder,
IByteArray::Offset offset = 0);
/**
* Constructs a new writer (with little-endian byte order).
*
* @param destination Byte array to write to.
* @param offset Offset in @a destination where to start writing.
*/
Writer(IByteArray &destination, IByteArray::Offset offset);
/**
* Constructs a new writer for writing to an I/O stream.
*
* @param stream Stream to write to.
* @param byteOrder Byte order to use.
*/
Writer(IOStream &stream, ByteOrder const &byteOrder = littleEndianByteOrder);
/**
* Constructs a new writer for writing into a byte array file.
*
* @param destination Byte array file to write to.
* @param byteOrder Byte order to use.
* @param offset Offset in @a destination where to start writing.
*/
Writer(ByteArrayFile &destination, ByteOrder const &byteOrder = littleEndianByteOrder,
IByteArray::Offset offset = 0);
/**
* Copy constructor.
*
* @param other Writer.
*/
Writer(Writer const &other);
/**
* Constructs a new writer that uses the current offset of @a other as its
* zero offset.
*
* @param other Writer.
* @param byteOrder Byte order.
*/
Writer(Writer const &other, ByteOrder const &byteOrder);
virtual ~Writer();
//@{ Write a number to the destination buffer, in the chosen byte order.
Writer &operator << (char const &byte);
Writer &operator << (dchar const &byte);
Writer &operator << (duchar const &byte);
Writer &operator << (dint16 const &word);
Writer &operator << (duint16 const &word);
Writer &operator << (dint32 const &dword);
Writer &operator << (duint32 const &dword);
Writer &operator << (dint64 const &qword);
Writer &operator << (duint64 const &qword);
Writer &operator << (dfloat const &value);
Writer &operator << (ddouble const &value);
//@}
/// Write a string to the destination buffer.
Writer &operator << (String const &text);
/// Writes a sequence bytes to the destination buffer.
Writer &operator << (IByteArray const &byteArray);
/**
* Writes a fixed-size sequence of bytes to the destination buffer.
* The size of the sequence is not included in the written data.
* When reading, the reader must know the size beforehand.
* @see Reader::operator >> (FixedByteArray &fixedByteArray)
*
* @param fixedByteArray Data to write.
*
* @return Reference to the Writer.
*/
Writer &operator << (FixedByteArray const &fixedByteArray);
/**
* Writes @a block into the destination buffer. Writes the size of the
* block in addition to its contents, so a Reader will not need to know
* beforehand how large the block is.
*
* @param block Block to write.
*
* @return Reference to the Writer.
*/
Writer &operator << (Block const &block);
/// Writes a writable object into the destination buffer.
Writer &operator << (IWritable const &writable);
/**
* Returns the destination byte array used by the writer.
*/
IByteArray const *destination() const;
/**
* Returns the destination byte array used by the writer.
*/
IByteArray *destination();
/**
* Returns the offset used by the writer.
*/
IByteArray::Offset offset() const;
void setOffset(IByteArray::Offset offset);
/**
* Returns the byte order of the writer.
*/
ByteOrder const &byteOrder() const;
/**
* Moves the writer offset forward by a number of bytes.
*
* @param count Number of bytes to move forward (negative to move backward).
*/
void seek(dint count);
inline void swap(Writer &other) {
std::swap(d, other.d);
}
private:
struct Instance;
Instance *d;
};
} // namespace de
#endif /* LIBDENG2_WRITER_H */