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
212 lines (178 loc) · 6.21 KB
/
Util.h
File metadata and controls
212 lines (178 loc) · 6.21 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
// 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)
#pragma once
#include "react/detail/Defs.h"
#include <assert.h>
#include <cstddef>
#include <ctime>
#include <functional>
#include <iostream>
#include <random>
#include <string>
#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&& ...) {}
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Format print bits
///////////////////////////////////////////////////////////////////////////////////////////////////
//assumes little endian
inline void PrintBits(size_t const size, void const* const p)
{
unsigned char *b = (unsigned char*) p;
unsigned char byte;
for (int i=size-1; i>=0; i--)
{
for (int j=7; j>=0; j--)
{
byte = b[i] & (1<<j);
byte >>= j;
printf("%u", byte);
if (!(j & 0x1))
printf(" ");
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// Identity (workaround to enable enable decltype()::X)
///////////////////////////////////////////////////////////////////////////////////////////////////
template <typename T>
struct Identity
{
using Type = T;
};
///////////////////////////////////////////////////////////////////////////////////////////////////
// Generic RAII scope guard
///////////////////////////////////////////////////////////////////////////////////////////////////
template <typename F>
class ScopeGuard
{
public:
explicit ScopeGuard(F func) :
func_{ std::move(func) }
{
}
~ScopeGuard() { func_(); }
ScopeGuard() = delete;
ScopeGuard(const ScopeGuard&) = delete;
ScopeGuard& operator=(const ScopeGuard&) = delete;
private:
F func_;
};
enum class ScopeGuardDummy_ {};
template <typename F>
ScopeGuard<F> operator+(ScopeGuardDummy_, F&& func)
{
return ScopeGuard<F>(std::forward<F>(func));
}
//
template <typename T>
class MoveBindWrapper
{
public:
explicit MoveBindWrapper(T&& v):
v_{ std::forward<T>(v) }
{}
template <typename ... U>
T&& operator()(U&& ...)
{
return std::forward<T>(v_);
}
private:
T v_;
};
template <typename T>
MoveBindWrapper<T> MoveIntoBind(T&& t)
{
return MoveBindWrapper<T>{std::forward<T>(t)};
}
/****************************************/ REACT_IMPL_END /***************************************/
namespace std
{
template <typename T>
struct is_bind_expression<REACT_IMPL::MoveBindWrapper<T>> : std::true_type {};
}
// 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) ...)
// Concat
#define REACT_CONCAT_(l, r) l##r
#define REACT_CONCAT(l, r) REACT_CONCAT_(l, r)
// Anon var
#ifdef __COUNTER__
#define REACT_ANON_VAR(prefix) \
REACT_CONCAT(prefix, __COUNTER__)
#else
#define REACT_ANON_VAR(s) \
REACT_CONCAT(prefix, __LINE__)
#endif
// Scope guard helper
#define REACT_SCOPE_EXIT_PREFIX scopeExitGuard_
#define REACT_SCOPE_EXIT \
auto REACT_ANON_VAR(REACT_SCOPE_EXIT_PREFIX) \
= REACT_IMPL::ScopeGuardDummy_() + [&]()