src/zserio/BitStreamWriter.h
Line | Count | Source |
1 | | #ifndef ZSERIO_BIT_STREAM_WRITER_H_INC |
2 | | #define ZSERIO_BIT_STREAM_WRITER_H_INC |
3 | | |
4 | | #include <algorithm> |
5 | | #include <cstring> |
6 | | |
7 | | #include "zserio/BitBuffer.h" |
8 | | #include "zserio/CppRuntimeException.h" |
9 | | #include "zserio/SizeConvertUtil.h" |
10 | | #include "zserio/Span.h" |
11 | | #include "zserio/StringView.h" |
12 | | #include "zserio/Types.h" |
13 | | |
14 | | namespace zserio |
15 | | { |
16 | | |
17 | | /** |
18 | | * Writer class which allows to write various data to the bit stream. |
19 | | */ |
20 | | class BitStreamWriter |
21 | | { |
22 | | public: |
23 | | /** Exception throw in case of insufficient capacity of the given buffer. */ |
24 | | class InsufficientCapacityException : public CppRuntimeException |
25 | | { |
26 | | public: |
27 | | using CppRuntimeException::CppRuntimeException; |
28 | | }; |
29 | | |
30 | | /** Type for bit position. */ |
31 | | using BitPosType = size_t; |
32 | | |
33 | | /** |
34 | | * Constructor from externally allocated byte buffer. |
35 | | * |
36 | | * \param buffer External byte buffer to create from. |
37 | | * \param bufferBitSize Size of the buffer in bits. |
38 | | */ |
39 | | explicit BitStreamWriter(uint8_t* buffer, size_t bufferBitSize, BitsTag); |
40 | | |
41 | | /** |
42 | | * Constructor from externally allocated byte buffer. |
43 | | * |
44 | | * \param buffer External byte buffer to create from. |
45 | | * \param bufferByteSize Size of the buffer in bytes. |
46 | | */ |
47 | | explicit BitStreamWriter(uint8_t* buffer, size_t bufferByteSize); |
48 | | |
49 | | /** |
50 | | * Constructor from externally allocated byte buffer. |
51 | | * |
52 | | * \param buffer External buffer to create from as a Span. |
53 | | */ |
54 | | explicit BitStreamWriter(Span<uint8_t> buffer); |
55 | | |
56 | | /** |
57 | | * Constructor from externally allocated byte buffer with exact bit size. |
58 | | * |
59 | | * \param buffer External buffer to create from as a Span. |
60 | | * \param bufferBitSize Size of the buffer in bits. |
61 | | */ |
62 | | explicit BitStreamWriter(Span<uint8_t> buffer, size_t bufferBitSize); |
63 | | |
64 | | /** |
65 | | * Constructor from externally allocated bit buffer. |
66 | | * |
67 | | * \param bitBuffer External bit buffer to create from. |
68 | | */ |
69 | | template <typename ALLOC> |
70 | | explicit BitStreamWriter(BasicBitBuffer<ALLOC>& bitBuffer) : |
71 | | BitStreamWriter(bitBuffer.getData(), bitBuffer.getBitSize()) |
72 | 469 | {} |
73 | | |
74 | | /** |
75 | | * Destructor. |
76 | | */ |
77 | | ~BitStreamWriter() = default; |
78 | | |
79 | | /** |
80 | | * Copying and moving is disallowed! |
81 | | * \{ |
82 | | */ |
83 | | BitStreamWriter(const BitStreamWriter&) = delete; |
84 | | BitStreamWriter& operator=(const BitStreamWriter&) = delete; |
85 | | |
86 | | BitStreamWriter(const BitStreamWriter&&) = delete; |
87 | | BitStreamWriter& operator=(BitStreamWriter&&) = delete; |
88 | | /** |
89 | | * \} |
90 | | */ |
91 | | |
92 | | /** |
93 | | * Writes unsigned bits up to 32 bits. |
94 | | * |
95 | | * \param data Data to write. |
96 | | * \param numBits Number of bits to write. |
97 | | */ |
98 | | void writeBits(uint32_t data, uint8_t numBits = 32); |
99 | | |
100 | | /** |
101 | | * Writes unsigned bits up to 64 bits. |
102 | | * |
103 | | * \param data Data to write. |
104 | | * \param numBits Number of bits to write. |
105 | | */ |
106 | | void writeBits64(uint64_t data, uint8_t numBits = 64); |
107 | | |
108 | | /** |
109 | | * Writes signed bits up to 32 bits. |
110 | | * |
111 | | * \param data Data to write. |
112 | | * \param numBits Number of bits to write. |
113 | | */ |
114 | | void writeSignedBits(int32_t data, uint8_t numBits = 32); |
115 | | |
116 | | /** |
117 | | * Writes signed bits up to 64 bits. |
118 | | * |
119 | | * \param data Data to write. |
120 | | * \param numBits Number of bits to write. |
121 | | */ |
122 | | void writeSignedBits64(int64_t data, uint8_t numBits = 64); |
123 | | |
124 | | /** |
125 | | * Writes signed variable integer up to 64 bits. |
126 | | * |
127 | | * \param data Varint64 to write. |
128 | | */ |
129 | | void writeVarInt64(int64_t data); |
130 | | |
131 | | /** |
132 | | * Writes signed variable integer up to 32 bits. |
133 | | * |
134 | | * \param data Varint32 to write. |
135 | | */ |
136 | | void writeVarInt32(int32_t data); |
137 | | |
138 | | /** |
139 | | * Writes signed variable integer up to 16 bits. |
140 | | * |
141 | | * \param data Varint16 to write. |
142 | | */ |
143 | | void writeVarInt16(int16_t data); |
144 | | |
145 | | /** |
146 | | * Writes unsigned variable integer up to 64 bits. |
147 | | * |
148 | | * \param data Varuint64 to write. |
149 | | */ |
150 | | void writeVarUInt64(uint64_t data); |
151 | | |
152 | | /** |
153 | | * Writes unsigned variable integer up to 32 bits. |
154 | | * |
155 | | * \param data Varuint32 to write. |
156 | | */ |
157 | | void writeVarUInt32(uint32_t data); |
158 | | |
159 | | /** |
160 | | * Writes unsigned variable integer up to 16 bits. |
161 | | * |
162 | | * \param data Varuint16 to write. |
163 | | */ |
164 | | void writeVarUInt16(uint16_t data); |
165 | | |
166 | | /** |
167 | | * Writes signed variable integer up to 72 bits. |
168 | | * |
169 | | * \param data Varuint64 to write. |
170 | | */ |
171 | | void writeVarInt(int64_t data); |
172 | | |
173 | | /** |
174 | | * Writes signed variable integer up to 72 bits. |
175 | | * |
176 | | * \param data Varuint64 to write. |
177 | | */ |
178 | | void writeVarUInt(uint64_t data); |
179 | | |
180 | | /** |
181 | | * Writes variable size integer up to 40 bits. |
182 | | * |
183 | | * \param data Varsize to write. |
184 | | */ |
185 | | void writeVarSize(uint32_t data); |
186 | | |
187 | | /** |
188 | | * Writes 16-bit float. |
189 | | * |
190 | | * \param data Float16 to write. |
191 | | */ |
192 | | void writeFloat16(float data); |
193 | | |
194 | | /** |
195 | | * Writes 32-bit float. |
196 | | * |
197 | | * \param data Float32 to write. |
198 | | */ |
199 | | void writeFloat32(float data); |
200 | | |
201 | | /** |
202 | | * Writes 64-bit float. |
203 | | * |
204 | | * \param data Float64 to write. |
205 | | */ |
206 | | void writeFloat64(double data); |
207 | | |
208 | | /** |
209 | | * Writes bytes. |
210 | | * |
211 | | * \param data Bytes to write. |
212 | | */ |
213 | | void writeBytes(Span<const uint8_t> data); |
214 | | |
215 | | /** |
216 | | * Writes UTF-8 string. |
217 | | * |
218 | | * \param data String view to write. |
219 | | */ |
220 | | void writeString(StringView data); |
221 | | |
222 | | /** |
223 | | * Writes bool as a single bit. |
224 | | * |
225 | | * \param data Bool to write. |
226 | | */ |
227 | | void writeBool(bool data); |
228 | | |
229 | | /** |
230 | | * Writes bit buffer. |
231 | | * |
232 | | * \param bitBuffer Bit buffer to write. |
233 | | */ |
234 | | template <typename ALLOC> |
235 | | void writeBitBuffer(const BasicBitBuffer<ALLOC>& bitBuffer) |
236 | 133 | { |
237 | 133 | const size_t bitSize = bitBuffer.getBitSize(); |
238 | 133 | writeVarSize(convertSizeToUInt32(bitSize)); |
239 | | |
240 | 133 | Span<const uint8_t> buffer = bitBuffer.getData(); |
241 | 133 | size_t numBytesToWrite = bitSize / 8; |
242 | 133 | const uint8_t numRestBits = static_cast<uint8_t>(bitSize - numBytesToWrite * 8); |
243 | 133 | const BitPosType beginBitPosition = getBitPosition(); |
244 | 133 | const Span<const uint8_t>::iterator itEnd = buffer.begin() + numBytesToWrite; |
245 | 133 | if ((beginBitPosition & 0x07U) != 0) |
246 | 67 | { |
247 | | // we are not aligned to byte |
248 | 147 | for (Span<const uint8_t>::iterator it = buffer.begin(); it != itEnd; ++it80 ) |
249 | 80 | { |
250 | 80 | writeUnsignedBits(*it, 8); |
251 | 80 | } |
252 | 67 | } |
253 | 66 | else |
254 | 66 | { |
255 | | // we are aligned to byte |
256 | 66 | setBitPosition(beginBitPosition + numBytesToWrite * 8); |
257 | 66 | if (hasWriteBuffer()) |
258 | 64 | { |
259 | 64 | (void)std::copy(buffer.begin(), buffer.begin() + numBytesToWrite, |
260 | 64 | m_buffer.data() + beginBitPosition / 8); |
261 | 64 | } |
262 | 66 | } |
263 | | |
264 | 133 | if (numRestBits > 0) |
265 | 128 | { |
266 | 128 | writeUnsignedBits(static_cast<uint32_t>(*itEnd) >> (8U - numRestBits), numRestBits); |
267 | 128 | } |
268 | 133 | } |
269 | | |
270 | | /** |
271 | | * Gets current bit position. |
272 | | * |
273 | | * \return Current bit position. |
274 | | */ |
275 | | BitPosType getBitPosition() const |
276 | 17.3k | { |
277 | 17.3k | return m_bitIndex; |
278 | 17.3k | } |
279 | | |
280 | | /** |
281 | | * Sets current bit position. Use with caution! |
282 | | * |
283 | | * \param position New bit position. |
284 | | */ |
285 | | void setBitPosition(BitPosType position); |
286 | | |
287 | | /** |
288 | | * Moves current bit position to perform the requested bit alignment. |
289 | | * |
290 | | * \param alignment Size of the alignment in bits. |
291 | | */ |
292 | | void alignTo(size_t alignment); |
293 | | |
294 | | /** |
295 | | * Gets whether the writer has assigned a write buffer. |
296 | | * |
297 | | * \return True when a buffer is assigned. False otherwise. |
298 | | */ |
299 | | bool hasWriteBuffer() const |
300 | 105k | { |
301 | 105k | return m_buffer.data() != nullptr; |
302 | 105k | } |
303 | | |
304 | | /** |
305 | | * Gets the write buffer. |
306 | | * |
307 | | * \return Pointer to the beginning of write buffer. |
308 | | */ |
309 | | const uint8_t* getWriteBuffer() const; |
310 | | |
311 | | /** |
312 | | * Gets the write buffer as span. |
313 | | * |
314 | | * \return Span which represents the write buffer. |
315 | | */ |
316 | | Span<const uint8_t> getBuffer() const; |
317 | | |
318 | | /** |
319 | | * Gets size of the underlying buffer in bits. |
320 | | * |
321 | | * \return Buffer bit size. |
322 | | */ |
323 | | size_t getBufferBitSize() const |
324 | 5 | { |
325 | 5 | return m_bufferBitSize; |
326 | 5 | } |
327 | | |
328 | | private: |
329 | | void writeUnsignedBits(uint32_t data, uint8_t numBits); |
330 | | void writeUnsignedBits64(uint64_t data, uint8_t numBits); |
331 | | void writeSignedVarNum(int64_t value, size_t maxVarBytes, size_t numVarBytes); |
332 | | void writeUnsignedVarNum(uint64_t value, size_t maxVarBytes, size_t numVarBytes); |
333 | | void writeVarNum(uint64_t value, bool hasSign, bool isNegative, size_t maxVarBytes, size_t numVarBytes); |
334 | | |
335 | | void checkCapacity(size_t bitSize) const; |
336 | | void throwInsufficientCapacityException() const; |
337 | | |
338 | | Span<uint8_t> m_buffer; |
339 | | size_t m_bitIndex; |
340 | | size_t m_bufferBitSize; |
341 | | }; |
342 | | |
343 | | } // namespace zserio |
344 | | |
345 | | #endif // ifndef ZSERIO_BIT_STREAM_WRITER_H_INC |