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