attr.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Hartmut Kaiser
  3. Copyright (c) 2001-2014 Joel de Guzman
  4. Copyright (c) 2013 Agustin Berge
  5. Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. ==============================================================================*/
  8. #ifndef BOOST_SPIRIT_X3_ATTR_JUL_23_2008_0956AM
  9. #define BOOST_SPIRIT_X3_ATTR_JUL_23_2008_0956AM
  10. #include <boost/spirit/home/x3/core/parser.hpp>
  11. #include <boost/spirit/home/x3/support/unused.hpp>
  12. #include <boost/spirit/home/x3/support/traits/container_traits.hpp>
  13. #include <boost/spirit/home/x3/support/traits/move_to.hpp>
  14. #include <boost/type_traits/is_same.hpp>
  15. #include <boost/type_traits/remove_cv.hpp>
  16. #include <boost/type_traits/remove_reference.hpp>
  17. #include <cstddef>
  18. #include <string>
  19. #include <utility>
  20. namespace boost { namespace spirit { namespace x3
  21. {
  22. namespace detail
  23. {
  24. template <typename Value, std::size_t N
  25. , typename = std::make_index_sequence<N>>
  26. struct array_helper;
  27. template <typename Value, std::size_t N, std::size_t... Is>
  28. struct array_helper<Value, N, std::index_sequence<Is...>>
  29. {
  30. constexpr array_helper(Value const (&value)[N])
  31. : value_{ value[Is]... } {}
  32. constexpr array_helper(Value (&&value)[N])
  33. : value_{ static_cast<Value&&>(value[Is])... } {}
  34. // silence MSVC warning C4512: assignment operator could not be generated
  35. array_helper& operator= (array_helper const&) = delete;
  36. Value value_[N];
  37. };
  38. }
  39. template <typename Value>
  40. struct attr_parser : parser<attr_parser<Value>>
  41. {
  42. typedef Value attribute_type;
  43. static bool const has_attribute =
  44. !is_same<unused_type, attribute_type>::value;
  45. static bool const handles_container =
  46. traits::is_container<attribute_type>::value;
  47. constexpr attr_parser(Value const& value)
  48. : value_(value) {}
  49. constexpr attr_parser(Value&& value)
  50. : value_(std::move(value)) {}
  51. template <typename Iterator, typename Context
  52. , typename RuleContext, typename Attribute>
  53. bool parse(Iterator& /* first */, Iterator const& /* last */
  54. , Context const& /* context */, RuleContext&, Attribute& attr_) const
  55. {
  56. // $$$ Change to copy_to once we have it $$$
  57. traits::move_to(value_, attr_);
  58. return true;
  59. }
  60. Value value_;
  61. // silence MSVC warning C4512: assignment operator could not be generated
  62. attr_parser& operator= (attr_parser const&) = delete;
  63. };
  64. template <typename Value, std::size_t N>
  65. struct attr_parser<Value[N]> : parser<attr_parser<Value[N]>>
  66. , detail::array_helper<Value, N>
  67. {
  68. using detail::array_helper<Value, N>::array_helper;
  69. typedef Value attribute_type[N];
  70. static bool const has_attribute =
  71. !is_same<unused_type, attribute_type>::value;
  72. static bool const handles_container = true;
  73. template <typename Iterator, typename Context
  74. , typename RuleContext, typename Attribute>
  75. bool parse(Iterator& /* first */, Iterator const& /* last */
  76. , Context const& /* context */, RuleContext&, Attribute& attr_) const
  77. {
  78. // $$$ Change to copy_to once we have it $$$
  79. traits::move_to(this->value_ + 0, this->value_ + N, attr_);
  80. return true;
  81. }
  82. // silence MSVC warning C4512: assignment operator could not be generated
  83. attr_parser& operator= (attr_parser const&) = delete;
  84. };
  85. template <typename Value>
  86. struct get_info<attr_parser<Value>>
  87. {
  88. typedef std::string result_type;
  89. std::string operator()(attr_parser<Value> const& /*p*/) const
  90. {
  91. return "attr";
  92. }
  93. };
  94. struct attr_gen
  95. {
  96. template <typename Value>
  97. constexpr attr_parser<typename remove_cv<
  98. typename remove_reference<Value>::type>::type>
  99. operator()(Value&& value) const
  100. {
  101. return { std::forward<Value>(value) };
  102. }
  103. };
  104. constexpr auto attr = attr_gen{};
  105. }}}
  106. #endif