generator_binder.hpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright (c) 2001-2011 Joel de Guzman
  2. // Copyright (c) 2001-2011 Hartmut Kaiser
  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(BOOST_SPIRIT_GENERATOR_BINDER_APR_17_2009_0952PM)
  7. #define BOOST_SPIRIT_GENERATOR_BINDER_APR_17_2009_0952PM
  8. #if defined(_MSC_VER)
  9. #pragma once
  10. #endif
  11. #include <boost/fusion/include/at.hpp>
  12. #include <boost/mpl/bool.hpp>
  13. #include <boost/spirit/home/support/has_semantic_action.hpp>
  14. namespace boost { namespace spirit { namespace karma { namespace detail
  15. {
  16. // generator_binder for plain rules
  17. template <typename Generator, typename Auto>
  18. struct generator_binder
  19. {
  20. generator_binder(Generator const& g)
  21. : g(g) {}
  22. template <typename OutputIterator, typename Delimiter, typename Context>
  23. bool call(OutputIterator& sink, Context& context
  24. , Delimiter const& delim, mpl::true_) const
  25. {
  26. // If DeducedAuto is false (semantic actions is present), the
  27. // component's attribute is unused.
  28. return g.generate(sink, context, delim, unused);
  29. }
  30. template <typename OutputIterator, typename Delimiter, typename Context>
  31. bool call(OutputIterator& sink, Context& context
  32. , Delimiter const& delim, mpl::false_) const
  33. {
  34. // If DeducedAuto is true (no semantic action), we pass the rule's
  35. // attribute on to the component.
  36. return g.generate(sink, context, delim
  37. , fusion::at_c<0>(context.attributes));
  38. }
  39. template <typename OutputIterator, typename Delimiter, typename Context>
  40. bool operator()(OutputIterator& sink, Context& context
  41. , Delimiter const& delim) const
  42. {
  43. // If Auto is false, we need to deduce whether to apply auto rule
  44. typedef typename traits::has_semantic_action<Generator>::type auto_rule;
  45. return call(sink, context, delim, auto_rule());
  46. }
  47. Generator g;
  48. };
  49. // generator_binder for auto rules
  50. template <typename Generator>
  51. struct generator_binder<Generator, mpl::true_>
  52. {
  53. generator_binder(Generator const& g)
  54. : g(g) {}
  55. template <typename OutputIterator, typename Delimiter, typename Context>
  56. bool operator()(OutputIterator& sink, Context& context
  57. , Delimiter const& delim) const
  58. {
  59. // If Auto is true, the component's attribute is unused.
  60. return g.generate(sink, context, delim
  61. , fusion::at_c<0>(context.attributes));
  62. }
  63. Generator g;
  64. };
  65. template <typename Auto, typename Generator>
  66. inline generator_binder<Generator, Auto>
  67. bind_generator(Generator const& g)
  68. {
  69. return generator_binder<Generator, Auto>(g);
  70. }
  71. }}}}
  72. #endif