Skip to content

Instantly share code, notes, and snippets.

@mirth
Created March 13, 2017 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mirth/89446f6d19215a1d0387a4b5232f2a04 to your computer and use it in GitHub Desktop.
Save mirth/89446f6d19215a1d0387a4b5232f2a04 to your computer and use it in GitHub Desktop.
#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