AGX Dynamics  2.37.3.2
ByteStream.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #ifndef AGXDATA_BYTESTREAM_H
18 #define AGXDATA_BYTESTREAM_H
19 
20 #include <agxData/agxData.h>
21 
22 namespace agxData
23 {
28  {
29  public:
30  ByteStream();
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>
169  AGX_FORCE_INLINE void ByteStream::write(const T& value)
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 */
bool AGXPHYSICS_EXPORT read(const std::string &filename, agxStream::SerializablePtrVector &readObjects)
Open and read serializable objects from a file on disk with previously stored objects.
size_t getCurrentOffset() const
Definition: ByteStream.h:141
void consume(const T &value)
Consume an expected value from the stream.
Definition: ByteStream.h:162
T topElement() const
Definition: ByteStream.h:144
#define agxVerify(expr)
Definition: debug.h:131
#define AGX_FORCE_INLINE
Definition: Object.h:59
AgXString< T > operator+(const Indentation &indentation, const AgXString< T > &str)
Definition: Indentation.h:66
void read(Byte *buffer, size_t numBytes)
Read a buffer.
Definition: ByteStream.h:197
Contains classes for low level data storage for AGX.
Definition: Container.h:23
agx::UInt8 Byte
Definition: agxData.h:45
bool AGXPHYSICS_EXPORT write(const std::string &filename, const agxStream::Serializable *data)
Write a Serializable object to a file.
LinearProbingHashSetImplementation< KeyT, HashT >::iterator begin(LinearProbingHashSetImplementation< KeyT, HashT > &set)
#define agxAssert(expr)
Definition: debug.h:143
ByteStream operator+(size_t offset) const
Definition: ByteStream.h:211
BytePtr & end()
Definition: ByteStream.h:134
void write(const T &value)
Write a value to the head of the stream and advance the head.
Definition: ByteStream.h:169
BytePtr popBytes(size_t numBytes)
Definition: ByteStream.h:176
Byte * BytePtr
Definition: agxData.h:46
BytePtr & begin()
Definition: ByteStream.h:131
LinearProbingHashSetImplementation< KeyT, HashT >::iterator end(LinearProbingHashSetImplementation< KeyT, HashT > &set)
ByteStream & operator+=(size_t offset)
Definition: ByteStream.h:212
Utility class for parsing/writing a byte stream.
Definition: ByteStream.h:27
BytePtr & head()
Definition: ByteStream.h:137
size_t size() const
Definition: ByteStream.h:140
#define AGXCORE_EXPORT
AGXCABLE_EXPORT SegmentDamage & operator+=(SegmentDamage &lhs, const SegmentDamage &rhs)