VR-Engage  2.2
Loading...
Searching...
No Matches
byteStream.h
Go to the documentation of this file.
1/*********************************************************************************
2** Copyright (c) 2025 MAK Technologies
3** All rights reserved.
4*********************************************************************************/
5
6#pragma once
7
8//! \file byteStream.h
9//! \brief Defines stream readers and writers for encoding and decoding messages from byte arrays
10//! \ingroup vreRadarFxShared
11
12#include "export.h"
13
14#include <vlpi/entityIdentifier.h>
15#include <vlpi/entityType.h>
16#include <matrix/vlVector.h>
17#include <matrix/vlTaitBryan.h>
18#include <matrix/vlQuaternion.h>
19
20#include <vector>
21#include <string>
22
23namespace makRadarFx
24{
25//! \brief Stream reader for decoding values from a byte array in little endian format
26//!
27//! This class reads and decodes various data types from a byte array,
28//! assuming the data in the byte stream is in little endian format.
29//! It provides methods for reading primitive types as well as common
30//! complex data structures.
32{
33public:
34 //! \brief Constructor for stream reader
35 //! \param buffer Pointer to the input byte array
36 //! \param length Length of the input buffer in bytes
37 //! \param makeCopy Whether to make a copy of the buffer (true) or use the original (false)
38 //!
39 //! Creates a new stream reader to work with the provided buffer
40 DtStreamReader(unsigned char* buffer, int length, bool makeCopy = false);
41
42 //! \brief Virtual destructor
43 //!
44 //! Cleans up resources, including the buffer if it was copied
45 virtual ~DtStreamReader();
46
47 //! \name Value Reading Methods
48 //! @{
49 //! \brief Each method reads a value and advances the read position
50
51 //! \brief Reads a boolean value
52 //! \return The boolean value read from the stream
53 virtual bool readBool();
54
55 //! \brief Reads an 8-bit signed integer
56 //! \return The 8-bit signed integer value read from the stream
57 virtual char readInt8();
58
59 //! \brief Reads an 8-bit unsigned integer
60 //! \return The 8-bit unsigned integer value read from the stream
61 virtual unsigned char readUInt8();
62
63 //! \brief Reads a 16-bit signed integer
64 //! \return The 16-bit signed integer value read from the stream
65 virtual short readInt16();
66
67 //! \brief Reads a 16-bit unsigned integer
68 //! \return The 16-bit unsigned integer value read from the stream
69 virtual unsigned short readUInt16();
70
71 //! \brief Reads a 32-bit signed integer
72 //! \return The 32-bit signed integer value read from the stream
73 virtual int readInt32();
74
75 //! \brief Reads a 32-bit unsigned integer
76 //! \return The 32-bit unsigned integer value read from the stream
77 virtual unsigned readUInt32();
78
79 //! \brief Reads a 64-bit signed integer
80 //! \return The 64-bit signed integer value read from the stream
81 virtual long long readInt64();
82
83 //! \brief Reads a 64-bit unsigned integer
84 //! \return The 64-bit unsigned integer value read from the stream
85 virtual unsigned long long readUInt64();
86
87 //! \brief Reads a 32-bit floating point value
88 //! \return The 32-bit float value read from the stream
89 virtual float readSingle();
90
91 //! \brief Reads a 64-bit floating point value
92 //! \return The 64-bit double value read from the stream
93 virtual double readDouble();
94
95 //! \brief Reads a string
96 //! \return The string value read from the stream
97 virtual std::string readString();
98
99 //! \brief Reads a 3D vector
100 //! \return The vector value read from the stream
101 virtual DtVector readVector();
102
103 //! \brief Reads a Tait-Bryan angle set
104 //! \return The Tait-Bryan angles read from the stream
105 virtual DtTaitBryan readTaitBryan();
106
107 //! \brief Reads a quaternion
108 //! \return The quaternion value read from the stream
109 DtQuaternion readQuaternion();
110
111 //! \brief Reads an entity identifier
112 //! \return The entity identifier read from the stream
113 DtEntityIdentifier readEntityId();
114
115 //! \brief Reads an entity type
116 //! \return The entity type read from the stream
117 DtEntityType readEntityType();
118
119 //! \brief Allocates and reads bytes into a new buffer
120 //! \param length Number of bytes to read
121 //! \return Pointer to a newly allocated buffer containing the read bytes
122 //!
123 //! This method allocates a new byte array and copies data into it
124 unsigned char* readBytes(int length);
125
126 //! \brief Reads bytes into a supplied buffer
127 //! \param buffer Pointer to the destination buffer
128 //! \param length Number of bytes to read
129 //!
130 //! This reads from the byte stream into a supplied buffer
131 virtual void readBytes(unsigned char* buffer, int length);
132
133 //@}
134
135protected:
136 //! \brief Determines the size of a string from a variable length encoded integer
137 //! \return The decoded size of the following string
139
140 //! \brief Pointer to the beginning of the buffer
141 unsigned char* myBuffer;
142
143 //! \brief Current read position within the buffer
144 unsigned char* myReadHead;
145
146 //! \brief Total length of the buffer in bytes
148
149 //! \brief Flag indicating if the buffer should be deleted on destruction
151};
152
153//! \brief Stream writer for encoding values into a byte array in little endian format
154//!
155//! This class writes and encodes various data types into a byte array
156//! in little endian format. It provides methods for writing primitive types
157//! as well as common complex data structures.
159{
160public:
161 //! \brief Constructor that creates an internal buffer
162 //! \param length Size of the internal buffer to create
163 //!
164 //! Creates a new stream writer with an internal buffer of the specified size.
165 //! Care must be taken to allocate enough space for all the intended writes.
167
168 //! \brief Constructor that uses an existing buffer
169 //! \param buffer Pointer to the buffer to write to
170 //! \param length Size of the buffer in bytes
171 //!
172 //! Creates a new stream writer using the provided buffer.
173 //! Care must be taken to ensure the buffer has enough space for all writes.
174 DtStreamWriter(unsigned char* buffer, int length);
175
176 //! \brief Virtual destructor
177 //!
178 //! Cleans up resources, including the buffer if it was allocated internally
180
181 //! \name Buffer Access Methods
182 //! @{
183
184 //! \brief Gets a pointer to the internal buffer
185 //! \return Pointer to the internal buffer
186 virtual unsigned char* buffer();
187
188 //! \brief Gets the current write position as a length
189 //! \return Number of bytes written to the buffer so far
190 virtual int length();
191
192 //! @}
193
194 //! \name Value Writing Methods
195 //! @{
196 //! \brief Each method writes a value and advances the write position
197
198 //! \brief Writes a boolean value
199 //! \param b Boolean value to write
200 virtual void writeBool(bool b);
201
202 //! \brief Writes an 8-bit signed integer
203 //! \param num Value to write
204 virtual void writeInt8(char num);
205
206 //! \brief Writes an 8-bit unsigned integer
207 //! \param num Value to write
208 virtual void writeUInt8(unsigned char num);
209
210 //! \brief Writes a 16-bit signed integer
211 //! \param num Value to write
212 virtual void writeInt16(short num);
213
214 //! \brief Writes a 16-bit unsigned integer
215 //! \param num Value to write
216 virtual void writeUInt16(unsigned short num);
217
218 //! \brief Writes a 32-bit signed integer
219 //! \param num Value to write
220 virtual void writeInt32(int num);
221
222 //! \brief Writes a 32-bit unsigned integer
223 //! \param num Value to write
224 virtual void writeUInt32(unsigned int num);
225
226 //! \brief Writes a 64-bit signed integer
227 //! \param num Value to write
228 virtual void writeInt64(long long num);
229
230 //! \brief Writes a 64-bit unsigned integer
231 //! \param num Value to write
232 virtual void writeUInt64(unsigned long long num);
233
234 //! \brief Writes a 32-bit floating point value
235 //! \param num Value to write
236 virtual void writeSingle(float num);
237
238 //! \brief Writes a 64-bit floating point value
239 //! \param num Value to write
240 virtual void writeDouble(double num);
241
242 //! \brief Writes a string
243 //! \param str String value to write
244 virtual void writeString(std::string& str);
245
246 //! \brief Writes a 3D vector
247 //! \param vec Vector to write
248 virtual void writeVector(DtVector& vec);
249
250 //! \brief Writes a Tait-Bryan angle set
251 //! \param tb Tait-Bryan angles to write
252 virtual void writeTaitBryan(DtTaitBryan& tb);
253
254 //! \brief Writes a quaternion
255 //! \param q Quaternion to write
256 virtual void writeQuaternion(const DtQuaternion& q);
257
258 //! \brief Writes an entity identifier
259 //! \param id Entity identifier to write
260 virtual void writeEntityId(const DtEntityIdentifier& id);
261
262 //! \brief Writes an entity type
263 //! \param type Entity type to write
264 virtual void writeEntityType(const DtEntityType& type);
265
266 //! \brief Writes raw bytes
267 //! \param buffer Pointer to the source buffer
268 //! \param length Number of bytes to write
269 virtual void writeBytes(unsigned char* buffer, int length);
270 //@}
271
272protected:
273 //! \brief Encodes the size of the string as a variable length integer
274 //! \param size Size to encode
275 void encodeStringSize(int size);
276
277 //! \brief Pointer to the beginning of the buffer
278 unsigned char* myBuffer;
279
280 //! \brief Current write position within the buffer
281 unsigned char* myWriteHead;
282
283 //! \brief Total length of the buffer in bytes
285
286 //! \brief Flag indicating if the buffer should be deleted on destruction
288};
289
290//! \brief Stream reader for decoding values from a byte array in big endian format
291//!
292//! This class extends DtStreamReader to read and decode data from a byte array
293//! where the data is stored in big endian format. It overrides the methods
294//! that need byte swapping to convert from big endian to the system's native format.
296{
297public:
298 //! \brief Constructor for big endian stream reader
299 //! \param buffer Pointer to the input byte array
300 //! \param length Length of the input buffer in bytes
301 //! \param makeCopy Whether to make a copy of the buffer (true) or use the original (false)
302 //!
303 //! Creates a new big endian stream reader to work with the provided buffer
304 DtBigEndianStreamReader(unsigned char* buffer, int length, bool makeCopy = false);
305
306 //! \brief Virtual destructor
308
309 //! \name Big Endian Value Reading Methods
310 //! @{
311 //! \brief Each method reads a big endian value and advances the read position
312
313 //! \brief Reads a 16-bit signed integer in big endian format
314 //! \return The 16-bit signed integer value read from the stream
315 virtual short readInt16();
316
317 //! \brief Reads a 16-bit unsigned integer in big endian format
318 //! \return The 16-bit unsigned integer value read from the stream
319 virtual unsigned short readUInt16();
320
321 //! \brief Reads a 32-bit signed integer in big endian format
322 //! \return The 32-bit signed integer value read from the stream
323 virtual int readInt32();
324
325 //! \brief Reads a 32-bit unsigned integer in big endian format
326 //! \return The 32-bit unsigned integer value read from the stream
327 virtual unsigned readUInt32();
328
329 //! \brief Reads a 64-bit signed integer in big endian format
330 //! \return The 64-bit signed integer value read from the stream
331 virtual long long readInt64();
332
333 //! \brief Reads a 64-bit unsigned integer in big endian format
334 //! \return The 64-bit unsigned integer value read from the stream
335 virtual unsigned long long readUInt64();
336
337 //! \brief Reads a 32-bit floating point value in big endian format
338 //! \return The 32-bit float value read from the stream
339 virtual float readSingle();
340
341 //! \brief Reads a 64-bit floating point value in big endian format
342 //! \return The 64-bit double value read from the stream
343 virtual double readDouble();
344 //@}
345
346protected:
347};
348
349//! \brief Stream writer for encoding values into a byte array in big endian format
350//!
351//! This class extends DtStreamWriter to write and encode data into a byte array
352//! in big endian format. It overrides the methods that need byte swapping
353//! to convert from the system's native format to big endian.
355{
356public:
357 //! \brief Constructor that creates an internal buffer
358 //! \param length Size of the internal buffer to create
359 //!
360 //! Creates a new big endian stream writer with an internal buffer of the specified size.
361 //! Care must be taken to allocate enough space for all the intended writes.
363
364 //! \brief Constructor that uses an existing buffer
365 //! \param buffer Pointer to the buffer to write to
366 //! \param length Size of the buffer in bytes
367 //!
368 //! Creates a new big endian stream writer using the provided buffer.
369 //! Care must be taken to ensure the buffer has enough space for all writes.
371
372 //! \brief Virtual destructor
374
375 //! \name Big Endian Value Writing Methods
376 //! @{
377 //! \brief Each method writes a value in big endian format and advances the write position
378
379 //! \brief Writes a 16-bit signed integer in big endian format
380 //! \param num Value to write
381 void writeInt16(short num);
382
383 //! \brief Writes a 16-bit unsigned integer in big endian format
384 //! \param num Value to write
385 void writeUInt16(unsigned short num);
386
387 //! \brief Writes a 32-bit signed integer in big endian format
388 //! \param num Value to write
389 void writeInt32(int num);
390
391 //! \brief Writes a 32-bit unsigned integer in big endian format
392 //! \param num Value to write
393 void writeUInt32(unsigned int num);
394
395 //! \brief Writes a 64-bit signed integer in big endian format
396 //! \param num Value to write
397 void writeInt64(long long num);
398
399 //! \brief Writes a 64-bit unsigned integer in big endian format
400 //! \param num Value to write
401 void writeUInt64(unsigned long long num);
402
403 //! \brief Writes a 32-bit floating point value in big endian format
404 //! \param num Value to write
405 void writeSingle(float num);
406
407 //! \brief Writes a 64-bit floating point value in big endian format
408 //! \param num Value to write
409 void writeDouble(double num);
410 //@}
411
412protected:
413};
414
415//! \brief Calculates the size in bytes of the string when encoded
416//! \param str String to calculate the encoded size for
417//! \return Size in bytes that the string will occupy when encoded
418//!
419//! This function calculates the total number of bytes that will be
420//! required to encode the given string, including the size prefix.
422} // namespace makRadarFx
virtual float readSingle()
Reads a 32-bit floating point value in big endian format.
DtBigEndianStreamReader(unsigned char *buffer, int length, bool makeCopy=false)
Constructor for big endian stream reader.
virtual ~DtBigEndianStreamReader()
Virtual destructor.
virtual long long readInt64()
Reads a 64-bit signed integer in big endian format.
virtual short readInt16()
Reads a 16-bit signed integer in big endian format.
virtual unsigned short readUInt16()
Reads a 16-bit unsigned integer in big endian format.
virtual int readInt32()
Reads a 32-bit signed integer in big endian format.
virtual unsigned readUInt32()
Reads a 32-bit unsigned integer in big endian format.
virtual unsigned long long readUInt64()
Reads a 64-bit unsigned integer in big endian format.
virtual double readDouble()
Reads a 64-bit floating point value in big endian format.
void writeInt16(short num)
Writes a 16-bit signed integer in big endian format.
void writeUInt16(unsigned short num)
Writes a 16-bit unsigned integer in big endian format.
DtBigEndianStreamWriter(unsigned char *buffer, int length)
Constructor that uses an existing buffer.
void writeInt64(long long num)
Writes a 64-bit signed integer in big endian format.
void writeInt32(int num)
Writes a 32-bit signed integer in big endian format.
void writeUInt64(unsigned long long num)
Writes a 64-bit unsigned integer in big endian format.
void writeDouble(double num)
Writes a 64-bit floating point value in big endian format.
DtBigEndianStreamWriter(int length)
Constructor that creates an internal buffer.
void writeSingle(float num)
Writes a 32-bit floating point value in big endian format.
virtual ~DtBigEndianStreamWriter()
Virtual destructor.
void writeUInt32(unsigned int num)
Writes a 32-bit unsigned integer in big endian format.
virtual char readInt8()
Reads an 8-bit signed integer.
virtual double readDouble()
Reads a 64-bit floating point value.
virtual bool readBool()
Reads a boolean value.
DtQuaternion readQuaternion()
Reads a quaternion.
virtual DtVector readVector()
Reads a 3D vector.
DtStreamReader(unsigned char *buffer, int length, bool makeCopy=false)
Constructor for stream reader.
int decodeStringSize()
Determines the size of a string from a variable length encoded integer.
virtual DtTaitBryan readTaitBryan()
Reads a Tait-Bryan angle set.
int myLength
Total length of the buffer in bytes.
Definition byteStream.h:147
DtEntityType readEntityType()
Reads an entity type.
virtual unsigned readUInt32()
Reads a 32-bit unsigned integer.
virtual void readBytes(unsigned char *buffer, int length)
Reads bytes into a supplied buffer.
virtual unsigned short readUInt16()
Reads a 16-bit unsigned integer.
DtEntityIdentifier readEntityId()
Reads an entity identifier.
unsigned char * myReadHead
Current read position within the buffer.
Definition byteStream.h:144
virtual unsigned long long readUInt64()
Reads a 64-bit unsigned integer.
virtual float readSingle()
Reads a 32-bit floating point value.
virtual long long readInt64()
Reads a 64-bit signed integer.
virtual int readInt32()
Reads a 32-bit signed integer.
virtual ~DtStreamReader()
Virtual destructor.
bool myShouldDelete
Flag indicating if the buffer should be deleted on destruction.
Definition byteStream.h:150
unsigned char * readBytes(int length)
Allocates and reads bytes into a new buffer.
virtual std::string readString()
Reads a string.
virtual short readInt16()
Reads a 16-bit signed integer.
virtual unsigned char readUInt8()
Reads an 8-bit unsigned integer.
unsigned char * myBuffer
Pointer to the beginning of the buffer.
Definition byteStream.h:141
virtual void writeQuaternion(const DtQuaternion &q)
Writes a quaternion.
virtual void writeInt32(int num)
Writes a 32-bit signed integer.
virtual void writeInt64(long long num)
Writes a 64-bit signed integer.
virtual void writeVector(DtVector &vec)
Writes a 3D vector.
virtual void writeSingle(float num)
Writes a 32-bit floating point value.
bool myShouldDelete
Flag indicating if the buffer should be deleted on destruction.
Definition byteStream.h:287
virtual void writeUInt8(unsigned char num)
Writes an 8-bit unsigned integer.
virtual void writeInt16(short num)
Writes a 16-bit signed integer.
virtual void writeBytes(unsigned char *buffer, int length)
Writes raw bytes.
unsigned char * myBuffer
Pointer to the beginning of the buffer.
Definition byteStream.h:278
virtual void writeEntityId(const DtEntityIdentifier &id)
Writes an entity identifier.
DtStreamWriter(int length)
Constructor that creates an internal buffer.
virtual void writeUInt64(unsigned long long num)
Writes a 64-bit unsigned integer.
virtual void writeUInt32(unsigned int num)
Writes a 32-bit unsigned integer.
DtStreamWriter(unsigned char *buffer, int length)
Constructor that uses an existing buffer.
virtual void writeBool(bool b)
Writes a boolean value.
virtual void writeDouble(double num)
Writes a 64-bit floating point value.
virtual void writeTaitBryan(DtTaitBryan &tb)
Writes a Tait-Bryan angle set.
unsigned char * myWriteHead
Current write position within the buffer.
Definition byteStream.h:281
virtual unsigned char * buffer()
Gets a pointer to the internal buffer.
virtual ~DtStreamWriter()
Virtual destructor.
virtual int length()
Gets the current write position as a length.
virtual void writeInt8(char num)
Writes an 8-bit signed integer.
virtual void writeUInt16(unsigned short num)
Writes a 16-bit unsigned integer.
virtual void writeString(std::string &str)
Writes a string.
int myLength
Total length of the buffer in bytes.
Definition byteStream.h:284
virtual void writeEntityType(const DtEntityType &type)
Writes an entity type.
void encodeStringSize(int size)
Encodes the size of the string as a variable length integer.
Defines export macros for the RadarFx Shared library.
#define RFX_DLL_SHARED
Export/import macro for non-Windows platforms (empty)
Definition export.h:25
Definition radarFxConnectorDriver.h:21
RFX_DLL_SHARED int DtEncodedStringLength(std::string str)
Calculates the size in bytes of the string when encoded.