StreamWriter.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2012, 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 endianess. */
  36. #ifndef AI_STREAMWRITER_H_INCLUDED
  37. #define AI_STREAMWRITER_H_INCLUDED
  38. #include "ByteSwap.h"
  39. namespace Assimp {
  40. // --------------------------------------------------------------------------------------------
  41. /** Wrapper class around IOStream to allow for consistent writing of binary data in both
  42. * little and big endian format. Don't attempt to instance the template directly. Use
  43. * StreamWriterLE to read from a little-endian stream and StreamWriterBE to read from a
  44. * BE stream. Alternatively, there is StreamWriterAny if the endianess of the output
  45. * stream is to be determined at runtime.
  46. */
  47. // --------------------------------------------------------------------------------------------
  48. template <bool SwapEndianess = false, bool RuntimeSwitch = false>
  49. class StreamWriter
  50. {
  51. enum {
  52. INITIAL_CAPACITY = 1024
  53. };
  54. public:
  55. // ---------------------------------------------------------------------
  56. /** Construction from a given stream with a well-defined endianess.
  57. *
  58. * The StreamReader holds a permanent strong reference to the
  59. * stream, which is released upon destruction.
  60. * @param stream Input stream. The stream is not re-seeked and writing
  61. continues at the current position of the stream cursor.
  62. * @param le If @c RuntimeSwitch is true: specifies whether the
  63. * stream is in little endian byte order. Otherwise the
  64. * endianess information is defined by the @c SwapEndianess
  65. * template parameter and this parameter is meaningless. */
  66. StreamWriter(boost::shared_ptr<IOStream> stream, bool le = false)
  67. : stream(stream)
  68. , le(le)
  69. , cursor()
  70. {
  71. ai_assert(stream);
  72. buffer.reserve(INITIAL_CAPACITY);
  73. }
  74. // ---------------------------------------------------------------------
  75. StreamWriter(IOStream* stream, bool le = false)
  76. : stream(boost::shared_ptr<IOStream>(stream))
  77. , le(le)
  78. , cursor()
  79. {
  80. ai_assert(stream);
  81. buffer.reserve(INITIAL_CAPACITY);
  82. }
  83. // ---------------------------------------------------------------------
  84. ~StreamWriter() {
  85. stream->Write(&buffer[0], 1, buffer.size());
  86. stream->Flush();
  87. }
  88. public:
  89. // ---------------------------------------------------------------------
  90. /** Write a float to the stream */
  91. void PutF4(float f)
  92. {
  93. Put(f);
  94. }
  95. // ---------------------------------------------------------------------
  96. /** Write a double to the stream */
  97. void PutF8(double d) {
  98. Put(d);
  99. }
  100. // ---------------------------------------------------------------------
  101. /** Write a signed 16 bit integer to the stream */
  102. void PutI2(int16_t n) {
  103. Put(n);
  104. }
  105. // ---------------------------------------------------------------------
  106. /** Write a signed 8 bit integer to the stream */
  107. void PutI1(int8_t n) {
  108. Put(n);
  109. }
  110. // ---------------------------------------------------------------------
  111. /** Write an signed 32 bit integer to the stream */
  112. void PutI4(int32_t n) {
  113. Put(n);
  114. }
  115. // ---------------------------------------------------------------------
  116. /** Write a signed 64 bit integer to the stream */
  117. void PutI8(int64_t n) {
  118. Put(n);
  119. }
  120. // ---------------------------------------------------------------------
  121. /** Write a unsigned 16 bit integer to the stream */
  122. void PutU2(uint16_t n) {
  123. Put(n);
  124. }
  125. // ---------------------------------------------------------------------
  126. /** Write a unsigned 8 bit integer to the stream */
  127. void PutU1(uint8_t n) {
  128. Put(n);
  129. }
  130. // ---------------------------------------------------------------------
  131. /** Write an unsigned 32 bit integer to the stream */
  132. void PutU4(uint32_t n) {
  133. Put(n);
  134. }
  135. // ---------------------------------------------------------------------
  136. /** Write a unsigned 64 bit integer to the stream */
  137. void PutU8(uint64_t n) {
  138. Put(n);
  139. }
  140. public:
  141. // ---------------------------------------------------------------------
  142. /** overload operator<< and allow chaining of MM ops. */
  143. template <typename T>
  144. StreamWriter& operator << (T f) {
  145. Put(f);
  146. return *this;
  147. }
  148. // ---------------------------------------------------------------------
  149. std::size_t GetCurrentPos() const {
  150. return cursor;
  151. }
  152. // ---------------------------------------------------------------------
  153. void SetCurrentPos(std::size_t new_cursor) {
  154. cursor = new_cursor;
  155. }
  156. private:
  157. // ---------------------------------------------------------------------
  158. /** Generic write method. ByteSwap::Swap(T*) *must* be defined */
  159. template <typename T>
  160. void Put(T f) {
  161. Intern :: Getter<SwapEndianess,T,RuntimeSwitch>() (&f, le);
  162. if (cursor + sizeof(T) >= buffer.size()) {
  163. buffer.resize(cursor + sizeof(T));
  164. }
  165. void* dest = &buffer[cursor];
  166. // reinterpret_cast + assignment breaks strict aliasing rules
  167. // and generally causes trouble on platforms such as ARM that
  168. // do not silently ignore alignment faults.
  169. ::memcpy(dest, &f, sizeof(T));
  170. cursor += sizeof(T);
  171. }
  172. private:
  173. boost::shared_ptr<IOStream> stream;
  174. bool le;
  175. std::vector<uint8_t> buffer;
  176. std::size_t cursor;
  177. };
  178. // --------------------------------------------------------------------------------------------
  179. // `static` StreamWriter. Their byte order is fixed and they might be a little bit faster.
  180. #ifdef AI_BUILD_BIG_ENDIAN
  181. typedef StreamWriter<true> StreamWriterLE;
  182. typedef StreamWriter<false> StreamWriterBE;
  183. #else
  184. typedef StreamWriter<true> StreamWriterBE;
  185. typedef StreamWriter<false> StreamWriterLE;
  186. #endif
  187. // `dynamic` StreamWriter. The byte order of the input data is specified in the
  188. // c'tor. This involves runtime branching and might be a little bit slower.
  189. typedef StreamWriter<true,true> StreamWriterAny;
  190. } // end namespace Assimp
  191. #endif // !! AI_STREAMWriter_H_INCLUDED