custom_string.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright (c) 2001-2010 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. // This example demonstrates the steps needed to integrate a custom container
  6. // type to be usable with Spirit.Qi. It shows how to utilize the QString data
  7. // type as a Qi attribute.
  8. // Note: you need to have Qt4 installed on your system and you have to make
  9. // sure your compiler finds the includes and your linker finds the
  10. // proper libraries.
  11. #include <boost/config/warning_disable.hpp>
  12. #include <boost/spirit/include/qi.hpp>
  13. #include <Qt/qstring.h>
  14. namespace boost { namespace spirit { namespace traits
  15. {
  16. // Make Qi recognize QString as a container
  17. template <> struct is_container<QString> : mpl::true_ {};
  18. // Expose the container's (QString's) value_type
  19. template <> struct container_value<QString> : mpl::identity<QChar> {};
  20. // Define how to insert a new element at the end of the container (QString)
  21. template <>
  22. struct push_back_container<QString, QChar>
  23. {
  24. static bool call(QString& c, QChar const& val)
  25. {
  26. c.append(val);
  27. return true;
  28. }
  29. };
  30. // Test if a QString is empty (required for debug)
  31. template <>
  32. struct is_empty_container<QString>
  33. {
  34. static bool call(QString const& c)
  35. {
  36. return c.isEmpty();
  37. }
  38. };
  39. // Define how to stream a QString (required for debug)
  40. template <typename Out, typename Enable>
  41. struct print_attribute_debug<Out, QString, Enable>
  42. {
  43. static void call(Out& out, QString const& val)
  44. {
  45. out << val.toStdString();
  46. }
  47. };
  48. }}}
  49. ///////////////////////////////////////////////////////////////////////////////
  50. namespace client
  51. {
  52. template <typename Iterator>
  53. bool parse_qstring(Iterator first, Iterator last, QString& t)
  54. {
  55. using boost::spirit::qi::char_;
  56. using boost::spirit::ascii::space;
  57. using boost::spirit::qi::phrase_parse;
  58. bool r = phrase_parse(first, last, +char_, space, t);
  59. if (!r || first != last) // fail if we did not get a full match
  60. return false;
  61. return r;
  62. }
  63. }
  64. ///////////////////////////////////////////////////////////////////////////////
  65. int main()
  66. {
  67. std::cout << "/////////////////////////////////////////////////////////\n\n";
  68. std::cout << "\t\tParsing into a QString from Spirit...\n\n";
  69. std::cout << "/////////////////////////////////////////////////////////\n\n";
  70. std::cout << "Give me a complex number of the form r or (r) or (r,i) \n";
  71. std::cout << "Type [q or Q] to quit\n\n";
  72. std::string str;
  73. while (getline(std::cin, str))
  74. {
  75. if (str.empty() || str[0] == 'q' || str[0] == 'Q')
  76. break;
  77. QString t;
  78. if (client::parse_qstring(str.begin(), str.end(), t))
  79. {
  80. std::cout << "-------------------------\n";
  81. std::cout << "Parsing succeeded\n";
  82. std::cout << "got: " << t.toStdString() << std::endl;
  83. std::cout << "\n-------------------------\n";
  84. }
  85. else
  86. {
  87. std::cout << "-------------------------\n";
  88. std::cout << "Parsing failed\n";
  89. std::cout << "-------------------------\n";
  90. }
  91. }
  92. std::cout << "Bye... :-) \n\n";
  93. return 0;
  94. }