//---------------------------------------------------------------------------// // Copyright (c) 2013-2014 Kyle Lutz // // Distributed under the Boost Software License, Version 1.0 // See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt // // See http://boostorg.github.com/compute for more information. //---------------------------------------------------------------------------// #ifndef BOOST_COMPUTE_FUNCTIONAL_BIND_HPP #define BOOST_COMPUTE_FUNCTIONAL_BIND_HPP #include #include #include #include #include namespace boost { namespace compute { namespace placeholders { /// \internal_ template struct placeholder : boost::integral_constant { placeholder() { } }; placeholder<0> const _1; placeholder<1> const _2; } // end placeholders namespace /// Meta-function returning \c true if \c T is a placeholder type. template struct is_placeholder : boost::false_type { }; /// \internal_ template struct is_placeholder > : boost::true_type { }; namespace detail { template struct invoked_bound_function { invoked_bound_function(Function f, BoundArgs bound_args, Args args) : m_function(f), m_bound_args(bound_args), m_args(args) { } // meta-function returning true if the N'th argument is a placeholder template struct is_placeholder_arg { typedef typename boost::tuples::element::type nth_bound_arg; typedef typename is_placeholder::type type; static const bool value = is_placeholder::value; }; template struct get_arg_type { typedef Arg type; }; template struct get_arg_type > { typedef typename boost::tuples::element::type type; }; // meta-function returning the type of the N'th argument when invoked template struct get_nth_arg_type { typedef typename boost::tuples::element::type nth_bound_arg; typedef typename get_arg_type::type type; }; template typename get_nth_arg_type::type get_nth_arg( typename boost::enable_if_c::value>::type* = 0 ) const { typedef typename boost::tuples::element::type nth_bound_arg; return boost::get(m_args); } template typename get_nth_arg_type::type get_nth_arg( typename boost::disable_if_c::value>::type* = 0 ) const { return boost::get(m_bound_args); } Function m_function; BoundArgs m_bound_args; Args m_args; }; template inline meta_kernel& apply_invoked_bound_function( meta_kernel &k, const invoked_bound_function &expr, typename boost::enable_if_c< boost::tuples::length::value == 1 >::type* = 0 ) { return k << expr.m_function(expr.template get_nth_arg<0>()); } template inline meta_kernel& apply_invoked_bound_function( meta_kernel &k, const invoked_bound_function &expr, typename boost::enable_if_c< boost::tuples::length::value == 2 >::type* = 0 ) { return k << expr.m_function(expr.template get_nth_arg<0>(), expr.template get_nth_arg<1>()); } template inline meta_kernel& apply_invoked_bound_function( meta_kernel &k, const invoked_bound_function &expr, typename boost::enable_if_c< boost::tuples::length::value == 3 >::type* = 0 ) { return k << expr.m_function(expr.template get_nth_arg<0>(), expr.template get_nth_arg<1>(), expr.template get_nth_arg<2>()); } template inline meta_kernel& operator<<( meta_kernel &k, const invoked_bound_function &expr ) { return apply_invoked_bound_function(k, expr); } template struct bound_function { typedef int result_type; bound_function(Function f, BoundArgs args) : m_function(f), m_args(args) { } template detail::invoked_bound_function< Function, BoundArgs, boost::tuple > operator()(const Arg1 &arg1) const { return detail::invoked_bound_function< Function, BoundArgs, boost::tuple >(m_function, m_args, boost::make_tuple(arg1)); } template detail::invoked_bound_function< Function, BoundArgs, boost::tuple > operator()(const Arg1 &arg1, const Arg2 &arg2) const { return detail::invoked_bound_function< Function, BoundArgs, boost::tuple >(m_function, m_args, boost::make_tuple(arg1, arg2)); } Function m_function; BoundArgs m_args; }; } // end detail namespace #if !defined(BOOST_COMPUTE_NO_VARIADIC_TEMPLATES) || defined(BOOST_COMPUTE_DOXYGEN_INVOKED) /// Returns a function wrapper which invokes \p f with \p args when called. /// /// For example, to generate a unary function object which returns \c true /// when its argument is less than \c 7: /// \code /// using boost::compute::less; /// using boost::compute::placeholders::_1; /// /// auto less_than_seven = boost::compute::bind(less(), _1, 7); /// \endcode template inline detail::bound_function > bind(F f, Args... args) { typedef typename boost::tuple ArgsTuple; return detail::bound_function(f, boost::make_tuple(args...)); } #else template inline detail::bound_function > bind(F f, A1 a1) { typedef typename boost::tuple Args; return detail::bound_function(f, boost::make_tuple(a1)); } template inline detail::bound_function > bind(F f, A1 a1, A2 a2) { typedef typename boost::tuple Args; return detail::bound_function(f, boost::make_tuple(a1, a2)); } template inline detail::bound_function > bind(F f, A1 a1, A2 a2, A3 a3) { typedef typename boost::tuple Args; return detail::bound_function(f, boost::make_tuple(a1, a2, a3)); } #endif // BOOST_COMPUTE_NO_VARIADIC_TEMPLATES } // end compute namespace } // end boost namespace #endif // BOOST_COMPUTE_FUNCTIONAL_BIND_HPP