io.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /// @ref gtx_io
  2. /// @file glm/gtx/io.hpp
  3. /// @author Jan P Springer (regnirpsj@gmail.com)
  4. ///
  5. /// @see core (dependence)
  6. /// @see gtc_matrix_access (dependence)
  7. /// @see gtc_quaternion (dependence)
  8. ///
  9. /// @defgroup gtx_io GLM_GTX_io
  10. /// @ingroup gtx
  11. ///
  12. /// Include <glm/gtx/io.hpp> to use the features of this extension.
  13. ///
  14. /// std::[w]ostream support for glm types
  15. ///
  16. /// std::[w]ostream support for glm types + qualifier/width/etc. manipulators
  17. /// based on howard hinnant's std::chrono io proposal
  18. /// [http://home.roadrunner.com/~hinnant/bloomington/chrono_io.html]
  19. #pragma once
  20. // Dependency:
  21. #include "../glm.hpp"
  22. #include "../gtx/quaternion.hpp"
  23. #ifndef GLM_ENABLE_EXPERIMENTAL
  24. # error "GLM: GLM_GTX_io is an experimental extension and may change in the future. Use #define GLM_ENABLE_EXPERIMENTAL before including it, if you really want to use it."
  25. #elif GLM_MESSAGES == GLM_ENABLE && !defined(GLM_EXT_INCLUDED)
  26. # pragma message("GLM: GLM_GTX_io extension included")
  27. #endif
  28. #if GLM_COMPILER & GLM_COMPILER_CLANG
  29. # pragma clang diagnostic push
  30. # pragma clang diagnostic ignored "-Wpadded"
  31. # pragma clang diagnostic ignored "-Wshorten-64-to-32"
  32. # pragma clang diagnostic ignored "-Wglobal-constructors"
  33. #endif
  34. #include <iosfwd> // std::basic_ostream<> (fwd)
  35. #include <locale> // std::locale, std::locale::facet, std::locale::id
  36. #include <utility> // std::pair<>
  37. namespace glm
  38. {
  39. /// @addtogroup gtx_io
  40. /// @{
  41. namespace io
  42. {
  43. enum order_type { column_major, row_major};
  44. template<typename CTy>
  45. class format_punct : public std::locale::facet
  46. {
  47. typedef CTy char_type;
  48. public:
  49. static std::locale::id id;
  50. bool formatted;
  51. unsigned precision;
  52. unsigned width;
  53. char_type separator;
  54. char_type delim_left;
  55. char_type delim_right;
  56. char_type space;
  57. char_type newline;
  58. order_type order;
  59. GLM_FUNC_DISCARD_DECL explicit format_punct(size_t a = 0);
  60. GLM_FUNC_DISCARD_DECL explicit format_punct(format_punct const&);
  61. };
  62. template<typename CTy, typename CTr = std::char_traits<CTy> >
  63. class basic_state_saver {
  64. public:
  65. GLM_FUNC_DISCARD_DECL explicit basic_state_saver(std::basic_ios<CTy,CTr>&);
  66. GLM_FUNC_DISCARD_DECL ~basic_state_saver();
  67. private:
  68. typedef ::std::basic_ios<CTy,CTr> state_type;
  69. typedef typename state_type::char_type char_type;
  70. typedef ::std::ios_base::fmtflags flags_type;
  71. typedef ::std::streamsize streamsize_type;
  72. typedef ::std::locale const locale_type;
  73. state_type& state_;
  74. flags_type flags_;
  75. streamsize_type precision_;
  76. streamsize_type width_;
  77. char_type fill_;
  78. locale_type locale_;
  79. GLM_FUNC_DECL basic_state_saver& operator=(basic_state_saver const&);
  80. };
  81. typedef basic_state_saver<char> state_saver;
  82. typedef basic_state_saver<wchar_t> wstate_saver;
  83. template<typename CTy, typename CTr = std::char_traits<CTy> >
  84. class basic_format_saver
  85. {
  86. public:
  87. GLM_FUNC_DISCARD_DECL explicit basic_format_saver(std::basic_ios<CTy,CTr>&);
  88. GLM_FUNC_DISCARD_DECL ~basic_format_saver();
  89. private:
  90. basic_state_saver<CTy> const bss_;
  91. GLM_FUNC_DECL basic_format_saver& operator=(basic_format_saver const&);
  92. };
  93. typedef basic_format_saver<char> format_saver;
  94. typedef basic_format_saver<wchar_t> wformat_saver;
  95. struct precision
  96. {
  97. unsigned value;
  98. GLM_FUNC_DISCARD_DECL explicit precision(unsigned);
  99. };
  100. struct width
  101. {
  102. unsigned value;
  103. GLM_FUNC_DISCARD_DECL explicit width(unsigned);
  104. };
  105. template<typename CTy>
  106. struct delimeter
  107. {
  108. CTy value[3];
  109. GLM_FUNC_DISCARD_DECL explicit delimeter(CTy /* left */, CTy /* right */, CTy /* separator */ = ',');
  110. };
  111. struct order
  112. {
  113. order_type value;
  114. GLM_FUNC_DISCARD_DECL explicit order(order_type);
  115. };
  116. // functions, inlined (inline)
  117. template<typename FTy, typename CTy, typename CTr>
  118. FTy const& get_facet(std::basic_ios<CTy,CTr>&);
  119. template<typename FTy, typename CTy, typename CTr>
  120. std::basic_ios<CTy,CTr>& formatted(std::basic_ios<CTy,CTr>&);
  121. template<typename FTy, typename CTy, typename CTr>
  122. std::basic_ios<CTy,CTr>& unformatted(std::basic_ios<CTy,CTr>&);
  123. template<typename CTy, typename CTr>
  124. std::basic_ostream<CTy, CTr>& operator<<(std::basic_ostream<CTy, CTr>&, precision const&);
  125. template<typename CTy, typename CTr>
  126. std::basic_ostream<CTy, CTr>& operator<<(std::basic_ostream<CTy, CTr>&, width const&);
  127. template<typename CTy, typename CTr>
  128. std::basic_ostream<CTy, CTr>& operator<<(std::basic_ostream<CTy, CTr>&, delimeter<CTy> const&);
  129. template<typename CTy, typename CTr>
  130. std::basic_ostream<CTy, CTr>& operator<<(std::basic_ostream<CTy, CTr>&, order const&);
  131. }//namespace io
  132. template<typename CTy, typename CTr, typename T, qualifier Q>
  133. GLM_FUNC_DISCARD_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, qua<T, Q> const&);
  134. template<typename CTy, typename CTr, typename T, qualifier Q>
  135. GLM_FUNC_DISCARD_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, vec<1, T, Q> const&);
  136. template<typename CTy, typename CTr, typename T, qualifier Q>
  137. GLM_FUNC_DISCARD_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, vec<2, T, Q> const&);
  138. template<typename CTy, typename CTr, typename T, qualifier Q>
  139. GLM_FUNC_DISCARD_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, vec<3, T, Q> const&);
  140. template<typename CTy, typename CTr, typename T, qualifier Q>
  141. GLM_FUNC_DISCARD_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, vec<4, T, Q> const&);
  142. template<typename CTy, typename CTr, typename T, qualifier Q>
  143. GLM_FUNC_DISCARD_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, mat<2, 2, T, Q> const&);
  144. template<typename CTy, typename CTr, typename T, qualifier Q>
  145. GLM_FUNC_DISCARD_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, mat<2, 3, T, Q> const&);
  146. template<typename CTy, typename CTr, typename T, qualifier Q>
  147. GLM_FUNC_DISCARD_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, mat<2, 4, T, Q> const&);
  148. template<typename CTy, typename CTr, typename T, qualifier Q>
  149. GLM_FUNC_DISCARD_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, mat<3, 2, T, Q> const&);
  150. template<typename CTy, typename CTr, typename T, qualifier Q>
  151. GLM_FUNC_DISCARD_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, mat<3, 3, T, Q> const&);
  152. template<typename CTy, typename CTr, typename T, qualifier Q>
  153. GLM_FUNC_DISCARD_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, mat<3, 4, T, Q> const&);
  154. template<typename CTy, typename CTr, typename T, qualifier Q>
  155. GLM_FUNC_DISCARD_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, mat<4, 2, T, Q> const&);
  156. template<typename CTy, typename CTr, typename T, qualifier Q>
  157. GLM_FUNC_DISCARD_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, mat<4, 3, T, Q> const&);
  158. template<typename CTy, typename CTr, typename T, qualifier Q>
  159. GLM_FUNC_DISCARD_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, mat<4, 4, T, Q> const&);
  160. template<typename CTy, typename CTr, typename T, qualifier Q>
  161. GLM_FUNC_DISCARD_DECL std::basic_ostream<CTy,CTr> & operator<<(std::basic_ostream<CTy,CTr> &,
  162. std::pair<mat<4, 4, T, Q> const, mat<4, 4, T, Q> const> const&);
  163. /// @}
  164. }//namespace glm
  165. #if GLM_COMPILER & GLM_COMPILER_CLANG
  166. # pragma clang diagnostic pop
  167. #endif
  168. #include "io.inl"