1 //-----------------------------------------------------------------------------
2 // boost-libs variant/test/recursive_variant_test.cpp source file
3 // See http://www.boost.org for updates, documentation, and revision history.
4 //-----------------------------------------------------------------------------
7 // Eric Friedman, Itay Maman
9 // Distributed under the Boost Software License, Version 1.0. (See
10 // accompanying file LICENSE_1_0.txt or copy at
11 // http://www.boost.org/LICENSE_1_0.txt)
13 #include "boost/test/minimal.hpp"
14 #include "boost/variant.hpp"
15 #include "boost/mpl/vector.hpp"
16 #include "boost/mpl/copy.hpp"
24 : boost::static_visitor<std::string>
27 std::string operator()(const std::vector<T>& vec) const
29 std::ostringstream ost;
33 typename std::vector<T>::const_iterator it = vec.begin();
34 for (; it != vec.end(); ++it)
35 ost << boost::apply_visitor( vector_printer(), *it );
43 std::string operator()(const T& operand) const
45 std::ostringstream ost;
46 ost << operand << ' ';
51 void test_recursive_variant()
53 typedef boost::make_recursive_variant<
55 , std::vector<boost::recursive_variant_>
58 std::vector<var1_t> vec1;
65 std::string result1( boost::apply_visitor( vector_printer(), var1 ) );
67 std::cout << "result1: " << result1 << '\n';
68 BOOST_CHECK(result1 == "( 3 5 ( 3 5 ) 7 ) ");
70 typedef boost::make_recursive_variant<
71 boost::variant<int, double>
72 , std::vector<boost::recursive_variant_>
75 std::vector<var2_t> vec2;
76 vec2.push_back(boost::variant<int, double>(3));
77 vec2.push_back(boost::variant<int, double>(3.5));
79 vec2.push_back(boost::variant<int, double>(7));
82 std::string result2( boost::apply_visitor( vector_printer(), var2 ) );
84 std::cout << "result2: " << result2 << '\n';
85 BOOST_CHECK(result2 == "( 3 3.5 ( 3 3.5 ) 7 ) ");
87 typedef boost::make_recursive_variant<
92 , std::vector<boost::recursive_variant_>
97 typedef boost::variant<double, std::vector<var3_t> > var4_t;
99 std::vector<var3_t> vec3;
102 std::vector<var4_t> vec4;
104 vec4.push_back(vec3);
105 vec3.push_back(vec4);
109 std::string result3( boost::apply_visitor( vector_printer(), var4 ) );
111 std::cout << "result2: " << result3 << '\n';
112 BOOST_CHECK(result3 == "( 3 5 ( 3.5 ( 3 5 ) ) 7 ) ");
114 typedef boost::make_recursive_variant<
119 std::vector<var5_t> vec5;
121 vec5.push_back(vec1);
122 vec5.push_back(17.25);
124 std::string result5( vector_printer()(vec5) );
126 std::cout << "result5: " << result5 << '\n';
127 BOOST_CHECK(result5 == "( 3.5 ( 3 5 ( 3 5 ) 7 ) 17.25 ) ");
129 typedef boost::make_recursive_variant<
131 std::map<int, boost::recursive_variant_>
136 void test_recursive_variant_over()
138 typedef boost::make_recursive_variant_over<
141 , std::vector<boost::recursive_variant_>
145 std::vector<var1_t> vec1;
148 vec1.push_back(vec1);
152 std::string result1( boost::apply_visitor( vector_printer(), var1 ) );
154 std::cout << "result1: " << result1 << '\n';
155 BOOST_CHECK(result1 == "( 3 5 ( 3 5 ) 7 ) ");
157 typedef boost::make_recursive_variant_over<
159 boost::make_variant_over<boost::mpl::vector<int, double> >::type
160 , std::vector<boost::recursive_variant_>
164 std::vector<var2_t> vec2;
165 vec2.push_back(boost::variant<int, double>(3));
166 vec2.push_back(boost::variant<int, double>(3.5));
167 vec2.push_back(vec2);
168 vec2.push_back(boost::variant<int, double>(7));
171 std::string result2( boost::apply_visitor( vector_printer(), var2 ) );
173 std::cout << "result2: " << result2 << '\n';
174 BOOST_CHECK(result2 == "( 3 3.5 ( 3 3.5 ) 7 ) ");
176 typedef boost::make_recursive_variant_over<
180 boost::make_variant_over<
183 , std::vector<boost::recursive_variant_>
190 typedef boost::make_variant_over<
194 , std::vector<var3_t>
199 std::vector<var3_t> vec3;
202 std::vector<var4_t> vec4;
204 vec4.push_back(vec3);
205 vec3.push_back(vec4);
209 std::string result3( boost::apply_visitor( vector_printer(), var3 ) );
211 std::cout << "result2: " << result3 << '\n';
212 BOOST_CHECK(result3 == "( 3 5 ( 3.5 ( 3 5 ) ) 7 ) ");
214 typedef boost::make_recursive_variant_over<
217 , std::vector<var1_t>
221 std::vector<var5_t> vec5;
223 vec5.push_back(vec1);
224 vec5.push_back(17.25);
226 std::string result5( vector_printer()(vec5) );
228 std::cout << "result5: " << result5 << '\n';
229 BOOST_CHECK(result5 == "( 3.5 ( 3 5 ( 3 5 ) 7 ) 17.25 ) ");
232 int test_main(int , char* [])
234 test_recursive_variant();
235 test_recursive_variant_over();
237 return boost::exit_success;