]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | //----------------------------------------------------------------------------- |
2 | // boost-libs variant/test/variant_multivisit_test.cpp source file | |
3 | // See http://www.boost.org for updates, documentation, and revision history. | |
4 | //----------------------------------------------------------------------------- | |
5 | // | |
1e59de90 | 6 | // Copyright (c) 2013-2022 Antony Polukhin |
7c673cae FG |
7 | // |
8 | // Distributed under the Boost Software License, Version 1.0. (See | |
9 | // accompanying file LICENSE_1_0.txt or copy at | |
10 | // http://www.boost.org/LICENSE_1_0.txt) | |
11 | ||
12 | #include "boost/config.hpp" | |
13 | #include "boost/noncopyable.hpp" | |
14 | #define BOOST_VARAINT_MAX_MULTIVIZITOR_PARAMS 5 | |
15 | #include "boost/variant/multivisitors.hpp" | |
16 | #include "boost/variant.hpp" | |
17 | ||
92f5a8d4 | 18 | #include "boost/core/lightweight_test.hpp" |
7c673cae FG |
19 | |
20 | struct my_noncopyable : boost::noncopyable { | |
21 | my_noncopyable(){} | |
22 | ~my_noncopyable(){} | |
23 | }; | |
24 | ||
25 | typedef boost::variant<my_noncopyable, int> variant_noncopy_t; | |
26 | ||
27 | ||
1e59de90 | 28 | typedef boost::variant<char, unsigned char, signed char, unsigned short, int> variant5_t; |
7c673cae FG |
29 | |
30 | struct test_visitor: boost::static_visitor<> { | |
31 | // operators that shall not be called | |
32 | template <class T1, class T2, class T3> | |
33 | void operator()(T1&, T2&, T3&) const | |
34 | { | |
92f5a8d4 | 35 | BOOST_TEST(false); |
7c673cae FG |
36 | } |
37 | ||
38 | template <class T1, class T2, class T3, class T4> | |
39 | void operator()(T1&, T2&, T3&, T4&) const | |
40 | { | |
92f5a8d4 | 41 | BOOST_TEST(false); |
7c673cae FG |
42 | } |
43 | ||
44 | template <class T1, class T2, class T3, class T4, class T5> | |
45 | void operator()(T1&, T2&, T3&, T4&, T5&) const | |
46 | { | |
92f5a8d4 | 47 | BOOST_TEST(false); |
7c673cae FG |
48 | } |
49 | ||
50 | // operators that are OK to call | |
51 | void operator()(char v0, unsigned char v1, signed char v2) const | |
52 | { | |
92f5a8d4 TL |
53 | BOOST_TEST(v0 == 0); |
54 | BOOST_TEST(v1 == 1); | |
55 | BOOST_TEST(v2 == 2); | |
7c673cae FG |
56 | } |
57 | ||
58 | void operator()(char v0, unsigned char v1, signed char v2, unsigned short v3) const | |
59 | { | |
92f5a8d4 TL |
60 | BOOST_TEST(v0 == 0); |
61 | BOOST_TEST(v1 == 1); | |
62 | BOOST_TEST(v2 == 2); | |
63 | BOOST_TEST(v3 == 3); | |
7c673cae FG |
64 | } |
65 | ||
66 | void operator()(char v0, unsigned char v1, signed char v2, unsigned short v3, int v4) const | |
67 | { | |
92f5a8d4 TL |
68 | BOOST_TEST(v0 == 0); |
69 | BOOST_TEST(v1 == 1); | |
70 | BOOST_TEST(v2 == 2); | |
71 | BOOST_TEST(v3 == 3); | |
72 | BOOST_TEST(v4 == 4); | |
7c673cae FG |
73 | } |
74 | ||
75 | ||
76 | // Noncopyables | |
77 | void operator()(my_noncopyable&, my_noncopyable&, my_noncopyable&) const { | |
92f5a8d4 | 78 | BOOST_TEST(true); |
7c673cae FG |
79 | } |
80 | void operator()(my_noncopyable&, my_noncopyable&, my_noncopyable&, my_noncopyable&) const { | |
92f5a8d4 | 81 | BOOST_TEST(true); |
7c673cae FG |
82 | } |
83 | void operator()(my_noncopyable&, my_noncopyable&, my_noncopyable&, my_noncopyable&, my_noncopyable&) const { | |
92f5a8d4 | 84 | BOOST_TEST(true); |
7c673cae FG |
85 | } |
86 | void operator()(my_noncopyable&, my_noncopyable&, my_noncopyable&, my_noncopyable&, my_noncopyable&, my_noncopyable&) const { | |
92f5a8d4 | 87 | BOOST_TEST(true); |
7c673cae FG |
88 | } |
89 | }; | |
90 | ||
91 | typedef boost::variant<int, double, bool> bool_like_t; | |
92 | typedef boost::variant<int, double> arithmetics_t; | |
93 | ||
94 | struct if_visitor: public boost::static_visitor<arithmetics_t> { | |
95 | template <class T0, class T1, class T2> | |
96 | arithmetics_t operator()(T0 b, T1 v1, T2 v2) const { | |
97 | if (!!b) { | |
98 | return v1; | |
99 | } else { | |
100 | return v2; | |
101 | } | |
102 | } | |
103 | }; | |
104 | ||
105 | ||
92f5a8d4 | 106 | int main() |
7c673cae FG |
107 | { |
108 | test_visitor v; | |
109 | ||
1e59de90 TL |
110 | variant5_t v_array5[5]; |
111 | v_array5[0] = char(0); | |
112 | v_array5[1] = static_cast<unsigned char>(1); | |
113 | v_array5[2] = static_cast<signed char>(2); | |
114 | v_array5[3] = static_cast<unsigned short>(3); | |
115 | v_array5[4] = static_cast<int>(4); | |
7c673cae | 116 | |
1e59de90 TL |
117 | boost::apply_visitor(v, v_array5[0], v_array5[1], v_array5[2]); |
118 | boost::apply_visitor(test_visitor(), v_array5[0], v_array5[1], v_array5[2]); | |
7c673cae FG |
119 | |
120 | // Following test also pass, but requires many Gigabytes of RAM for compilation and compile for about 15 minutes | |
121 | //#define BOOST_VARIANT_MULTIVISITORS_TEST_VERY_EXTREME | |
122 | #ifdef BOOST_VARIANT_MULTIVISITORS_TEST_VERY_EXTREME | |
1e59de90 TL |
123 | boost::apply_visitor(v, v_array5[0], v_array5[1], v_array5[2], v_array5[3]); |
124 | boost::apply_visitor(test_visitor(), v_array5[0], v_array5[1], v_array5[2], v_array5[3]); | |
7c673cae | 125 | |
1e59de90 TL |
126 | boost::apply_visitor(v, v_array5[0], v_array5[1], v_array5[2], v_array5[3], v_array5[4]); |
127 | boost::apply_visitor(test_visitor(), v_array5[0], v_array5[1], v_array5[2], v_array5[3], v_array5[4]); | |
7c673cae FG |
128 | #endif |
129 | ||
130 | bool_like_t v0(1), v1(true), v2(1.0); | |
131 | ||
92f5a8d4 | 132 | BOOST_TEST( |
7c673cae FG |
133 | boost::apply_visitor(if_visitor(), v0, v1, v2) |
134 | == | |
135 | arithmetics_t(true) | |
136 | ); | |
137 | ||
138 | #if !defined(BOOST_VARIANT_DO_NOT_USE_VARIADIC_TEMPLATES) | |
139 | if_visitor if_vis; | |
92f5a8d4 | 140 | BOOST_TEST( |
7c673cae FG |
141 | boost::apply_visitor(if_vis)(v0, v1, v2) |
142 | == | |
143 | arithmetics_t(true) | |
144 | ); | |
145 | #endif | |
146 | ||
147 | ||
148 | variant_noncopy_t vnonc[6]; | |
149 | boost::apply_visitor(v, vnonc[0], vnonc[1], vnonc[2]); | |
150 | boost::apply_visitor(test_visitor(), vnonc[0], vnonc[1], vnonc[2], vnonc[3]); | |
151 | ||
152 | #ifdef BOOST_VARIANT_MULTIVISITORS_TEST_VERY_EXTREME | |
153 | boost::apply_visitor(v, vnonc[0], vnonc[1], vnonc[2], vnonc[3], vnonc[4]); | |
154 | boost::apply_visitor(test_visitor(), vnonc[0], vnonc[1], vnonc[2], vnonc[3], vnonc[4], vnonc[5]); | |
155 | #endif | |
156 | ||
92f5a8d4 | 157 | return boost::report_errors(); |
7c673cae | 158 | } |