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