LeechCraft  0.6.70-6645-gcd10d7e
Modular cross-platform feature rich live environment.
dropargs.h
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #pragma once
31 
32 #include "typelist.h"
33 #include "oldcppkludges.h"
34 
35 namespace LeechCraft
36 {
37 namespace Util
38 {
39  namespace detail
40  {
41  template<typename F, template<typename...> class List, typename... Args>
42  constexpr List<Args...> GetInvokablePartImpl (int, List<Args...>, typename std::result_of<F (Args...)>::type* = nullptr)
43  {
44  return {};
45  }
46 
47  template<typename F, template<typename...> class List>
48  constexpr Typelist<> GetInvokablePartImpl (float, List<>)
49  {
50  return {};
51  }
52 
53  template<typename F, typename List>
54  struct InvokableType;
55 
56  template<typename F, template<typename...> class List, typename... Args>
58  {
59  return {};
60  }
61 
62  template<typename F, typename List>
63  struct InvokableType
64  {
65  using RetType_t = decltype (GetInvokablePartImpl<F> (0, List {}));
66  };
67 
68  template<typename F, typename... Args>
69  constexpr auto GetInvokablePart () -> decltype (GetInvokablePartImpl<F> (0, Typelist<Args...> {}))
70  {
71  return {};
72  }
73 
74  template<template<typename...> class List, typename... Args>
75  constexpr size_t Length (List<Args...>)
76  {
77  return sizeof... (Args);
78  }
79 
80  template<typename T>
81  struct Dumbifier
82  {
83  using Type_t = T;
84  };
85 
86  template<typename T>
87  using Dumbify = typename Dumbifier<T>::Type_t;
88 
89  template<typename F, typename List>
91 
92  template<typename F, template<typename...> class List, typename... Args>
93  struct InvokableResGetter<F, List<Args...>>
94  {
95  using RetType_t = ResultOf_t<F (Args...)>;
96  };
97 
98  template<typename F>
99  class Dropper
100  {
101  F F_;
102  public:
103  Dropper (const F& f)
104  : F_ (f)
105  {
106  }
107 
108  template<typename... Args>
109  auto operator() (Args... args) -> typename InvokableResGetter<F, decltype (GetInvokablePart<F, Args...> ())>::RetType_t
110  {
111  auto invokableList = GetInvokablePart<F, Args...> ();
112  auto ignoreList = Drop<Length (decltype (invokableList) {})> (Typelist<Args...> {});
113  return Invoke (invokableList, ignoreList, args...);
114  }
115  private:
116  template<typename... InvokableArgs, typename... Rest>
118  {
119  return F_ (std::forward<InvokableArgs> (args)...);
120  }
121  };
122  }
123 
124  template<typename F>
126  {
127  return detail::Dropper<F> { f };
128  }
129 }
130 }
typename Dumbifier< T >::Type_t Dumbify
Definition: dropargs.h:87
typename std::result_of< T >::type ResultOf_t
Definition: oldcppkludges.h:64
constexpr size_t Length(List< Args... >)
Definition: dropargs.h:75
decltype(GetInvokablePartImpl< F >(0, List{})) RetType_t
Definition: dropargs.h:65
detail::Dropper< F > DropArgs(const F &f)
Definition: dropargs.h:125
constexpr detail::DropImpl< N, List< Args... > >::Result_t Drop(List< Args... >)
Definition: typelist.h:65
constexpr List< Args... > GetInvokablePartImpl(int, List< Args... >, typename std::result_of< F(Args...)>::type *=nullptr)
Definition: dropargs.h:42
auto Invoke(F &&f, Args &&...args) -> decltype(std::forward< F >(f)(std::forward< Args >(args)...))
Definition: oldcppkludges.h:39
constexpr auto GetInvokablePart() -> decltype(GetInvokablePartImpl< F >(0, Typelist< Args... >
Definition: dropargs.h:69