siplasplas
function.hpp
1 //
2 // Created by manu343726 on 8/12/15.
3 //
4 
5 #ifndef SIPLASPLAS_UTILITY_FUNCTION_HPP
6 #define SIPLASPLAS_UTILITY_FUNCTION_HPP
7 
8 #include "meta.hpp"
9 #include <utility>
10 
11 namespace cpp
12 {
13  template<typename... Fs>
14  struct Function;
15 
16  template<typename F>
17  struct Function<F> : public F
18  {
19  using F::operator();
20 
21  template<typename _F, typename... _Fs>
22  Function(_F&& f, _Fs&&... fs) :
23  F{std::forward<_F>(f)}
24  {}
25  };
26 
27  template<typename R, typename... Args>
28  struct Function<R(Args...)>
29  {
30  template<typename _F>
31  Function(_F&& f) :
32  _f{std::forward<_F>(f)}
33  {}
34 
35  template<typename... _Args>
36  R operator()(_Args&&... args) const
37  {
38  return _f(std::forward<_Args>(args)...);
39  }
40 
41  private:
42  R(*_f)(Args...);
43  };
44 
45  template<typename First, typename Second, typename... Tail>
46  struct Function<First, Second, Tail...> : public First, public Function<Second, Tail...>
47  {
48  using First::operator();
50 
51  template<typename _First, typename _Second, typename... _Tail>
52  Function(_First&& first, _Second&& second, _Tail&&... tail) :
53  First(std::forward<_First>(first)),
54  Function<Second, Tail...>( std::forward<_Second>(second), std::forward<Tail>(tail)...)
55  {}
56  };
57 
58  template<typename... Fs>
59  Function<cpp::meta::decay_t<Fs>...> make_function(Fs&&... fs)
60  {
61  return { std::forward<Fs>(fs)... };
62  }
63 }
64 
65 #endif //SIPLASPLAS_UTILITY_FUNCTION_HPP
Definition: messaging.hpp:8
Definition: function.hpp:14