1 // -*- mode:C++; tab-width:8; c-basic-offset:4; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=4 smarttab
5 * https://github.com/exclipy/inline_variant_visitor/blob/master/inline_variant.hpp
8 #ifndef INLINE_VARIANT_H
9 #define INLINE_VARIANT_H
11 #include <boost/function_types/function_arity.hpp>
12 #include <boost/fusion/algorithm/transformation/transform.hpp>
13 #include <boost/mpl/contains.hpp>
14 #include <boost/mpl/map.hpp>
15 #include <boost/mpl/vector.hpp>
16 #include <boost/mpl/range_c.hpp>
18 #include "function_signature.h"
22 // A metafunction class for getting the argument type from a unary function or functor type
23 struct function_arg_extractor
25 // Function is either a function type like void(int const&), or a functor - eg. a class with void operator(int)
26 // Sets type to the argument type with the constness and referenceness stripped (eg. int)
27 template <typename Function>
31 typedef typename boost::remove_const< typename boost::remove_reference<Function>::type >::type bare_type;
32 typedef typename signature_of<bare_type>::type normalized_function_type;
33 typedef typename boost::function_types::function_arity<normalized_function_type>::type arity;
34 typedef typename boost::function_types::parameter_types<normalized_function_type>::type parameter_types;
35 typedef typename boost::function_types::result_type<normalized_function_type>::type result_type;
37 BOOST_STATIC_ASSERT_MSG((arity::value == 1), "make_visitor called with a non-unary function");
39 typedef typename boost::mpl::front<parameter_types>::type parameter_type;
41 typedef typename boost::remove_const< typename boost::remove_reference<parameter_type>::type >::type type;
47 template <typename AType, typename Ind>
49 typedef boost::mpl::pair<AType, Ind> type;
53 // A metafunction class that asserts the second argument is in Allowed, and returns void
54 template<typename Allowed>
57 template <typename Type1, typename Type2>
61 BOOST_STATIC_ASSERT_MSG((boost::mpl::contains<Allowed, typename boost::mpl::first<Type2>::type>::value),
62 "make_visitor called with spurious handler functions");
68 template <typename Seq>
72 struct insert_helper {
73 template <typename M, typename P>
76 typedef typename boost::mpl::insert<
82 typedef typename boost::mpl::fold<Seq, boost::mpl::map0<>, insert_helper>::type type;
85 // A functor template suitable for passing into apply_visitor. The constructor accepts the list of handler functions,
86 // which are then exposed through a set of operator()s
87 template <typename Result, typename Variant, typename... Functions>
88 struct generic_visitor : boost::static_visitor<Result>, boost::noncopyable
91 typedef generic_visitor<Result, Variant, Functions...> type;
93 // Compute the function_map type
94 typedef boost::mpl::vector<Functions...> function_types;
95 typedef typename boost::mpl::transform<function_types, function_arg_extractor>::type arg_types;
96 typedef typename boost::mpl::transform<
98 boost::mpl::range_c<int, 0, boost::mpl::size<arg_types>::value>,
101 typedef typename as_map<pair_list>::type fmap;
103 // Check that the argument types are unique
104 BOOST_STATIC_ASSERT_MSG((boost::mpl::size<fmap>::value == boost::mpl::size<arg_types>::value),
105 "make_visitor called with non-unique argument types for handler functions");
107 // Check that there aren't any argument types not in the variant types
108 typedef typename boost::mpl::fold<fmap, void, check_in<typename Variant::types> >::type dummy;
110 boost::fusion::vector<Functions...> fvec;
113 template <typename T>
114 Result apply_helper(const T& object, boost::mpl::true_) const {
115 typedef typename boost::mpl::at<fmap, T>::type Ind;
116 return boost::fusion::at<Ind>(fvec)(object);
119 template <typename T>
120 Result apply_helper(const T& object, boost::mpl::false_) const {
124 BOOST_MOVABLE_BUT_NOT_COPYABLE(generic_visitor)
127 generic_visitor(BOOST_RV_REF(type) other)
129 fvec(boost::move(other.fvec))
132 generic_visitor(Functions&&... functions)
134 fvec(std::forward<Functions>(functions)...)
138 template <typename T>
139 Result operator()(const T& object) const {
140 typedef typename boost::mpl::has_key<fmap, T>::type correct_key;
141 BOOST_STATIC_ASSERT_MSG(correct_key::value,
142 "make_visitor called without specifying handlers for all required types");
143 return apply_helper(object, correct_key());
147 // A metafunction class for getting the return type of a function
148 struct function_return_extractor
150 template <typename Function>
151 struct apply : boost::function_types::result_type<typename signature_of<Function>::type>
156 // A metafunction class that asserts the two arguments are the same and returns the first one
159 template <typename Type1, typename Type2>
163 BOOST_STATIC_ASSERT_MSG((boost::is_same<Type1, Type2>::value),
164 "make_visitor called with functions of differing return types");
170 // A metafunction for getting the required generic_visitor type for the set of Functions
171 template <typename Variant, typename... Functions>
172 struct get_generic_visitor
175 typedef boost::mpl::vector<Functions...> function_types;
176 typedef typename boost::mpl::transform<
178 boost::remove_const< boost::remove_reference<boost::mpl::_1> >
179 >::type bare_function_types;
180 typedef typename boost::mpl::transform<bare_function_types, function_return_extractor>::type return_types;
183 // Set result_type to the return type of the first function
184 typedef typename boost::mpl::front<return_types>::type result_type;
185 typedef generic_visitor<result_type, Variant, Functions...> type;
188 // Assert that every return type is the same as the first one
189 typedef typename boost::mpl::fold<return_types, result_type, check_same>::type dummy;
192 // Accepts a set of functions and returns an object suitable for apply_visitor
193 template <typename Variant, typename... Functions>
194 auto make_visitor(BOOST_RV_REF(Functions)... functions)
195 -> typename detail::get_generic_visitor<Variant, Functions...>::type
197 return typename detail::get_generic_visitor<Variant, Functions...>::type(boost::forward<Functions>(functions)...);
202 template <typename Variant, typename... Functions>
203 auto match(Variant const& variant, BOOST_RV_REF(Functions)... functions)
204 -> typename detail::get_generic_visitor<Variant, Functions...>::result_type
206 return boost::apply_visitor(detail::make_visitor<Variant>(
207 boost::forward<Functions>(functions)...), variant);