common: Introduce utility function VisitVariant

VisitVariant allows one to use a generic lambda to visit a boost::variant.
This is necessary because boost::visit_variant requires the visitor type
to provide a return type.
This commit is contained in:
MerryMage 2017-02-16 19:30:56 +00:00
parent 5a20a37d3f
commit e197b10b96
2 changed files with 34 additions and 0 deletions

View file

@ -48,6 +48,7 @@ set(HEADERS
common/mp.h common/mp.h
common/scope_exit.h common/scope_exit.h
common/string_util.h common/string_util.h
common/variant_util.h
frontend/arm/FPSCR.h frontend/arm/FPSCR.h
frontend/arm/PSR.h frontend/arm/PSR.h
frontend/arm/types.h frontend/arm/types.h

33
src/common/variant_util.h Normal file
View file

@ -0,0 +1,33 @@
/* This file is part of the dynarmic project.
* Copyright (c) 2016 MerryMage
* This software may be used and distributed according to the terms of the GNU
* General Public License version 2 or any later version.
*/
#pragma once
#include <boost/variant.hpp>
namespace Dynarmic {
namespace Common {
namespace detail {
template <typename ReturnT, typename Lambda>
struct VariantVisitor : boost::static_visitor<ReturnT>, Lambda {
VariantVisitor(Lambda&& lambda)
: Lambda(std::move(lambda))
{}
using Lambda::operator();
};
} // namespace detail
template<typename ReturnT, typename Variant, typename Lambda>
inline ReturnT VisitVariant(Variant&& variant, Lambda&& lambda) {
return boost::apply_visitor(detail::VariantVisitor<ReturnT, Lambda>(std::move(lambda)), variant);
}
} // namespace Common
} // namespace Dynarmic