Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / spirit / test / karma / regression_adapt_adt.cpp
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:
26     data1()
27       : width_(400), height_(400) 
28     {}
29
30     data1(int width, int height)
31       : width_(width), height_(height) 
32     {}
33
34     int width() const { return width_;}
35     int height() const { return height_;}
36
37     void set_width(int width) { width_ = width;}
38     void set_height(int height) { height_ = height;}
39 };
40
41 BOOST_FUSION_ADAPT_ADT(
42     data1,
43     (int, int, obj.width(),  obj.set_width(val))
44     (int, int, obj.height(), obj.set_height(val))
45 )
46
47 ///////////////////////////////////////////////////////////////////////////////
48 class data2
49 {
50 private:
51     std::string data_;
52
53 public:
54     data2()
55       : data_("test")
56     {}
57
58     data2(std::string const& data)
59       : data_(data)
60     {}
61
62     std::string const& data() const { return 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:
78     data3(double data = 0.0)
79       : data_(data) 
80     {}
81
82     double data() const { return data_;}
83     void set_data(double data) { data_ = data;}
84 };
85
86 BOOST_FUSION_ADAPT_ADT(
87     data3,
88     (double, double, 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
99     boost::optional<int> const& a() const { return a_; }
100     boost::optional<double> const& b() const { return b_; }
101     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 ///////////////////////////////////////////////////////////////////////////////
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 #if defined(_MSC_VER) && _MSC_VER < 1900
142 # pragma warning(push)
143 # pragma warning(disable: 4127) // conditional expression is constant
144 #endif
145         BOOST_TEST(test("x=inf\n", "x=" << double_ << "\n", 
146             data3(std::numeric_limits<double>::infinity())));
147         if (std::numeric_limits<double>::has_quiet_NaN) {
148             BOOST_TEST(test("x=nan\n", "x=" << double_ << "\n", 
149                 data3(std::numeric_limits<double>::quiet_NaN())));
150         }
151         if (std::numeric_limits<double>::has_signaling_NaN) {
152             BOOST_TEST(test("x=nan\n", "x=" << double_ << "\n", 
153                 data3(std::numeric_limits<double>::signaling_NaN())));
154         }
155 #if defined(_MSC_VER) && _MSC_VER < 1900
156 # pragma warning(pop)
157 #endif
158     }
159
160     {
161         using boost::spirit::karma::double_;
162         using boost::spirit::karma::int_;
163         using boost::spirit::karma::string;
164
165         data4 d;
166         d.b_ = 10;
167
168         BOOST_TEST(test(
169             "Testing: b: 10.0\n", 
170             "Testing: " << -("a: " << int_ << "\n")
171                         << -("b: " << double_ << "\n")
172                         << -("c: " << string << "\n"), d));
173     }
174
175     return boost::report_errors();
176 }