• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <boost/assert.hpp>
17 #include <boost/core/ignore_unused.hpp>
18 
19 #include "test.hpp"
20 
21 ///////////////////////////////////////////////////////////////////////////////
22 class data1
23 {
24 private:
25     int width_;
26     int height_;
27 
28 public:
data1()29     data1()
30       : width_(400), height_(400)
31     {}
32 
data1(int width,int height)33     data1(int width, int height)
34       : width_(width), height_(height)
35     {}
36 
width() const37     int width() const { return width_;}
height() const38     int height() const { return height_;}
39 
set_width(int width)40     void set_width(int width) { width_ = width;}
set_height(int height)41     void set_height(int height) { height_ = height;}
42 };
43 
44 BOOST_FUSION_ADAPT_ADT(
45     data1,
46     (int, int, obj.width(),  obj.set_width(val))
47     (int, int, obj.height(), obj.set_height(val))
48 )
49 
50 ///////////////////////////////////////////////////////////////////////////////
51 class data2
52 {
53 private:
54     std::string data_;
55 
56 public:
data2()57     data2()
58       : data_("test")
59     {}
60 
data2(std::string const & data)61     data2(std::string const& data)
62       : data_(data)
63     {}
64 
data() const65     std::string const& data() const { return data_;}
set_data(std::string const & data)66     void set_data(std::string const& data) { data_ = data;}
67 };
68 
69 BOOST_FUSION_ADAPT_ADT(
70     data2,
71     (std::string, std::string const&, obj.data(), obj.set_data(val))
72 )
73 
74 ///////////////////////////////////////////////////////////////////////////////
75 class data3
76 {
77 private:
78     double data_;
79 
80 public:
data3(double data=0.0)81     data3(double data = 0.0)
82       : data_(data)
83     {}
84 
data() const85     double data() const { return data_;}
set_data(double data)86     void set_data(double data) { data_ = data;}
87 };
88 
89 BOOST_FUSION_ADAPT_ADT(
90     data3,
91     (double, double, obj.data(), obj.set_data(val))
92 )
93 
94 ///////////////////////////////////////////////////////////////////////////////
95 class data4
96 {
97 public:
98     boost::optional<int> a_;
99     boost::optional<double> b_;
100     boost::optional<std::string> c_;
101 
a() const102     boost::optional<int> const& a() const { return a_; }
b() const103     boost::optional<double> const& b() const { return b_; }
c() const104     boost::optional<std::string> const& c() const { return c_; }
105 };
106 
107 #define NO_SETTER (BOOST_ASSERT_MSG(false, "unused setter called"), \
108                    boost::ignore_unused(obj, val))
109 
110 BOOST_FUSION_ADAPT_ADT(
111     data4,
112     (boost::optional<int>, boost::optional<int> const&, obj.a(), NO_SETTER)
113     (boost::optional<double>, boost::optional<double> const&, obj.b(), NO_SETTER)
114     (boost::optional<std::string>, boost::optional<std::string> const&, obj.c(), NO_SETTER)
115 )
116 
117 #undef NO_SETTER
118 
119 ///////////////////////////////////////////////////////////////////////////////
main()120 int main ()
121 {
122     using spirit_test::test;
123 
124     {
125         using boost::spirit::karma::int_;
126 
127         data1 b(800, 600);
128         BOOST_TEST(test("width: 800\nheight: 600\n",
129             "width: " << int_ << "\n" << "height: " << int_ << "\n", b));
130     }
131 
132     {
133         using boost::spirit::karma::char_;
134         using boost::spirit::karma::string;
135 
136         data2 d("test");
137         BOOST_TEST(test("data: test\n", "data: " << +char_ << "\n", d));
138         BOOST_TEST(test("data: test\n", "data: " << string << "\n", d));
139     }
140 
141     {
142         using boost::spirit::karma::double_;
143 
144         BOOST_TEST(test("x=0.0\n", "x=" << double_ << "\n", data3(0)));
145         BOOST_TEST(test("x=1.1\n", "x=" << double_ << "\n", data3(1.1)));
146         BOOST_TEST(test("x=1.0e10\n", "x=" << double_ << "\n", data3(1e10)));
147 
148 #if defined(_MSC_VER) && _MSC_VER < 1900
149 # pragma warning(push)
150 # pragma warning(disable: 4127) // conditional expression is constant
151 #endif
152         BOOST_TEST(test("x=inf\n", "x=" << double_ << "\n",
153             data3(std::numeric_limits<double>::infinity())));
154         if (std::numeric_limits<double>::has_quiet_NaN) {
155             BOOST_TEST(test("x=nan\n", "x=" << double_ << "\n",
156                 data3(std::numeric_limits<double>::quiet_NaN())));
157         }
158         if (std::numeric_limits<double>::has_signaling_NaN) {
159             BOOST_TEST(test("x=nan\n", "x=" << double_ << "\n",
160                 data3(std::numeric_limits<double>::signaling_NaN())));
161         }
162 #if defined(_MSC_VER) && _MSC_VER < 1900
163 # pragma warning(pop)
164 #endif
165     }
166 
167     {
168         using boost::spirit::karma::double_;
169         using boost::spirit::karma::int_;
170         using boost::spirit::karma::string;
171 
172         data4 d;
173         d.b_ = 10;
174 
175         BOOST_TEST(test(
176             "Testing: b: 10.0\n",
177             "Testing: " << -("a: " << int_ << "\n")
178                         << -("b: " << double_ << "\n")
179                         << -("c: " << string << "\n"), d));
180     }
181 
182     return boost::report_errors();
183 }
184