attributes.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright (c) 2001-2011 Hartmut Kaiser
  2. // Copyright (c) 2001-2011 Joel de Guzman
  3. //
  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. #if !defined(SPIRIT_QI_DETAIL_ATTRIBUTES_APR_18_2010_0458PM)
  7. #define SPIRIT_QI_DETAIL_ATTRIBUTES_APR_18_2010_0458PM
  8. #include <boost/spirit/home/qi/domain.hpp>
  9. #include <boost/spirit/home/support/attributes_fwd.hpp>
  10. #include <boost/spirit/home/support/attributes.hpp>
  11. #include <boost/spirit/home/support/utree/utree_traits_fwd.hpp>
  12. ///////////////////////////////////////////////////////////////////////////////
  13. namespace boost { namespace spirit { namespace qi
  14. {
  15. template <typename Exposed, typename Transformed>
  16. struct default_transform_attribute
  17. {
  18. typedef Transformed type;
  19. static Transformed pre(Exposed&) { return Transformed(); }
  20. static void post(Exposed& val, Transformed const& attr)
  21. {
  22. traits::assign_to(attr, val);
  23. }
  24. // fail() will be called by Qi rule's if the rhs failed parsing
  25. static void fail(Exposed&) {}
  26. };
  27. // handle case where no transformation is required as the types are the same
  28. template <typename Attribute>
  29. struct default_transform_attribute<Attribute, Attribute>
  30. {
  31. typedef Attribute& type;
  32. static Attribute& pre(Attribute& val) { return val; }
  33. static void post(Attribute&, Attribute const&) {}
  34. static void fail(Attribute&) {}
  35. };
  36. template <typename Exposed, typename Transformed>
  37. struct proxy_transform_attribute
  38. {
  39. typedef Transformed type;
  40. static Transformed pre(Exposed& val) { return Transformed(val); }
  41. static void post(Exposed&, Transformed const&) { /* no-op */ }
  42. // fail() will be called by Qi rule's if the rhs failed parsing
  43. static void fail(Exposed&) {}
  44. };
  45. // handle case where no transformation is required as the types are the same
  46. template <typename Attribute>
  47. struct proxy_transform_attribute<Attribute, Attribute>
  48. {
  49. typedef Attribute& type;
  50. static Attribute& pre(Attribute& val) { return val; }
  51. static void post(Attribute&, Attribute const&) {}
  52. static void fail(Attribute&) {}
  53. };
  54. // main specialization for Qi
  55. template <typename Exposed, typename Transformed, typename Enable = void>
  56. struct transform_attribute
  57. : mpl::if_<
  58. mpl::and_<
  59. mpl::not_<is_const<Exposed> >
  60. , mpl::not_<is_reference<Exposed> >
  61. , traits::is_proxy<Transformed> >
  62. , proxy_transform_attribute<Exposed, Transformed>
  63. , default_transform_attribute<Exposed, Transformed>
  64. >::type
  65. {};
  66. template <typename Exposed, typename Transformed>
  67. struct transform_attribute<boost::optional<Exposed>, Transformed
  68. , typename disable_if<is_same<boost::optional<Exposed>, Transformed> >::type>
  69. {
  70. typedef Transformed& type;
  71. static Transformed& pre(boost::optional<Exposed>& val)
  72. {
  73. if (!val)
  74. val = Transformed();
  75. return boost::get<Transformed>(val);
  76. }
  77. static void post(boost::optional<Exposed>&, Transformed const&) {}
  78. static void fail(boost::optional<Exposed>& val)
  79. {
  80. val = none; // leave optional uninitialized if rhs failed
  81. }
  82. };
  83. // unused_type needs some special handling as well
  84. template <>
  85. struct transform_attribute<unused_type, unused_type>
  86. {
  87. typedef unused_type type;
  88. static unused_type pre(unused_type) { return unused; }
  89. static void post(unused_type, unused_type) {}
  90. static void fail(unused_type) {}
  91. };
  92. template <>
  93. struct transform_attribute<unused_type const, unused_type>
  94. : transform_attribute<unused_type, unused_type>
  95. {};
  96. template <typename Attribute>
  97. struct transform_attribute<Attribute, unused_type>
  98. : transform_attribute<unused_type, unused_type>
  99. {};
  100. template <typename Attribute>
  101. struct transform_attribute<Attribute const, unused_type>
  102. : transform_attribute<unused_type, unused_type>
  103. {};
  104. }}}
  105. ///////////////////////////////////////////////////////////////////////////////
  106. namespace boost { namespace spirit { namespace traits
  107. {
  108. namespace detail {
  109. template <typename Exposed, typename Transformed>
  110. struct transform_attribute_base<Exposed, Transformed, qi::domain>
  111. : qi::transform_attribute<Exposed, Transformed>
  112. {};
  113. }
  114. }}}
  115. #endif