1 //  Copyright (c) 2001-2011 Hartmut Kaiser
2 //  Copyright (c) 2011 Colin Rundel
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/mpl/print.hpp>
8 #include <boost/config/warning_disable.hpp>
9 #include <boost/detail/lightweight_test.hpp>
10 
11 #include <boost/fusion/include/adapt_adt.hpp>
12 
13 #include <boost/spirit/include/karma.hpp>
14 #include <boost/spirit/include/support_adapt_adt_attributes.hpp>
15 
16 #include "test.hpp"
17 
18 ///////////////////////////////////////////////////////////////////////////////
19 class data1
20 {
21 private:
22     int width_;
23     int height_;
24 
25 public:
data1()26     data1()
27       : width_(400), height_(400)
28     {}
29 
data1(int width,int height)30     data1(int width, int height)
31       : width_(width), height_(height)
32     {}
33 
width() const34     int const& width() const { return width_;}
height() const35     int const& height() const { return height_;}
36 
set_width(int width)37     void set_width(int width) { width_ = width;}
set_height(int height)38     void set_height(int height) { height_ = height;}
39 };
40 
41 BOOST_FUSION_ADAPT_ADT(
42     data1,
43     (int, int const&, obj.width(),  obj.set_width(val))
44     (int, int const&, obj.height(), obj.set_height(val))
45 );
46 
47 ///////////////////////////////////////////////////////////////////////////////
48 class data2
49 {
50 private:
51     std::string data_;
52 
53 public:
data2()54     data2()
55       : data_("test")
56     {}
57 
data2(std::string const & data)58     data2(std::string const& data)
59       : data_(data)
60     {}
61 
data() const62     std::string const& data() const { return data_;}
set_data(std::string const & data)63     void set_data(std::string const& data) { data_ = data;}
64 };
65 
66 BOOST_FUSION_ADAPT_ADT(
67     data2,
68     (std::string, std::string const&, obj.data(), obj.set_data(val))
69 );
70 
71 ///////////////////////////////////////////////////////////////////////////////
72 class data3
73 {
74 private:
75     double data_;
76 
77 public:
data3(double data=0.0)78     data3(double data = 0.0)
79       : data_(data)
80     {}
81 
data() const82     double const& data() const { return data_;}
set_data(double data)83     void set_data(double data) { data_ = data;}
84 };
85 
86 BOOST_FUSION_ADAPT_ADT(
87     data3,
88     (double, double const&, obj.data(), obj.set_data(val))
89 );
90 
91 ///////////////////////////////////////////////////////////////////////////////
92 class data4
93 {
94 public:
95     boost::optional<int> a_;
96     boost::optional<double> b_;
97     boost::optional<std::string> c_;
98 
a() const99     boost::optional<int> const& a() const { return a_; }
b() const100     boost::optional<double> const& b() const { return b_; }
c() const101     boost::optional<std::string> const& c() const { return c_; }
102 };
103 
104 
105 BOOST_FUSION_ADAPT_ADT(
106     data4,
107     (boost::optional<int>, boost::optional<int> const&, obj.a(), /**/)
108     (boost::optional<double>, boost::optional<double> const&, obj.b(), /**/)
109     (boost::optional<std::string>, boost::optional<std::string> const&, obj.c(), /**/)
110 );
111 
112 ///////////////////////////////////////////////////////////////////////////////
main()113 int main ()
114 {
115     using spirit_test::test;
116 
117     {
118         using boost::spirit::karma::int_;
119 
120         data1 b(800, 600);
121         BOOST_TEST(test("width: 800\nheight: 600\n",
122             "width: " << int_ << "\n" << "height: " << int_ << "\n", b));
123     }
124 
125     {
126         using boost::spirit::karma::char_;
127         using boost::spirit::karma::string;
128 
129         data2 d("test");
130         BOOST_TEST(test("data: test\n", "data: " << +char_ << "\n", d));
131         BOOST_TEST(test("data: test\n", "data: " << string << "\n", d));
132     }
133 
134     {
135         using boost::spirit::karma::double_;
136 
137         BOOST_TEST(test("x=0.0\n", "x=" << double_ << "\n", data3(0)));
138         BOOST_TEST(test("x=1.1\n", "x=" << double_ << "\n", data3(1.1)));
139         BOOST_TEST(test("x=1.0e10\n", "x=" << double_ << "\n", data3(1e10)));
140 
141         BOOST_TEST(test("x=inf\n", "x=" << double_ << "\n",
142             data3(std::numeric_limits<double>::infinity())));
143         if (std::numeric_limits<double>::has_quiet_NaN) {
144             BOOST_TEST(test("x=nan\n", "x=" << double_ << "\n",
145                 data3(std::numeric_limits<double>::quiet_NaN())));
146         }
147         if (std::numeric_limits<double>::has_signaling_NaN) {
148             BOOST_TEST(test("x=nan\n", "x=" << double_ << "\n",
149                 data3(std::numeric_limits<double>::signaling_NaN())));
150         }
151     }
152 
153     {
154         using boost::spirit::karma::double_;
155         using boost::spirit::karma::int_;
156         using boost::spirit::karma::string;
157 
158         data4 d;
159         d.b_ = 10;
160 
161         BOOST_TEST(test(
162             "Testing: b: 10.0\n",
163             "Testing: " << -("a: " << int_ << "\n")
164                         << -("b: " << double_ << "\n")
165                         << -("c: " << string << "\n"), d));
166     }
167 
168     return boost::report_errors();
169 }
170