buffers.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. // boost/endian/buffers.hpp ----------------------------------------------------------//
  2. // (C) Copyright Darin Adler 2000
  3. // (C) Copyright Beman Dawes 2006, 2009, 2014
  4. // (C) Copyright Peter Dimov 2019
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // See http://www.boost.org/LICENSE_1_0.txt
  7. // See library home page at http://www.boost.org/libs/endian
  8. //--------------------------------------------------------------------------------------//
  9. // Original design developed by Darin Adler based on classes developed by Mark
  10. // Borgerding. Four original class templates were combined into a single endian
  11. // class template by Beman Dawes, who also added the unrolled_byte_loops sign
  12. // partial specialization to correctly extend the sign when cover integer size
  13. // differs from endian representation size.
  14. // TODO: When a compiler supporting constexpr becomes available, try possible uses.
  15. #ifndef BOOST_ENDIAN_BUFFERS_HPP
  16. #define BOOST_ENDIAN_BUFFERS_HPP
  17. #if defined(_MSC_VER)
  18. # pragma warning(push)
  19. # pragma warning(disable: 4127) // conditional expression is constant
  20. #endif
  21. #include <boost/endian/detail/endian_store.hpp>
  22. #include <boost/endian/detail/endian_load.hpp>
  23. #include <boost/endian/detail/static_assert.hpp>
  24. #include <boost/config.hpp>
  25. #include <cstdint>
  26. #include <iosfwd>
  27. #include <climits>
  28. #include <cstring>
  29. #if defined(BOOST_BORLANDC) || defined(BOOST_CODEGEARC)
  30. # pragma pack(push, 1)
  31. #endif
  32. # if CHAR_BIT != 8
  33. # error Platforms with CHAR_BIT != 8 are not supported
  34. # endif
  35. //---------------------------------- synopsis ----------------------------------------//
  36. namespace boost
  37. {
  38. namespace endian
  39. {
  40. enum class align
  41. {no, yes
  42. # ifdef BOOST_ENDIAN_DEPRECATED_NAMES
  43. , unaligned = no, aligned = yes
  44. # endif
  45. };
  46. template <order Order, class T, std::size_t n_bits,
  47. align A = align::no>
  48. class endian_buffer;
  49. // aligned big endian signed integer buffers
  50. typedef endian_buffer<order::big, std::int8_t, 8, align::yes> big_int8_buf_at;
  51. typedef endian_buffer<order::big, std::int16_t, 16, align::yes> big_int16_buf_at;
  52. typedef endian_buffer<order::big, std::int32_t, 32, align::yes> big_int32_buf_at;
  53. typedef endian_buffer<order::big, std::int64_t, 64, align::yes> big_int64_buf_at;
  54. // aligned big endian unsigned integer buffers
  55. typedef endian_buffer<order::big, std::uint8_t, 8, align::yes> big_uint8_buf_at;
  56. typedef endian_buffer<order::big, std::uint16_t, 16, align::yes> big_uint16_buf_at;
  57. typedef endian_buffer<order::big, std::uint32_t, 32, align::yes> big_uint32_buf_at;
  58. typedef endian_buffer<order::big, std::uint64_t, 64, align::yes> big_uint64_buf_at;
  59. // aligned little endian signed integer buffers
  60. typedef endian_buffer<order::little, std::int8_t, 8, align::yes> little_int8_buf_at;
  61. typedef endian_buffer<order::little, std::int16_t, 16, align::yes> little_int16_buf_at;
  62. typedef endian_buffer<order::little, std::int32_t, 32, align::yes> little_int32_buf_at;
  63. typedef endian_buffer<order::little, std::int64_t, 64, align::yes> little_int64_buf_at;
  64. // aligned little endian unsigned integer buffers
  65. typedef endian_buffer<order::little, std::uint8_t, 8, align::yes> little_uint8_buf_at;
  66. typedef endian_buffer<order::little, std::uint16_t, 16, align::yes> little_uint16_buf_at;
  67. typedef endian_buffer<order::little, std::uint32_t, 32, align::yes> little_uint32_buf_at;
  68. typedef endian_buffer<order::little, std::uint64_t, 64, align::yes> little_uint64_buf_at;
  69. // aligned floating point buffers
  70. typedef endian_buffer<order::big, float, 32, align::yes> big_float32_buf_at;
  71. typedef endian_buffer<order::big, double, 64, align::yes> big_float64_buf_at;
  72. typedef endian_buffer<order::little, float, 32, align::yes> little_float32_buf_at;
  73. typedef endian_buffer<order::little, double, 64, align::yes> little_float64_buf_at;
  74. // aligned native endian typedefs are not provided because
  75. // <cstdint> types are superior for this use case
  76. // unaligned big endian signed integer buffers
  77. typedef endian_buffer<order::big, std::int_least8_t, 8> big_int8_buf_t;
  78. typedef endian_buffer<order::big, std::int_least16_t, 16> big_int16_buf_t;
  79. typedef endian_buffer<order::big, std::int_least32_t, 24> big_int24_buf_t;
  80. typedef endian_buffer<order::big, std::int_least32_t, 32> big_int32_buf_t;
  81. typedef endian_buffer<order::big, std::int_least64_t, 40> big_int40_buf_t;
  82. typedef endian_buffer<order::big, std::int_least64_t, 48> big_int48_buf_t;
  83. typedef endian_buffer<order::big, std::int_least64_t, 56> big_int56_buf_t;
  84. typedef endian_buffer<order::big, std::int_least64_t, 64> big_int64_buf_t;
  85. // unaligned big endian unsigned integer buffers
  86. typedef endian_buffer<order::big, std::uint_least8_t, 8> big_uint8_buf_t;
  87. typedef endian_buffer<order::big, std::uint_least16_t, 16> big_uint16_buf_t;
  88. typedef endian_buffer<order::big, std::uint_least32_t, 24> big_uint24_buf_t;
  89. typedef endian_buffer<order::big, std::uint_least32_t, 32> big_uint32_buf_t;
  90. typedef endian_buffer<order::big, std::uint_least64_t, 40> big_uint40_buf_t;
  91. typedef endian_buffer<order::big, std::uint_least64_t, 48> big_uint48_buf_t;
  92. typedef endian_buffer<order::big, std::uint_least64_t, 56> big_uint56_buf_t;
  93. typedef endian_buffer<order::big, std::uint_least64_t, 64> big_uint64_buf_t;
  94. // unaligned little endian signed integer buffers
  95. typedef endian_buffer<order::little, std::int_least8_t, 8> little_int8_buf_t;
  96. typedef endian_buffer<order::little, std::int_least16_t, 16> little_int16_buf_t;
  97. typedef endian_buffer<order::little, std::int_least32_t, 24> little_int24_buf_t;
  98. typedef endian_buffer<order::little, std::int_least32_t, 32> little_int32_buf_t;
  99. typedef endian_buffer<order::little, std::int_least64_t, 40> little_int40_buf_t;
  100. typedef endian_buffer<order::little, std::int_least64_t, 48> little_int48_buf_t;
  101. typedef endian_buffer<order::little, std::int_least64_t, 56> little_int56_buf_t;
  102. typedef endian_buffer<order::little, std::int_least64_t, 64> little_int64_buf_t;
  103. // unaligned little endian unsigned integer buffers
  104. typedef endian_buffer<order::little, std::uint_least8_t, 8> little_uint8_buf_t;
  105. typedef endian_buffer<order::little, std::uint_least16_t, 16> little_uint16_buf_t;
  106. typedef endian_buffer<order::little, std::uint_least32_t, 24> little_uint24_buf_t;
  107. typedef endian_buffer<order::little, std::uint_least32_t, 32> little_uint32_buf_t;
  108. typedef endian_buffer<order::little, std::uint_least64_t, 40> little_uint40_buf_t;
  109. typedef endian_buffer<order::little, std::uint_least64_t, 48> little_uint48_buf_t;
  110. typedef endian_buffer<order::little, std::uint_least64_t, 56> little_uint56_buf_t;
  111. typedef endian_buffer<order::little, std::uint_least64_t, 64> little_uint64_buf_t;
  112. // unaligned native endian signed integer buffers
  113. typedef endian_buffer<order::native, std::int_least8_t, 8> native_int8_buf_t;
  114. typedef endian_buffer<order::native, std::int_least16_t, 16> native_int16_buf_t;
  115. typedef endian_buffer<order::native, std::int_least32_t, 24> native_int24_buf_t;
  116. typedef endian_buffer<order::native, std::int_least32_t, 32> native_int32_buf_t;
  117. typedef endian_buffer<order::native, std::int_least64_t, 40> native_int40_buf_t;
  118. typedef endian_buffer<order::native, std::int_least64_t, 48> native_int48_buf_t;
  119. typedef endian_buffer<order::native, std::int_least64_t, 56> native_int56_buf_t;
  120. typedef endian_buffer<order::native, std::int_least64_t, 64> native_int64_buf_t;
  121. // unaligned native endian unsigned integer buffers
  122. typedef endian_buffer<order::native, std::uint_least8_t, 8> native_uint8_buf_t;
  123. typedef endian_buffer<order::native, std::uint_least16_t, 16> native_uint16_buf_t;
  124. typedef endian_buffer<order::native, std::uint_least32_t, 24> native_uint24_buf_t;
  125. typedef endian_buffer<order::native, std::uint_least32_t, 32> native_uint32_buf_t;
  126. typedef endian_buffer<order::native, std::uint_least64_t, 40> native_uint40_buf_t;
  127. typedef endian_buffer<order::native, std::uint_least64_t, 48> native_uint48_buf_t;
  128. typedef endian_buffer<order::native, std::uint_least64_t, 56> native_uint56_buf_t;
  129. typedef endian_buffer<order::native, std::uint_least64_t, 64> native_uint64_buf_t;
  130. // unaligned floating point buffers
  131. typedef endian_buffer<order::big, float, 32, align::no> big_float32_buf_t;
  132. typedef endian_buffer<order::big, double, 64, align::no> big_float64_buf_t;
  133. typedef endian_buffer<order::little, float, 32, align::no> little_float32_buf_t;
  134. typedef endian_buffer<order::little, double, 64, align::no> little_float64_buf_t;
  135. typedef endian_buffer<order::native, float, 32, align::no> native_float32_buf_t;
  136. typedef endian_buffer<order::native, double, 64, align::no> native_float64_buf_t;
  137. // Stream inserter
  138. template <class charT, class traits, order Order, class T,
  139. std::size_t n_bits, align A>
  140. std::basic_ostream<charT, traits>&
  141. operator<<(std::basic_ostream<charT, traits>& os,
  142. const endian_buffer<Order, T, n_bits, A>& x)
  143. {
  144. return os << x.value();
  145. }
  146. // Stream extractor
  147. template <class charT, class traits, order Order, class T,
  148. std::size_t n_bits, align A>
  149. std::basic_istream<charT, traits>&
  150. operator>>(std::basic_istream<charT, traits>& is,
  151. endian_buffer<Order, T, n_bits, A>& x)
  152. {
  153. T i;
  154. if (is >> i)
  155. x = i;
  156. return is;
  157. }
  158. //---------------------------------- end synopsis ------------------------------------//
  159. // endian_buffer class template specializations --------------------------------------//
  160. // Specializations that represent unaligned bytes.
  161. // Taking an integer type as a parameter provides a nice way to pass both
  162. // the size and signedness of the desired integer and get the appropriate
  163. // corresponding integer type for the interface.
  164. // Q: Should endian_buffer supply "value_type operator value_type() const noexcept"?
  165. // A: No. The rationale for endian_buffers is to prevent high-cost hidden
  166. // conversions. If an implicit conversion operator is supplied, hidden conversions
  167. // can occur.
  168. // unaligned endian_buffer specialization
  169. template< order Order, class T, std::size_t n_bits >
  170. class endian_buffer<Order, T, n_bits, align::no>
  171. {
  172. #ifdef BOOST_ENDIAN_NO_CTORS
  173. public:
  174. #endif
  175. BOOST_ENDIAN_STATIC_ASSERT( (n_bits/8)*8 == n_bits );
  176. unsigned char value_[ n_bits / 8 ];
  177. public:
  178. typedef T value_type;
  179. #ifndef BOOST_ENDIAN_NO_CTORS
  180. endian_buffer() = default;
  181. explicit endian_buffer( T val ) BOOST_NOEXCEPT
  182. {
  183. boost::endian::endian_store<T, n_bits / 8, Order>( value_, val );
  184. }
  185. #endif
  186. endian_buffer& operator=( T val ) BOOST_NOEXCEPT
  187. {
  188. boost::endian::endian_store<T, n_bits / 8, Order>( value_, val );
  189. return *this;
  190. }
  191. value_type value() const BOOST_NOEXCEPT
  192. {
  193. return boost::endian::endian_load<T, n_bits / 8, Order>( value_ );
  194. }
  195. unsigned char const * data() const BOOST_NOEXCEPT
  196. {
  197. return value_;
  198. }
  199. unsigned char * data() BOOST_NOEXCEPT
  200. {
  201. return value_;
  202. }
  203. };
  204. // aligned specializations; only n_bits == 16/32/64 supported
  205. // aligned endian_buffer specialization
  206. template< order Order, class T, std::size_t n_bits >
  207. class endian_buffer<Order, T, n_bits, align::yes>
  208. {
  209. private:
  210. BOOST_ENDIAN_STATIC_ASSERT( (n_bits/8)*8 == n_bits );
  211. BOOST_ENDIAN_STATIC_ASSERT( sizeof(T) == n_bits/8 );
  212. union
  213. {
  214. unsigned char value_[ n_bits / 8 ];
  215. T align_;
  216. };
  217. public:
  218. typedef T value_type;
  219. #ifndef BOOST_ENDIAN_NO_CTORS
  220. endian_buffer() = default;
  221. explicit endian_buffer( T val ) BOOST_NOEXCEPT
  222. {
  223. boost::endian::endian_store<T, n_bits / 8, Order>( value_, val );
  224. }
  225. #endif
  226. endian_buffer& operator=( T val ) BOOST_NOEXCEPT
  227. {
  228. boost::endian::endian_store<T, n_bits / 8, Order>( value_, val );
  229. return *this;
  230. }
  231. value_type value() const BOOST_NOEXCEPT
  232. {
  233. return boost::endian::endian_load<T, n_bits / 8, Order>( value_ );
  234. }
  235. unsigned char const * data() const BOOST_NOEXCEPT
  236. {
  237. return value_;
  238. }
  239. unsigned char * data() BOOST_NOEXCEPT
  240. {
  241. return value_;
  242. }
  243. };
  244. // aligned native endian_buffer specialization
  245. template< class T, std::size_t n_bits >
  246. class endian_buffer<order::native, T, n_bits, align::yes>
  247. {
  248. private:
  249. BOOST_ENDIAN_STATIC_ASSERT( (n_bits/8)*8 == n_bits );
  250. BOOST_ENDIAN_STATIC_ASSERT( sizeof(T) == n_bits/8 );
  251. T value_;
  252. public:
  253. typedef T value_type;
  254. #ifndef BOOST_ENDIAN_NO_CTORS
  255. endian_buffer() = default;
  256. explicit endian_buffer( T val ) BOOST_NOEXCEPT: value_( val )
  257. {
  258. }
  259. #endif
  260. endian_buffer& operator=( T val ) BOOST_NOEXCEPT
  261. {
  262. value_ = val;
  263. return *this;
  264. }
  265. value_type value() const BOOST_NOEXCEPT
  266. {
  267. return value_;
  268. }
  269. unsigned char const * data() const BOOST_NOEXCEPT
  270. {
  271. return reinterpret_cast< unsigned char const* >( &value_ );
  272. }
  273. unsigned char * data() BOOST_NOEXCEPT
  274. {
  275. return reinterpret_cast< unsigned char* >( &value_ );
  276. }
  277. };
  278. } // namespace endian
  279. } // namespace boost
  280. #if defined(BOOST_BORLANDC) || defined(BOOST_CODEGEARC)
  281. # pragma pack(pop)
  282. #endif
  283. #if defined(_MSC_VER)
  284. # pragma warning(pop)
  285. #endif
  286. #endif // BOOST_ENDIAN_BUFFERS_HPP