File: invoke.hpp

package info (click to toggle)
libfplus 0.2.13-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,904 kB
  • sloc: cpp: 27,543; javascript: 634; sh: 105; python: 103; makefile: 6
file content (275 lines) | stat: -rw-r--r-- 8,468 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Copyright 2015, Tobias Hermann and the FunctionalPlus contributors.
// https://github.com/Dobiasd/FunctionalPlus
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)

#pragma once

#include <functional>
#include <type_traits>
#include <utility>

#include <fplus/internal/meta.hpp>

// borrowed to libc++
#define FPLUS_INVOKE_RETURN(...) \
  ->decltype(__VA_ARGS__)        \
  {                              \
    return __VA_ARGS__;          \
  }

namespace fplus
{
namespace internal
{
// We need std::invoke to detect callable objects
//
// source:
// http://en.cppreference.com/mwiki/index.php?title=cpp/utility/functional/invoke&oldid=82514
template <typename U>
static std::true_type is_refwrap_test(const std::reference_wrapper<U>&);

template <typename U>
static std::false_type is_refwrap_test(const U&);

template <typename T>
struct is_reference_wrapper : decltype(is_refwrap_test(std::declval<T>()))
{
};

template <typename T, typename U = typename std::decay<T>::type>
struct unwrap_reference_wrapper
{
  using type = T;
};

template <typename T, typename U>
struct unwrap_reference_wrapper<T, std::reference_wrapper<U>>
{
  using type = U&;
};

template <typename T>
using unwrap_reference_wrapper_t = typename unwrap_reference_wrapper<T>::type;

// note: clang only triggers the second static_assert
//      - static_assert(is_invocable<&base_class::non_const_method, const derived_class&>::value, "");
//      - static_assert(is_invocable<&base_class::non_const_method, const base_class&>::value, "");
// GCC triggers both. To workaround this clang bug, we have to manage cv correctness ourselves

template <typename T>
struct is_const_member_function : std::false_type
{
};

// decay doesn't add pointer to abominable functions, don't bother writing them
template <typename R, typename... Args>
struct is_const_member_function<R(Args...) const> : std::true_type
{
};

template <typename R, typename... Args>
struct is_const_member_function<R(Args...) const&> : std::true_type
{
};

template <typename R, typename... Args>
struct is_const_member_function<R(Args...) const&&> : std::true_type
{
};

template <typename R, typename... Args>
struct is_const_member_function<R(Args...) const volatile> : std::true_type
{
};

template <typename R, typename... Args>
struct is_const_member_function<R(Args...) const volatile&> : std::true_type
{
};

template <typename R, typename... Args>
struct is_const_member_function<R(Args...) const volatile&&> : std::true_type
{
};

template <typename T>
struct is_volatile_member_function : std::false_type
{
};

// decay doesn't add pointer to abominable functions, don't bother writing them
template <typename R, typename... Args>
struct is_volatile_member_function<R(Args...) volatile> : std::true_type
{
};

template <typename R, typename... Args>
struct is_volatile_member_function<R(Args...) volatile&> : std::true_type
{
};

template <typename R, typename... Args>
struct is_volatile_member_function<R(Args...) volatile&&> : std::true_type
{
};

template <typename R, typename... Args>
struct is_volatile_member_function<R(Args...) const volatile> : std::true_type
{
};

template <typename R, typename... Args>
struct is_volatile_member_function<R(Args...) const volatile&> : std::true_type
{
};

template <typename R, typename... Args>
struct is_volatile_member_function<R(Args...) const volatile&&> : std::true_type
{
};

template <typename Object, typename Signature>
struct has_correct_cv
{
  // if object has no cv, every method can be called
  // else the method must have the same cv than the object
  static constexpr bool value =
      std::is_same<typename std::remove_cv<Object>::type, Object>::value ||
      ((is_volatile_member_function<Signature>::value ==
        std::is_volatile<Object>::value) &&
       (is_const_member_function<Signature>::value ==
        std::is_const<Object>::value));
};

// pointer to member function - reference to object
template <
    typename Base,
    typename T,
    typename Derived,
    typename... Args,
    typename Unwrapped = unwrap_reference_wrapper_t<Derived>,
    typename std::enable_if<
        is_function<T>::value &&
            has_correct_cv<typename std::remove_reference<Unwrapped>::type, T>::value &&
            std::is_base_of<Base, typename std::decay<Unwrapped>::type>::value,
        int>::type = 0>
inline auto invoke_impl(T Base::*pmf, Derived&& ref, Args&&... args)
    FPLUS_INVOKE_RETURN((std::forward<Unwrapped>(ref).*
                         pmf)(std::forward<Args>(args)...))

// pointer to member function - pointer to object
template <
    typename Base,
    typename T,
    typename Pointer,
    typename... Args,
    typename std::enable_if<
        is_function<T>::value &&
            has_correct_cv<typename std::remove_pointer<
                               typename std::decay<Pointer>::type>::type,
                           T>::value &&
            !std::is_base_of<Base, typename std::decay<Pointer>::type>::value,
        int>::type = 0>
inline auto invoke_impl(T Base::*pmf, Pointer&& ptr, Args&&... args)
    FPLUS_INVOKE_RETURN(((*std::forward<Pointer>(ptr)).*
                         pmf)(std::forward<Args>(args)...))

// pointer to non-static data member - reference to object
template <
    typename Base,
    typename T,
    typename Derived,
    typename Unwrapped = unwrap_reference_wrapper_t<Derived>,
    typename std::enable_if<
        !is_function<T>::value &&
            std::is_base_of<Base, typename std::decay<Unwrapped>::type>::value,
        int>::type = 0>
inline auto invoke_impl(T Base::*pmd, Derived&& ref)
    FPLUS_INVOKE_RETURN((std::forward<Unwrapped>(ref).*pmd))

// pointer to non-static data member - pointer to object
template <
    typename Base,
    typename T,
    typename Pointer,
    typename std::enable_if<
        !is_function<T>::value &&
            !std::is_base_of<Base, typename std::decay<Pointer>::type>::value,
        int>::type = 0>
inline auto invoke_impl(T Base::*pmd, Pointer&& ptr)
    FPLUS_INVOKE_RETURN((*std::forward<Pointer>(ptr)).*pmd)

// normal case - functions, lambdas, function objects
template <typename F,
          typename... Args,
          typename std::enable_if<
              !std::is_member_pointer<typename std::decay<F>::type>::value,
              int>::type = 0>
inline auto invoke_impl(F&& f, Args&&... args)
    FPLUS_INVOKE_RETURN((std::forward<F>(f)(std::forward<Args>(args)...)))

template <typename AlwaysVoid, typename, typename...>
struct invoke_result_impl
{
};

template <typename F, typename... Args>
struct invoke_result_impl<decltype(void(invoke_impl(std::declval<F>(),
                                                    std::declval<Args>()...))),
                          F,
                          Args...>
{
  using type =
      decltype(invoke_impl(std::declval<F>(), std::declval<Args>()...));
};

template <typename F, typename... ArgTypes>
struct invoke_result : invoke_result_impl<void, F, ArgTypes...>
{
};

template <typename F, typename... Args>
using invoke_result_t = typename invoke_result<F, Args...>::type;

// noexcept omitted on purpose, cannot be implemented without C++17.
// GCC 7.1 works with libstdc++, but clang fails, even with latest build,
// on both libstdc++/libc++, I suspect an internal compiler trait is at
// play to make GCC work.
//
// We could detect if C++17 is used and use std::invoke directly.
template <typename F, typename... ArgTypes>
invoke_result_t<F, ArgTypes...> invoke(F&& f, ArgTypes&&... args)
{
  return invoke_impl(std::forward<F>(f), std::forward<ArgTypes>(args)...);
}

// Invoke useful traits (libstdc++ 7.1.0's implementation, ugly-case removed)
template <typename Result, typename ReturnType, typename = void>
struct is_invocable_impl : std::false_type
{
};

template <typename Result, typename ReturnType>
struct is_invocable_impl<Result, ReturnType, void_t<typename Result::type>>
    : disjunction<std::is_void<ReturnType>,
                  std::is_convertible<typename Result::type, ReturnType>>::type
{
};

template <typename F, typename... ArgTypes>
struct is_invocable
    : is_invocable_impl<invoke_result<F, ArgTypes...>, void>::type
{
};

template <typename ReturnType, typename F, typename... ArgTypes>
struct is_invocable_r
    : is_invocable_impl<invoke_result<F, ArgTypes...>, ReturnType>::type
{
};
}
}

#undef FPLUS_INVOKE_RETURN