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 | //----------------------------------------------------------------------------- |
---|
5 | // |
---|
6 | // Copyright (c) 2003 |
---|
7 | // Eric Friedman, Itay Maman |
---|
8 | // |
---|
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) |
---|
12 | |
---|
13 | #include "boost/test/minimal.hpp" |
---|
14 | #include "boost/variant.hpp" |
---|
15 | |
---|
16 | #include <iostream> |
---|
17 | #include <sstream> |
---|
18 | #include <vector> |
---|
19 | |
---|
20 | struct vector_printer |
---|
21 | : boost::static_visitor<std::string> |
---|
22 | { |
---|
23 | template <typename T> |
---|
24 | std::string operator()(const std::vector<T>& vec) const |
---|
25 | { |
---|
26 | std::ostringstream ost; |
---|
27 | |
---|
28 | ost << "( "; |
---|
29 | |
---|
30 | typename std::vector<T>::const_iterator it = vec.begin(); |
---|
31 | for (; it != vec.end(); ++it) |
---|
32 | ost << boost::apply_visitor( vector_printer(), *it ); |
---|
33 | |
---|
34 | ost << ") "; |
---|
35 | |
---|
36 | return ost.str(); |
---|
37 | } |
---|
38 | |
---|
39 | template <typename T> |
---|
40 | std::string operator()(const T& operand) const |
---|
41 | { |
---|
42 | std::ostringstream ost; |
---|
43 | ost << operand << ' '; |
---|
44 | return ost.str(); |
---|
45 | } |
---|
46 | }; |
---|
47 | |
---|
48 | int test_main(int , char* []) |
---|
49 | { |
---|
50 | typedef boost::make_recursive_variant< |
---|
51 | int |
---|
52 | , std::vector<boost::recursive_variant_> |
---|
53 | >::type var_t; |
---|
54 | |
---|
55 | std::vector<var_t> vec; |
---|
56 | vec.push_back(3); |
---|
57 | vec.push_back(5); |
---|
58 | vec.push_back(vec); |
---|
59 | vec.push_back(7); |
---|
60 | |
---|
61 | var_t var(vec); |
---|
62 | std::string result( boost::apply_visitor( vector_printer(), var ) ); |
---|
63 | |
---|
64 | std::cout << "result: " << result << '\n'; |
---|
65 | BOOST_CHECK(result == "( 3 5 ( 3 5 ) 7 ) "); |
---|
66 | |
---|
67 | return boost::exit_success; |
---|
68 | } |
---|