Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:30:00

0001 //---------------------------------------------------------------------------//
0002 // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
0003 //
0004 // Distributed under the Boost Software License, Version 1.0
0005 // See accompanying file LICENSE_1_0.txt or copy at
0006 // http://www.boost.org/LICENSE_1_0.txt
0007 //
0008 // See http://boostorg.github.com/compute for more information.
0009 //---------------------------------------------------------------------------//
0010 
0011 #ifndef BOOST_COMPUTE_FUNCTIONAL_DETAIL_UNPACK_HPP
0012 #define BOOST_COMPUTE_FUNCTIONAL_DETAIL_UNPACK_HPP
0013 
0014 #include <boost/compute/functional/get.hpp>
0015 #include <boost/compute/type_traits/is_vector_type.hpp>
0016 #include <boost/compute/type_traits/result_of.hpp>
0017 #include <boost/compute/type_traits/vector_size.hpp>
0018 #include <boost/compute/detail/meta_kernel.hpp>
0019 
0020 namespace boost {
0021 namespace compute {
0022 namespace detail {
0023 
0024 template<class Function, class Arg, size_t Arity>
0025 struct invoked_unpacked
0026 {
0027     invoked_unpacked(const Function &f, const Arg &arg)
0028         : m_function(f),
0029           m_arg(arg)
0030     {
0031     }
0032 
0033     Function m_function;
0034     Arg m_arg;
0035 };
0036 
0037 template<class Function, class Arg, size_t Arity>
0038 inline meta_kernel& operator<<(meta_kernel &k, const invoked_unpacked<Function, Arg, Arity> &expr);
0039 
0040 template<class Function, class Arg>
0041 inline meta_kernel& operator<<(meta_kernel &k, const invoked_unpacked<Function, Arg, 1> &expr)
0042 {
0043     return k << expr.m_function(get<0>()(expr.m_arg));
0044 }
0045 
0046 template<class Function, class Arg>
0047 inline meta_kernel& operator<<(meta_kernel &k, const invoked_unpacked<Function, Arg, 2> &expr)
0048 {
0049     return k << expr.m_function(get<0>()(expr.m_arg), get<1>()(expr.m_arg));
0050 }
0051 
0052 template<class Function, class Arg>
0053 inline meta_kernel& operator<<(meta_kernel &k, const invoked_unpacked<Function, Arg, 3> &expr)
0054 {
0055     return k << expr.m_function(get<0>()(expr.m_arg), get<1>()(expr.m_arg), get<2>()(expr.m_arg));
0056 }
0057 
0058 template<class Function>
0059 struct unpacked
0060 {
0061     template<class T, class Enable = void>
0062     struct aggregate_length
0063     {
0064         BOOST_STATIC_CONSTANT(size_t, value = boost::tuples::length<T>::value);
0065     };
0066 
0067     template<class T>
0068     struct aggregate_length<T, typename enable_if<is_vector_type<T> >::type>
0069     {
0070         BOOST_STATIC_CONSTANT(size_t, value = vector_size<T>::value);
0071     };
0072 
0073     template<class TupleArg, size_t TupleSize>
0074     struct result_impl {};
0075 
0076     template<class TupleArg>
0077     struct result_impl<TupleArg, 1>
0078     {
0079         typedef typename detail::get_result_type<0, TupleArg>::type T1;
0080 
0081         typedef typename boost::compute::result_of<Function(T1)>::type type;
0082     };
0083 
0084     template<class TupleArg>
0085     struct result_impl<TupleArg, 2>
0086     {
0087         typedef typename detail::get_result_type<0, TupleArg>::type T1;
0088         typedef typename detail::get_result_type<1, TupleArg>::type T2;
0089 
0090         typedef typename boost::compute::result_of<Function(T1, T2)>::type type;
0091     };
0092 
0093     template<class TupleArg>
0094     struct result_impl<TupleArg, 3>
0095     {
0096         typedef typename detail::get_result_type<0, TupleArg>::type T1;
0097         typedef typename detail::get_result_type<1, TupleArg>::type T2;
0098         typedef typename detail::get_result_type<2, TupleArg>::type T3;
0099 
0100         typedef typename boost::compute::result_of<Function(T1, T2, T3)>::type type;
0101     };
0102 
0103     template<class Signature>
0104     struct result {};
0105 
0106     template<class This, class Arg>
0107     struct result<This(Arg)>
0108     {
0109         typedef typename result_impl<Arg, aggregate_length<Arg>::value>::type type;
0110     };
0111 
0112     unpacked(const Function &f)
0113         : m_function(f)
0114     {
0115     }
0116 
0117     template<class Arg>
0118     detail::invoked_unpacked<
0119         Function, Arg, aggregate_length<typename Arg::result_type>::value
0120     >
0121     operator()(const Arg &arg) const
0122     {
0123         return detail::invoked_unpacked<
0124                    Function,
0125                    Arg,
0126                    aggregate_length<typename Arg::result_type>::value
0127                 >(m_function, arg);
0128     }
0129 
0130     Function m_function;
0131 };
0132 
0133 template<class Function>
0134 inline unpacked<Function> unpack(const Function &f)
0135 {
0136     return unpacked<Function>(f);
0137 }
0138 
0139 } // end detail namespace
0140 } // end compute namespace
0141 } // end boost namespace
0142 
0143 #endif // BOOST_COMPUTE_FUNCTIONAL_DETAIL_UNPACK_HPP