VR-Engage  2.3
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//! \file byteStream.h
7//! \ingroup vreUtil
8//! \brief Provides binary serialization and deserialization capabilities
9//!
10//! This file defines the DtStreamReader and DtStreamWriter classes that handle
11//! reading and writing of various data types to and from byte streams. These
12//! classes support primitives, strings, vectors, and VREngage-specific types.
13
14#pragma once
15
16#include "vreUtil/export.h"
18
19#include <vlpi/entityIdentifier.h>
20#include <vlpi/entityType.h>
21#include <vlpi/simulationAddress.h>
22#include <matrix/vlVector.h>
23#include <matrix/vlTaitBryan.h>
24#include <matrix/vlQuaternion.h>
25
26#include <string>
27#include <vrfutil/uuid.h>
28
29namespace makVre
30{
31
32
33//! \brief Calculates the size of a string when encoded in the byte stream format
34//! \param str The string to calculate the encoded size for
35//! \return The size in bytes that the string will occupy when encoded
36int UTIL_DLL DtEncodedStringSize(const std::string& str);
37
38//! \brief Reader class for decoding values from a byte array
39//!
40//! This class provides methods for reading various data types from a byte stream,
41//! maintaining the current read position, and handling platform-specific byte ordering.
43{
44public:
45 //! \brief Constructor
46 //! \param buffer Pointer to the buffer to read from
47 //! \param length Length of the buffer in bytes
48 //! \param makeCopy If true, make a copy of the buffer; if false, use the provided buffer directly
49 DtStreamReader(char* buffer, int length, bool makeCopy = false);
50
51 //! \brief Virtual destructor
52 //!
53 //! Cleans up resources, including the buffer if it was copied
54 virtual ~DtStreamReader();
55
56 //! \name Value Readers
57 //! \brief Methods to read values from the byte stream
58 //!
59 //! These methods return a value and increment the current position in the byte stream
60 //! based on the size of the value read.
61 //! @{
62
63 //! \brief Reads a boolean value
64 //! \return The boolean value read from the stream
65 bool readBool();
66 //! \brief Reads a signed 8-bit integer
67 //! \return The signed 8-bit integer read from the stream
68 char readInt8();
69
70 //! \brief Reads an unsigned 8-bit integer
71 //! \return The unsigned 8-bit integer read from the stream
72 unsigned char readUInt8();
73
74 //! \brief Reads a signed 16-bit integer
75 //! \return The signed 16-bit integer read from the stream
76 short readInt16();
77
78 //! \brief Reads an unsigned 16-bit integer
79 //! \return The unsigned 16-bit integer read from the stream
80 unsigned short readUInt16();
81
82 //! \brief Reads a signed 32-bit integer
83 //! \return The signed 32-bit integer read from the stream
84 int readInt32();
85
86 //! \brief Reads an unsigned 32-bit integer
87 //! \return The unsigned 32-bit integer read from the stream
88 unsigned int readUInt32();
89
90 //! \brief Reads a signed 64-bit integer
91 //! \return The signed 64-bit integer read from the stream
93
94 //! \brief Reads an unsigned 64-bit integer
95 //! \return The unsigned 64-bit integer read from the stream
97
98 //! \brief Reads a 32-bit floating point value
99 //! \return The 32-bit float read from the stream
100 float readSingle();
101
102 //! \brief Reads a 64-bit floating point value
103 //! \return The 64-bit double read from the stream
104 double readDouble();
105 //! \brief Reads a string value
106 //! \return The string read from the stream
107 std::string readString();
108
109 //! \brief Reads a 3D vector
110 //! \return The vector read from the stream
111 DtVector readVector();
112
113 //! \brief Reads a Tait-Bryan angles representation
114 //! \return The Tait-Bryan angles read from the stream
115 DtTaitBryan readTaitBryan();
116
117 //! \brief Reads a quaternion
118 //! \return The quaternion read from the stream
119 DtQuaternion readQuaternion();
120
121 //! \brief Reads an entity identifier
122 //! \return The entity identifier read from the stream
123 DtEntityIdentifier readEntityId();
124
125 //! \brief Reads an entity type
126 //! \return The entity type read from the stream
127 DtEntityType readEntityType();
128
129 //! \brief Reads a simulation address
130 //! \return The simulation address read from the stream
131 DtSimulationAddress readSimulationAddress();
132
133 //! \brief Reads a UUID
134 //! \return The UUID read from the stream
135 DtUUID readUUID();
136
137 //! \brief Reads a byte array of specified length
138 //! \param length Number of bytes to read
139 //! \return Pointer to the read bytes (caller must manage memory)
140 char* readBytes(int length);
141 //@}
142
143protected:
144 //! \brief Determines the size of a string from a variable length encoded integer
145 //! \return The decoded string size
147
148 //! \brief Pointer to the buffer being read from
149 char* myBuffer;
150
151 //! \brief Current position in the buffer
153
154 //! \brief Total length of the buffer
156
157 //! \brief Flag indicating if the buffer should be deleted on destruction
159};
160
161//! \brief Writer class for encoding values into a byte array
162//!
163//! This class provides methods for writing various data types to a byte stream,
164//! maintaining the current write position, and handling platform-specific byte ordering.
166{
167public:
168 //! \brief Constructor that creates an internal buffer
169 //! \param length Size of internal buffer to create (in bytes)
170 //! \note Care must be taken to allocate enough space
172
173 //! \brief Constructor that uses an existing buffer
174 //! \param buffer Pointer to the buffer to write to
175 //! \param length Length of the buffer in bytes
176 //! \note Care must be taken to ensure the buffer has enough space
178
179 //! \brief Virtual destructor
180 //!
181 //! Cleans up resources, including the buffer if it was created internally
183
184 //! \name Buffer Access
185 //! \brief Methods to access the internal buffer
186 //! @{
187
188 //! \brief Gets a pointer to the internal buffer
189 //! \return Pointer to the buffer
190 char* buffer();
191
192 //! \brief Gets the current length of data in the buffer
193 //! \return Current length in bytes
194 int length();
195 //@}
196
197 //! \name Value Writers
198 //! \brief Methods to write values to the byte stream
199 //!
200 //! These methods write a value and increment the current position in the byte stream
201 //! based on the size of the value written.
202 //! @{
203
204 //! \brief Writes a boolean value
205 //! \param b The boolean value to write
206 void writeBool(bool b);
207 //! \brief Writes a signed 8-bit integer
208 //! \param num The signed 8-bit integer to write
209 void writeInt8(char num);
210
211 //! \brief Writes an unsigned 8-bit integer
212 //! \param num The unsigned 8-bit integer to write
213 void writeUInt8(unsigned char num);
214
215 //! \brief Writes a signed 16-bit integer
216 //! \param num The signed 16-bit integer to write
217 void writeInt16(short num);
218
219 //! \brief Writes an unsigned 16-bit integer
220 //! \param num The unsigned 16-bit integer to write
221 void writeUInt16(unsigned short num);
222
223 //! \brief Writes a signed 32-bit integer
224 //! \param num The signed 32-bit integer to write
225 void writeInt32(int num);
226
227 //! \brief Writes an unsigned 32-bit integer
228 //! \param num The unsigned 32-bit integer to write
229 void writeUInt32(unsigned int num);
230
231 //! \brief Writes a signed 64-bit integer
232 //! \param num The signed 64-bit integer to write
233 void writeInt64(Int64 num);
234
235 //! \brief Writes an unsigned 64-bit integer
236 //! \param num The unsigned 64-bit integer to write
238
239 //! \brief Writes a 32-bit floating point value
240 //! \param num The 32-bit float to write
241 void writeSingle(float num);
242
243 //! \brief Writes a 64-bit floating point value
244 //! \param num The 64-bit double to write
245 void writeDouble(double num);
246 //! \brief Writes a string value
247 //! \param str The string to write
248 void writeString(const std::string& str);
249
250 //! \brief Writes a 3D vector
251 //! \param vec The vector to write
252 void writeVector(const DtVector& vec);
253
254 //! \brief Writes a Tait-Bryan angles representation
255 //! \param tb The Tait-Bryan angles to write
256 void writeTaitBryan(const DtTaitBryan& tb);
257
258 //! \brief Writes a quaternion
259 //! \param q The quaternion to write
260 void writeQuaternion(const DtQuaternion& q);
261
262 //! \brief Writes an entity identifier
263 //! \param id The entity identifier to write
264 void writeEntityId(const DtEntityIdentifier& id);
265
266 //! \brief Writes a simulation address
267 //! \param addr The simulation address to write
268 void writeSimulationAddress(const DtSimulationAddress& addr);
269
270 //! \brief Writes a UUID
271 //! \param uuid The UUID to write
272 void writeUUID(const DtUUID& uuid);
273
274 //! \brief Writes an entity type
275 //! \param type The entity type to write
276 void writeEntityType(const DtEntityType& type);
277
278 //! \brief Writes a byte array
279 //! \param bytes Pointer to the bytes to write
280 //! \param length Number of bytes to write
281 void writeBytes(char* bytes, int length);
282 //@}
283
284protected:
285 //! \brief Encodes the size of a string as a variable length integer
286 //! \param size The size to encode
287 void encodeStringSize(int size);
288
289 //! \brief Pointer to the buffer being written to
290 char* myBuffer;
291
292 //! \brief Current position in the buffer
294
295 //! \brief Total length of the buffer
297
298 //! \brief Flag indicating if the buffer should be deleted on destruction
300};
301
302
303} // namespace makVre
unsigned char readUInt8()
Reads an unsigned 8-bit integer.
virtual ~DtStreamReader()
Virtual destructor.
short readInt16()
Reads a signed 16-bit integer.
DtQuaternion readQuaternion()
Reads a quaternion.
float readSingle()
Reads a 32-bit floating point value.
int readInt32()
Reads a signed 32-bit integer.
std::string readString()
Reads a string value.
DtUUID readUUID()
Reads a UUID.
Int64 readInt64()
Reads a signed 64-bit integer.
DtStreamReader(char *buffer, int length, bool makeCopy=false)
Constructor.
DtEntityType readEntityType()
Reads an entity type.
DtEntityIdentifier readEntityId()
Reads an entity identifier.
UInt64 readUInt64()
Reads an unsigned 64-bit integer.
char * myReadHead
Current position in the buffer.
Definition byteStream.h:152
int myLength
Total length of the buffer.
Definition byteStream.h:155
DtVector readVector()
Reads a 3D vector.
unsigned int readUInt32()
Reads an unsigned 32-bit integer.
unsigned short readUInt16()
Reads an unsigned 16-bit integer.
int decodeStringSize()
Determines the size of a string from a variable length encoded integer.
char * readBytes(int length)
Reads a byte array of specified length.
DtSimulationAddress readSimulationAddress()
Reads a simulation address.
double readDouble()
Reads a 64-bit floating point value.
bool myShouldDelete
Flag indicating if the buffer should be deleted on destruction.
Definition byteStream.h:158
char readInt8()
Reads a signed 8-bit integer.
bool readBool()
Reads a boolean value.
DtTaitBryan readTaitBryan()
Reads a Tait-Bryan angles representation.
char * myBuffer
Pointer to the buffer being read from.
Definition byteStream.h:149
char * myWriteHead
Current position in the buffer.
Definition byteStream.h:293
void writeInt16(short num)
Writes a signed 16-bit integer.
char * myBuffer
Pointer to the buffer being written to.
Definition byteStream.h:290
bool myShouldDelete
Flag indicating if the buffer should be deleted on destruction.
Definition byteStream.h:299
void writeUInt16(unsigned short num)
Writes an unsigned 16-bit integer.
void encodeStringSize(int size)
Encodes the size of a string as a variable length integer.
char * buffer()
Gets a pointer to the internal buffer.
void writeSingle(float num)
Writes a 32-bit floating point value.
void writeVector(const DtVector &vec)
Writes a 3D vector.
void writeUInt32(unsigned int num)
Writes an unsigned 32-bit integer.
int myLength
Total length of the buffer.
Definition byteStream.h:296
void writeEntityId(const DtEntityIdentifier &id)
Writes an entity identifier.
virtual ~DtStreamWriter()
Virtual destructor.
DtStreamWriter(int length=0)
Constructor that creates an internal buffer.
void writeBool(bool b)
Writes a boolean value.
void writeInt8(char num)
Writes a signed 8-bit integer.
void writeUInt64(UInt64 num)
Writes an unsigned 64-bit integer.
void writeDouble(double num)
Writes a 64-bit floating point value.
int length()
Gets the current length of data in the buffer.
void writeTaitBryan(const DtTaitBryan &tb)
Writes a Tait-Bryan angles representation.
void writeInt32(int num)
Writes a signed 32-bit integer.
void writeEntityType(const DtEntityType &type)
Writes an entity type.
void writeInt64(Int64 num)
Writes a signed 64-bit integer.
void writeBytes(char *bytes, int length)
Writes a byte array.
void writeString(const std::string &str)
Writes a string value.
DtStreamWriter(char *buffer, int length)
Constructor that uses an existing buffer.
void writeUInt8(unsigned char num)
Writes an unsigned 8-bit integer.
void writeSimulationAddress(const DtSimulationAddress &addr)
Writes a simulation address.
void writeUUID(const DtUUID &uuid)
Writes a UUID.
void writeQuaternion(const DtQuaternion &q)
Writes a quaternion.
Defines export macros for the VREngage Utility library.
#define UTIL_DLL
Export/import macro for non-Windows platforms.
Definition export.h:39
!
Definition glsVreMessageUtil.h:50
unsigned long long UInt64
Unsigned 64-bit integer type.
Definition utilFunctions.h:42
int UTIL_DLL DtEncodedStringSize(const std::string &str)
Calculates the size of a string when encoded in the byte stream format.
long long Int64
Signed 64-bit integer type.
Definition utilFunctions.h:44
Provides common utility functions for the VREngage system.