-
Notifications
You must be signed in to change notification settings - Fork 1
/
ep.hpp
193 lines (165 loc) · 5.94 KB
/
ep.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
#ifndef CHARMLITE_CORE_EP_HPP
#define CHARMLITE_CORE_EP_HPP
#include <charmlite/core/common.hpp>
#include <charmlite/utilities/traits.hpp>
#include <typeindex>
#include <typeinfo>
namespace cmk {
inline const entry_record_* record_for(entry_id_t id)
{
if (id == cmk::helper_::nil_entry_)
{
return nullptr;
}
else
{
return &(CMK_ACCESS_SINGLETON(entry_table_)[id - 1]);
}
}
template <entry_fn_t Fn, bool Constructor>
struct entry_fn_helper_
{
static entry_id_t id_;
};
template <auto Fn>
struct entry_fn_impl_
{
private:
template <typename... Args_>
static void func_invoker_impl_helper(void* self, Args_&&... args)
{
using template_t =
typename cmk::generate_marshall_msg<decltype(Fn)>::template_t;
(static_cast<template_t*>(self)->*Fn)(std::forward<Args_>(args)...);
}
template <typename Tuple, std::size_t... Indices>
static void func_invoker_impl(
void* self, Tuple&& t, std::index_sequence<Indices...>)
{
func_invoker_impl_helper(
self, std::get<Indices>(std::forward<Tuple>(t))...);
}
template <typename Tuple>
static void func_invoker(void* self, Tuple&& t)
{
func_invoker_impl(self, std::forward<Tuple>(t),
std::make_index_sequence<
std::tuple_size<std::decay_t<Tuple>>::value>());
}
public:
static void call_(void* self, message_ptr<>&& msg)
{
if constexpr (cmk::is_member_fn_t<decltype(Fn)>::value)
{
if constexpr (cmk::is_message<typename cmk::extract_message<
decltype(Fn)>::type>::value)
{
using message_t =
typename cmk::extract_message<decltype(Fn)>::type;
using template_t =
typename cmk::extract_message<decltype(Fn)>::template_t;
auto* typed = static_cast<message_t*>(msg.release());
message_ptr<message_t> owned(typed);
(static_cast<template_t*>(self)->*Fn)(std::move(owned));
}
else
{
CmiAbort("entry_fn_impl_::call_ called with incompatible "
"message type");
}
}
else if constexpr (cmk::is_member_fn_args_t<decltype(Fn)>::value)
{
using message_t =
typename cmk::generate_marshall_msg<decltype(Fn)>::type;
using tuple_t = typename cmk::generate_marshall_msg<
decltype(Fn)>::tuple_type;
auto* typed = static_cast<message_t*>(msg.release());
tuple_t t{};
// Unpack to tuple_t
PUP::fromMem unpacker(((char*) (typed)) + sizeof(message));
impl::args_unfolder(unpacker, t);
func_invoker(self, t);
}
else
{
CmiAbort("no matching call to compatible functiono call.");
}
}
static const entry_id_t& id_(void)
{
return entry_fn_helper_<(&call_), false>::id_;
}
};
template <typename Chare, typename Argument>
struct constructor_caller_
{
private:
template <typename... Args_>
static void func_invoker_impl_helper(void* self, Args_&&... args)
{
new (static_cast<Chare*>(self)) Chare(std::forward<Args_>(args)...);
}
template <typename Tuple, std::size_t... Indices>
static void func_invoker_impl(
void* self, Tuple&& t, std::index_sequence<Indices...>)
{
func_invoker_impl_helper(
self, std::get<Indices>(std::forward<Tuple>(t))...);
}
template <typename Tuple>
static void func_invoker(void* self, Tuple&& t)
{
func_invoker_impl(self, std::forward<Tuple>(t),
std::make_index_sequence<
std::tuple_size<std::decay_t<Tuple>>::value>());
}
public:
auto operator()(void* self, message_ptr<>&& msg)
{
if constexpr (cmk::is_marshall_type_v<std::decay_t<Argument>>)
{
using Message = cmk::get_message_t<Argument>;
auto* typed = static_cast<Message*>(msg.release());
using tuple_t = marshall_args_t<Message>;
tuple_t t{};
// Unpack to tuple_t
PUP::fromMem unpacker(((char*) (typed)) + sizeof(message));
impl::args_unfolder(unpacker, t);
func_invoker(self, t);
}
else if constexpr (cmk::message_compatibility_v<Argument>)
{
using Message = cmk::get_message_t<Argument>;
auto* typed = static_cast<Message*>(msg.release());
message_ptr<Message> owned(typed);
new (static_cast<Chare*>(self)) Chare(std::move(owned));
}
else if constexpr (std::is_same_v<Argument, void>)
{
new (static_cast<Chare*>(self)) Chare;
}
else
{
CmiAbort("constructor_caller_ called with a message pointer of "
"non-message type");
}
}
};
template <typename T, typename Arg>
void call_constructor_(void* self, message_ptr<>&& msg)
{
constructor_caller_<T, Arg>()(self, std::move(msg));
}
template <auto Fn>
entry_id_t entry(void)
{
return entry_fn_impl_<Fn>::id_();
}
template <typename T, typename Message>
entry_id_t constructor(void)
{
return entry_fn_helper_<(&call_constructor_<T, Message>), true>::id_;
}
} // namespace cmk
#endif