]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/test/qi/regression_adapt_adt.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / spirit / test / qi / regression_adapt_adt.cpp
1 // Copyright (c) 2011 Roji Philip
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
7 #include <boost/config/warning_disable.hpp>
8 #include <boost/detail/lightweight_test.hpp>
9
10 #include <boost/fusion/include/adapt_adt.hpp>
11 #include <boost/optional.hpp>
12
13 #include <boost/spirit/include/qi.hpp>
14 #include <boost/spirit/include/support_adapt_adt_attributes.hpp>
15
16 #include "test.hpp"
17
18 ///////////////////////////////////////////////////////////////////////////////
19 struct test1
20 {
21 unsigned var;
22 boost::optional<unsigned> opt;
23
24 unsigned& getvar() { return var; }
25 unsigned const& getvar() const { return var; }
26 void setvar(unsigned val) { var = val; }
27
28 boost::optional<unsigned>& getopt() { return opt; }
29 boost::optional<unsigned> const& getopt() const { return opt; }
30 void setopt(boost::optional<unsigned> const& val) { opt = val; }
31 };
32
33 BOOST_FUSION_ADAPT_ADT(
34 test1,
35 (unsigned&, unsigned const&, obj.getvar(), obj.setvar(val))
36 (boost::optional<unsigned>&, boost::optional<unsigned> const&,
37 obj.getopt(), obj.setopt(val))
38 )
39
40 ///////////////////////////////////////////////////////////////////////////////
41 struct test2
42 {
43 std::string str;
44 boost::optional<std::string> optstr;
45
46 std::string& getstring() { return str; }
47 std::string const& getstring() const { return str; }
48 void setstring(std::string const& val) { str = val; }
49
50 boost::optional<std::string>& getoptstring() { return optstr; }
51 boost::optional<std::string> const& getoptstring() const { return optstr; }
52 void setoptstring(boost::optional<std::string> const& val) { optstr = val; }
53 };
54
55 BOOST_FUSION_ADAPT_ADT(
56 test2,
57 (std::string&, std::string const&, obj.getstring(), obj.setstring(val))
58 (boost::optional<std::string>&, boost::optional<std::string> const&,
59 obj.getoptstring(), obj.setoptstring(val))
60 )
61
62 ///////////////////////////////////////////////////////////////////////////////
63 int main()
64 {
65 using spirit_test::test_attr;
66 namespace qi = boost::spirit::qi;
67
68 {
69 test1 data;
70 BOOST_TEST(test_attr("123@999", qi::uint_ >> -('@' >> qi::uint_), data) &&
71 data.var == 123 && data.opt && data.opt.get() == 999);
72 }
73
74 {
75 test1 data;
76 BOOST_TEST(test_attr("123", qi::uint_ >> -('@' >> qi::uint_), data) &&
77 data.var == 123 && !data.opt);
78 }
79
80 {
81 test2 data;
82 BOOST_TEST(test_attr("Hello:OptionalHello",
83 +qi::alnum >> -(':' >> +qi::alnum), data) &&
84 data.str == "Hello" &&
85 data.optstr && data.optstr.get() == "OptionalHello");
86 }
87
88 {
89 test2 data;
90 BOOST_TEST(test_attr("Hello",
91 +qi::alnum >> -(':' >> +qi::alnum), data) &&
92 data.str == "Hello" && !data.optstr);
93 }
94
95 return boost::report_errors();
96 }