AGX Dynamics 2.42.1.1
Loading...
Searching...
No Matches
ByteStream.h
Go to the documentation of this file.
1/*
2Copyright 2007-2025. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx Simulation AB.
15*/
16
17#ifndef AGXDATA_BYTESTREAM_H
18#define AGXDATA_BYTESTREAM_H
19
20#include <agxData/agxData.h>
21
22namespace agxData
23{
28 {
29 public:
30 ByteStream();
31 ByteStream(BytePtr begin, BytePtr end);
32 ByteStream(BytePtr begin, BytePtr end, BytePtr head);
33
37 BytePtr& begin();
38 const BytePtr& begin() const;
39
43 BytePtr& end();
44 const BytePtr& end() const;
45
49 BytePtr& head();
50 const BytePtr& head() const;
51
55 size_t size() const;
56
60 size_t getCurrentOffset() const;
61
65 template <typename T>
66 T topElement() const;
67
71 template <typename T>
72 T popElement();
73
74
78 BytePtr popBytes(size_t numBytes);
79
83 template <typename T>
84 void consume(const T& value);
85
89 template <typename T>
90 void write(const T& value);
91
95 void write(const Byte *buffer, size_t numBytes);
96 void write(const char *buffer, size_t numBytes);
97
101 void read(Byte *buffer, size_t numBytes);
102 void read(char *buffer, size_t numBytes);
103
104
105 ByteStream operator+ (size_t offset) const;
106 ByteStream& operator+= (size_t offset);
107
108 private:
109 BytePtr m_begin;
110 BytePtr m_end;
111 mutable BytePtr m_head;
112 };
113
114
115
116 /* Implementation */
117 AGX_FORCE_INLINE ByteStream::ByteStream() : m_begin(nullptr), m_end(nullptr), m_head(nullptr)
118 {
119 }
120
121 AGX_FORCE_INLINE ByteStream::ByteStream(BytePtr begin, BytePtr end) : m_begin(begin), m_end(end), m_head(begin)
122 {
123 }
124
125 AGX_FORCE_INLINE ByteStream::ByteStream(BytePtr begin, BytePtr end, BytePtr head) : m_begin(begin), m_end(end), m_head(head)
126 {
127 agxAssert(head >= begin && head <= end);
128 }
129
130
132 AGX_FORCE_INLINE const BytePtr& ByteStream::begin() const { return m_begin; }
133
135 AGX_FORCE_INLINE const BytePtr& ByteStream::end() const { return m_end; }
136
138 AGX_FORCE_INLINE const BytePtr& ByteStream::head() const { return m_head; }
139
140 AGX_FORCE_INLINE size_t ByteStream::size() const { return (size_t)(m_end - m_begin); }
141 AGX_FORCE_INLINE size_t ByteStream::getCurrentOffset() const { return (size_t)(m_head - m_begin); }
142
143 template <typename T>
144 inline T ByteStream::topElement() const
145 {
146 agxVerify((size_t)(m_end - m_head) >= sizeof(T));
147 T element;
148 ::memcpy((void *)&element, (const void *)m_head, sizeof(T));
149 return element;
150 }
151
152 template <typename T>
154 {
155 T element = this->topElement<T>();
156 m_head += sizeof(T);
157 return element;
158 }
159
160
161 template <typename T>
163 {
164 T element = this->popElement<T>();
165 agxVerify(element == value);
166 }
167
168 template <typename T>
170 {
171 agxVerify((size_t)(m_end - m_head) >= sizeof(T));
172 ::memcpy((void *)m_head, (const void *)&value, sizeof(T));
173 m_head += sizeof(T);
174 }
175
177 {
178 agxVerify((size_t)(m_end-m_head) >= numBytes);
179 BytePtr ptr = m_head;
180 m_head += numBytes;
181 return ptr;
182 }
183
184
185 AGX_FORCE_INLINE void ByteStream::write(const Byte *buffer, size_t numBytes)
186 {
187 agxVerify((size_t)(m_end-m_head) >= numBytes);
188 ::memcpy((void *)m_head, (const void *)buffer, numBytes);
189 m_head += numBytes;
190 }
191
192 AGX_FORCE_INLINE void ByteStream::write(const char *buffer, size_t numBytes)
193 {
194 this->write((const Byte *)buffer, numBytes);
195 }
196
197 AGX_FORCE_INLINE void ByteStream::read(Byte *buffer, size_t numBytes)
198 {
199 agxVerify((size_t)(m_end-m_head) >= numBytes);
200 ::memcpy((void *)buffer, (const void *)m_head, numBytes);
201 m_head += numBytes;
202 }
203
204 AGX_FORCE_INLINE void ByteStream::read(char *buffer, size_t numBytes)
205 {
206 this->read((Byte *)buffer, numBytes);
207 }
208
209
210
211 AGX_FORCE_INLINE ByteStream ByteStream::operator+ (size_t offset) const { return ByteStream(m_begin, m_end, m_head + offset); }
212 AGX_FORCE_INLINE ByteStream& ByteStream::operator+= (size_t offset) { m_head += offset; return *this; }
213}
214
215
216#endif /* AGXDATA_BYTESTREAM_H */
AGXCORE_EXPORT agx::String operator+(const std::string &str, const agx::Name &name)
#define AGXCORE_EXPORT
Utility class for parsing/writing a byte stream.
Definition: ByteStream.h:28
void read(Byte *buffer, size_t numBytes)
Read a buffer.
Definition: ByteStream.h:197
T topElement() const
Definition: ByteStream.h:144
void consume(const T &value)
Consume an expected value from the stream.
Definition: ByteStream.h:162
size_t getCurrentOffset() const
Definition: ByteStream.h:141
BytePtr & head()
Definition: ByteStream.h:137
size_t size() const
Definition: ByteStream.h:140
BytePtr popBytes(size_t numBytes)
Definition: ByteStream.h:176
BytePtr & begin()
Definition: ByteStream.h:131
ByteStream operator+(size_t offset) const
Definition: ByteStream.h:211
void write(const T &value)
Write a value to the head of the stream and advance the head.
Definition: ByteStream.h:169
BytePtr & end()
Definition: ByteStream.h:134
ByteStream & operator+=(size_t offset)
Definition: ByteStream.h:212
#define agxVerify(expr)
Definition: debug.h:131
#define agxAssert(expr)
Definition: debug.h:143
#define AGX_FORCE_INLINE
Definition: macros.h:58
Contains classes for low level data storage for AGX.
Definition: Container.h:23
Byte * BytePtr
Definition: agxData.h:46
agx::UInt8 Byte
Definition: agxData.h:45