as.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright (c) 2001-2011 Hartmut Kaiser
  2. // Copyright (c) 2010 Bryce Lelbach
  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. #ifndef BOOST_SPIRIT_KARMA_DIRECTIVE_AS_HPP
  7. #define BOOST_SPIRIT_KARMA_DIRECTIVE_AS_HPP
  8. #if defined(_MSC_VER)
  9. #pragma once
  10. #endif
  11. #include <boost/spirit/home/karma/meta_compiler.hpp>
  12. #include <boost/spirit/home/karma/generator.hpp>
  13. #include <boost/spirit/home/karma/delimit_out.hpp>
  14. #include <boost/spirit/home/karma/domain.hpp>
  15. #include <boost/spirit/home/karma/detail/output_iterator.hpp>
  16. #include <boost/spirit/home/karma/detail/as.hpp>
  17. #include <boost/spirit/home/support/unused.hpp>
  18. #include <boost/spirit/home/support/info.hpp>
  19. #include <boost/spirit/home/support/common_terminals.hpp>
  20. #include <boost/spirit/home/support/has_semantic_action.hpp>
  21. #include <boost/spirit/home/support/handles_container.hpp>
  22. #include <boost/spirit/home/support/assert_msg.hpp>
  23. #include <boost/spirit/home/support/container.hpp>
  24. #include <boost/spirit/home/karma/detail/attributes.hpp>
  25. namespace boost { namespace spirit { namespace karma
  26. {
  27. template <typename T>
  28. struct as
  29. : stateful_tag_type<T, tag::as>
  30. {
  31. BOOST_SPIRIT_ASSERT_MSG(
  32. (traits::is_container<T>::type::value),
  33. error_type_must_be_a_container,
  34. (T));
  35. };
  36. }}}
  37. namespace boost { namespace spirit
  38. {
  39. ///////////////////////////////////////////////////////////////////////////
  40. // Enablers
  41. ///////////////////////////////////////////////////////////////////////////
  42. // enables as_string[...]
  43. template <>
  44. struct use_directive<karma::domain, tag::as_string>
  45. : mpl::true_ {};
  46. // enables as_wstring[...]
  47. template <>
  48. struct use_directive<karma::domain, tag::as_wstring>
  49. : mpl::true_ {};
  50. // enables as<T>[...]
  51. template <typename T>
  52. struct use_directive<karma::domain, tag::stateful_tag<T, tag::as> >
  53. : mpl::true_
  54. {};
  55. }}
  56. namespace boost { namespace spirit { namespace karma
  57. {
  58. #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
  59. using spirit::as_string;
  60. using spirit::as_wstring;
  61. #endif
  62. using spirit::as_string_type;
  63. using spirit::as_wstring_type;
  64. ///////////////////////////////////////////////////////////////////////////
  65. // as_directive allows to hook custom conversions to string into the
  66. // output generation process
  67. ///////////////////////////////////////////////////////////////////////////
  68. template <typename Subject, typename T>
  69. struct as_directive
  70. : unary_generator<as_directive<Subject, T> >
  71. {
  72. typedef Subject subject_type;
  73. typedef typename subject_type::properties properties;
  74. as_directive(Subject const& subject)
  75. : subject(subject) {}
  76. template <typename Context, typename Iterator>
  77. struct attribute
  78. {
  79. typedef T type;
  80. };
  81. template <typename OutputIterator, typename Context, typename Delimiter
  82. , typename Attribute>
  83. bool generate(OutputIterator& sink, Context& ctx, Delimiter const& d
  84. , Attribute const& attr) const
  85. {
  86. if (!traits::valid_as<T>(attr))
  87. return false;
  88. return subject.generate(sink, ctx, d, traits::as<T>(attr)) &&
  89. karma::delimit_out(sink, d); // always do post-delimiting
  90. }
  91. template <typename Context>
  92. info what(Context& context) const
  93. {
  94. return info("as", subject.what(context));
  95. }
  96. Subject subject;
  97. };
  98. ///////////////////////////////////////////////////////////////////////////
  99. // Generator generators: make_xxx function (objects)
  100. ///////////////////////////////////////////////////////////////////////////
  101. template <typename Subject, typename Modifiers>
  102. struct make_directive<tag::as_string, Subject, Modifiers>
  103. {
  104. typedef as_directive<Subject, std::string> result_type;
  105. result_type operator()(unused_type, Subject const& subject
  106. , unused_type) const
  107. {
  108. return result_type(subject);
  109. }
  110. };
  111. template <typename Subject, typename Modifiers>
  112. struct make_directive<tag::as_wstring, Subject, Modifiers>
  113. {
  114. typedef as_directive<Subject, std::basic_string<wchar_t> > result_type;
  115. result_type operator()(unused_type, Subject const& subject
  116. , unused_type) const
  117. {
  118. return result_type(subject);
  119. }
  120. };
  121. template <typename T, typename Subject, typename Modifiers>
  122. struct make_directive<tag::stateful_tag<T, tag::as>, Subject, Modifiers>
  123. {
  124. typedef as_directive<Subject, T> result_type;
  125. result_type operator()(unused_type, Subject const& subject
  126. , unused_type) const
  127. {
  128. return result_type(subject);
  129. }
  130. };
  131. }}}
  132. namespace boost { namespace spirit { namespace traits
  133. {
  134. ///////////////////////////////////////////////////////////////////////////
  135. template <typename Subject, typename T>
  136. struct has_semantic_action<karma::as_directive<Subject, T> >
  137. : unary_has_semantic_action<Subject> {};
  138. ///////////////////////////////////////////////////////////////////////////
  139. template <typename Subject, typename T, typename Attribute
  140. , typename Context, typename Iterator>
  141. struct handles_container<karma::as_directive<Subject, T>, Attribute
  142. , Context, Iterator>
  143. : mpl::false_ {}; // always dereference attribute if used in sequences
  144. }}}
  145. #endif