buffers.hpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. // boost/endian/buffers.hpp ----------------------------------------------------------//
  2. // (C) Copyright Darin Adler 2000
  3. // (C) Copyright Beman Dawes 2006, 2009, 2014
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // See http://www.boost.org/LICENSE_1_0.txt
  6. // See library home page at http://www.boost.org/libs/endian
  7. //--------------------------------------------------------------------------------------//
  8. // Original design developed by Darin Adler based on classes developed by Mark
  9. // Borgerding. Four original class templates were combined into a single endian
  10. // class template by Beman Dawes, who also added the unrolled_byte_loops sign
  11. // partial specialization to correctly extend the sign when cover integer size
  12. // differs from endian representation size.
  13. // TODO: When a compiler supporting constexpr becomes available, try possible uses.
  14. #ifndef BOOST_ENDIAN_BUFFERS_HPP
  15. #define BOOST_ENDIAN_BUFFERS_HPP
  16. #if defined(_MSC_VER)
  17. # pragma warning(push)
  18. # pragma warning(disable:4365) // conversion ... signed/unsigned mismatch
  19. #endif
  20. #ifdef BOOST_ENDIAN_LOG
  21. # include <iostream>
  22. #endif
  23. #if defined(__BORLANDC__) || defined( __CODEGEARC__)
  24. # pragma pack(push, 1)
  25. #endif
  26. #include <boost/config.hpp>
  27. #include <boost/predef/detail/endian_compat.h>
  28. #include <boost/endian/conversion.hpp>
  29. #include <boost/type_traits/is_signed.hpp>
  30. #include <boost/type_traits/make_unsigned.hpp>
  31. #include <boost/type_traits/conditional.hpp>
  32. #include <boost/type_traits/is_integral.hpp>
  33. #include <boost/type_traits/type_identity.hpp>
  34. #include <boost/cstdint.hpp>
  35. #include <boost/static_assert.hpp>
  36. #include <boost/core/scoped_enum.hpp>
  37. #include <iosfwd>
  38. #include <climits>
  39. #include <cstring>
  40. # if CHAR_BIT != 8
  41. # error Platforms with CHAR_BIT != 8 are not supported
  42. # endif
  43. # ifdef BOOST_NO_CXX11_DEFAULTED_FUNCTIONS
  44. # define BOOST_ENDIAN_DEFAULT_CONSTRUCT {} // C++03
  45. # else
  46. # define BOOST_ENDIAN_DEFAULT_CONSTRUCT = default; // C++0x
  47. # endif
  48. # if defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS) && defined(BOOST_ENDIAN_FORCE_PODNESS)
  49. # define BOOST_ENDIAN_NO_CTORS
  50. # endif
  51. //---------------------------------- synopsis ----------------------------------------//
  52. namespace boost
  53. {
  54. namespace endian
  55. {
  56. BOOST_SCOPED_ENUM_START(align)
  57. {no, yes
  58. # ifdef BOOST_ENDIAN_DEPRECATED_NAMES
  59. , unaligned = no, aligned = yes
  60. # endif
  61. }; BOOST_SCOPED_ENUM_END
  62. template <BOOST_SCOPED_ENUM(order) Order, class T, std::size_t n_bits,
  63. BOOST_SCOPED_ENUM(align) A = align::no>
  64. class endian_buffer;
  65. // aligned big endian signed integer buffers
  66. typedef endian_buffer<order::big, int8_t, 8, align::yes> big_int8_buf_at;
  67. typedef endian_buffer<order::big, int16_t, 16, align::yes> big_int16_buf_at;
  68. typedef endian_buffer<order::big, int32_t, 32, align::yes> big_int32_buf_at;
  69. typedef endian_buffer<order::big, int64_t, 64, align::yes> big_int64_buf_at;
  70. // aligned big endian unsigned integer buffers
  71. typedef endian_buffer<order::big, uint8_t, 8, align::yes> big_uint8_buf_at;
  72. typedef endian_buffer<order::big, uint16_t, 16, align::yes> big_uint16_buf_at;
  73. typedef endian_buffer<order::big, uint32_t, 32, align::yes> big_uint32_buf_at;
  74. typedef endian_buffer<order::big, uint64_t, 64, align::yes> big_uint64_buf_at;
  75. // aligned little endian signed integer buffers
  76. typedef endian_buffer<order::little, int8_t, 8, align::yes> little_int8_buf_at;
  77. typedef endian_buffer<order::little, int16_t, 16, align::yes> little_int16_buf_at;
  78. typedef endian_buffer<order::little, int32_t, 32, align::yes> little_int32_buf_at;
  79. typedef endian_buffer<order::little, int64_t, 64, align::yes> little_int64_buf_at;
  80. // aligned little endian unsigned integer buffers
  81. typedef endian_buffer<order::little, uint8_t, 8, align::yes> little_uint8_buf_at;
  82. typedef endian_buffer<order::little, uint16_t, 16, align::yes> little_uint16_buf_at;
  83. typedef endian_buffer<order::little, uint32_t, 32, align::yes> little_uint32_buf_at;
  84. typedef endian_buffer<order::little, uint64_t, 64, align::yes> little_uint64_buf_at;
  85. // aligned native endian typedefs are not provided because
  86. // <cstdint> types are superior for this use case
  87. // unaligned big endian signed integer buffers
  88. typedef endian_buffer<order::big, int_least8_t, 8> big_int8_buf_t;
  89. typedef endian_buffer<order::big, int_least16_t, 16> big_int16_buf_t;
  90. typedef endian_buffer<order::big, int_least32_t, 24> big_int24_buf_t;
  91. typedef endian_buffer<order::big, int_least32_t, 32> big_int32_buf_t;
  92. typedef endian_buffer<order::big, int_least64_t, 40> big_int40_buf_t;
  93. typedef endian_buffer<order::big, int_least64_t, 48> big_int48_buf_t;
  94. typedef endian_buffer<order::big, int_least64_t, 56> big_int56_buf_t;
  95. typedef endian_buffer<order::big, int_least64_t, 64> big_int64_buf_t;
  96. // unaligned big endian unsigned integer buffers
  97. typedef endian_buffer<order::big, uint_least8_t, 8> big_uint8_buf_t;
  98. typedef endian_buffer<order::big, uint_least16_t, 16> big_uint16_buf_t;
  99. typedef endian_buffer<order::big, uint_least32_t, 24> big_uint24_buf_t;
  100. typedef endian_buffer<order::big, uint_least32_t, 32> big_uint32_buf_t;
  101. typedef endian_buffer<order::big, uint_least64_t, 40> big_uint40_buf_t;
  102. typedef endian_buffer<order::big, uint_least64_t, 48> big_uint48_buf_t;
  103. typedef endian_buffer<order::big, uint_least64_t, 56> big_uint56_buf_t;
  104. typedef endian_buffer<order::big, uint_least64_t, 64> big_uint64_buf_t;
  105. // unaligned little endian signed integer buffers
  106. typedef endian_buffer<order::little, int_least8_t, 8> little_int8_buf_t;
  107. typedef endian_buffer<order::little, int_least16_t, 16> little_int16_buf_t;
  108. typedef endian_buffer<order::little, int_least32_t, 24> little_int24_buf_t;
  109. typedef endian_buffer<order::little, int_least32_t, 32> little_int32_buf_t;
  110. typedef endian_buffer<order::little, int_least64_t, 40> little_int40_buf_t;
  111. typedef endian_buffer<order::little, int_least64_t, 48> little_int48_buf_t;
  112. typedef endian_buffer<order::little, int_least64_t, 56> little_int56_buf_t;
  113. typedef endian_buffer<order::little, int_least64_t, 64> little_int64_buf_t;
  114. // unaligned little endian unsigned integer buffers
  115. typedef endian_buffer<order::little, uint_least8_t, 8> little_uint8_buf_t;
  116. typedef endian_buffer<order::little, uint_least16_t, 16> little_uint16_buf_t;
  117. typedef endian_buffer<order::little, uint_least32_t, 24> little_uint24_buf_t;
  118. typedef endian_buffer<order::little, uint_least32_t, 32> little_uint32_buf_t;
  119. typedef endian_buffer<order::little, uint_least64_t, 40> little_uint40_buf_t;
  120. typedef endian_buffer<order::little, uint_least64_t, 48> little_uint48_buf_t;
  121. typedef endian_buffer<order::little, uint_least64_t, 56> little_uint56_buf_t;
  122. typedef endian_buffer<order::little, uint_least64_t, 64> little_uint64_buf_t;
  123. # ifdef BOOST_BIG_ENDIAN
  124. // unaligned native endian signed integer buffers
  125. typedef big_int8_buf_t native_int8_buf_t;
  126. typedef big_int16_buf_t native_int16_buf_t;
  127. typedef big_int24_buf_t native_int24_buf_t;
  128. typedef big_int32_buf_t native_int32_buf_t;
  129. typedef big_int40_buf_t native_int40_buf_t;
  130. typedef big_int48_buf_t native_int48_buf_t;
  131. typedef big_int56_buf_t native_int56_buf_t;
  132. typedef big_int64_buf_t native_int64_buf_t;
  133. // unaligned native endian unsigned integer buffers
  134. typedef big_uint8_buf_t native_uint8_buf_t;
  135. typedef big_uint16_buf_t native_uint16_buf_t;
  136. typedef big_uint24_buf_t native_uint24_buf_t;
  137. typedef big_uint32_buf_t native_uint32_buf_t;
  138. typedef big_uint40_buf_t native_uint40_buf_t;
  139. typedef big_uint48_buf_t native_uint48_buf_t;
  140. typedef big_uint56_buf_t native_uint56_buf_t;
  141. typedef big_uint64_buf_t native_uint64_buf_t;
  142. # else
  143. // unaligned native endian signed integer buffers
  144. typedef little_int8_buf_t native_int8_buf_t;
  145. typedef little_int16_buf_t native_int16_buf_t;
  146. typedef little_int24_buf_t native_int24_buf_t;
  147. typedef little_int32_buf_t native_int32_buf_t;
  148. typedef little_int40_buf_t native_int40_buf_t;
  149. typedef little_int48_buf_t native_int48_buf_t;
  150. typedef little_int56_buf_t native_int56_buf_t;
  151. typedef little_int64_buf_t native_int64_buf_t;
  152. // unaligned native endian unsigned integer buffers
  153. typedef little_uint8_buf_t native_uint8_buf_t;
  154. typedef little_uint16_buf_t native_uint16_buf_t;
  155. typedef little_uint24_buf_t native_uint24_buf_t;
  156. typedef little_uint32_buf_t native_uint32_buf_t;
  157. typedef little_uint40_buf_t native_uint40_buf_t;
  158. typedef little_uint48_buf_t native_uint48_buf_t;
  159. typedef little_uint56_buf_t native_uint56_buf_t;
  160. typedef little_uint64_buf_t native_uint64_buf_t;
  161. # endif
  162. // Stream inserter
  163. template <class charT, class traits, BOOST_SCOPED_ENUM(order) Order, class T,
  164. std::size_t n_bits, BOOST_SCOPED_ENUM(align) A>
  165. std::basic_ostream<charT, traits>&
  166. operator<<(std::basic_ostream<charT, traits>& os,
  167. const endian_buffer<Order, T, n_bits, A>& x)
  168. {
  169. return os << x.value();
  170. }
  171. // Stream extractor
  172. template <class charT, class traits, BOOST_SCOPED_ENUM(order) Order, class T,
  173. std::size_t n_bits, BOOST_SCOPED_ENUM(align) A>
  174. std::basic_istream<charT, traits>&
  175. operator>>(std::basic_istream<charT, traits>& is,
  176. endian_buffer<Order, T, n_bits, A>& x)
  177. {
  178. T i;
  179. if (is >> i)
  180. x = i;
  181. return is;
  182. }
  183. //---------------------------------- end synopsis ------------------------------------//
  184. namespace detail
  185. {
  186. // Unrolled loops for loading and storing streams of bytes.
  187. template <typename T, std::size_t n_bytes,
  188. bool sign=boost::is_signed<T>::value >
  189. struct unrolled_byte_loops
  190. {
  191. typedef unrolled_byte_loops<T, n_bytes - 1, sign> next;
  192. // shifting a negative number is flagged by -fsanitize=undefined
  193. // so use the corresponding unsigned type for the shifts
  194. typedef typename boost::conditional<
  195. boost::is_integral<T>::value,
  196. boost::make_unsigned<T>, boost::type_identity<T> >::type::type U;
  197. static T load_big(const unsigned char* bytes) BOOST_NOEXCEPT
  198. { return static_cast<T>(*(bytes - 1) | (static_cast<U>(next::load_big(bytes - 1)) << 8)); }
  199. static T load_little(const unsigned char* bytes) BOOST_NOEXCEPT
  200. { return static_cast<T>(*bytes | (static_cast<U>(next::load_little(bytes + 1)) << 8)); }
  201. static void store_big(char* bytes, T value) BOOST_NOEXCEPT
  202. {
  203. *(bytes - 1) = static_cast<char>(value);
  204. next::store_big(bytes - 1, static_cast<T>(static_cast<U>(value) >> 8));
  205. }
  206. static void store_little(char* bytes, T value) BOOST_NOEXCEPT
  207. {
  208. *bytes = static_cast<char>(value);
  209. next::store_little(bytes + 1, static_cast<T>(static_cast<U>(value) >> 8));
  210. }
  211. };
  212. template <typename T>
  213. struct unrolled_byte_loops<T, 1, false>
  214. {
  215. static T load_big(const unsigned char* bytes) BOOST_NOEXCEPT
  216. { return *(bytes - 1); }
  217. static T load_little(const unsigned char* bytes) BOOST_NOEXCEPT
  218. { return *bytes; }
  219. static void store_big(char* bytes, T value) BOOST_NOEXCEPT
  220. { *(bytes - 1) = static_cast<char>(value); }
  221. static void store_little(char* bytes, T value) BOOST_NOEXCEPT
  222. { *bytes = static_cast<char>(value); }
  223. };
  224. template <typename T>
  225. struct unrolled_byte_loops<T, 1, true>
  226. {
  227. static T load_big(const unsigned char* bytes) BOOST_NOEXCEPT
  228. { return *reinterpret_cast<const signed char*>(bytes - 1); }
  229. static T load_little(const unsigned char* bytes) BOOST_NOEXCEPT
  230. { return *reinterpret_cast<const signed char*>(bytes); }
  231. static void store_big(char* bytes, T value) BOOST_NOEXCEPT
  232. { *(bytes - 1) = static_cast<char>(value); }
  233. static void store_little(char* bytes, T value) BOOST_NOEXCEPT
  234. { *bytes = static_cast<char>(value); }
  235. };
  236. template <typename T, std::size_t n_bytes>
  237. inline
  238. T load_big_endian(const void* bytes) BOOST_NOEXCEPT
  239. {
  240. return unrolled_byte_loops<T, n_bytes>::load_big
  241. (static_cast<const unsigned char*>(bytes) + n_bytes);
  242. }
  243. template <typename T, std::size_t n_bytes>
  244. inline
  245. T load_little_endian(const void* bytes) BOOST_NOEXCEPT
  246. {
  247. # if defined(__x86_64__) || defined(_M_X64) || defined(__i386) || defined(_M_IX86)
  248. // On x86 (which is little endian), unaligned loads are permitted
  249. if (sizeof(T) == n_bytes) // GCC 4.9, VC++ 14.0, and probably others, elide this
  250. // test and generate code only for the applicable return
  251. // case since sizeof(T) and n_bytes are known at compile
  252. // time.
  253. {
  254. // Avoids -fsanitize=undefined violations due to unaligned loads
  255. // All major x86 compilers optimize a short-sized memcpy into a single instruction
  256. T t;
  257. std::memcpy( &t, bytes, sizeof(T) );
  258. return t;
  259. }
  260. # endif
  261. return unrolled_byte_loops<T, n_bytes>::load_little
  262. (static_cast<const unsigned char*>(bytes));
  263. }
  264. template <typename T, std::size_t n_bytes>
  265. inline
  266. void store_big_endian(void* bytes, T value) BOOST_NOEXCEPT
  267. {
  268. unrolled_byte_loops<T, n_bytes>::store_big
  269. (static_cast<char*>(bytes) + n_bytes, value);
  270. }
  271. template <typename T, std::size_t n_bytes>
  272. inline
  273. void store_little_endian(void* bytes, T value) BOOST_NOEXCEPT
  274. {
  275. # if defined(__x86_64__) || defined(_M_X64) || defined(__i386) || defined(_M_IX86)
  276. // On x86 (which is little endian), unaligned stores are permitted
  277. if (sizeof(T) == n_bytes) // GCC 4.9, VC++ 14.0, and probably others, elide this
  278. // test and generate code only for the applicable return
  279. // case since sizeof(T) and n_bytes are known at compile
  280. // time.
  281. {
  282. // Avoids -fsanitize=undefined violations due to unaligned stores
  283. // All major x86 compilers optimize a short-sized memcpy into a single instruction
  284. std::memcpy( bytes, &value, sizeof(T) );
  285. return;
  286. }
  287. # endif
  288. unrolled_byte_loops<T, n_bytes>::store_little
  289. (static_cast<char*>(bytes), value);
  290. }
  291. } // namespace detail
  292. # ifdef BOOST_ENDIAN_LOG
  293. bool endian_log(true);
  294. # endif
  295. // endian_buffer class template specializations --------------------------------------//
  296. // Specializations that represent unaligned bytes.
  297. // Taking an integer type as a parameter provides a nice way to pass both
  298. // the size and signedness of the desired integer and get the appropriate
  299. // corresponding integer type for the interface.
  300. // Q: Should endian_buffer supply "value_type operator value_type() const noexcept"?
  301. // A: No. The rationale for endian_buffers is to prevent high-cost hidden
  302. // conversions. If an implicit conversion operator is supplied, hidden conversions
  303. // can occur.
  304. // unaligned big endian_buffer specialization
  305. template <typename T, std::size_t n_bits>
  306. class endian_buffer< order::big, T, n_bits, align::no >
  307. {
  308. BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits );
  309. public:
  310. typedef T value_type;
  311. # ifndef BOOST_ENDIAN_NO_CTORS
  312. endian_buffer() BOOST_ENDIAN_DEFAULT_CONSTRUCT
  313. explicit endian_buffer(T val) BOOST_NOEXCEPT
  314. {
  315. # ifdef BOOST_ENDIAN_LOG
  316. if ( endian_log )
  317. std::cout << "big, unaligned, "
  318. << n_bits << "-bits, construct(" << val << ")\n";
  319. # endif
  320. detail::store_big_endian<T, n_bits/8>(m_value, val);
  321. }
  322. # endif
  323. endian_buffer & operator=(T val) BOOST_NOEXCEPT
  324. {
  325. # ifdef BOOST_ENDIAN_LOG
  326. if (endian_log)
  327. std::cout << "big, unaligned, " << n_bits << "-bits, assign(" << val << ")\n";
  328. # endif
  329. detail::store_big_endian<T, n_bits/8>(m_value, val);
  330. return *this;
  331. }
  332. value_type value() const BOOST_NOEXCEPT
  333. {
  334. # ifdef BOOST_ENDIAN_LOG
  335. if ( endian_log )
  336. std::cout << "big, unaligned, " << n_bits << "-bits, convert("
  337. << detail::load_big_endian<T, n_bits/8>(m_value) << ")\n";
  338. # endif
  339. return detail::load_big_endian<T, n_bits/8>(m_value);
  340. }
  341. const char* data() const BOOST_NOEXCEPT { return m_value; }
  342. protected:
  343. char m_value[n_bits/8];
  344. };
  345. // unaligned little endian_buffer specialization
  346. template <typename T, std::size_t n_bits>
  347. class endian_buffer< order::little, T, n_bits, align::no >
  348. {
  349. BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits );
  350. public:
  351. typedef T value_type;
  352. # ifndef BOOST_ENDIAN_NO_CTORS
  353. endian_buffer() BOOST_ENDIAN_DEFAULT_CONSTRUCT
  354. explicit endian_buffer(T val) BOOST_NOEXCEPT
  355. {
  356. # ifdef BOOST_ENDIAN_LOG
  357. if ( endian_log )
  358. std::cout << "little, unaligned, " << n_bits << "-bits, construct("
  359. << val << ")\n";
  360. # endif
  361. detail::store_little_endian<T, n_bits/8>(m_value, val);
  362. }
  363. # endif
  364. endian_buffer & operator=(T val) BOOST_NOEXCEPT
  365. { detail::store_little_endian<T, n_bits/8>(m_value, val); return *this; }
  366. value_type value() const BOOST_NOEXCEPT
  367. {
  368. # ifdef BOOST_ENDIAN_LOG
  369. if ( endian_log )
  370. std::cout << "little, unaligned, " << n_bits << "-bits, convert("
  371. << detail::load_little_endian<T, n_bits/8>(m_value) << ")\n";
  372. # endif
  373. return detail::load_little_endian<T, n_bits/8>(m_value);
  374. }
  375. const char* data() const BOOST_NOEXCEPT { return m_value; }
  376. protected:
  377. char m_value[n_bits/8];
  378. };
  379. // align::yes specializations; only n_bits == 16/32/64 supported
  380. // aligned big endian_buffer specialization
  381. template <typename T, std::size_t n_bits>
  382. class endian_buffer<order::big, T, n_bits, align::yes>
  383. {
  384. BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits );
  385. BOOST_STATIC_ASSERT( sizeof(T) == n_bits/8 );
  386. public:
  387. typedef T value_type;
  388. # ifndef BOOST_ENDIAN_NO_CTORS
  389. endian_buffer() BOOST_ENDIAN_DEFAULT_CONSTRUCT
  390. explicit endian_buffer(T val) BOOST_NOEXCEPT
  391. {
  392. # ifdef BOOST_ENDIAN_LOG
  393. if ( endian_log )
  394. std::cout << "big, aligned, " << n_bits
  395. << "-bits, construct(" << val << ")\n";
  396. # endif
  397. m_value = ::boost::endian::native_to_big(val);
  398. }
  399. # endif
  400. endian_buffer& operator=(T val) BOOST_NOEXCEPT
  401. {
  402. m_value = ::boost::endian::native_to_big(val);
  403. return *this;
  404. }
  405. //operator value_type() const BOOST_NOEXCEPT
  406. //{
  407. // return ::boost::endian::big_to_native(m_value);
  408. //}
  409. value_type value() const BOOST_NOEXCEPT
  410. {
  411. # ifdef BOOST_ENDIAN_LOG
  412. if ( endian_log )
  413. std::cout << "big, aligned, " << n_bits << "-bits, convert("
  414. << ::boost::endian::big_to_native(m_value) << ")\n";
  415. # endif
  416. return ::boost::endian::big_to_native(m_value);
  417. }
  418. const char* data() const BOOST_NOEXCEPT
  419. {return reinterpret_cast<const char*>(&m_value);}
  420. protected:
  421. T m_value;
  422. };
  423. // aligned little endian_buffer specialization
  424. template <typename T, std::size_t n_bits>
  425. class endian_buffer<order::little, T, n_bits, align::yes>
  426. {
  427. BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits );
  428. BOOST_STATIC_ASSERT( sizeof(T) == n_bits/8 );
  429. public:
  430. typedef T value_type;
  431. # ifndef BOOST_ENDIAN_NO_CTORS
  432. endian_buffer() BOOST_ENDIAN_DEFAULT_CONSTRUCT
  433. explicit endian_buffer(T val) BOOST_NOEXCEPT
  434. {
  435. # ifdef BOOST_ENDIAN_LOG
  436. if ( endian_log )
  437. std::cout << "little, aligned, " << n_bits
  438. << "-bits, construct(" << val << ")\n";
  439. # endif
  440. m_value = ::boost::endian::native_to_little(val);
  441. }
  442. # endif
  443. endian_buffer& operator=(T val) BOOST_NOEXCEPT
  444. {
  445. m_value = ::boost::endian::native_to_little(val);
  446. return *this;
  447. }
  448. value_type value() const BOOST_NOEXCEPT
  449. {
  450. # ifdef BOOST_ENDIAN_LOG
  451. if ( endian_log )
  452. std::cout << "little, aligned, " << n_bits << "-bits, convert("
  453. << ::boost::endian::little_to_native(m_value) << ")\n";
  454. # endif
  455. return ::boost::endian::little_to_native(m_value);
  456. }
  457. const char* data() const BOOST_NOEXCEPT
  458. {return reinterpret_cast<const char*>(&m_value);}
  459. protected:
  460. T m_value;
  461. };
  462. } // namespace endian
  463. } // namespace boost
  464. #if defined(__BORLANDC__) || defined( __CODEGEARC__)
  465. # pragma pack(pop)
  466. #endif
  467. #if defined(_MSC_VER)
  468. # pragma warning(pop)
  469. #endif
  470. #endif // BOOST_ENDIAN_BUFFERS_HPP