source: filezilla/trunk/fuentes/src/include/apply.h @ 130

Last change on this file since 130 was 130, checked in by jrpelegrina, 4 years ago

First release to xenial

File size: 1.8 KB
Line 
1#ifndef FILEZILLA_APPLY_HEADER
2#define FILEZILLA_APPLY_HEADER
3
4#include <tuple>
5#include <type_traits>
6
7// apply takes a function and a tuple as argument
8// and calls the function with the tuple's elements as argument
9
10// An integer sequence...
11template<int ...>
12struct int_seq {};
13
14// ... and the machinery to construct it
15template<int N, int ...Seq>
16struct int_seq_gen : int_seq_gen<N - 1, N - 1, Seq...> {};
17
18template<int ...Seq>
19struct int_seq_gen<0, Seq...>
20{
21        typedef int_seq<Seq...> type;
22};
23
24// Apply tuple to ordinary functor
25template<typename F, typename T, int... I>
26auto apply_(F&& f, T&& t, int_seq<I...> const&) -> decltype(std::forward<F>(f)(std::get<I>(std::forward<T>(t))...))
27{
28        return std::forward<F>(f)(std::get<I>(std::forward<T>(t))...);
29}
30
31template<typename F, typename T, typename Seq = typename int_seq_gen<std::tuple_size<typename std::remove_reference<T>::type>::value>::type>
32auto apply(F && f, T&& args) -> decltype(apply_(std::forward<F>(f), std::forward<T>(args), Seq()))
33{
34        return apply_(std::forward<F>(f), std::forward<T>(args), Seq());
35}
36
37// Apply tuple to pointer to member function
38template<typename Obj, typename F, typename T, int... I>
39auto apply_(Obj&& obj, F&& f, T&& t, int_seq<I...> const&) -> decltype((std::forward<Obj>(obj)->*std::forward<F>(f))(std::get<I>(std::forward<T>(t))...))
40{
41        return (std::forward<Obj>(obj)->*std::forward<F>(f))(std::get<I>(std::forward<T>(t))...);
42}
43
44template<typename Obj, typename F, typename T, typename Seq = typename int_seq_gen<std::tuple_size<typename std::remove_reference<T>::type>::value>::type>
45auto apply(Obj&& obj, F && f, T&& args) -> decltype(apply_(std::forward<Obj>(obj), std::forward<F>(f), std::forward<T>(args), Seq()))
46{
47        return apply_(std::forward<Obj>(obj), std::forward<F>(f), std::forward<T>(args), Seq());
48}
49
50#endif
Note: See TracBrowser for help on using the repository browser.