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

source code of boost/libs/spirit/example/qi/custom_string.cpp