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/spirit/include/support_adapt_adt_attributes.hpp>
8
9#include <boost/spirit/include/karma.hpp>
10
11#include <boost/core/lightweight_test.hpp>
12#include <boost/assert.hpp>
13#include <boost/core/ignore_unused.hpp>
14#include <boost/fusion/include/adapt_adt.hpp>
15
16#include "test.hpp"
17
18///////////////////////////////////////////////////////////////////////////////
19class data1
20{
21private:
22 int width_;
23 int height_;
24
25public:
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
41BOOST_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///////////////////////////////////////////////////////////////////////////////
48class data2
49{
50private:
51 std::string data_;
52
53public:
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
66BOOST_FUSION_ADAPT_ADT(
67 data2,
68 (std::string, std::string const&, obj.data(), obj.set_data(val))
69)
70
71///////////////////////////////////////////////////////////////////////////////
72class data3
73{
74private:
75 double data_;
76
77public:
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
86BOOST_FUSION_ADAPT_ADT(
87 data3,
88 (double, double, obj.data(), obj.set_data(val))
89)
90
91///////////////////////////////////////////////////////////////////////////////
92class data4
93{
94public:
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#define NO_SETTER (BOOST_ASSERT_MSG(false, "unused setter called"), \
105 boost::ignore_unused(obj, val))
106
107BOOST_FUSION_ADAPT_ADT(
108 data4,
109 (boost::optional<int>, boost::optional<int> const&, obj.a(), NO_SETTER)
110 (boost::optional<double>, boost::optional<double> const&, obj.b(), NO_SETTER)
111 (boost::optional<std::string>, boost::optional<std::string> const&, obj.c(), NO_SETTER)
112)
113
114#undef NO_SETTER
115
116///////////////////////////////////////////////////////////////////////////////
117int main ()
118{
119 using spirit_test::test;
120
121 {
122 using boost::spirit::karma::int_;
123
124 data1 b(800, 600);
125 BOOST_TEST(test("width: 800\nheight: 600\n",
126 "width: " << int_ << "\n" << "height: " << int_ << "\n", b));
127 }
128
129 {
130 using boost::spirit::karma::char_;
131 using boost::spirit::karma::string;
132
133 data2 d("test");
134 BOOST_TEST(test("data: test\n", "data: " << +char_ << "\n", d));
135 BOOST_TEST(test("data: test\n", "data: " << string << "\n", d));
136 }
137
138 {
139 using boost::spirit::karma::double_;
140
141 BOOST_TEST(test("x=0.0\n", "x=" << double_ << "\n", data3(0)));
142 BOOST_TEST(test("x=1.1\n", "x=" << double_ << "\n", data3(1.1)));
143 BOOST_TEST(test("x=1.0e10\n", "x=" << double_ << "\n", data3(1e10)));
144
145#if defined(_MSC_VER) && _MSC_VER < 1900
146# pragma warning(push)
147# pragma warning(disable: 4127) // conditional expression is constant
148#endif
149 BOOST_TEST(test("x=inf\n", "x=" << double_ << "\n",
150 data3(std::numeric_limits<double>::infinity())));
151 if (std::numeric_limits<double>::has_quiet_NaN) {
152 BOOST_TEST(test("x=nan\n", "x=" << double_ << "\n",
153 data3(std::numeric_limits<double>::quiet_NaN())));
154 }
155 if (std::numeric_limits<double>::has_signaling_NaN) {
156 BOOST_TEST(test("x=nan\n", "x=" << double_ << "\n",
157 data3(std::numeric_limits<double>::signaling_NaN())));
158 }
159#if defined(_MSC_VER) && _MSC_VER < 1900
160# pragma warning(pop)
161#endif
162 }
163
164 {
165 using boost::spirit::karma::double_;
166 using boost::spirit::karma::int_;
167 using boost::spirit::karma::string;
168
169 data4 d;
170 d.b_ = 10;
171
172 BOOST_TEST(test(
173 "Testing: b: 10.0\n",
174 "Testing: " << -("a: " << int_ << "\n")
175 << -("b: " << double_ << "\n")
176 << -("c: " << string << "\n"), d));
177 }
178
179 return boost::report_errors();
180}
181

source code of boost/libs/spirit/test/karma/regression_adapt_adt.cpp