attributes.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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_t(); // leave optional uninitialized if rhs failed
  81. }
  82. };
  83. // reference types need special handling
  84. template <typename Attribute>
  85. struct transform_attribute<Attribute&, Attribute>
  86. {
  87. typedef Attribute& type;
  88. static Attribute& pre(Attribute& val) { return val; }
  89. static void post(Attribute&, Attribute const&) {}
  90. static void fail(Attribute&) {}
  91. };
  92. // unused_type needs some special handling as well
  93. template <>
  94. struct transform_attribute<unused_type, unused_type>
  95. {
  96. typedef unused_type type;
  97. static unused_type pre(unused_type) { return unused; }
  98. static void post(unused_type, unused_type) {}
  99. static void fail(unused_type) {}
  100. };
  101. template <>
  102. struct transform_attribute<unused_type const, unused_type>
  103. : transform_attribute<unused_type, unused_type>
  104. {};
  105. template <typename Attribute>
  106. struct transform_attribute<unused_type, Attribute>
  107. : transform_attribute<unused_type, unused_type>
  108. {};
  109. template <typename Attribute>
  110. struct transform_attribute<unused_type const, Attribute>
  111. : transform_attribute<unused_type, unused_type>
  112. {};
  113. template <typename Attribute>
  114. struct transform_attribute<Attribute, unused_type>
  115. : transform_attribute<unused_type, unused_type>
  116. {};
  117. template <typename Attribute>
  118. struct transform_attribute<Attribute const, unused_type>
  119. : transform_attribute<unused_type, unused_type>
  120. {};
  121. }}}
  122. ///////////////////////////////////////////////////////////////////////////////
  123. namespace boost { namespace spirit { namespace traits
  124. {
  125. template <typename Exposed, typename Transformed>
  126. struct transform_attribute<Exposed, Transformed, qi::domain>
  127. : qi::transform_attribute<Exposed, Transformed>
  128. {};
  129. template <typename Exposed, typename Transformed>
  130. struct transform_attribute<Exposed&, Transformed, qi::domain>
  131. : transform_attribute<Exposed, Transformed, qi::domain>
  132. {};
  133. template <typename Attribute>
  134. struct transform_attribute<Attribute&, Attribute, qi::domain>
  135. : qi::transform_attribute<Attribute&, Attribute>
  136. {};
  137. ///////////////////////////////////////////////////////////////////////////
  138. template <typename Exposed, typename Transformed>
  139. void post_transform(Exposed& dest, Transformed const& attr)
  140. {
  141. return transform_attribute<Exposed, Transformed, qi::domain>::post(dest, attr);
  142. }
  143. ///////////////////////////////////////////////////////////////////////////
  144. template <typename Exposed, typename Transformed>
  145. void fail_transform(Exposed& dest, Transformed const&)
  146. {
  147. return transform_attribute<Exposed, Transformed, qi::domain>::fail(dest);
  148. }
  149. }}}
  150. #endif