regression_container_variant_sequence.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright (c) 2001-2012 Hartmut Kaiser
  2. // Copyright (c) 2012 Benjamin Schindler
  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. #include <boost/config/warning_disable.hpp>
  7. #include <boost/detail/lightweight_test.hpp>
  8. #include <boost/spirit/include/karma.hpp>
  9. #include <boost/fusion/include/adapt_struct.hpp>
  10. #include <set>
  11. namespace generator
  12. {
  13. struct Enum
  14. {
  15. std::string enumName;
  16. std::vector<std::string> enumEntries;
  17. };
  18. typedef boost::variant<std::string, Enum> VariantType;
  19. namespace karma = boost::spirit::karma;
  20. // Our grammar definition
  21. template<typename Iterator>
  22. struct SettingsHeaderGenerator: karma::grammar<Iterator, VariantType()>
  23. {
  24. SettingsHeaderGenerator() : SettingsHeaderGenerator::base_type(baseRule)
  25. {
  26. using karma::lit;
  27. using karma::string;
  28. enumRule = lit("enum ") << string << lit("\n{\n") << string % ",\n" << "}";
  29. declarationRule = lit("class ") << string << ';';
  30. baseRule = (declarationRule | enumRule) << lit("\n");
  31. baseRule.name("base");
  32. enumRule.name("enum");
  33. declarationRule.name("declaration");
  34. }
  35. karma::rule<Iterator, std::string()> declarationRule;
  36. karma::rule<Iterator, Enum()> enumRule;
  37. karma::rule<Iterator, VariantType()> baseRule;
  38. };
  39. template <typename OutputIterator>
  40. bool generate_header(OutputIterator& sink, VariantType& data)
  41. {
  42. SettingsHeaderGenerator<OutputIterator> generator;
  43. return karma::generate(sink, generator, data);
  44. }
  45. }
  46. BOOST_FUSION_ADAPT_STRUCT(generator::Enum,
  47. (std::string, enumName)
  48. (std::vector<std::string>, enumEntries)
  49. )
  50. int main()
  51. {
  52. generator::VariantType variant = "bla";
  53. std::string generated;
  54. std::back_insert_iterator<std::string> sink(generated);
  55. BOOST_TEST(generator::generate_header(sink, variant));
  56. BOOST_TEST(generated == "class bla;\n");
  57. return boost::report_errors();
  58. }