Sprout/sprout/variant/as_variant_visitor.hpp

57 lines
1.9 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
2014-01-08 07:48:12 +00:00
Copyright (c) 2011-2014 Bolero MURAKAMI
2013-08-08 09:54:33 +00:00
https://github.com/bolero-MURAKAMI/Sprout
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
2012-12-19 16:06:50 +00:00
#ifndef SPROUT_VARIANT_AS_VARIANT_VISITOR_HPP
#define SPROUT_VARIANT_AS_VARIANT_VISITOR_HPP
#include <utility>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/utility/forward.hpp>
#include <sprout/utility/lvalue_forward.hpp>
2013-02-07 14:12:57 +00:00
#include <sprout/utility/value_holder/value_holder.hpp>
2012-12-19 16:06:50 +00:00
#include <sprout/type_traits/lvalue_reference.hpp>
#include <sprout/variant/static_variant_visitor.hpp>
namespace sprout {
//
// variant_visitor_wrapper
//
template<typename Visitor>
class variant_visitor_wrapper
: public sprout::static_variant_visitor
{
public:
typedef Visitor visitor_type;
typedef sprout::value_holder<visitor_type&> holder_type;
private:
holder_type v_;
public:
SPROUT_CONSTEXPR explicit variant_visitor_wrapper(typename holder_type::param_type v)
: v_(v)
{}
template<typename T>
2013-06-17 14:44:18 +00:00
SPROUT_CONSTEXPR auto operator()(T&& t) const
-> decltype(std::declval<holder_type const>().get()(SPROUT_FORWARD(T, t)))
2012-12-19 16:06:50 +00:00
{
return v_.get()(SPROUT_FORWARD(T, t));
2012-12-19 16:06:50 +00:00
}
};
//
// as_variant_visitor
//
template<typename Visitor>
inline SPROUT_CONSTEXPR sprout::variant_visitor_wrapper<typename std::remove_reference<typename sprout::lvalue_reference<Visitor>::type>::type>
as_variant_visitor(Visitor&& visitor) {
typedef sprout::variant_visitor_wrapper<typename std::remove_reference<typename sprout::lvalue_reference<Visitor>::type>::type> type;
return type(sprout::lvalue_forward<Visitor>(visitor));
}
} // namespace sprout
#endif // #ifndef SPROUT_VARIANT_AS_VARIANT_VISITOR_HPP