binary.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Hartmut Kaiser
  3. Copyright (c) 2001-2011 Joel de Guzman
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #if !defined(BOOST_SPIRIT_X3_BINARY_MAY_08_2007_0808AM)
  8. #define BOOST_SPIRIT_X3_BINARY_MAY_08_2007_0808AM
  9. #include <boost/spirit/home/x3/core/parser.hpp>
  10. #include <boost/spirit/home/x3/core/skip_over.hpp>
  11. #include <boost/spirit/home/x3/support/traits/move_to.hpp>
  12. #include <cstdint>
  13. #include <boost/endian/conversion.hpp>
  14. #include <boost/endian/arithmetic.hpp>
  15. #include <boost/mpl/or.hpp>
  16. #include <boost/type_traits/is_integral.hpp>
  17. #include <boost/type_traits/is_enum.hpp>
  18. #include <boost/type_traits/is_floating_point.hpp>
  19. #include <boost/config.hpp>
  20. #include <climits>
  21. namespace boost { namespace spirit { namespace x3
  22. {
  23. template <typename T, boost::endian::order endian, std::size_t bits>
  24. struct binary_lit_parser
  25. : parser<binary_lit_parser<T, endian, bits> >
  26. {
  27. static bool const has_attribute = false;
  28. typedef unused_type attribute_type;
  29. constexpr binary_lit_parser(T n_)
  30. : n(n_) {}
  31. template <typename Iterator, typename Context, typename Attribute>
  32. bool parse(Iterator& first, Iterator const& last
  33. , Context const& context, unused_type, Attribute& attr_param) const
  34. {
  35. x3::skip_over(first, last, context);
  36. unsigned char const* bytes = n.data();
  37. Iterator it = first;
  38. for (unsigned int i = 0; i < sizeof(n); ++i)
  39. {
  40. if (it == last || *bytes++ != static_cast<unsigned char>(*it++))
  41. return false;
  42. }
  43. first = it;
  44. x3::traits::move_to(n, attr_param);
  45. return true;
  46. }
  47. boost::endian::endian_arithmetic<endian, T, bits> n;
  48. };
  49. ///////////////////////////////////////////////////////////////////////////
  50. template <typename T, boost::endian::order endian, std::size_t bits>
  51. struct any_binary_parser : parser<any_binary_parser<T, endian, bits > >
  52. {
  53. typedef T attribute_type;
  54. static bool const has_attribute =
  55. !is_same<unused_type, attribute_type>::value;
  56. template <typename Iterator, typename Context, typename Attribute>
  57. bool parse(Iterator& first, Iterator const& last
  58. , Context const& context, unused_type, Attribute& attr_param) const
  59. {
  60. x3::skip_over(first, last, context);
  61. // Properly align the buffer for performance reasons
  62. alignas(T) unsigned char buf[sizeof(T)];
  63. unsigned char * bytes = buf;
  64. Iterator it = first;
  65. for (unsigned int i = 0; i < sizeof(T); ++i)
  66. {
  67. if (it == last)
  68. return false;
  69. *bytes++ = *it++;
  70. }
  71. first = it;
  72. static_assert(bits % CHAR_BIT == 0,
  73. "Boost.Endian supports only multiples of CHAR_BIT");
  74. x3::traits::move_to(
  75. endian::endian_load<T, bits / CHAR_BIT, endian>(buf),
  76. attr_param);
  77. return true;
  78. }
  79. constexpr binary_lit_parser<T, endian, bits> operator()(T n) const
  80. {
  81. return {n};
  82. }
  83. };
  84. #define BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(name, endiantype, attrtype, bits) \
  85. typedef any_binary_parser< attrtype, boost::endian::order::endiantype, bits > name##type; \
  86. constexpr name##type name = name##type();
  87. BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(byte_, native, uint_least8_t, 8)
  88. BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(word, native, uint_least16_t, 16)
  89. BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(big_word, big, uint_least16_t, 16)
  90. BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(little_word, little, uint_least16_t, 16)
  91. BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(dword, native, uint_least32_t, 32)
  92. BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(big_dword, big, uint_least32_t, 32)
  93. BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(little_dword, little, uint_least32_t, 32)
  94. #ifdef BOOST_HAS_LONG_LONG
  95. BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(qword, native, uint_least64_t, 64)
  96. BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(big_qword, big, uint_least64_t, 64)
  97. BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(little_qword, little, uint_least64_t, 64)
  98. #endif
  99. BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(bin_float, native, float, sizeof(float) * CHAR_BIT)
  100. BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(big_bin_float, big, float, sizeof(float) * CHAR_BIT)
  101. BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(little_bin_float, little, float, sizeof(float) * CHAR_BIT)
  102. BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(bin_double, native, double, sizeof(double) * CHAR_BIT)
  103. BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(big_bin_double, big, double, sizeof(double) * CHAR_BIT)
  104. BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE(little_bin_double, little, double, sizeof(double) * CHAR_BIT)
  105. #undef BOOST_SPIRIT_MAKE_BINARY_PRIMITIVE
  106. ///////////////////////////////////////////////////////////////////////////
  107. template <typename T, std::size_t bits>
  108. struct get_info<any_binary_parser<T, endian::order::little, bits>>
  109. {
  110. typedef std::string result_type;
  111. std::string operator()(any_binary_parser<T, endian::order::little, bits> const&) const
  112. {
  113. return "little-endian binary";
  114. }
  115. };
  116. template <typename T, std::size_t bits>
  117. struct get_info<any_binary_parser<T, endian::order::big, bits>>
  118. {
  119. typedef std::string result_type;
  120. std::string operator()(any_binary_parser<T, endian::order::big, bits> const&) const
  121. {
  122. return "big-endian binary";
  123. }
  124. };
  125. template <typename T, std::size_t bits>
  126. struct get_info<binary_lit_parser<T, endian::order::little, bits>>
  127. {
  128. typedef std::string result_type;
  129. std::string operator()(binary_lit_parser<T, endian::order::little, bits> const&) const
  130. {
  131. return "little-endian binary";
  132. }
  133. };
  134. template <typename T, std::size_t bits>
  135. struct get_info<binary_lit_parser<T, endian::order::big, bits>>
  136. {
  137. typedef std::string result_type;
  138. std::string operator()(binary_lit_parser<T, endian::order::big, bits> const&) const
  139. {
  140. return "big-endian binary";
  141. }
  142. };
  143. }}}
  144. #endif