forked from qicosmos/cosmos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
function_message_bus.hpp
218 lines (180 loc) · 8.33 KB
/
function_message_bus.hpp
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
#include <string>
#include <map>
#include <functional>
#include <cassert>
#include "function_traits.hpp"
#include "type_name.hpp"
template <typename... Args, typename Func, std::size_t... Idx>
void for_each(const std::tuple<Args...>& t, Func&& f, std::index_sequence<Idx...>) {
(void)std::initializer_list<int> { (f(std::get<Idx>(t)), void(), 0)...};
}
struct FnKey {
std::string key;
};
class FunctionMsgBus {
public:
static FunctionMsgBus& get()
{
static FunctionMsgBus instance;
return instance;
}
template<typename Function, typename=std::enable_if_t<!std::is_member_function_pointer<Function>::value>>
void register_handler(const Function& f, std::string const & additional="")
{
using namespace std::placeholders;
auto key = get_key<Function>(additional);
check_duplicate(key);
invokers_[key] = { std::bind(&invoker<Function>::apply, f, _1, _2) };
}
template <typename Function, typename Self, typename = std::enable_if_t<std::is_member_function_pointer<Function>::value>>
void register_handler(Function f, Self* t, std::string const & additional ="") {
using namespace std::placeholders;
auto key = get_key<Function>(additional);
check_duplicate(key);
invokers_[key] = { std::bind(&invoker<Function>::template apply_mem<Self>, f, t, _1, _2) };
}
//non-void function
template <typename T, typename U, typename ... Args>
T call(U&& u, Args&& ... args)
{
std::string key = get_key(std::forward<U>(u), std::forward<Args>(args)...);
auto it = invokers_.find(key);
assert(it != invokers_.end());
T t;
call_impl(it, &t, std::forward<U>(u), std::forward<Args>(args)...);
return t;
}
//void function
template <typename U, typename ... Args>
void call(U&& u, Args&& ... args)
{
std::string key = get_key(std::forward<U>(u), std::forward<Args>(args)...);
auto it = invokers_.find(key);
assert(it != invokers_.end());
call_impl(it, nullptr, std::forward<U>(u), std::forward<Args>(args)...);
}
private:
FunctionMsgBus() {};
FunctionMsgBus(const FunctionMsgBus&) = delete;
FunctionMsgBus(FunctionMsgBus&&) = delete;
template <typename T, typename U, typename ... Args>
void call_impl(T it, void* ptr, U&& u, Args&& ... args){
auto args_tuple = get_args_tuple(std::integral_constant<bool, std::is_same<U, FnKey>::value>{},
std::forward<U>(u), std::forward<Args>(args)...);
using Tuple = decltype(args_tuple);
using storage_type = typename std::aligned_storage<sizeof(Tuple), alignof(Tuple)>::type;
storage_type data;
Tuple* tp = new (&data) Tuple;
*tp = args_tuple;
it->second(tp, ptr);
}
void check_duplicate(const std::string& key){
auto it = invokers_.find(key);
if(it!=invokers_.end())
assert("duplicate register");
}
template<typename Function>
std::string get_key(std::string const & additional) {
if(!additional.empty())
return additional;
using tuple_type = typename function_traits<Function>::bare_tuple_type;
auto key = get_name_from_tuple(tuple_type{});
return key;
}
template <typename U, typename ... Args>
std::enable_if_t<std::is_same<U, FnKey>::value, std::string> get_key(U&& u, Args&& ... args) {
return u.key;
}
template <typename U, typename ... Args>
std::enable_if_t<!std::is_same<U, FnKey>::value, std::string> get_key(U&& u, Args&& ... args) {
return get_name(std::forward<U>(u), std::forward<Args>(args)...);
}
template <typename U, typename ... Args>
auto get_args_tuple(std::true_type, U&& u, Args&& ... args)-> decltype(std::make_tuple(std::forward<Args>(args)...)) {
return std::make_tuple(std::forward<Args>(args)...);
}
template <typename U, typename ... Args>
auto get_args_tuple(std::false_type, U&& u, Args&& ... args) ->decltype(std::make_tuple(std::forward<U>(u), std::forward<Args>(args)...)){
return std::make_tuple(std::forward<U>(u), std::forward<Args>(args)...);
}
template<typename... Args>
std::string get_name(Args&&... args) {
std::string name = "";
std::initializer_list<int>{(name += type_name<Args>(), 0)...};
return name;
}
struct caller{
caller(std::string& name):name_(name){
}
template<typename T>
void operator()(const T& t){
name_+= type_name<T>();
}
std::string& name(){
return name_;
}
std::string& name_;
};
template<typename T>
std::string get_name_from_tuple(T t) {
std::string name = "";
for_each(t, caller{name}, std::make_index_sequence<std::tuple_size<T>::value>{});
return name;
}
template<typename Function>
struct invoker
{
static inline void apply(const Function& func, void* bl, void* result)
{
using tuple_type = typename function_traits<Function>::bare_tuple_type;
const tuple_type* tp = static_cast<tuple_type*>(bl);
call(func, *tp, result);
}
template<typename F, typename ... Args>
static typename std::enable_if<std::is_void<typename std::result_of<F(Args...)>::type>::value>::type
call(const F& f, const std::tuple<Args...>& tp, void*)
{
call_helper(f, std::make_index_sequence<sizeof... (Args)>{}, tp);
}
template<typename F, typename ... Args>
static typename std::enable_if<!std::is_void<typename std::result_of<F(Args...)>::type>::value>::type
call(const F& f, const std::tuple<Args...>& tp, void* result)
{
auto r = call_helper(f, std::make_index_sequence<sizeof... (Args)>{}, tp);
if(result)
*(decltype(r)*)result = r;
}
template<typename F, size_t... I, typename ... Args>
static auto call_helper(const F& f, const std::index_sequence<I...>&, const std::tuple<Args...>& tup)-> typename std::result_of<F(Args...)>::type
{
return f(std::get<I>(tup)...);
}
template <typename Self>
static inline void apply_mem(Function f, Self* self, void* bl, void* result)
{
using tuple_type = typename function_traits<Function>::bare_tuple_type;
const tuple_type* tp = static_cast<tuple_type*>(bl);
using return_type = typename function_traits<Function>::return_type;
call_mem(f, self, *tp, result, std::integral_constant<bool, std::is_void<return_type>::value>{});
}
template<typename F, typename Self, typename ... Args>
static void call_mem(F f, Self* self, const std::tuple<Args...>& tp, void*, std::true_type)
{
call_member_helper(f, self, std::make_index_sequence<sizeof...(Args)>{}, tp);
}
template<typename F, typename Self, typename ... Args>
static void call_mem(F f, Self* self, const std::tuple<Args...>& tp, void* result, std::false_type)
{
auto r = call_member_helper(f, self, std::make_index_sequence<sizeof...(Args)>{}, tp);
if(result)
*(decltype(r)*)result = r;
}
template<typename F, typename Self, size_t... I, typename ... Args>
static auto call_member_helper(F f, Self* self, const std::index_sequence<I...>&, const std::tuple<Args...>& tup)-> decltype((self->*f)(std::get<I>(tup)...))
{
return (self->*f)(std::get<I>(tup)...);
}
};
private:
std::map<std::string, std::function<void(void*, void*)>> invokers_;
};