attr_cast.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. using spirit::attr_cast;
  30. ///////////////////////////////////////////////////////////////////////////
  31. // attr_cast_generator consumes the attribute of subject generator without
  32. // generating anything
  33. ///////////////////////////////////////////////////////////////////////////
  34. template <typename Exposed, typename Transformed, typename Subject>
  35. struct attr_cast_generator
  36. : unary_generator<attr_cast_generator<Exposed, Transformed, Subject> >
  37. {
  38. typedef typename result_of::compile<karma::domain, Subject>::type
  39. subject_type;
  40. typedef mpl::int_<subject_type::properties::value> properties;
  41. typedef typename mpl::eval_if<
  42. traits::not_is_unused<Transformed>
  43. , mpl::identity<Transformed>
  44. , traits::attribute_of<subject_type> >::type
  45. transformed_attribute_type;
  46. attr_cast_generator(Subject const& subject)
  47. : subject(subject)
  48. {
  49. // If you got an error_invalid_expression error message here,
  50. // then the expression (Subject) is not a valid spirit karma
  51. // expression.
  52. BOOST_SPIRIT_ASSERT_MATCH(karma::domain, Subject);
  53. }
  54. // If Exposed is given, we use the given type, otherwise all we can do
  55. // is to guess, so we expose our inner type as an attribute and
  56. // deal with the passed attribute inside the parse function.
  57. template <typename Context, typename Unused>
  58. struct attribute
  59. : mpl::if_<traits::not_is_unused<Exposed>, Exposed
  60. , transformed_attribute_type>
  61. {};
  62. template <typename OutputIterator, typename Context, typename Delimiter
  63. , typename Attribute>
  64. bool generate(OutputIterator& sink, Context& ctx, Delimiter const& d
  65. , Attribute const& attr) const
  66. {
  67. typedef traits::transform_attribute<
  68. Attribute const, transformed_attribute_type, domain>
  69. transform;
  70. return compile<karma::domain>(subject).generate(
  71. sink, ctx, d, transform::pre(attr));
  72. }
  73. template <typename Context>
  74. info what(Context& context) const
  75. {
  76. return info("attr_cast"
  77. , compile<karma::domain>(subject).what(context));
  78. }
  79. Subject subject;
  80. };
  81. ///////////////////////////////////////////////////////////////////////////
  82. // Generator generator: make_xxx function (objects)
  83. ///////////////////////////////////////////////////////////////////////////
  84. template <typename Expr, typename Exposed, typename Transformed
  85. , typename Modifiers>
  86. struct make_primitive<
  87. tag::stateful_tag<Expr, tag::attr_cast, Exposed, Transformed>, Modifiers>
  88. {
  89. typedef attr_cast_generator<Exposed, Transformed, Expr> result_type;
  90. template <typename Terminal>
  91. result_type operator()(Terminal const& term, unused_type) const
  92. {
  93. typedef tag::stateful_tag<
  94. Expr, tag::attr_cast, Exposed, Transformed> tag_type;
  95. using spirit::detail::get_stateful_data;
  96. return result_type(get_stateful_data<tag_type>::call(term));
  97. }
  98. };
  99. }}}
  100. #endif