diff options
Diffstat (limited to '')
-rw-r--r-- | src/mapbox/variant_visitor.hpp | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/mapbox/variant_visitor.hpp b/src/mapbox/variant_visitor.hpp new file mode 100644 index 0000000..54ddba0 --- /dev/null +++ b/src/mapbox/variant_visitor.hpp @@ -0,0 +1,43 @@ +#ifndef MAPBOX_UTIL_VARIANT_VISITOR_HPP +#define MAPBOX_UTIL_VARIANT_VISITOR_HPP + +#include <utility> + +namespace mapbox { +namespace util { + +template <typename... Fns> +struct visitor; + +template <typename Fn> +struct visitor<Fn> : Fn +{ + using Fn::operator(); + + template<typename T> + visitor(T&& fn) : Fn(std::forward<T>(fn)) {} +}; + +template <typename Fn, typename... Fns> +struct visitor<Fn, Fns...> : Fn, visitor<Fns...> +{ + using Fn::operator(); + using visitor<Fns...>::operator(); + + template<typename T, typename... Ts> + visitor(T&& fn, Ts&&... fns) + : Fn(std::forward<T>(fn)) + , visitor<Fns...>(std::forward<Ts>(fns)...) {} +}; + +template <typename... Fns> +visitor<typename std::decay<Fns>::type...> make_visitor(Fns&&... fns) +{ + return visitor<typename std::decay<Fns>::type...> + (std::forward<Fns>(fns)...); +} + +} // namespace util +} // namespace mapbox + +#endif // MAPBOX_UTIL_VARIANT_VISITOR_HPP |