/
block.h
207 lines (177 loc) · 6.88 KB
/
block.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
* The Doomsday Engine Project -- libcore
*
* Copyright © 2004-2017 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 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 Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#ifndef LIBCORE_BLOCK_H
#define LIBCORE_BLOCK_H
#include "../IByteArray"
#include "../IBlock"
#include "../ISerializable"
#include "../List"
#include "../Writer"
#include <c_plus/block.h>
#include <array>
namespace de {
class String;
class IIStream;
/**
* Data buffer that implements the byte array interface.
*
* Note that Block is based on QByteArray, and thus automatically always ensures
* that the data is followed by a terminating \0 character (even if one is not
* part of the actual Block contents). Therefore it is safe to use it in functions
* that assume zero-terminated strings.
*
* @ingroup data
*/
class DE_PUBLIC Block
: public IByteArray
, public IBlock
, public ISerializable
{
public:
Block(Size initialSize = 0);
Block(const iBlock *);
Block(IByteArray const &array);
Block(Block const &other);
Block(Block &&moved);
Block(char const *nullTerminatedCStr);
Block(void const *data, Size length);
/**
* Constructs a block by reading the contents of an input stream. The block
* will contain all the data that is available immediately; will not wait
* for additional data to become available later.
*
* @param stream Stream to read from.
*/
Block(IIStream &stream);
/**
* Constructs a block by reading the contents of an input stream in const
* mode. The block will contain all the data that is available immediately;
* will not wait for additional data to become available later.
*
* @param stream Stream to read from.
*/
Block(IIStream const &stream);
/**
* Construct a new block and copy its contents from the specified
* location at another array.
*
* @param array Source data.
* @param at Offset within source data.
* @param count Number of bytes to copy. Also the size of the new block.
*/
Block(IByteArray const &array, Offset at, Size count);
virtual ~Block();
Byte *data();
Byte const *dataConst() const;
inline Byte const *cdata() const { return dataConst(); }
inline Byte const *constData() const { return dataConst(); }
Block &append(Byte b);
Block &append(char const *str, int len);
operator const iBlock *() const { return &_block; }
inline explicit operator bool() const { return !isEmpty_Block(&_block); }
Block &operator += (char const *nullTerminatedCStr);
// Block &operator += (QByteArray const &bytes);
/// Appends a block after this one.
Block &operator += (Block const &other);
/// Appends a byte array after this one.
Block &operator += (IByteArray const &byteArray);
/// Copies the contents of another block.
Block &operator = (Block const &other);
Block &operator = (IByteArray const &byteArray);
Block compressed(int level = -1) const;
Block decompressed() const;
Block md5Hash() const;
String asHexadecimalText() const;
/**
* Uses each byte in the block as an index to @a values and composes a new
* block by copying sets of values by indices.
*
* In the resulting block, each byte of this block is represented by four bytes.
* If @a valuesPerIndex is less than four, the missing values are taken from
* @a defaultValues.
*
* In each quartet of output values, the bytes are packed in little-endian order.
*
* @param valuesPerIndex Number of values per index stored in @a values.
* @param values Value array.
* @param defaultValues Output to use for missing values.
*
* @return New block whose size is four times larger than this block.
*/
Block mapAsIndices(int valuesPerIndex,
const IByteArray & values,
const std::array<Byte, 4> &defaultValues) const;
/**
* Uses each byte in the block as an index to @a values and composes a new
* block by copying sets of values by indices.
*
* In the resulting block, each byte of this block is represented by four bytes.
* If @a valuesPerIndex is less than four, the missing values are taken from
* the @a defaultValues array.
*
* In each quartet of output values, the bytes are packed in little-endian order.
*
* @param valuesPerIndex Number of values per index stored in @a values.
* @param values Value array.
* @param defaultValues Output to use for missing values. The size must match
* this block's size. Treated as a separate layer whose
* dimensions match this block. Each missing value in the
* output gets the same byte from @a defaultValues, whose
* position matches the position of the source byte.
*
* @return New block whose size is four times larger than this block.
*/
Block mapAsIndices(int valuesPerIndex,
const IByteArray &values,
const IByteArray &defaultValues) const;
// Implements IByteArray.
Size size() const;
void get(Offset at, Byte *values, Size count) const;
void set(Offset at, Byte const *values, Size count);
// Implements IBlock.
void clear();
void copyFrom(IByteArray const &array, Offset at, Size count);
void resize(Size size);
Byte const *data() const;
// Implements ISerializable.
/**
* 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.
*/
void operator >> (Writer &to) const;
void operator << (Reader &from);
public:
static Block join(List<Block> const &blocks, Block const &sep = Block());
private:
iBlock _block;
};
template <typename... Args>
Block md5Hash(Args... args) {
Block data;
Writer writer(data);
writer.writeMultiple(args...);
return data.md5Hash();
}
} // namespace de
#endif // LIBCORE_BLOCK_H