00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #ifndef ABSL_BASE_INTERNAL_INVOKE_H_
00036 #define ABSL_BASE_INTERNAL_INVOKE_H_
00037
00038 #include <algorithm>
00039 #include <type_traits>
00040 #include <utility>
00041
00042
00043
00044
00045 namespace absl {
00046 namespace base_internal {
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 template <typename Derived>
00057 struct StrippedAccept {
00058 template <typename... Args>
00059 struct Accept : Derived::template AcceptImpl<typename std::remove_cv<
00060 typename std::remove_reference<Args>::type>::type...> {};
00061 };
00062
00063
00064
00065
00066 struct MemFunAndRef : StrippedAccept<MemFunAndRef> {
00067 template <typename... Args>
00068 struct AcceptImpl : std::false_type {};
00069
00070 template <typename R, typename C, typename... Params, typename Obj,
00071 typename... Args>
00072 struct AcceptImpl<R (C::*)(Params...), Obj, Args...>
00073 : std::is_base_of<C, Obj> {};
00074
00075 template <typename R, typename C, typename... Params, typename Obj,
00076 typename... Args>
00077 struct AcceptImpl<R (C::*)(Params...) const, Obj, Args...>
00078 : std::is_base_of<C, Obj> {};
00079
00080 template <typename MemFun, typename Obj, typename... Args>
00081 static decltype((std::declval<Obj>().*
00082 std::declval<MemFun>())(std::declval<Args>()...))
00083 Invoke(MemFun&& mem_fun, Obj&& obj, Args&&... args) {
00084 return (std::forward<Obj>(obj).*
00085 std::forward<MemFun>(mem_fun))(std::forward<Args>(args)...);
00086 }
00087 };
00088
00089
00090
00091 struct MemFunAndPtr : StrippedAccept<MemFunAndPtr> {
00092 template <typename... Args>
00093 struct AcceptImpl : std::false_type {};
00094
00095 template <typename R, typename C, typename... Params, typename Ptr,
00096 typename... Args>
00097 struct AcceptImpl<R (C::*)(Params...), Ptr, Args...>
00098 : std::integral_constant<bool, !std::is_base_of<C, Ptr>::value> {};
00099
00100 template <typename R, typename C, typename... Params, typename Ptr,
00101 typename... Args>
00102 struct AcceptImpl<R (C::*)(Params...) const, Ptr, Args...>
00103 : std::integral_constant<bool, !std::is_base_of<C, Ptr>::value> {};
00104
00105 template <typename MemFun, typename Ptr, typename... Args>
00106 static decltype(((*std::declval<Ptr>()).*
00107 std::declval<MemFun>())(std::declval<Args>()...))
00108 Invoke(MemFun&& mem_fun, Ptr&& ptr, Args&&... args) {
00109 return ((*std::forward<Ptr>(ptr)).*
00110 std::forward<MemFun>(mem_fun))(std::forward<Args>(args)...);
00111 }
00112 };
00113
00114
00115
00116
00117 struct DataMemAndRef : StrippedAccept<DataMemAndRef> {
00118 template <typename... Args>
00119 struct AcceptImpl : std::false_type {};
00120
00121 template <typename R, typename C, typename Obj>
00122 struct AcceptImpl<R C::*, Obj> : std::is_base_of<C, Obj> {};
00123
00124 template <typename DataMem, typename Ref>
00125 static decltype(std::declval<Ref>().*std::declval<DataMem>()) Invoke(
00126 DataMem&& data_mem, Ref&& ref) {
00127 return std::forward<Ref>(ref).*std::forward<DataMem>(data_mem);
00128 }
00129 };
00130
00131
00132
00133 struct DataMemAndPtr : StrippedAccept<DataMemAndPtr> {
00134 template <typename... Args>
00135 struct AcceptImpl : std::false_type {};
00136
00137 template <typename R, typename C, typename Ptr>
00138 struct AcceptImpl<R C::*, Ptr>
00139 : std::integral_constant<bool, !std::is_base_of<C, Ptr>::value> {};
00140
00141 template <typename DataMem, typename Ptr>
00142 static decltype((*std::declval<Ptr>()).*std::declval<DataMem>()) Invoke(
00143 DataMem&& data_mem, Ptr&& ptr) {
00144 return (*std::forward<Ptr>(ptr)).*std::forward<DataMem>(data_mem);
00145 }
00146 };
00147
00148
00149 struct Callable {
00150
00151
00152 template <typename F, typename... Args>
00153 static decltype(std::declval<F>()(std::declval<Args>()...)) Invoke(
00154 F&& f, Args&&... args) {
00155 return std::forward<F>(f)(std::forward<Args>(args)...);
00156 }
00157 };
00158
00159
00160 template <typename... Args>
00161 struct Invoker {
00162 typedef typename std::conditional<
00163 MemFunAndRef::Accept<Args...>::value, MemFunAndRef,
00164 typename std::conditional<
00165 MemFunAndPtr::Accept<Args...>::value, MemFunAndPtr,
00166 typename std::conditional<
00167 DataMemAndRef::Accept<Args...>::value, DataMemAndRef,
00168 typename std::conditional<DataMemAndPtr::Accept<Args...>::value,
00169 DataMemAndPtr, Callable>::type>::type>::
00170 type>::type type;
00171 };
00172
00173
00174 template <typename F, typename... Args>
00175 using InvokeT = decltype(Invoker<F, Args...>::type::Invoke(
00176 std::declval<F>(), std::declval<Args>()...));
00177
00178
00179
00180 template <typename F, typename... Args>
00181 InvokeT<F, Args...> Invoke(F&& f, Args&&... args) {
00182 return Invoker<F, Args...>::type::Invoke(std::forward<F>(f),
00183 std::forward<Args>(args)...);
00184 }
00185 }
00186 }
00187
00188 #endif // ABSL_BASE_INTERNAL_INVOKE_H_