attr_cast.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright (c) 2001-2011 Hartmut Kaiser
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #if !defined(SPIRIT_KARMA_ATTR_CAST_SEP_26_2009_0348PM)
  6. #define SPIRIT_KARMA_ATTR_CAST_SEP_26_2009_0348PM
  7. #if defined(_MSC_VER)
  8. #pragma once
  9. #endif
  10. #include <boost/spirit/home/karma/meta_compiler.hpp>
  11. #include <boost/spirit/home/karma/generator.hpp>
  12. #include <boost/spirit/home/karma/domain.hpp>
  13. #include <boost/spirit/home/support/unused.hpp>
  14. #include <boost/spirit/home/support/info.hpp>
  15. #include <boost/spirit/home/support/common_terminals.hpp>
  16. #include <boost/spirit/home/karma/detail/attributes.hpp>
  17. #include <boost/spirit/home/support/auxiliary/attr_cast.hpp>
  18. namespace boost { namespace spirit
  19. {
  20. ///////////////////////////////////////////////////////////////////////////
  21. // enables attr_cast<>() pseudo generator
  22. template <typename Expr, typename Exposed, typename Transformed>
  23. struct use_terminal<karma::domain
  24. , tag::stateful_tag<Expr, tag::attr_cast, Exposed, Transformed> >
  25. : mpl::true_ {};
  26. }}
  27. namespace boost { namespace spirit { namespace karma
  28. {
  29. #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
  30. using spirit::attr_cast;
  31. #endif
  32. ///////////////////////////////////////////////////////////////////////////
  33. // attr_cast_generator consumes the attribute of subject generator without
  34. // generating anything
  35. ///////////////////////////////////////////////////////////////////////////
  36. template <typename Exposed, typename Transformed, typename Subject>
  37. struct attr_cast_generator
  38. : unary_generator<attr_cast_generator<Exposed, Transformed, Subject> >
  39. {
  40. typedef typename result_of::compile<karma::domain, Subject>::type
  41. subject_type;
  42. typedef mpl::int_<subject_type::properties::value> properties;
  43. typedef typename mpl::eval_if<
  44. traits::not_is_unused<Transformed>
  45. , mpl::identity<Transformed>
  46. , traits::attribute_of<subject_type> >::type
  47. transformed_attribute_type;
  48. attr_cast_generator(Subject const& subject)
  49. : subject(subject)
  50. {
  51. // If you got an error_invalid_expression error message here,
  52. // then the expression (Subject) is not a valid spirit karma
  53. // expression.
  54. BOOST_SPIRIT_ASSERT_MATCH(karma::domain, Subject);
  55. }
  56. // If Exposed is given, we use the given type, otherwise all we can do
  57. // is to guess, so we expose our inner type as an attribute and
  58. // deal with the passed attribute inside the parse function.
  59. template <typename Context, typename Unused>
  60. struct attribute
  61. : mpl::if_<traits::not_is_unused<Exposed>, Exposed
  62. , transformed_attribute_type>
  63. {};
  64. template <typename OutputIterator, typename Context, typename Delimiter
  65. , typename Attribute>
  66. bool generate(OutputIterator& sink, Context& ctx, Delimiter const& d
  67. , Attribute const& attr) const
  68. {
  69. typedef traits::transform_attribute<
  70. Attribute const, transformed_attribute_type, domain>
  71. transform;
  72. return compile<karma::domain>(subject).generate(
  73. sink, ctx, d, transform::pre(attr));
  74. }
  75. template <typename Context>
  76. info what(Context& context) const
  77. {
  78. return info("attr_cast"
  79. , compile<karma::domain>(subject).what(context));
  80. }
  81. Subject subject;
  82. };
  83. ///////////////////////////////////////////////////////////////////////////
  84. // Generator generator: make_xxx function (objects)
  85. ///////////////////////////////////////////////////////////////////////////
  86. template <typename Expr, typename Exposed, typename Transformed
  87. , typename Modifiers>
  88. struct make_primitive<
  89. tag::stateful_tag<Expr, tag::attr_cast, Exposed, Transformed>, Modifiers>
  90. {
  91. typedef attr_cast_generator<Exposed, Transformed, Expr> result_type;
  92. template <typename Terminal>
  93. result_type operator()(Terminal const& term, unused_type) const
  94. {
  95. typedef tag::stateful_tag<
  96. Expr, tag::attr_cast, Exposed, Transformed> tag_type;
  97. using spirit::detail::get_stateful_data;
  98. return result_type(get_stateful_data<tag_type>::call(term));
  99. }
  100. };
  101. }}}
  102. #endif