Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:43:51

0001 /*=============================================================================
0002     Copyright (c) 2012 Paul Fultz II
0003     match.h
0004     Distributed under the Boost Software License, Version 1.0. (See accompanying
0005     file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 ==============================================================================*/
0007 
0008 #ifndef BOOST_HOF_GUARD_FUNCTION_OVERLOAD_H
0009 #define BOOST_HOF_GUARD_FUNCTION_OVERLOAD_H
0010 
0011 /// match
0012 /// =====
0013 /// 
0014 /// Description
0015 /// -----------
0016 /// 
0017 /// The `match` function adaptor combines several functions together and
0018 /// resolves which one should be called by using C++ overload resolution. This
0019 /// is different than the [`first_of`](/include/boost/hof/conditional) adaptor which resolves
0020 /// them based on order.
0021 /// 
0022 /// Synopsis
0023 /// --------
0024 /// 
0025 ///     template<class... Fs>
0026 ///     constexpr match_adaptor<Fs...> match(Fs...fs);
0027 /// 
0028 /// Requirements
0029 /// ------------
0030 /// 
0031 /// Fs must be:
0032 /// 
0033 /// * [ConstInvocable](ConstInvocable)
0034 /// * MoveConstructible
0035 /// 
0036 /// Example
0037 /// -------
0038 /// 
0039 ///     #include <boost/hof.hpp>
0040 ///     using namespace boost::hof;
0041 /// 
0042 ///     struct int_class
0043 ///     {
0044 ///         int operator()(int) const
0045 ///         {
0046 ///             return 1;
0047 ///         }
0048 ///     };
0049 /// 
0050 ///     struct foo
0051 ///     {};
0052 /// 
0053 ///     struct foo_class
0054 ///     {
0055 ///         foo operator()(foo) const
0056 ///         {
0057 ///             return foo();
0058 ///         }
0059 ///     };
0060 /// 
0061 ///     typedef match_adaptor<int_class, foo_class> fun;
0062 /// 
0063 ///     static_assert(std::is_same<int, decltype(fun()(1))>::value, "Failed match");
0064 ///     static_assert(std::is_same<foo, decltype(fun()(foo()))>::value, "Failed match");
0065 /// 
0066 ///     int main() {}
0067 /// 
0068 /// References
0069 /// ----------
0070 /// 
0071 /// * [POO51](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0051r2.pdf) - Proposal for C++
0072 ///   Proposal for C++ generic overload function
0073 /// 
0074 
0075 #include <boost/hof/reveal.hpp>
0076 #include <boost/hof/detail/callable_base.hpp>
0077 #include <boost/hof/detail/delegate.hpp>
0078 #include <boost/hof/detail/move.hpp>
0079 #include <boost/hof/detail/make.hpp>
0080 #include <boost/hof/detail/static_const_var.hpp>
0081 
0082 namespace boost { namespace hof {
0083 
0084 template<class...Fs> struct match_adaptor;
0085  
0086 template<class F, class...Fs>
0087 struct match_adaptor<F, Fs...> : detail::callable_base<F>, match_adaptor<Fs...>
0088 {
0089     typedef match_adaptor<Fs...> base;
0090     typedef match_adaptor fit_rewritable_tag;
0091 
0092     struct failure
0093     : failure_for<detail::callable_base<F>, Fs...>
0094     {};
0095 
0096     BOOST_HOF_INHERIT_DEFAULT(match_adaptor, detail::callable_base<F>, base);
0097 
0098     template<class X, class... Xs, BOOST_HOF_ENABLE_IF_CONVERTIBLE(X, detail::callable_base<F>), BOOST_HOF_ENABLE_IF_CONSTRUCTIBLE(base, Xs...)>
0099     constexpr match_adaptor(X&& f1, Xs&& ... fs) 
0100     : detail::callable_base<F>(BOOST_HOF_FORWARD(X)(f1)), base(BOOST_HOF_FORWARD(Xs)(fs)...)
0101     {}
0102 
0103     using F::operator();
0104     using base::operator();
0105 };
0106 
0107 template<class F>
0108 struct match_adaptor<F> : detail::callable_base<F>
0109 {
0110     typedef detail::callable_base<F> base;
0111     typedef match_adaptor fit_rewritable_tag;
0112     using F::operator();
0113 
0114     BOOST_HOF_INHERIT_CONSTRUCTOR(match_adaptor, detail::callable_base<F>);
0115 };
0116 
0117 BOOST_HOF_DECLARE_STATIC_VAR(match, detail::make<match_adaptor>);
0118 
0119 }} // namespace boost::hof
0120 
0121 #endif