Path Tracer
StreamWriter.h
1 /*
2 ---------------------------------------------------------------------------
3 Open Asset Import Library (assimp)
4 ---------------------------------------------------------------------------
5 
6 Copyright (c) 2006-2020, assimp team
7 
8 
9 
10 All rights reserved.
11 
12 Redistribution and use of this software in source and binary forms,
13 with or without modification, are permitted provided that the following
14 conditions are met:
15 
16 * Redistributions of source code must retain the above
17  copyright notice, this list of conditions and the
18  following disclaimer.
19 
20 * Redistributions in binary form must reproduce the above
21  copyright notice, this list of conditions and the
22  following disclaimer in the documentation and/or other
23  materials provided with the distribution.
24 
25 * Neither the name of the assimp team, nor the names of its
26  contributors may be used to endorse or promote products
27  derived from this software without specific prior
28  written permission of the assimp team.
29 
30 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 ---------------------------------------------------------------------------
42 */
43 
46 #pragma once
47 #ifndef AI_STREAMWRITER_H_INCLUDED
48 #define AI_STREAMWRITER_H_INCLUDED
49 
50 #ifdef __GNUC__
51 # pragma GCC system_header
52 #endif
53 
54 #include <assimp/ByteSwapper.h>
55 #include <assimp/IOStream.hpp>
56 
57 #include <memory>
58 #include <vector>
59 
60 namespace Assimp {
61 
62 // --------------------------------------------------------------------------------------------
69 // --------------------------------------------------------------------------------------------
70 template <bool SwapEndianess = false, bool RuntimeSwitch = false>
72 {
73  enum {
74  INITIAL_CAPACITY = 1024
75  };
76 
77 public:
78 
79  // ---------------------------------------------------------------------
90  StreamWriter(std::shared_ptr<IOStream> stream, bool le = false)
91  : stream(stream)
92  , le(le)
93  , cursor()
94  {
95  ai_assert(stream);
96  buffer.reserve(INITIAL_CAPACITY);
97  }
98 
99  // ---------------------------------------------------------------------
100  StreamWriter(IOStream* stream, bool le = false)
101  : stream(std::shared_ptr<IOStream>(stream))
102  , le(le)
103  , cursor()
104  {
105  ai_assert(stream);
106  buffer.reserve(INITIAL_CAPACITY);
107  }
108 
109  // ---------------------------------------------------------------------
110  ~StreamWriter() {
111  stream->Write(buffer.data(), 1, buffer.size());
112  stream->Flush();
113  }
114 
115 public:
116 
117  // ---------------------------------------------------------------------
119  void Flush()
120  {
121  stream->Write(buffer.data(), 1, buffer.size());
122  stream->Flush();
123  buffer.clear();
124  cursor = 0;
125  }
126 
127  // ---------------------------------------------------------------------
131  aiReturn Seek(size_t pOffset, aiOrigin pOrigin=aiOrigin_SET)
132  {
133  Flush();
134  return stream->Seek(pOffset, pOrigin);
135  }
136 
137  // ---------------------------------------------------------------------
141  size_t Tell()
142  {
143  Flush();
144  return stream->Tell();
145  }
146 
147 public:
148 
149  // ---------------------------------------------------------------------
151  void PutF4(float f)
152  {
153  Put(f);
154  }
155 
156  // ---------------------------------------------------------------------
158  void PutF8(double d) {
159  Put(d);
160  }
161 
162  // ---------------------------------------------------------------------
164  void PutI2(int16_t n) {
165  Put(n);
166  }
167 
168  // ---------------------------------------------------------------------
170  void PutI1(int8_t n) {
171  Put(n);
172  }
173 
174  // ---------------------------------------------------------------------
176  void PutI4(int32_t n) {
177  Put(n);
178  }
179 
180  // ---------------------------------------------------------------------
182  void PutI8(int64_t n) {
183  Put(n);
184  }
185 
186  // ---------------------------------------------------------------------
188  void PutU2(uint16_t n) {
189  Put(n);
190  }
191 
192  // ---------------------------------------------------------------------
194  void PutU1(uint8_t n) {
195  Put(n);
196  }
197 
198  // ---------------------------------------------------------------------
200  void PutU4(uint32_t n) {
201  Put(n);
202  }
203 
204  // ---------------------------------------------------------------------
206  void PutU8(uint64_t n) {
207  Put(n);
208  }
209 
210  // ---------------------------------------------------------------------
212  void PutChar(char c) {
213  Put(c);
214  }
215 
216  // ---------------------------------------------------------------------
218  void PutString(const aiString& s)
219  {
220  // as Put(T f) below
221  if (cursor + s.length >= buffer.size()) {
222  buffer.resize(cursor + s.length);
223  }
224  void* dest = &buffer[cursor];
225  ::memcpy(dest, s.C_Str(), s.length);
226  cursor += s.length;
227  }
228 
229  // ---------------------------------------------------------------------
231  void PutString(const std::string& s)
232  {
233  // as Put(T f) below
234  if (cursor + s.size() >= buffer.size()) {
235  buffer.resize(cursor + s.size());
236  }
237  void* dest = &buffer[cursor];
238  ::memcpy(dest, s.c_str(), s.size());
239  cursor += s.size();
240  }
241 
242 public:
243 
244  // ---------------------------------------------------------------------
246  template <typename T>
248  Put(f);
249  return *this;
250  }
251 
252  // ---------------------------------------------------------------------
253  std::size_t GetCurrentPos() const {
254  return cursor;
255  }
256 
257  // ---------------------------------------------------------------------
258  void SetCurrentPos(std::size_t new_cursor) {
259  cursor = new_cursor;
260  }
261 
262  // ---------------------------------------------------------------------
264  template <typename T>
265  void Put(T f) {
266  Intern :: Getter<SwapEndianess,T,RuntimeSwitch>() (&f, le);
267 
268  if (cursor + sizeof(T) >= buffer.size()) {
269  buffer.resize(cursor + sizeof(T));
270  }
271 
272  void* dest = &buffer[cursor];
273 
274  // reinterpret_cast + assignment breaks strict aliasing rules
275  // and generally causes trouble on platforms such as ARM that
276  // do not silently ignore alignment faults.
277  ::memcpy(dest, &f, sizeof(T));
278  cursor += sizeof(T);
279  }
280 
281 private:
282 
283  std::shared_ptr<IOStream> stream;
284  bool le;
285 
286  std::vector<uint8_t> buffer;
287  std::size_t cursor;
288 };
289 
290 
291 // --------------------------------------------------------------------------------------------
292 // `static` StreamWriter. Their byte order is fixed and they might be a little bit faster.
293 #ifdef AI_BUILD_BIG_ENDIAN
294  typedef StreamWriter<true> StreamWriterLE;
295  typedef StreamWriter<false> StreamWriterBE;
296 #else
297  typedef StreamWriter<true> StreamWriterBE;
298  typedef StreamWriter<false> StreamWriterLE;
299 #endif
300 
301 // `dynamic` StreamWriter. The byte order of the input data is specified in the
302 // c'tor. This involves runtime branching and might be a little bit slower.
303 typedef StreamWriter<true,true> StreamWriterAny;
304 
305 } // end namespace Assimp
306 
307 #endif // !! AI_STREAMWriter_H_INCLUDED
Assimp::StreamWriter::PutI4
void PutI4(int32_t n)
Definition: StreamWriter.h:176
Assimp::IOStream::Seek
virtual aiReturn Seek(size_t pOffset, aiOrigin pOrigin)=0
Set the read/write cursor of the file.
aiOrigin
aiOrigin
Definition: types.h:426
Assimp::StreamWriter::PutU4
void PutU4(uint32_t n)
Definition: StreamWriter.h:200
aiOrigin_SET
@ aiOrigin_SET
Definition: types.h:428
Assimp::StreamWriter::PutU1
void PutU1(uint8_t n)
Definition: StreamWriter.h:194
Assimp::StreamWriter::PutF4
void PutF4(float f)
Definition: StreamWriter.h:151
Assimp::StreamWriter::StreamWriter
StreamWriter(std::shared_ptr< IOStream > stream, bool le=false)
Definition: StreamWriter.h:90
Assimp::IOStream::Write
virtual size_t Write(const void *pvBuffer, size_t pSize, size_t pCount)=0
Write to the file.
aiReturn
aiReturn
Definition: types.h:397
aiString::length
ai_uint32 length
Definition: types.h:387
Assimp::StreamWriter::PutString
void PutString(const aiString &s)
Definition: StreamWriter.h:218
Assimp::StreamWriter::PutI1
void PutI1(int8_t n)
Definition: StreamWriter.h:170
Assimp::StreamWriter::Tell
size_t Tell()
Definition: StreamWriter.h:141
Assimp::StreamWriter::Put
void Put(T f)
Definition: StreamWriter.h:265
Assimp::StreamWriter::PutI8
void PutI8(int64_t n)
Definition: StreamWriter.h:182
Assimp::StreamWriter::PutU8
void PutU8(uint64_t n)
Definition: StreamWriter.h:206
Assimp::StreamWriter::PutU2
void PutU2(uint16_t n)
Definition: StreamWriter.h:188
Assimp::IOStream::Flush
virtual void Flush()=0
Flush the contents of the file buffer (for writers) See fflush() for more details.
aiString
Definition: types.h:266
Assimp::StreamWriter::Seek
aiReturn Seek(size_t pOffset, aiOrigin pOrigin=aiOrigin_SET)
Definition: StreamWriter.h:131
Assimp::StreamWriter::operator<<
StreamWriter & operator<<(T f)
Definition: StreamWriter.h:247
Assimp::StreamWriter::PutF8
void PutF8(double d)
Definition: StreamWriter.h:158
Assimp::StreamWriter::PutChar
void PutChar(char c)
Definition: StreamWriter.h:212
IOStream.hpp
File I/O wrappers for C++.
Assimp::StreamWriter::PutI2
void PutI2(int16_t n)
Definition: StreamWriter.h:164
Assimp
Definition: ai_assert.h:50
Assimp::IOStream::Tell
virtual size_t Tell() const =0
Get the current position of the read/write cursor.
Assimp::IOStream
CPP-API: Class to handle file I/O for C++.
Definition: IOStream.hpp:75
Assimp::StreamWriter
Definition: StreamWriter.h:72
Assimp::StreamWriter::Flush
void Flush()
Definition: StreamWriter.h:119
Assimp::StreamWriter::PutString
void PutString(const std::string &s)
Definition: StreamWriter.h:231