XRootD
XrdClApply.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // Copyright (c) 2011-2012 by European Organization for Nuclear Research (CERN)
3 // Author: Michal Simon <michal.simon@cern.ch>
4 //------------------------------------------------------------------------------
5 // XRootD is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // XRootD is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with XRootD. If not, see <http://www.gnu.org/licenses/>.
17 //------------------------------------------------------------------------------
18 
19 #ifndef SRC_XRDCL_XRDCLAPPLY_HH_
20 #define SRC_XRDCL_XRDCLAPPLY_HH_
21 
22 #include <functional>
23 #include <tuple>
24 
25 namespace XrdCl
26 {
27 
28  // This is the type which holds sequences
29  template<int ... Is> struct sequence {};
30 
31  // First define the template signature
32  template <int ... Ns> struct seq_gen;
33 
34  // Recursion case
35  template <int I, int ... Ns>
36  struct seq_gen<I, Ns...>
37  {
38  using type = typename seq_gen<I - 1, I - 1, Ns...>::type;
39  };
40 
41  // Recursion abort
42  template <int ... Ns>
43  struct seq_gen<0, Ns...>
44  {
45  using type = sequence<Ns...>;
46  };
47 
48  template <typename FUNC, typename TUPL, int ... INDICES>
49  inline static auto tuple_call_impl( FUNC &func, TUPL &args, sequence<INDICES...> ) ->
50  decltype( func( std::move( std::get<INDICES>( args ) )... ) )
51  {
52  return func( std::move( std::get<INDICES>( args ) )... );
53  }
54 
55  //---------------------------------------------------------------------------
56  // Applies tuple members as arguments to the function
57  //
58  // @param func : function to be called
59  // @param tup : tuple with the packed argumnets
60  //
61  // Note: Once we can use c++17 we can move to std::apply
62  //---------------------------------------------------------------------------
63  template <typename FUNC, typename ... ARGs>
64  inline static auto Apply( FUNC &&func, std::tuple<ARGs...> &tup ) ->
65  decltype( tuple_call_impl( func, tup, typename seq_gen<sizeof...(ARGs)>::type{} ) )
66  {
67  return tuple_call_impl( func, tup, typename seq_gen<sizeof...(ARGs)>::type{} );
68  }
69 
70  //---------------------------------------------------------------------------
71  // Applies tuple members as arguments to the function
72  //
73  // @param method : method to be called
74  // @param obj : the object to call the method on
75  // @param tup : tuple with the packed argumnets
76  //
77  // Note: Once we can use c++17 we can move to std::apply
78  //---------------------------------------------------------------------------
79  template <typename METH, typename OBJ, typename ... ARGs>
80  inline static auto Apply( METH &&method, OBJ &obj, std::tuple<ARGs...> &tup ) ->
81  decltype( Apply( std::bind( method, &obj, std::placeholders::_1, std::placeholders::_2 ), tup ) )
82  {
83  return Apply( std::bind( method, &obj, std::placeholders::_1, std::placeholders::_2 ), tup );
84  }
85 
86 }
87 
88 #endif /* SRC_XRDCL_XRDCLAPPLY_HH_ */
#define I(x)
static auto Apply(FUNC &&func, std::tuple< ARGs... > &tup) -> decltype(tuple_call_impl(func, tup, typename seq_gen< sizeof...(ARGs)>::type{}))
Definition: XrdClApply.hh:64
static auto tuple_call_impl(FUNC &func, TUPL &args, sequence< INDICES... >) -> decltype(func(std::move(std::get< INDICES >(args))...))
Definition: XrdClApply.hh:49
typename seq_gen< I - 1, I - 1, Ns... >::type type
Definition: XrdClApply.hh:38