40 using Variant_t = boost::variant<int, char, std::string, QString, double, float>;
56 void VisitorTest::testBasicVisitor ()
59 const auto& res =
Visit (v,
60 [] (
char) {
return true; },
61 [] (int) {
return false; },
62 [] (std::string) {
return false; },
63 [] (QString) {
return false; },
64 [] (double) {
return false; },
65 [] (float) {
return false; });
69 void VisitorTest::testBasicVisitorGenericFallback ()
72 const auto& res =
Visit (v,
73 [] (
char) {
return true; },
74 [] (int) {
return false; },
75 [] (
auto) {
return false; });
79 void VisitorTest::testBasicVisitorCoercion ()
82 const auto& res =
Visit (v,
83 [] (
int) {
return true; },
84 [] (std::string) {
return false; },
85 [] (QString) {
return false; },
86 [] (double) {
return false; },
87 [] (float) {
return false; });
91 void VisitorTest::testBasicVisitorCoercionGenericFallback ()
94 const auto& res =
Visit (v,
95 [] (
int) {
return false; },
96 [] (QString) {
return false; },
97 [] (
auto) {
return true; });
101 #define NC nc = std::unique_ptr<int> {} 103 void VisitorTest::testNonCopyableFunctors ()
106 const auto& res =
Visit (v,
107 [
NC] (
char) {
return true; },
108 [
NC] (int) {
return false; },
109 [
NC] (std::string) {
return false; },
110 [
NC] (QString) {
return false; },
111 [
NC] (double) {
return false; },
112 [
NC] (float) {
return false; });
113 QCOMPARE (res,
true);
117 void VisitorTest::testDifferentReturnTypes ()
123 decltype (
auto) res =
Visit (v,
124 [] (
int) -> Foo* {
return nullptr; },
125 [] (char) -> Foo* {
return nullptr; },
126 [] (
auto) -> Bar* {
return nullptr; });
128 decltype (
auto) res2 =
Visit (v,
129 [] (
int) -> Bar* {
return nullptr; },
130 [] (char) -> Bar* {
return nullptr; },
131 [] (
auto) -> Foo* {
return nullptr; });
133 static_assert (std::is_same_v<decltype (res), Foo*>);
134 static_assert (std::is_same_v<decltype (res2), Foo*>);
136 QCOMPARE (res,
nullptr);
137 QCOMPARE (res2,
nullptr);
140 void VisitorTest::testAcceptsRValueRef ()
143 [] (char) {
return true; },
144 [] (
auto) {
return false; });
145 QCOMPARE (res,
true);
148 void VisitorTest::testLValueRef ()
152 auto& res =
Visit (v, [&ref] (
auto) ->
int& {
return ref; });
157 void VisitorTest::testLValueRef2 ()
160 Visit (v, [] (
auto& s) ->
int& {
return s.field1; }) = 10;
161 const auto& res =
Visit (v, [] (
const auto& s) ->
const int& {
return s.field1; });
165 void VisitorTest::testPrepareVisitor ()
170 [] (char) {
return true; },
171 [] (int) {
return false; },
172 [] (std::string) {
return false; },
173 [] (QString) {
return false; },
174 [] (double) {
return false; },
175 [] (float) {
return false; }
178 const auto& res = visitor (v);
179 QCOMPARE (res,
true);
182 void VisitorTest::testPrepareVisitorConst ()
187 [] (char) {
return true; },
188 [] (int) {
return false; },
189 [] (std::string) {
return false; },
190 [] (QString) {
return false; },
191 [] (double) {
return false; },
192 [] (float) {
return false; }
195 const auto& res = visitor (v);
196 QCOMPARE (res,
true);
199 void VisitorTest::testPrepareVisitorRValue ()
203 [] (char) {
return true; },
204 [] (int) {
return false; },
205 [] (std::string) {
return false; },
206 [] (QString) {
return false; },
207 [] (double) {
return false; },
208 [] (float) {
return false; }
211 const auto& res = visitor (
Variant_t {
'a' });
212 QCOMPARE (res,
true);
215 void VisitorTest::testPrepareVisitorFinally ()
223 [] (char) {
return true; },
224 [] (
auto) {
return false; }
225 }.Finally ([&fin] { fin =
true; });
227 const auto& res = visitor (v);
228 QCOMPARE (res,
true);
229 QCOMPARE (fin,
true);
232 void VisitorTest::testPrepareJustAutoVisitor ()
234 using Variant_t = boost::variant<int, double, float>;
238 [] (
auto e) {
return std::to_string (e); }
241 const auto& res = visitor (
Variant_t { 10 });
242 QCOMPARE (res, std::string {
"10" });
245 void VisitorTest::testPrepareRecursiveVisitor ()
247 using SubVariant_t = boost::variant<int, double, float>;
248 using Variant_t = boost::variant<SubVariant_t, QString>;
252 [] (
const QString& str) {
return str; },
253 Visitor { [] (
auto e) {
return QString::fromStdString (std::to_string (e)); } }
256 const auto& res = visitor (
Variant_t { SubVariant_t { 10 } });
257 QCOMPARE (res, QString {
"10" });
260 void VisitorTest::testPrepareVisitorMutable ()
265 [] (int)
mutable {
return true; },
266 [] (
auto)
mutable {
return false; }
269 const auto& res = visitor (v);
270 QCOMPARE (res,
true);
boost::variant< S1, S2 > SVariant_t
auto Visit(const Either< Left, Right > &either, Args &&... args)
Visitor(Args &&...) -> Visitor< Void, Args... >
boost::variant< int, char, std::string, QString, double, float > Variant_t