-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'daniel-j-h-lambda-visitor'
- Loading branch information
Showing
3 changed files
with
133 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#ifndef MAPBOX_UTIL_VARIANT_VISITOR_HPP | ||
#define MAPBOX_UTIL_VARIANT_VISITOR_HPP | ||
|
||
namespace mapbox { | ||
namespace util { | ||
|
||
template <typename... Fns> | ||
struct visitor; | ||
|
||
template <typename Fn> | ||
struct visitor<Fn> : Fn | ||
{ | ||
using Fn::operator(); | ||
|
||
visitor(Fn fn) : Fn(fn) {} | ||
}; | ||
|
||
template <typename Fn, typename... Fns> | ||
struct visitor<Fn, Fns...> : Fn, visitor<Fns...> | ||
{ | ||
using Fn::operator(); | ||
using visitor<Fns...>::operator(); | ||
|
||
visitor(Fn fn, Fns... fns) : Fn(fn), visitor<Fns...>(fns...) {} | ||
}; | ||
|
||
template <typename... Fns> | ||
visitor<Fns...> make_visitor(Fns... fns) | ||
{ | ||
return visitor<Fns...>(fns...); | ||
} | ||
|
||
} // namespace util | ||
} // namespace mapbox | ||
|
||
#endif // MAPBOX_UTIL_VARIANT_VISITOR_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
#include <iostream> | ||
#include <vector> | ||
|
||
#include <mapbox/variant.hpp> | ||
#include <mapbox/variant_visitor.hpp> | ||
|
||
#if __cplusplus >= 201402L | ||
#define HAS_CPP14_SUPPORT | ||
#endif | ||
|
||
using namespace mapbox::util; | ||
|
||
template <typename Left, typename Right> | ||
using Either = mapbox::util::variant<Left, Right>; | ||
|
||
struct Response | ||
{ | ||
}; | ||
|
||
struct Error | ||
{ | ||
}; | ||
|
||
void test_lambda_overloads() | ||
{ | ||
Either<Error, Response> rv; | ||
|
||
rv = Response{}; | ||
|
||
auto visitor = make_visitor([](Response) { std::cout << "Response\n"; }, // | ||
[](Error) { std::cout << "Error\n"; }); // | ||
apply_visitor(visitor, rv); | ||
} | ||
|
||
void test_lambda_overloads_capture() | ||
{ | ||
Either<Error, Response> rv; | ||
|
||
rv = Error{}; | ||
|
||
int ok = 0; | ||
int err = 0; | ||
|
||
auto visitor = make_visitor([&](Response) { ok += 1; }, // | ||
[&](Error) { err += 1; }); // | ||
apply_visitor(visitor, rv); | ||
|
||
std::cout << "Got " << ok << " ok, " << err << " err" << std::endl; | ||
} | ||
|
||
void test_singleton_variant() | ||
{ | ||
|
||
variant<int> singleton; | ||
apply_visitor(make_visitor([](int) {}), singleton); | ||
} | ||
|
||
#ifdef HAS_CPP14_SUPPORT | ||
void test_lambda_overloads_sfinae() | ||
{ | ||
variant<int, float, std::vector<int>> var; | ||
|
||
auto visitor = make_visitor([](auto range) -> decltype(std::begin(range), void()) { | ||
for (auto each : range) | ||
std::cout << each << ' '; }, | ||
[](auto x) -> decltype(std::cout << x, void()) { | ||
std::cout << x << std::endl; | ||
}); | ||
|
||
var = 1; | ||
apply_visitor(visitor, var); | ||
|
||
var = 2.f; | ||
apply_visitor(visitor, var); | ||
|
||
var = std::vector<int>{4, 5, 6}; | ||
apply_visitor(visitor, var); | ||
} | ||
#endif | ||
|
||
int main() | ||
{ | ||
test_lambda_overloads(); | ||
test_singleton_variant(); | ||
test_lambda_overloads_capture(); | ||
|
||
#ifdef HAS_CPP14_SUPPORT | ||
test_lambda_overloads_sfinae(); | ||
#endif | ||
} | ||
|
||
#undef HAS_CPP14_SUPPORT |