StreamWriter.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2025, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file Defines the StreamWriter class which writes data to
  35. * a binary stream with a well-defined endianness. */
  36. #pragma once
  37. #ifndef AI_STREAMWRITER_H_INCLUDED
  38. #define AI_STREAMWRITER_H_INCLUDED
  39. #ifdef __GNUC__
  40. # pragma GCC system_header
  41. #endif
  42. #include <assimp/ByteSwapper.h>
  43. #include <assimp/IOStream.hpp>
  44. #include <memory>
  45. #include <vector>
  46. namespace Assimp {
  47. // --------------------------------------------------------------------------------------------
  48. /** Wrapper class around IOStream to allow for consistent writing of binary data in both
  49. * little and big endian format. Don't attempt to instance the template directly. Use
  50. * StreamWriterLE to write to a little-endian stream and StreamWriterBE to write to a
  51. * BE stream. Alternatively, there is StreamWriterAny if the endianness of the output
  52. * stream is to be determined at runtime.
  53. */
  54. // --------------------------------------------------------------------------------------------
  55. template <bool SwapEndianness = false, bool RuntimeSwitch = false>
  56. class StreamWriter {
  57. enum {
  58. INITIAL_CAPACITY = 1024
  59. };
  60. public:
  61. // ---------------------------------------------------------------------
  62. /** Construction from a given stream with a well-defined endianness.
  63. *
  64. * The StreamReader holds a permanent strong reference to the
  65. * stream, which is released upon destruction.
  66. * @param stream Input stream. The stream is not re-seeked and writing
  67. continues at the current position of the stream cursor.
  68. * @param le If @c RuntimeSwitch is true: specifies whether the
  69. * stream is in little endian byte order. Otherwise the
  70. * endianness information is defined by the @c SwapEndianness
  71. * template parameter and this parameter is meaningless. */
  72. StreamWriter(std::shared_ptr<IOStream> stream, bool le = false)
  73. : stream(stream)
  74. , le(le)
  75. , cursor()
  76. {
  77. ai_assert(stream);
  78. buffer.reserve(INITIAL_CAPACITY);
  79. }
  80. // ---------------------------------------------------------------------
  81. StreamWriter(IOStream* stream, bool le = false)
  82. : stream(std::shared_ptr<IOStream>(stream))
  83. , le(le)
  84. , cursor()
  85. {
  86. ai_assert(stream);
  87. buffer.reserve(INITIAL_CAPACITY);
  88. }
  89. // ---------------------------------------------------------------------
  90. ~StreamWriter() {
  91. stream->Write(buffer.data(), 1, buffer.size());
  92. stream->Flush();
  93. }
  94. public:
  95. // ---------------------------------------------------------------------
  96. /** Flush the contents of the internal buffer, and the output IOStream */
  97. void Flush()
  98. {
  99. stream->Write(buffer.data(), 1, buffer.size());
  100. stream->Flush();
  101. buffer.clear();
  102. cursor = 0;
  103. }
  104. // ---------------------------------------------------------------------
  105. /** Seek to the given offset / origin in the output IOStream.
  106. *
  107. * Flushes the internal buffer and the output IOStream prior to seeking. */
  108. aiReturn Seek(size_t pOffset, aiOrigin pOrigin=aiOrigin_SET)
  109. {
  110. Flush();
  111. return stream->Seek(pOffset, pOrigin);
  112. }
  113. // ---------------------------------------------------------------------
  114. /** Tell the current position in the output IOStream.
  115. *
  116. * First flushes the internal buffer and the output IOStream. */
  117. size_t Tell()
  118. {
  119. Flush();
  120. return stream->Tell();
  121. }
  122. public:
  123. // ---------------------------------------------------------------------
  124. /** Write a float to the stream */
  125. void PutF4(float f)
  126. {
  127. Put(f);
  128. }
  129. // ---------------------------------------------------------------------
  130. /** Write a double to the stream */
  131. void PutF8(double d) {
  132. Put(d);
  133. }
  134. // ---------------------------------------------------------------------
  135. /** Write a signed 16 bit integer to the stream */
  136. void PutI2(int16_t n) {
  137. Put(n);
  138. }
  139. // ---------------------------------------------------------------------
  140. /** Write a signed 8 bit integer to the stream */
  141. void PutI1(int8_t n) {
  142. Put(n);
  143. }
  144. // ---------------------------------------------------------------------
  145. /** Write an signed 32 bit integer to the stream */
  146. void PutI4(int32_t n) {
  147. Put(n);
  148. }
  149. // ---------------------------------------------------------------------
  150. /** Write a signed 64 bit integer to the stream */
  151. void PutI8(int64_t n) {
  152. Put(n);
  153. }
  154. // ---------------------------------------------------------------------
  155. /** Write a unsigned 16 bit integer to the stream */
  156. void PutU2(uint16_t n) {
  157. Put(n);
  158. }
  159. // ---------------------------------------------------------------------
  160. /** Write a unsigned 8 bit integer to the stream */
  161. void PutU1(uint8_t n) {
  162. Put(n);
  163. }
  164. // ---------------------------------------------------------------------
  165. /** Write an unsigned 32 bit integer to the stream */
  166. void PutU4(uint32_t n) {
  167. Put(n);
  168. }
  169. // ---------------------------------------------------------------------
  170. /** Write a unsigned 64 bit integer to the stream */
  171. void PutU8(uint64_t n) {
  172. Put(n);
  173. }
  174. // ---------------------------------------------------------------------
  175. /** Write a single character to the stream */
  176. void PutChar(char c) {
  177. Put(c);
  178. }
  179. // ---------------------------------------------------------------------
  180. /** Write an aiString to the stream */
  181. void PutString(const aiString& s)
  182. {
  183. // as Put(T f) below
  184. if (cursor + s.length >= buffer.size()) {
  185. buffer.resize(cursor + s.length);
  186. }
  187. void* dest = &buffer[cursor];
  188. ::memcpy(dest, s.C_Str(), s.length);
  189. cursor += s.length;
  190. }
  191. // ---------------------------------------------------------------------
  192. /** Write a std::string to the stream */
  193. void PutString(const std::string& s)
  194. {
  195. // as Put(T f) below
  196. if (cursor + s.size() >= buffer.size()) {
  197. buffer.resize(cursor + s.size());
  198. }
  199. void* dest = &buffer[cursor];
  200. ::memcpy(dest, s.c_str(), s.size());
  201. cursor += s.size();
  202. }
  203. public:
  204. // ---------------------------------------------------------------------
  205. /** overload operator<< and allow chaining of MM ops. */
  206. template <typename T>
  207. StreamWriter& operator << (T f) {
  208. Put(f);
  209. return *this;
  210. }
  211. // ---------------------------------------------------------------------
  212. std::size_t GetCurrentPos() const {
  213. return cursor;
  214. }
  215. // ---------------------------------------------------------------------
  216. void SetCurrentPos(std::size_t new_cursor) {
  217. cursor = new_cursor;
  218. }
  219. // ---------------------------------------------------------------------
  220. /** Generic write method. ByteSwap::Swap(T*) *must* be defined */
  221. template <typename T>
  222. void Put(T f) {
  223. Intern :: Getter<SwapEndianness,T,RuntimeSwitch>() (&f, le);
  224. if (cursor + sizeof(T) >= buffer.size()) {
  225. buffer.resize(cursor + sizeof(T));
  226. }
  227. void* dest = &buffer[cursor];
  228. // reinterpret_cast + assignment breaks strict aliasing rules
  229. // and generally causes trouble on platforms such as ARM that
  230. // do not silently ignore alignment faults.
  231. ::memcpy(dest, &f, sizeof(T));
  232. cursor += sizeof(T);
  233. }
  234. private:
  235. std::shared_ptr<IOStream> stream;
  236. bool le;
  237. std::vector<uint8_t> buffer;
  238. std::size_t cursor;
  239. };
  240. // --------------------------------------------------------------------------------------------
  241. // `static` StreamWriter. Their byte order is fixed and they might be a little bit faster.
  242. #ifdef AI_BUILD_BIG_ENDIAN
  243. typedef StreamWriter<true> StreamWriterLE;
  244. typedef StreamWriter<false> StreamWriterBE;
  245. #else
  246. typedef StreamWriter<true> StreamWriterBE;
  247. typedef StreamWriter<false> StreamWriterLE;
  248. #endif
  249. // `dynamic` StreamWriter. The byte order of the input data is specified in the
  250. // c'tor. This involves runtime branching and might be a little bit slower.
  251. typedef StreamWriter<true,true> StreamWriterAny;
  252. } // end namespace Assimp
  253. #endif // !! AI_STREAMWriter_H_INCLUDED