-
Notifications
You must be signed in to change notification settings - Fork 0
/
bomba_dynamic_object.hpp
204 lines (172 loc) · 5.34 KB
/
bomba_dynamic_object.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
#ifndef BOMBA_DYNAMIC_OBJECT
#define BOMBA_DYNAMIC_OBJECT
#ifndef BOMBA_CORE // Needed to run in godbolt
#include "bomba_core.hpp"
#endif
#ifndef BOMBA_RPC_OBJECT // Needed to run in godbolt
#include "bomba_rpc_object.hpp"
#endif
#include <vector>
namespace Bomba {
template <typename T>
concept IsAFunctor = requires() {
&std::decay_t<T>::operator();
};
class RpcLambdaHolder {
union {
IRemoteCallable* held = nullptr;
std::array<char, sizeof(RpcStatelessLambda<[] {}>) + 3 * sizeof(void*)> local;
} _content;
const Detail::RpcLambdaInformationHolder* _info = nullptr; // Not nullptr if it's leaf
bool _isLocal = {};
bool _owning = {};
void reset() {
if (!_isLocal && _owning && _content.held) {
delete _content.held;
_content.held = 0;
}
}
RpcLambdaHolder(const RpcLambdaHolder&) = delete;
void operator=(const RpcLambdaHolder&) = delete;
RpcLambdaHolder() = default;
public:
template <IsAFunctor LambdaType>
RpcLambdaHolder(LambdaType&& from)
: _isLocal(sizeof(std::decay_t<RpcLambda<std::decay_t<LambdaType>>>) <= sizeof(_content.local) &&
std::is_trivially_destructible_v<LambdaType>)
, _owning(true)
{
//_descriptionFiller = RpcLambda<std::decay_t<LambdaType>>;
if (_isLocal) {
auto made = new (&_content.local) RpcLambda<std::decay_t<LambdaType>>(std::forward<LambdaType>(from));
_info = &made->info;
} else {
auto made = new RpcLambda<std::decay_t<LambdaType>>(std::forward<LambdaType>(from));
_info = &made->info;
_content.held = made;
}
}
template <std::derived_from<IRemoteCallable> T>
RpcLambdaHolder(T&& instance)
: _isLocal(false)
, _owning(true)
{
_content.held = new T(std::move<T>(instance));
}
RpcLambdaHolder(RpcLambdaHolder&& other) {
operator=(std::forward<RpcLambdaHolder>(other));
}
~RpcLambdaHolder() {
reset();
}
RpcLambdaHolder& operator=(RpcLambdaHolder&& other) {
reset();
_isLocal = other._isLocal;
_info = other._info;
_owning = other._owning;
if (_isLocal) {
memcpy(&_content.local, &other._content.local, sizeof(_content.local));
} else {
_content.held = other._content.held;
other._content.held = nullptr;
}
return *this;
}
IRemoteCallable* operator->() const {
if (_isLocal) {
return const_cast<IRemoteCallable*>(reinterpret_cast<const IRemoteCallable*>(&_content.local));
} else {
return _content.held;
}
}
IRemoteCallable& operator*() const { return *operator->(); }
bool isLocal() const {
return _isLocal;
}
void listTypes(ISerialisableDescriptionFiller& filler) const {
_info->subtypesAdder(filler);
}
void addToDescription(std::string_view name, IRemoteCallableDescriptionFiller& filler) const {
_info->unnamedChildAdder(name, filler);
}
static RpcLambdaHolder nonOwning(IRemoteCallable& referree) {
RpcLambdaHolder made;
made._owning = false;
made._isLocal = false;
made._content.held = &referree;
return made;
}
};
template <typename Invalid>
class TypedRpcLambdaHolder {
static_assert(std::is_same_v<Invalid, Invalid>, "Template arguments to TypedRpcLambdaHolder must be in the form Returned(Arg1, Arg2, AndSoOn)");
};
template <typename Returned, typename... Args>
class TypedRpcLambdaHolder<Returned(Args...)> : public RpcLambdaHolder {
Returned (*call)(IRemoteCallable*, Args...) = nullptr;
public:
template <FunctorForCallbackConstruction<Returned, Args...> LambdaType>
TypedRpcLambdaHolder(LambdaType&& from)
: RpcLambdaHolder(std::forward<LambdaType>(from))
{
call = [](IRemoteCallable* callable, Args... args) {
return (*reinterpret_cast<RpcLambda<std::decay_t<LambdaType>>*>(callable))(args...);
};
}
template <typename... Args2>
Returned operator()(Args2... args) {
return call(operator->(), std::forward<Args>(args)...);
}
};
class DynamicRpcObject : public IRemoteCallable {
std::vector<std::pair<std::string, RpcLambdaHolder>> _contents;
public:
void add(std::string_view name, RpcLambdaHolder&& function) {
_contents.emplace_back(std::pair<std::string, RpcLambdaHolder>(name, std::move(function)));
setSelfAsParent(&*_contents.back().second);
}
bool call(IStructuredInput*, IStructuredOutput&, Callback<>,
Callback<void(std::string_view)>, std::optional<UserId>) const override {
return false;
}
const IRemoteCallable* getChild(std::string_view name) const override {
for (auto& [funcName, func] : _contents) {
if (funcName == name)
return func.operator->();
}
return nullptr;
}
const IRemoteCallable* getChild(int index) const override {
if (index < std::ssize(_contents))
return _contents[index].second.operator->();
else
return nullptr;
}
std::pair<std::string_view, int> childName(const IRemoteCallable* child) const override {
int index = 0;
for (auto& [name, func] : _contents) {
if (func.operator->() == child)
return {name, index};
index++;
}
return {"", NO_SUCH_STRUCTURE};
}
void listTypes(ISerialisableDescriptionFiller& filler) const override {
for (auto& [name, func] : _contents) {
func.listTypes(filler);
}
}
void generateDescription(IRemoteCallableDescriptionFiller& filler) const override {
for (auto& [name, func] : _contents) {
func.addToDescription(name, filler);
}
}
DynamicRpcObject() = default;
DynamicRpcObject(DynamicRpcObject&& from) : _contents(std::move(from._contents)) {
for (auto& [name, functor] : _contents) {
setSelfAsParent(&*functor);
}
}
};
} // namespace Bomba
#endif // BOMBA_DYNAMIC_OBJECT