1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2024-11-12 21:09:01 +00:00
Sprout/sprout/functional/bind/bind.hpp

489 lines
16 KiB
C++
Raw Normal View History

2012-04-09 16:38:55 +00:00
#ifndef SPROUT_FUNCTIONAL_BIND_BIND_HPP
#define SPROUT_FUNCTIONAL_BIND_BIND_HPP
#include <cstddef>
#include <utility>
#include <tuple>
#include <type_traits>
#include <sprout/config.hpp>
2013-04-06 04:06:51 +00:00
#include <sprout/index_tuple/metafunction.hpp>
2012-04-09 16:38:55 +00:00
#include <sprout/utility/forward.hpp>
#include <sprout/utility/lvalue_forward.hpp>
2013-02-07 15:49:47 +00:00
#include <sprout/tuple/tuple/tuple.hpp>
#include <sprout/tuple/tuple/get.hpp>
2013-02-10 02:54:54 +00:00
#include <sprout/tuple/tuple/make_tuple.hpp>
2012-04-09 16:38:55 +00:00
#include <sprout/functional/ref.hpp>
#include <sprout/functional/mem_fn.hpp>
2012-11-15 02:54:57 +00:00
#include <sprout/functional/type_traits/weak_result_type.hpp>
#include <sprout/functional/bind/placeholder.hpp>
2012-04-09 16:38:55 +00:00
namespace sprout {
// 20.8.9 bind
//
// is_bind_expression
//
2012-04-09 16:38:55 +00:00
template<typename T>
struct is_bind_expression
: public std::false_type
{};
template<typename T>
struct is_bind_expression<T const>
: public sprout::is_bind_expression<T>
{};
2012-04-13 12:23:49 +00:00
template<typename T>
struct is_bind_expression<T volatile>
: public sprout::is_bind_expression<T>
{};
template<typename T>
struct is_bind_expression<T const volatile>
: public sprout::is_bind_expression<T>
{};
2012-04-09 16:38:55 +00:00
namespace detail {
struct no_tuple_element;
template<std::size_t I, typename Tuple, bool IsSafe>
struct safe_tuple_element_impl
: tuple_element<I, Tuple>
{};
template<std::size_t I, typename Tuple>
struct safe_tuple_element_impl<I, Tuple, false> {
public:
typedef sprout::detail::no_tuple_element type;
};
template<std::size_t I, typename Tuple>
struct safe_tuple_element
: public sprout::detail::safe_tuple_element_impl<I, Tuple, (I < sprout::tuples::tuple_size<Tuple>::value)>
{};
template<
typename Arg,
bool IsBindExp = sprout::is_bind_expression<Arg>::value,
bool IsPlaceholder = (sprout::is_placeholder<Arg>::value > 0)
>
class mu;
template<typename T>
class mu<sprout::reference_wrapper<T>, false, false> {
public:
typedef T& result_type;
public:
template<typename CVRef, typename Tuple>
SPROUT_CONSTEXPR result_type operator()(CVRef& arg, Tuple&) const volatile {
2012-08-13 01:56:48 +00:00
//return arg.get(); // ???
2012-04-09 16:38:55 +00:00
return const_cast<typename std::remove_volatile<typename std::remove_reference<CVRef>::type>::type&>(arg).get();
}
};
template<typename Arg>
class mu<Arg, true, false> {
private:
template<typename CVArg, typename... Args, sprout::index_t... Indexes>
SPROUT_CONSTEXPR auto call(
CVArg& arg,
sprout::tuples::tuple<Args...>& tuple,
sprout::index_tuple<Indexes...>
) const volatile
2012-10-05 15:58:56 +00:00
-> decltype(arg(std::declval<Args>()...))
2012-04-09 16:38:55 +00:00
{
return arg(sprout::forward<Args>(sprout::tuples::get<Indexes>(tuple))...);
}
public:
template<typename CVArg, typename... Args>
SPROUT_CONSTEXPR auto operator()(CVArg& arg, sprout::tuples::tuple<Args...>& tuple) const volatile
2012-10-05 15:58:56 +00:00
-> decltype(arg(std::declval<Args>()...))
2012-04-09 16:38:55 +00:00
{
return call(arg, tuple, sprout::index_pack<Args...>::make());
2012-04-09 16:38:55 +00:00
}
};
template<typename Arg>
class mu<Arg, false, true> {
public:
template<typename Signature>
class result;
template<typename CVMu, typename CVArg, typename Tuple>
class result<CVMu (CVArg, Tuple)> {
private:
typedef typename sprout::detail::safe_tuple_element<
(sprout::is_placeholder<Arg>::value - 1),
Tuple
>::type base_type;
public:
typedef typename std::add_rvalue_reference<base_type>::type type;
};
public:
template<typename Tuple>
typename result<mu (Arg, Tuple)>::type
SPROUT_CONSTEXPR operator()(Arg const volatile&, Tuple& tuple) const volatile {
return sprout::forward<typename result<mu (Arg, Tuple)>::type>(
sprout::tuples::get<(sprout::is_placeholder<Arg>::value - 1)>(tuple)
);
}
};
template<typename Arg>
class mu<Arg, false, false> {
public:
template<typename Signature>
struct result;
template<typename CVMu, typename CVArg, typename Tuple>
struct result<CVMu (CVArg, Tuple)> {
public:
typedef typename std::add_lvalue_reference<CVArg>::type type;
};
public:
template<typename CVArg, typename Tuple>
SPROUT_CONSTEXPR CVArg&& operator()(CVArg&& arg, Tuple&) const volatile {
return sprout::forward<CVArg>(arg);
}
};
template<typename T>
struct maybe_wrap_member_pointer {
public:
typedef T type;
public:
static SPROUT_CONSTEXPR T const& do_wrap(T const& x) {
return x;
}
static SPROUT_CONSTEXPR T&& do_wrap(T&& x) {
return static_cast<T&&>(x);
}
};
template<typename T, typename Class>
struct maybe_wrap_member_pointer<T Class::*> {
public:
typedef sprout::mem_fn_adaptor<T Class::*> type;
public:
static SPROUT_CONSTEXPR type do_wrap(T Class::* pm) {
return type(pm);
}
};
template<>
struct maybe_wrap_member_pointer<void> {
public:
typedef void type;
};
template<std::size_t I, typename... Types>
inline auto volget(sprout::tuples::tuple<Types...> volatile& tuple)
2012-10-05 15:58:56 +00:00
-> typename sprout::tuples::tuple_element<I, sprout::tuples::tuple<Types...> >::type volatile&
2012-04-09 16:38:55 +00:00
{
return sprout::tuples::get<I>(const_cast<sprout::tuples::tuple<Types...>&>(tuple));
}
template<std::size_t I, typename... Types>
inline SPROUT_CONSTEXPR auto volget(sprout::tuples::tuple<Types...> const volatile& tuple)
2012-10-05 15:58:56 +00:00
-> typename sprout::tuples::tuple_element<I, sprout::tuples::tuple<Types...> >::type const volatile&
2012-04-09 16:38:55 +00:00
{
return sprout::tuples::get<I>(const_cast<sprout::tuples::tuple<Types...> const&>(tuple));
}
} // namespace detail
//
// binder
//
template<typename Signature>
2012-06-23 23:22:12 +00:00
class binder;
2012-04-09 16:38:55 +00:00
template<typename Functor, typename... BoundArgs>
class binder<Functor(BoundArgs...)>
: public sprout::weak_result_type<Functor>
{
private:
typedef binder self_type;
typedef typename sprout::index_pack<BoundArgs...>::type bound_indexes;
2012-04-09 16:38:55 +00:00
private:
Functor f_;
sprout::tuples::tuple<BoundArgs...> bound_args_;
private:
template<typename Result, typename... Args, sprout::index_t... Indexes>
Result call(sprout::tuples::tuple<Args...>&& args, sprout::index_tuple<Indexes...>) {
return f_(sprout::detail::mu<BoundArgs>()(sprout::tuples::get<Indexes>(bound_args_), args)...);
}
template<typename Result, typename... Args, sprout::index_t... Indexes>
SPROUT_CONSTEXPR Result call_c(sprout::tuples::tuple<Args...>&& args, sprout::index_tuple<Indexes...>) const {
return f_(sprout::detail::mu<BoundArgs>()(sprout::tuples::get<Indexes>(bound_args_), args)...);
}
template<typename Result, typename... Args, sprout::index_t... Indexes>
Result call_v(sprout::tuples::tuple<Args...>&& args, sprout::index_tuple<Indexes...>) volatile {
return f_(sprout::detail::mu<BoundArgs>()(sprout::detail::volget<Indexes>(bound_args_), args)...);
}
template<typename Result, typename... Args, sprout::index_t... Indexes>
SPROUT_CONSTEXPR Result call_cv(sprout::tuples::tuple<Args...>&& args, sprout::index_tuple<Indexes...>) const volatile {
return f_(sprout::detail::mu<BoundArgs>()(sprout::detail::volget<Indexes>(bound_args_), args)...);
}
public:
2012-04-09 16:38:55 +00:00
template<typename... Args>
explicit SPROUT_CONSTEXPR binder(Functor const& f, Args&&... args)
: f_(f)
, bound_args_(sprout::forward<Args>(args)...)
{}
binder(binder const&) = default;
template<
typename... Args,
typename Result = decltype(
std::declval<Functor>()(
sprout::detail::mu<BoundArgs>()(
std::declval<BoundArgs&>(),
std::declval<sprout::tuples::tuple<Args...>&>()
)...
)
)
>
Result operator()(Args&&... args) {
return call<Result>(
sprout::tuples::forward_as_tuple(sprout::forward<Args>(args)...),
bound_indexes()
);
}
template<
typename... Args,
typename Result = decltype(
std::declval<typename std::enable_if<(sizeof...(Args) >= 0), typename std::add_const<Functor>::type>::type>()(
sprout::detail::mu<BoundArgs>()(
std::declval<BoundArgs const&>(),
std::declval<sprout::tuples::tuple<Args...>&>()
)...
)
)
>
SPROUT_CONSTEXPR Result operator()(Args&&... args) const {
return call_c<Result>(
sprout::tuples::forward_as_tuple(sprout::forward<Args>(args)...),
bound_indexes()
);
}
template<
typename... Args,
typename Result = decltype(
std::declval<typename std::enable_if<(sizeof...(Args) >= 0), typename std::add_volatile<Functor>::type>::type>()(
sprout::detail::mu<BoundArgs>()(
std::declval<BoundArgs volatile&>(),
std::declval<sprout::tuples::tuple<Args...>&>()
)...
)
)
>
Result operator()(Args&&... args) volatile {
return call_v<Result>(
sprout::tuples::forward_as_tuple(sprout::forward<Args>(args)...),
bound_indexes()
);
}
template<
typename... Args,
typename Result = decltype(
std::declval<typename std::enable_if<(sizeof...(Args) >= 0), typename std::add_cv<Functor>::type>::type>()(
sprout::detail::mu<BoundArgs>()(
std::declval<BoundArgs const volatile&>(),
std::declval<sprout::tuples::tuple<Args...>&>()
)...
)
)
>
SPROUT_CONSTEXPR Result operator()(Args&&... args) const volatile {
return call_cv<Result>(
sprout::tuples::forward_as_tuple(sprout::forward<Args>(args)...),
bound_indexes()
);
}
};
//
// bind_result
//
template<typename Result, typename Signature>
2012-06-23 23:22:12 +00:00
class bind_result;
2012-04-09 16:38:55 +00:00
template<typename Result, typename Functor, typename... BoundArgs>
class bind_result<Result, Functor(BoundArgs...)> {
private:
typedef bind_result self_type;
typedef typename sprout::index_pack<BoundArgs...>::type bound_indexes;
2012-04-09 16:38:55 +00:00
private:
template<typename Res>
struct enable_if_void
: public std::enable_if<std::is_void<Res>::value, int>
{};
template<typename Res>
struct disable_if_void
: public std::enable_if<!std::is_void<Res>::value, int>
{};
private:
Functor f_;
sprout::tuples::tuple<BoundArgs...> bound_args_;
private:
template<typename Res, typename... Args, sprout::index_t... Indexes>
Result call(
sprout::tuples::tuple<Args...>&& args,
sprout::index_tuple<Indexes...>,
typename disable_if_void<Res>::type = 0
)
{
return f_(sprout::detail::mu<BoundArgs>()(sprout::tuples::get<Indexes>(bound_args_), args)...);
}
template<typename Res, typename... Args, sprout::index_t... Indexes>
Result call(
sprout::tuples::tuple<Args...>&& args,
sprout::index_tuple<Indexes...>,
typename enable_if_void<Res>::type = 0
)
{
f_(sprout::detail::mu<BoundArgs>()(sprout::tuples::get<Indexes>(bound_args_), args)...);
2012-04-09 16:38:55 +00:00
}
template<typename Res, typename... Args, sprout::index_t... Indexes>
SPROUT_CONSTEXPR Result call(
sprout::tuples::tuple<Args...>&& args,
sprout::index_tuple<Indexes...>,
typename disable_if_void<Res>::type = 0
) const
{
return f_(sprout::detail::mu<BoundArgs>()(sprout::tuples::get<Indexes>(bound_args_), args)...);
}
template<typename Res, typename... Args, sprout::index_t... Indexes>
Result call(
sprout::tuples::tuple<Args...>&& args,
sprout::index_tuple<Indexes...>,
typename enable_if_void<Res>::type = 0
) const
{
f_(sprout::detail::mu<BoundArgs>()(sprout::tuples::get<Indexes>(bound_args_), args)...);
}
template<typename Res, typename... Args, sprout::index_t... Indexes>
Result call(
sprout::tuples::tuple<Args...>&& args,
sprout::index_tuple<Indexes...>,
typename disable_if_void<Res>::type = 0
) volatile
{
return f_(sprout::detail::mu<BoundArgs>()(sprout::detail::volget<Indexes>(bound_args_), args)...);
}
template<typename Res, typename... Args, sprout::index_t... Indexes>
Result call(
sprout::tuples::tuple<Args...>&& args,
sprout::index_tuple<Indexes...>,
typename enable_if_void<Res>::type = 0
) volatile
{
f_(sprout::detail::mu<BoundArgs>()(sprout::detail::volget<Indexes>(bound_args_), args)...);
}
template<typename Res, typename... Args, sprout::index_t... Indexes>
SPROUT_CONSTEXPR Result call(
sprout::tuples::tuple<Args...>&& args,
sprout::index_tuple<Indexes...>,
typename disable_if_void<Res>::type = 0
) const volatile
{
return f_(sprout::detail::mu<BoundArgs>()(sprout::detail::volget<Indexes>(bound_args_), args)...);
}
template<typename Res, typename... Args, sprout::index_t... Indexes>
Result call(
sprout::tuples::tuple<Args...>&& args,
sprout::index_tuple<Indexes...>,
typename enable_if_void<Res>::type = 0
) const volatile
{
f_(sprout::detail::mu<BoundArgs>()(sprout::detail::volget<Indexes>(bound_args_), args)...);
}
public:
typedef Result result_type;
template<typename... Args>
explicit bind_result(Functor const& f, Args&&... args)
: f_(f)
, bound_args_(sprout::forward<Args>(args)...)
{}
bind_result(bind_result const&) = default;
template<typename... Args>
result_type operator()(Args&&... args) {
return call<Result>(
sprout::tuples::forward_as_tuple(sprout::forward<Args>(args)...),
bound_indexes()
);
}
template<typename... Args>
SPROUT_CONSTEXPR result_type operator()(Args&&... args) const {
return call<Result>(
sprout::tuples::forward_as_tuple(sprout::forward<Args>(args)...),
bound_indexes()
);
}
template<typename... Args>
result_type operator()(Args&&... args) volatile {
return call<Result>(
sprout::tuples::forward_as_tuple(sprout::forward<Args>(args)...),
bound_indexes()
);
}
template<typename... Args>
SPROUT_CONSTEXPR result_type operator()(Args&&... args) const volatile {
return call<Result>(
sprout::tuples::forward_as_tuple(sprout::forward<Args>(args)...),
bound_indexes()
);
}
};
//
// is_bind_expression
//
2012-04-09 16:38:55 +00:00
template<typename Signature>
struct is_bind_expression<sprout::binder<Signature> >
: public std::true_type
{};
template<typename Result, typename Signature>
struct is_bind_expression<sprout::bind_result<Result, Signature> >
: public std::true_type
{};
namespace detail {
template<typename Func, typename... BoundArgs>
struct bind_helper {
public:
typedef sprout::detail::maybe_wrap_member_pointer<typename std::decay<Func>::type> maybe_type;
typedef typename maybe_type::type func_type;
typedef sprout::binder<func_type (typename std::decay<BoundArgs>::type...)> type;
};
template<typename Result, typename Func, typename... BoundArgs>
struct bindres_helper {
public:
typedef sprout::detail::maybe_wrap_member_pointer<typename std::decay<Func>::type> maybe_type;
typedef typename maybe_type::type functor_type;
typedef sprout::bind_result<Result, functor_type (typename std::decay<BoundArgs>::type...)> type;
};
} // namespace detail
//
// bind
//
template<typename F, typename... BoundArgs>
inline SPROUT_CONSTEXPR typename sprout::detail::bind_helper<F, BoundArgs...>::type
bind(F&& f, BoundArgs&&... args) {
typedef sprout::detail::bind_helper<F, BoundArgs...> helper_type;
typedef typename helper_type::maybe_type maybe_type;
typedef typename helper_type::type result_type;
return result_type(maybe_type::do_wrap(sprout::forward<F>(f)), sprout::forward<BoundArgs>(args)...);
}
template<typename R, typename F, typename... BoundArgs>
inline SPROUT_CONSTEXPR typename sprout::detail::bindres_helper<R, F, BoundArgs...>::type
bind(F&& f, BoundArgs&&... args) {
typedef sprout::detail::bindres_helper<R, F, BoundArgs...> helper_type;
typedef typename helper_type::maybe_type maybe_type;
typedef typename helper_type::type result_type;
return result_type(maybe_type::do_wrap(sprout::forward<F>(f)), sprout::forward<BoundArgs>(args)...);
}
2012-08-11 12:53:12 +00:00
//
// cbind
//
template<typename F, typename... BoundArgs>
inline SPROUT_CONSTEXPR typename sprout::detail::bind_helper<F, BoundArgs...>::type const
cbind(F&& f, BoundArgs&&... args) {
2012-08-13 14:55:30 +00:00
return sprout::bind(sprout::forward<F>(f), sprout::forward<BoundArgs>(args)...);
2012-08-11 12:53:12 +00:00
}
template<typename R, typename F, typename... BoundArgs>
inline SPROUT_CONSTEXPR typename sprout::detail::bindres_helper<R, F, BoundArgs...>::type const
cbind(F&& f, BoundArgs&&... args) {
2012-08-13 14:55:30 +00:00
return sprout::bind<R>(sprout::forward<F>(f), sprout::forward<BoundArgs>(args)...);
2012-08-11 12:53:12 +00:00
}
2012-04-09 16:38:55 +00:00
} // namespace sprout
#endif // #ifndef SPROUT_FUNCTIONAL_BIND_BIND_HPP