Created
March 13, 2017 21:23
-
-
Save mirth/89446f6d19215a1d0387a4b5232f2a04 to your computer and use it in GitHub Desktop.
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
#include <functional> | |
#include <boost/mpl/inherit_linearly.hpp> | |
#include <boost/mpl/inherit.hpp> | |
#include <boost/mpl/vector.hpp> | |
template<typename TargetsVector, typename SignatureVector> | |
struct visit_method; | |
template<typename TargetType, typename ReturnType, typename ... ArgsTypes> | |
struct visit_method<TargetType, std::tuple<ReturnType, ArgsTypes ...>> { | |
virtual void visit(TargetType, ArgsTypes&& ...) = 0; | |
virtual ~visit_method() { | |
} | |
}; | |
template<typename T, typename TargetType, typename Signature> | |
struct inherit_wrapper: boost::mpl::inherit<T, | |
visit_method<TargetType, Signature>> { | |
}; | |
template<typename TargetsVector, typename SignatureVector> | |
class basic_visitor; | |
template<typename ... TargetsTypes, typename SignatureVector> | |
class basic_visitor<std::tuple<TargetsTypes ...>, SignatureVector> : public boost::mpl::inherit_linearly< | |
boost::mpl::vector<TargetsTypes ...>, | |
inherit_wrapper<boost::mpl::_1, boost::mpl::_2, SignatureVector>>::type { | |
}; | |
template<typename SelfType, typename ReturnType> | |
class visitable { | |
public: | |
template<typename VisitorType, typename ... ArgsTypes> | |
ReturnType accept(VisitorType& visitor, ArgsTypes&& ... args) { | |
return visitor.visit(static_cast<SelfType&>(*this), args ...); | |
} | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment