forked from snakster/cpp.react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtil.h
More file actions
231 lines (192 loc) · 7.49 KB
/
Util.h
File metadata and controls
231 lines (192 loc) · 7.49 KB
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
// Copyright Sebastian Jeckel 2014.
// 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)
#ifndef REACT_COMMON_UTIL_H_INCLUDED
#define REACT_COMMON_UTIL_H_INCLUDED
#pragma once
#include "react/detail/Defs.h"
#include <tuple>
#include <type_traits>
#include <utility>
/***************************************/ REACT_IMPL_BEGIN /**************************************/
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Unpack tuple - see
/// http://stackoverflow.com/questions/687490/how-do-i-expand-a-tuple-into-variadic-template-functions-arguments
///////////////////////////////////////////////////////////////////////////////////////////////////
template<size_t N>
struct Apply {
template<typename F, typename T, typename... A>
static inline auto apply(F && f, T && t, A &&... a)
-> decltype(Apply<N-1>::apply(std::forward<F>(f), std::forward<T>(t), std::get<N-1>(std::forward<T>(t)), std::forward<A>(a)...))
{
return Apply<N-1>::apply(std::forward<F>(f), std::forward<T>(t), std::get<N-1>(std::forward<T>(t)), std::forward<A>(a)...);
}
};
template<>
struct Apply<0>
{
template<typename F, typename T, typename... A>
static inline auto apply(F && f, T &&, A &&... a)
-> decltype(std::forward<F>(f)(std::forward<A>(a)...))
{
return std::forward<F>(f)(std::forward<A>(a)...);
}
};
template<typename F, typename T>
inline auto apply(F && f, T && t)
-> decltype(Apply<std::tuple_size<typename std::decay<T>::type>::value>::apply(std::forward<F>(f), std::forward<T>(t)))
{
return Apply<std::tuple_size<typename std::decay<T>::type>::value>
::apply(std::forward<F>(f), std::forward<T>(t));
}
template<size_t N>
struct ApplyMemberFn {
template <typename O, typename F, typename T, typename... A>
static inline auto apply(O obj, F f, T && t, A &&... a)
-> decltype(ApplyMemberFn<N-1>::apply(obj, f, std::forward<T>(t), std::get<N-1>(std::forward<T>(t)), std::forward<A>(a)...))
{
return ApplyMemberFn<N-1>::apply(obj, f, std::forward<T>(t), std::get<N-1>(std::forward<T>(t)), std::forward<A>(a)...);
}
};
template<>
struct ApplyMemberFn<0>
{
template<typename O, typename F, typename T, typename... A>
static inline auto apply(O obj, F f, T &&, A &&... a)
-> decltype((obj->*f)(std::forward<A>(a)...))
{
return (obj->*f)(std::forward<A>(a)...);
}
};
template <typename O, typename F, typename T>
inline auto applyMemberFn(O obj, F f, T&& t)
-> decltype(ApplyMemberFn<std::tuple_size<typename std::decay<T>::type>::value>::apply(obj, f, std::forward<T>(t)))
{
return ApplyMemberFn<std::tuple_size<typename std::decay<T>::type>::value>
::apply(obj, f, std::forward<T>(t));
}
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Helper to enable calling a function on each element of an argument pack.
/// We can't do f(args) ...; because ... expands with a comma.
/// But we can do nop_func(f(args) ...);
///////////////////////////////////////////////////////////////////////////////////////////////////
template <typename... TArgs>
inline void pass(TArgs&& ...) {}
///////////////////////////////////////////////////////////////////////////////////////////////////
// Identity (workaround to enable enable decltype()::X)
///////////////////////////////////////////////////////////////////////////////////////////////////
template <typename T>
struct Identity
{
using Type = T;
};
///////////////////////////////////////////////////////////////////////////////////////////////////
/// DontMove!
///////////////////////////////////////////////////////////////////////////////////////////////////
struct DontMove {};
///////////////////////////////////////////////////////////////////////////////////////////////////
/// DisableIfSame
///////////////////////////////////////////////////////////////////////////////////////////////////
template <typename T, typename U>
struct DisableIfSame :
std::enable_if<! std::is_same<
typename std::decay<T>::type,
typename std::decay<U>::type>::value> {};
///////////////////////////////////////////////////////////////////////////////////////////////////
/// AddDummyArgWrapper
///////////////////////////////////////////////////////////////////////////////////////////////////
template <typename TArg, typename F, typename TRet, typename ... TDepValues>
struct AddDummyArgWrapper
{
AddDummyArgWrapper(const AddDummyArgWrapper& other) = default;
AddDummyArgWrapper(AddDummyArgWrapper&& other) :
MyFunc( std::move(other.MyFunc) )
{}
template <typename FIn, class = typename DisableIfSame<FIn,AddDummyArgWrapper>::type>
explicit AddDummyArgWrapper(FIn&& func) : MyFunc( std::forward<FIn>(func) ) {}
TRet operator()(TArg, TDepValues& ... args)
{
return MyFunc(args ...);
}
F MyFunc;
};
template <typename TArg, typename F, typename ... TDepValues>
struct AddDummyArgWrapper<TArg,F,void,TDepValues...>
{
public:
AddDummyArgWrapper(const AddDummyArgWrapper& other) = default;
AddDummyArgWrapper(AddDummyArgWrapper&& other) :
MyFunc( std::move(other.MyFunc) )
{}
template <typename FIn, class = typename DisableIfSame<FIn,AddDummyArgWrapper>::type>
explicit AddDummyArgWrapper(FIn&& func) : MyFunc( std::forward<FIn>(func) ) {}
void operator()(TArg, TDepValues& ... args)
{
MyFunc(args ...);
}
F MyFunc;
};
///////////////////////////////////////////////////////////////////////////////////////////////////
/// AddDefaultReturnValueWrapper
///////////////////////////////////////////////////////////////////////////////////////////////////
template
<
typename F,
typename TRet,
TRet return_value
>
struct AddDefaultReturnValueWrapper
{
AddDefaultReturnValueWrapper(const AddDefaultReturnValueWrapper&) = default;
AddDefaultReturnValueWrapper(AddDefaultReturnValueWrapper&& other) :
MyFunc( std::move(other.MyFunc) )
{}
template
<
typename FIn,
class = typename DisableIfSame<FIn,AddDefaultReturnValueWrapper>::type
>
explicit AddDefaultReturnValueWrapper(FIn&& func) :
MyFunc( std::forward<FIn>(func) )
{}
template <typename ... TArgs>
TRet operator()(TArgs&& ... args)
{
MyFunc(std::forward<TArgs>(args) ...);
return return_value;
}
F MyFunc;
};
///////////////////////////////////////////////////////////////////////////////////////////////////
/// IsCallableWith
///////////////////////////////////////////////////////////////////////////////////////////////////
template
<
typename F,
typename TRet,
typename ... TArgs
>
class IsCallableWith
{
private:
using NoT = char[1];
using YesT = char[2];
template
<
typename U,
class = decltype(
static_cast<TRet>(
(std::declval<U>())(std::declval<TArgs>() ...)))
>
static YesT& check(void*);
template <typename U>
static NoT& check(...);
public:
enum { value = sizeof(check<F>(nullptr)) == sizeof(YesT) };
};
/****************************************/ REACT_IMPL_END /***************************************/
// Expand args by wrapping them in a dummy function
// Use comma operator to replace potential void return value with 0
#define REACT_EXPAND_PACK(...) REACT_IMPL::pass((__VA_ARGS__ , 0) ...)
#endif // REACT_COMMON_UTIL_H_INCLUDED