]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/example/qi/custom_string.cpp
1 // Copyright (c) 2001-2010 Hartmut Kaiser
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)
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.
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
14 #include <boost/spirit/include/qi.hpp>
16 #include <Qt/qstring.h>
18 namespace boost
{ namespace spirit
{ namespace traits
20 // Make Qi recognize QString as a container
21 template <> struct is_container
<QString
> : mpl::true_
{};
23 // Expose the container's (QString's) value_type
24 template <> struct container_value
<QString
> : mpl::identity
<QChar
> {};
26 // Define how to insert a new element at the end of the container (QString)
28 struct push_back_container
<QString
, QChar
>
30 static bool call(QString
& c
, QChar
const& val
)
37 // Test if a QString is empty (required for debug)
39 struct is_empty_container
<QString
>
41 static bool call(QString
const& c
)
47 // Define how to stream a QString (required for debug)
48 template <typename Out
, typename Enable
>
49 struct print_attribute_debug
<Out
, QString
, Enable
>
51 static void call(Out
& out
, QString
const& val
)
53 out
<< val
.toStdString();
58 ///////////////////////////////////////////////////////////////////////////////
61 template <typename Iterator
>
62 bool parse_qstring(Iterator first
, Iterator last
, QString
& t
)
64 using boost::spirit::qi::char_
;
65 using boost::spirit::ascii::space
;
66 using boost::spirit::qi::phrase_parse
;
68 bool r
= phrase_parse(first
, last
, +char_
, space
, t
);
69 if (!r
|| first
!= last
) // fail if we did not get a full match
76 ///////////////////////////////////////////////////////////////////////////////
79 std::cout
<< "/////////////////////////////////////////////////////////\n\n";
80 std::cout
<< "\t\tParsing into a QString from Spirit...\n\n";
81 std::cout
<< "/////////////////////////////////////////////////////////\n\n";
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";
87 while (getline(std::cin
, str
))
89 if (str
.empty() || str
[0] == 'q' || str
[0] == 'Q')
93 if (client::parse_qstring(str
.begin(), str
.end(), t
))
95 std::cout
<< "-------------------------\n";
96 std::cout
<< "Parsing succeeded\n";
97 std::cout
<< "got: " << t
.toStdString() << std::endl
;
98 std::cout
<< "\n-------------------------\n";
102 std::cout
<< "-------------------------\n";
103 std::cout
<< "Parsing failed\n";
104 std::cout
<< "-------------------------\n";
108 std::cout
<< "Bye... :-) \n\n";