summaryrefslogtreecommitdiff
path: root/absl/base/internal/invoke.h
blob: ab5ad91025946b40d05ce135e312e3196a16be83 (plain)
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
// Copyright 2017 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// absl::base_internal::Invoke(f, args...) is an implementation of
// INVOKE(f, args...) from section [func.require] of the C++ standard.
//
// [func.require]
// Define INVOKE (f, t1, t2, ..., tN) as follows:
// 1. (t1.*f)(t2, ..., tN) when f is a pointer to a member function of a class T
//    and is_base_of_v<T, remove_reference_t<decltype(t1)>> is true;
// 2. (t1.get().*f)(t2, ..., tN) when f is a pointer to a member function of a
//    class T and remove_cvref_t<decltype(t1)> is a specialization of
//    reference_wrapper;
// 3. ((*t1).*f)(t2, ..., tN) when f is a pointer to a member function of a
//    class T and t1 does not satisfy the previous two items;
// 4. t1.*f when N == 1 and f is a pointer to data member of a class T and
//    is_base_of_v<T, remove_reference_t<decltype(t1)>> is true;
// 5. t1.get().*f when N == 1 and f is a pointer to data member of a class T and
//    remove_cvref_t<decltype(t1)> is a specialization of reference_wrapper;
// 6. (*t1).*f when N == 1 and f is a pointer to data member of a class T and t1
//    does not satisfy the previous two items;
// 7. f(t1, t2, ..., tN) in all other cases.
//
// The implementation is SFINAE-friendly: substitution failure within Invoke()
// isn't an error.

#ifndef ABSL_BASE_INTERNAL_INVOKE_H_
#define ABSL_BASE_INTERNAL_INVOKE_H_

#include <algorithm>
#include <type_traits>
#include <utility>

#include "absl/meta/type_traits.h"

// The following code is internal implementation detail.  See the comment at the
// top of this file for the API documentation.

namespace absl {
ABSL_NAMESPACE_BEGIN
namespace base_internal {

template <typename T>
struct IsReferenceWrapper : std::false_type {};
template <typename T>
struct IsReferenceWrapper<std::reference_wrapper<T>> : std::true_type {};

template <typename T>
using RemoveCvrefT =
    typename std::remove_cv<typename std::remove_reference<T>::type>::type;

// The seven classes below each implement one of the clauses from the definition
// of INVOKE. The inner class template Accept<F, Args...> checks whether the
// clause is applicable; static function template Invoke(f, args...) does the
// invocation.
//
// By separating the clause selection logic from invocation we make sure that
// Invoke() does exactly what the standard says.

template <typename Derived>
struct StrippedAccept {
  template <typename... Args>
  struct Accept : Derived::template AcceptImpl<typename std::remove_cv<
                      typename std::remove_reference<Args>::type>::type...> {};
};

// (t1.*f)(t2, ..., tN) when f is a pointer to a member function of a class T
// and t1 is an object of type T or a reference to an object of type T or a
// reference to an object of a type derived from T.
struct MemFunAndRef : StrippedAccept<MemFunAndRef> {
  template <typename... Args>
  struct AcceptImpl : std::false_type {};

  template <typename MemFunType, typename C, typename Obj, typename... Args>
  struct AcceptImpl<MemFunType C::*, Obj, Args...>
      : std::integral_constant<
            bool, std::is_base_of<
                      C, typename std::remove_reference<Obj>::type>::value &&
                      absl::is_function<MemFunType>::value> {};

  template <typename MemFun, typename Obj, typename... Args>
  static decltype((std::declval<Obj>().*
                   std::declval<MemFun>())(std::declval<Args>()...))
  Invoke(MemFun&& mem_fun, Obj&& obj, Args&&... args) {
    return (std::forward<Obj>(obj).*
            std::forward<MemFun>(mem_fun))(std::forward<Args>(args)...);
  }
};

// (t1.get().*f)(t2, ..., tN) when f is a pointer to a member function of a
// class T and remove_cvref_t<decltype(t1)> is a specialization of
// reference_wrapper;
struct MemFunAndRefWrap : StrippedAccept<MemFunAndRefWrap> {
  template <typename... Args>
  struct AcceptImpl : std::false_type {};

  template <typename MemFunType, typename C, typename RefWrap, typename... Args>
  struct AcceptImpl<MemFunType C::*, RefWrap, Args...>
      : std::integral_constant<
            bool, IsReferenceWrapper<RemoveCvrefT<RefWrap>>::value &&
                      absl::is_function<MemFunType>::value> {};

  template <typename MemFun, typename RefWrap, typename... Args>
  static decltype((std::declval<RefWrap>().get().*
                   std::declval<MemFun>())(std::declval<Args>()...))
  Invoke(MemFun&& mem_fun, RefWrap&& ref_wrap, Args&&... args) {
    return (std::forward<RefWrap>(ref_wrap).get().*
            std::forward<MemFun>(mem_fun))(std::forward<Args>(args)...);
  }
};

// ((*t1).*f)(t2, ..., tN) when f is a pointer to a member function of a
// class T and t1 does not satisfy the previous two items;
struct MemFunAndPtr : StrippedAccept<MemFunAndPtr> {
  template <typename... Args>
  struct AcceptImpl : std::false_type {};

  template <typename MemFunType, typename C, typename Ptr, typename... Args>
  struct AcceptImpl<MemFunType C::*, Ptr, Args...>
      : std::integral_constant<
            bool, !std::is_base_of<
                      C, typename std::remove_reference<Ptr>::type>::value &&
                      !IsReferenceWrapper<RemoveCvrefT<Ptr>>::value &&
                      absl::is_function<MemFunType>::value> {};

  template <typename MemFun, typename Ptr, typename... Args>
  static decltype(((*std::declval<Ptr>()).*
                   std::declval<MemFun>())(std::declval<Args>()...))
  Invoke(MemFun&& mem_fun, Ptr&& ptr, Args&&... args) {
    return ((*std::forward<Ptr>(ptr)).*
            std::forward<MemFun>(mem_fun))(std::forward<Args>(args)...);
  }
};

// t1.*f when N == 1 and f is a pointer to data member of a class T and
// is_base_of_v<T, remove_reference_t<decltype(t1)>> is true;
struct DataMemAndRef : StrippedAccept<DataMemAndRef> {
  template <typename... Args>
  struct AcceptImpl : std::false_type {};

  template <typename R, typename C, typename Obj>
  struct AcceptImpl<R C::*, Obj>
      : std::integral_constant<
            bool, std::is_base_of<
                      C, typename std::remove_reference<Obj>::type>::value &&
                      !absl::is_function<R>::value> {};

  template <typename DataMem, typename Ref>
  static decltype(std::declval<Ref>().*std::declval<DataMem>()) Invoke(
      DataMem&& data_mem, Ref&& ref) {
    return std::forward<Ref>(ref).*std::forward<DataMem>(data_mem);
  }
};

// t1.get().*f when N == 1 and f is a pointer to data member of a class T and
// remove_cvref_t<decltype(t1)> is a specialization of reference_wrapper;
struct DataMemAndRefWrap : StrippedAccept<DataMemAndRefWrap> {
  template <typename... Args>
  struct AcceptImpl : std::false_type {};

  template <typename R, typename C, typename RefWrap>
  struct AcceptImpl<R C::*, RefWrap>
      : std::integral_constant<
            bool, IsReferenceWrapper<RemoveCvrefT<RefWrap>>::value &&
                      !absl::is_function<R>::value> {};

  template <typename DataMem, typename RefWrap>
  static decltype(std::declval<RefWrap>().get().*std::declval<DataMem>())
  Invoke(DataMem&& data_mem, RefWrap&& ref_wrap) {
    return std::forward<RefWrap>(ref_wrap).get().*
           std::forward<DataMem>(data_mem);
  }
};

// (*t1).*f when N == 1 and f is a pointer to data member of a class T and t1
// does not satisfy the previous two items;
struct DataMemAndPtr : StrippedAccept<DataMemAndPtr> {
  template <typename... Args>
  struct AcceptImpl : std::false_type {};

  template <typename R, typename C, typename Ptr>
  struct AcceptImpl<R C::*, Ptr>
      : std::integral_constant<
            bool, !std::is_base_of<
                      C, typename std::remove_reference<Ptr>::type>::value &&
                      !IsReferenceWrapper<RemoveCvrefT<Ptr>>::value &&
                      !absl::is_function<R>::value> {};

  template <typename DataMem, typename Ptr>
  static decltype((*std::declval<Ptr>()).*std::declval<DataMem>()) Invoke(
      DataMem&& data_mem, Ptr&& ptr) {
    return (*std::forward<Ptr>(ptr)).*std::forward<DataMem>(data_mem);
  }
};

// f(t1, t2, ..., tN) in all other cases.
struct Callable {
  // Callable doesn't have Accept because it's the last clause that gets picked
  // when none of the previous clauses are applicable.
  template <typename F, typename... Args>
  static decltype(std::declval<F>()(std::declval<Args>()...)) Invoke(
      F&& f, Args&&... args) {
    return std::forward<F>(f)(std::forward<Args>(args)...);
  }
};

// Resolves to the first matching clause.
template <typename... Args>
struct Invoker {
  typedef typename std::conditional<
      MemFunAndRef::Accept<Args...>::value, MemFunAndRef,
      typename std::conditional<
          MemFunAndRefWrap::Accept<Args...>::value, MemFunAndRefWrap,
          typename std::conditional<
              MemFunAndPtr::Accept<Args...>::value, MemFunAndPtr,
              typename std::conditional<
                  DataMemAndRef::Accept<Args...>::value, DataMemAndRef,
                  typename std::conditional<
                      DataMemAndRefWrap::Accept<Args...>::value,
                      DataMemAndRefWrap,
                      typename std::conditional<
                          DataMemAndPtr::Accept<Args...>::value, DataMemAndPtr,
                          Callable>::type>::type>::type>::type>::type>::type
      type;
};

// The result type of Invoke<F, Args...>.
template <typename F, typename... Args>
using InvokeT = decltype(Invoker<F, Args...>::type::Invoke(
    std::declval<F>(), std::declval<Args>()...));

// Invoke(f, args...) is an implementation of INVOKE(f, args...) from section
// [func.require] of the C++ standard.
template <typename F, typename... Args>
InvokeT<F, Args...> Invoke(F&& f, Args&&... args) {
  return Invoker<F, Args...>::type::Invoke(std::forward<F>(f),
                                           std::forward<Args>(args)...);
}
}  // namespace base_internal
ABSL_NAMESPACE_END
}  // namespace absl

#endif  // ABSL_BASE_INTERNAL_INVOKE_H_