Commit 408022cf authored by neverlord's avatar neverlord

apply_tuple

parent c1d882fb
...@@ -148,7 +148,6 @@ nobase_library_include_HEADERS = \ ...@@ -148,7 +148,6 @@ nobase_library_include_HEADERS = \
cppa/get_view.hpp \ cppa/get_view.hpp \
cppa/group.hpp \ cppa/group.hpp \
cppa/intrusive_ptr.hpp \ cppa/intrusive_ptr.hpp \
cppa/invoke.hpp \
cppa/invoke_rules.hpp \ cppa/invoke_rules.hpp \
cppa/object.hpp \ cppa/object.hpp \
cppa/on.hpp \ cppa/on.hpp \
......
...@@ -35,7 +35,6 @@ cppa/util/has_copy_member_fun.hpp ...@@ -35,7 +35,6 @@ cppa/util/has_copy_member_fun.hpp
cppa/detail/intermediate.hpp cppa/detail/intermediate.hpp
cppa/detail/invokable.hpp cppa/detail/invokable.hpp
cppa/invoke_rules.hpp cppa/invoke_rules.hpp
cppa/invoke.hpp
cppa/on.hpp cppa/on.hpp
unit_testing/test__serialization.cpp unit_testing/test__serialization.cpp
cppa/serializer.hpp cppa/serializer.hpp
......
...@@ -34,7 +34,6 @@ ...@@ -34,7 +34,6 @@
#include "cppa/cow_ptr.hpp" #include "cppa/cow_ptr.hpp"
#include "cppa/tuple.hpp" #include "cppa/tuple.hpp"
#include "cppa/tuple_view.hpp"
#include "cppa/detail/abstract_tuple.hpp" #include "cppa/detail/abstract_tuple.hpp"
namespace cppa { namespace cppa {
......
...@@ -40,7 +40,6 @@ ...@@ -40,7 +40,6 @@
#include "cppa/self.hpp" #include "cppa/self.hpp"
#include "cppa/tuple.hpp" #include "cppa/tuple.hpp"
#include "cppa/actor.hpp" #include "cppa/actor.hpp"
#include "cppa/invoke.hpp"
#include "cppa/channel.hpp" #include "cppa/channel.hpp"
#include "cppa/receive.hpp" #include "cppa/receive.hpp"
#include "cppa/announce.hpp" #include "cppa/announce.hpp"
......
...@@ -33,8 +33,9 @@ ...@@ -33,8 +33,9 @@
#include <type_traits> #include <type_traits>
#include "cppa/invoke.hpp"
#include "cppa/util/rm_ref.hpp" #include "cppa/util/rm_ref.hpp"
#include "cppa/util/apply_tuple.hpp"
#include "cppa/detail/tdata.hpp" #include "cppa/detail/tdata.hpp"
#include "cppa/scheduled_actor.hpp" #include "cppa/scheduled_actor.hpp"
...@@ -74,7 +75,7 @@ class ftor_behavior<true, true, F, Args...> : public scheduled_actor ...@@ -74,7 +75,7 @@ class ftor_behavior<true, true, F, Args...> : public scheduled_actor
ftor_behavior(F ptr, Args const&... args) : m_fun(ptr), m_args(args...) { } ftor_behavior(F ptr, Args const&... args) : m_fun(ptr), m_args(args...) { }
virtual void act() { invoke(m_fun, m_args); } virtual void act() { util::apply_tuple(m_fun, m_args); }
}; };
...@@ -120,7 +121,7 @@ class ftor_behavior<false, true, F, Args...> : public scheduled_actor ...@@ -120,7 +121,7 @@ class ftor_behavior<false, true, F, Args...> : public scheduled_actor
{ {
} }
virtual void act() { invoke(m_fun, m_args); } virtual void act() { util::apply_tuple(m_fun, m_args); }
}; };
......
...@@ -35,9 +35,9 @@ ...@@ -35,9 +35,9 @@
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include "cppa/invoke.hpp"
#include "cppa/any_tuple.hpp" #include "cppa/any_tuple.hpp"
#include "cppa/util/duration.hpp" #include "cppa/util/duration.hpp"
#include "cppa/util/apply_tuple.hpp"
#include "cppa/util/fixed_vector.hpp" #include "cppa/util/fixed_vector.hpp"
#include "cppa/detail/matches.hpp" #include "cppa/detail/matches.hpp"
...@@ -131,7 +131,7 @@ class invokable_impl : public invokable ...@@ -131,7 +131,7 @@ class invokable_impl : public invokable
void invoke() // override void invoke() // override
{ {
cppa::invoke(m_target, m_args); util::apply_tuple(m_target, m_args);
} }
}; };
...@@ -156,13 +156,13 @@ class invokable_impl : public invokable ...@@ -156,13 +156,13 @@ class invokable_impl : public invokable
{ {
// "perfect" match; no mapping needed at all // "perfect" match; no mapping needed at all
TupleView tv = TupleView::from(data.vals()); TupleView tv = TupleView::from(data.vals());
cppa::invoke(m_iimpl.m_target, tv); util::apply_tuple(m_iimpl.m_target, tv);
} }
else else
{ {
// mapping needed // mapping needed
TupleView tv(data.vals(), mv); TupleView tv(data.vals(), mv);
cppa::invoke(m_iimpl.m_target, tv); util::apply_tuple(m_iimpl.m_target, tv);
} }
return true; return true;
} }
......
/******************************************************************************\
* ___ __ *
* /\_ \ __/\ \ *
* \//\ \ /\_\ \ \____ ___ _____ _____ __ *
* \ \ \ \/\ \ \ '__`\ /'___\/\ '__`\/\ '__`\ /'__`\ *
* \_\ \_\ \ \ \ \L\ \/\ \__/\ \ \L\ \ \ \L\ \/\ \L\.\_ *
* /\____\\ \_\ \_,__/\ \____\\ \ ,__/\ \ ,__/\ \__/.\_\ *
* \/____/ \/_/\/___/ \/____/ \ \ \/ \ \ \/ \/__/\/_/ *
* \ \_\ \ \_\ *
* \/_/ \/_/ *
* *
* Copyright (C) 2011, 2012 *
* Dominik Charousset <dominik.charousset@haw-hamburg.de> *
* *
* This file is part of libcppa. *
* libcppa is free software: you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation, either version 3 of the License *
* or (at your option) any later version. *
* *
* libcppa is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with libcppa. If not, see <http://www.gnu.org/licenses/>. *
\******************************************************************************/
#ifndef INVOKE_HPP
#define INVOKE_HPP
#include <type_traits>
#include "cppa/get.hpp"
#include "cppa/tuple.hpp"
#include "cppa/any_tuple.hpp"
#include "cppa/tuple_view.hpp"
#include "cppa/util/at.hpp"
#include "cppa/util/type_list.hpp"
#include "cppa/util/element_at.hpp"
#include "cppa/util/callable_trait.hpp"
//#include "cppa/util/reverse_type_list.hpp"
namespace cppa { namespace detail {
template<size_t N, typename F, typename ResultType, class Tuple,
typename ArgTypeList, typename... Args>
struct invoke_helper
{
//typedef typename util::reverse_type_list<ArgTypeList>::type rlist;
typedef typename ArgTypeList::back back;
typedef typename util::element_at<N, Tuple>::type tuple_val_type;
typedef typename util::tl_pop_back<ArgTypeList>::type next_list;
inline static ResultType _(F& f, Tuple const& t, Args const&... args)
{
static_assert(std::is_convertible<tuple_val_type, back>::value,
"tuple element is not convertible to expected argument");
return invoke_helper<N - 1, F, ResultType, Tuple, next_list, tuple_val_type, Args...>
::_(f, t, get<N>(t), args...);
}
};
template<size_t N, typename F, typename ResultType, class Tuple, typename... Args>
struct invoke_helper<N, F, ResultType, Tuple, util::type_list<>, Args...>
{
inline static ResultType _(F& f, Tuple const&, Args const&... args)
{
return f(args...);
}
};
template<bool HasCallableTrait, typename F, class Tuple>
struct invoke_impl;
template<typename F, template<typename...> class Tuple, typename... TTypes>
struct invoke_impl<true, F, Tuple<TTypes...> >
{
static_assert(sizeof...(TTypes) > 0, "empty tuple type");
typedef util::callable_trait<F> trait;
typedef typename trait::arg_types arg_types;
typedef typename trait::result_type result_type;
typedef Tuple<TTypes...> tuple_type;
inline static result_type _(F& f, tuple_type const& t)
{
return invoke_helper<sizeof...(TTypes) - 1, F, result_type, tuple_type, arg_types>::_(f, t);
}
};
template<typename F, template<typename...> class Tuple, typename... TTypes>
struct invoke_impl<false, F, Tuple<TTypes...> >
{
static_assert(sizeof...(TTypes) > 0, "empty tuple type");
typedef util::callable_trait<decltype(&F::operator())> trait;
typedef typename trait::arg_types arg_types;
typedef typename trait::result_type result_type;
typedef Tuple<TTypes...> tuple_type;
inline static result_type _(F& f, tuple_type const& t)
{
return invoke_helper<sizeof...(TTypes) - 1, F, result_type, tuple_type, arg_types>::_(f, t);
}
};
} } // namespace cppa::detail
namespace cppa {
template<typename F, class Tuple>
typename detail::invoke_impl<std::is_function<typename std::remove_pointer<F>::type>::value, F, Tuple>::result_type
invoke(F what, Tuple const& args)
{
typedef typename std::remove_pointer<F>::type f_type;
static constexpr bool is_fun = std::is_function<f_type>::value;
return detail::invoke_impl<is_fun, F, Tuple>::_(what, args);
}
} // namespace cppa
#endif // INVOKE_HPP
...@@ -35,10 +35,10 @@ ...@@ -35,10 +35,10 @@
#include <memory> #include <memory>
#include "cppa/atom.hpp" #include "cppa/atom.hpp"
#include "cppa/invoke.hpp"
#include "cppa/pattern.hpp" #include "cppa/pattern.hpp"
#include "cppa/anything.hpp" #include "cppa/anything.hpp"
#include "cppa/any_tuple.hpp" #include "cppa/any_tuple.hpp"
#include "cppa/tuple_view.hpp"
#include "cppa/invoke_rules.hpp" #include "cppa/invoke_rules.hpp"
#include "cppa/util/duration.hpp" #include "cppa/util/duration.hpp"
......
...@@ -67,6 +67,16 @@ struct apply_tuple_util<X, X, Args...> : apply_tuple_impl<X, Args...> ...@@ -67,6 +67,16 @@ struct apply_tuple_util<X, X, Args...> : apply_tuple_impl<X, Args...>
{ {
}; };
template<>
struct apply_tuple_util<1, 0>
{
template<typename F, class Unused>
static auto apply(F&& f, Unused const&) -> typename get_result_type<F>::type
{
return f();
}
};
template<typename F, template<typename...> class Tuple, typename... T> template<typename F, template<typename...> class Tuple, typename... T>
auto apply_tuple(F&& fun, Tuple<T...>& tup) auto apply_tuple(F&& fun, Tuple<T...>& tup)
-> typename get_result_type<F>::type -> typename get_result_type<F>::type
...@@ -75,8 +85,9 @@ auto apply_tuple(F&& fun, Tuple<T...>& tup) ...@@ -75,8 +85,9 @@ auto apply_tuple(F&& fun, Tuple<T...>& tup)
static constexpr size_t tup_size = sizeof...(T); static constexpr size_t tup_size = sizeof...(T);
static_assert(tup_size >= fun_args::size, static_assert(tup_size >= fun_args::size,
"cannot conjure up additional arguments"); "cannot conjure up additional arguments");
static constexpr size_t from = tup_size - fun_args::size; static constexpr size_t args_size = fun_args::size;
static constexpr size_t to = tup_size - 1; static constexpr size_t from = (args_size > 0) ? tup_size - args_size : 1;
static constexpr size_t to = (args_size > 0) ? tup_size - 1 : 0;
return apply_tuple_util<from, to>::apply(std::forward<F>(fun), tup); return apply_tuple_util<from, to>::apply(std::forward<F>(fun), tup);
} }
...@@ -88,8 +99,9 @@ auto apply_tuple(F&& fun, Tuple<T...> const& tup) ...@@ -88,8 +99,9 @@ auto apply_tuple(F&& fun, Tuple<T...> const& tup)
static constexpr size_t tup_size = sizeof...(T); static constexpr size_t tup_size = sizeof...(T);
static_assert(tup_size >= fun_args::size, static_assert(tup_size >= fun_args::size,
"cannot conjure up additional arguments"); "cannot conjure up additional arguments");
static constexpr size_t from = tup_size - fun_args::size; static constexpr size_t args_size = fun_args::size;
static constexpr size_t to = tup_size - 1; static constexpr size_t from = (args_size > 0) ? tup_size - args_size : 1;
static constexpr size_t to = (args_size > 0) ? tup_size - 1 : 0;
return apply_tuple_util<from, to>::apply(std::forward<F>(fun), tup); return apply_tuple_util<from, to>::apply(std::forward<F>(fun), tup);
} }
......
...@@ -28,7 +28,6 @@ ...@@ -28,7 +28,6 @@
\******************************************************************************/ \******************************************************************************/
#include "cppa/invoke.hpp"
#include "cppa/invoke_rules.hpp" #include "cppa/invoke_rules.hpp"
#include "cppa/util/duration.hpp" #include "cppa/util/duration.hpp"
#include "cppa/detail/invokable.hpp" #include "cppa/detail/invokable.hpp"
......
...@@ -11,7 +11,6 @@ ...@@ -11,7 +11,6 @@
#include "cppa/on.hpp" #include "cppa/on.hpp"
#include "cppa/util.hpp" #include "cppa/util.hpp"
#include "cppa/tuple.hpp" #include "cppa/tuple.hpp"
#include "cppa/invoke.hpp"
#include "cppa/get_view.hpp" #include "cppa/get_view.hpp"
#include "cppa/any_tuple.hpp" #include "cppa/any_tuple.hpp"
#include "cppa/to_string.hpp" #include "cppa/to_string.hpp"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment