This repository was archived by the owner on Aug 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBundleInstaceImpl.h
More file actions
342 lines (291 loc) · 8.72 KB
/
BundleInstaceImpl.h
File metadata and controls
342 lines (291 loc) · 8.72 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#pragma once
#include "BundleInstance.h"
#include "BundleRef.h"
#include "ServiceBundle.h"
#include "ServiceRef.h"
#include "meta/constructor.h"
#include <cstring>
#include <map>
#include <memory>
#include <mutex>
#include <type_traits>
#include <vector>
namespace sb {
class BundlesRealm
{
public:
bool hasBundle(const char* bundleId) const
{
return bundles_.find(bundleId) != bundles_.end();
}
void addBundle(const char* bundleId,
std::shared_ptr<IBundleInstaceImpl> bundle)
{
bundles_[bundleId] = bundle;
}
std::shared_ptr<IBundleInstaceImpl> getBundle(const char* bundleId)
{
auto bundle = bundles_.find(bundleId);
if (bundle == bundles_.end())
return std::shared_ptr<IBundleInstaceImpl>();
return bundle->second;
}
private:
struct CmpStr
{
bool operator()(char const* a, char const* b) const
{
return std::strcmp(a, b) < 0;
}
};
std::map<char const*, std::shared_ptr<IBundleInstaceImpl>> bundles_;
};
template<class Providers, int N>
struct ProvidersSetElement
{
using Type = meta::TypeListGetType<Providers, N>;
Type provider;
};
template<class, class>
struct ProvidersSetBase;
template<typename... ProviderTT, int... NN>
struct ProvidersSetBase<TypeList<ProviderTT...>,
std::integer_sequence<int, NN...>>
: ProvidersSetElement<TypeList<ProviderTT...>, NN>...
{
using Interfaces = TypeList<typename ProviderTT::Interface...>;
template<class I>
constexpr static int GetTypeIndex()
{
return meta::TypeIndex<I, Interfaces>::value;
}
template<class I, class Injector>
ServiceRef<I> getDependency(Injector& injector)
{
return ProvidersSetElement<TypeList<ProviderTT...>,
GetTypeIndex<I>()>::provider.get(injector);
}
private:
template<class I, class Injector>
int callBeforeBundleActivate(Injector& injector)
{
ProvidersSetElement<TypeList<ProviderTT...>, GetTypeIndex<I>()>::provider
.beforeBundleActivate(injector);
return 0;
}
template<class I>
int callAfterBundleActivate()
{
ProvidersSetElement<TypeList<ProviderTT...>, GetTypeIndex<I>()>::provider
.afterBundleActivate();
return 0;
}
public:
template<class Injector>
void beforeBundleActivate(Injector& injector)
{
const int _[] = {
0, callBeforeBundleActivate<typename ProviderTT::Interface>(injector)...
};
}
void afterBundleActivate()
{
const int _[] = {
0, callAfterBundleActivate<typename ProviderTT::Interface>()...
};
}
};
template<class ProvidersList>
struct ProvidersSet
: ProvidersSetBase<ProvidersList,
std::make_integer_sequence<int, ProvidersList::size>>
{};
template<class ReferencesList>
class ReferencesSet;
template<class... References>
class ReferencesSet<TypeList<References...>>
{
public:
void linkReferences(BundlesRealm& realm)
{
references_ = { realm.getBundle(References::BundleId).get()... };
}
template<class Bundle>
BundleRef<Bundle> getReference()
{
constexpr int referenceIndex =
meta::TypeIndex<Bundle, TypeList<References...>>::value;
static_assert(referenceIndex != meta::InvalidTypeIndex,
"Bundle reference is not listed on bundle references list");
if (0 <= referenceIndex && referenceIndex < references_.size()) {
return BundleRef<Bundle>(references_[referenceIndex]);
}
throw std::logic_error("getReference: referenceIndex out of bounds");
}
private:
std::vector<IBundleInstaceImpl*> references_;
};
template<class ProvidersSetT, class BundleReferencesSetT>
class Injector
{
public:
template<class T>
ServiceRef<T> create() const
{
return providersSet_.template getDependency<T>(*this);
}
template<class T>
BundleRef<T> getBundleRef() const
{
return bundleReferencesSet_.template getReference<T>();
}
Injector(ProvidersSetT& providersSet,
BundleReferencesSetT& bundleReferencesSet)
: providersSet_(providersSet)
, bundleReferencesSet_(bundleReferencesSet)
{}
private:
ProvidersSetT& providersSet_;
BundleReferencesSetT& bundleReferencesSet_;
};
template<class BundleActivator>
class BundleInstaceImplT : public IBundleInstaceImpl
{
using ExportsTable = std::vector<std::shared_ptr<ExportRef>>;
using ExternalsTable = std::vector<std::unique_ptr<ExternalsRef>>;
using BundleInstaceImpl = BundleInstaceImplT<BundleActivator>;
public:
using Bundle = typename BundleActivator::Bundle;
using Exports = typename Bundle::Exports;
using Externals = typename Bundle::Externals;
using Providers = typename BundleActivator::Providers;
using References = typename BundleActivator::References;
using BundleProvidersSet = ProvidersSet<Providers>;
using BundleReferencesSet = ReferencesSet<References>;
using BundleInjector = Injector<BundleProvidersSet, BundleReferencesSet>;
BundleInstaceImplT()
: externalsTable_(Externals::size)
, bundleInjector_(bundleProvidersSet_, bundleReferencesSet_)
{
onActivatedFuture_ = onActivatedPromise_.get_future().share();
}
virtual ExportRef& getExportRef(uint32_t exportIndex) override
{
if (0 <= exportIndex && exportIndex < exportsTable_.size()) {
if (auto ptr = exportsTable_[exportIndex]) {
return *ptr;
}
throw std::logic_error("exportsTable_ is not properly initialized");
}
throw std::logic_error("exportIndex out of bounds");
}
virtual void setExternalsRef(
uint32_t externalsIndex,
std::unique_ptr<ExternalsRef> externalsRef) override
{
if (0 <= externalsIndex && externalsIndex < externalsTable_.size()) {
externalsTable_[externalsIndex] = std::move(externalsRef);
} else {
throw std::logic_error("setExternalsRef: externalsIndex out of bounds");
}
}
template<class T>
ServiceRef<T> getService()
{
return bundleInjector_.template create<T>();
}
template<class External>
External& getExternal()
{
constexpr int externalsIndex = meta::TypeIndex<External, Externals>::value;
static_assert(externalsIndex != meta::InvalidTypeIndex,
"Type is not listed on bundle extrenals list");
if (0 <= externalsIndex && externalsIndex < externalsTable_.size()) {
ExternalsRef* external = externalsTable_[externalsIndex].get();
auto& typedefRef = dynamic_cast<ExternalsRefT<External>&>(*external);
return typedefRef.ref;
}
throw std::logic_error("getExternal: externalsIndex out of bounds");
}
template<class Bundle>
BundleRef<Bundle> getReference()
{
return bundleReferencesSet_.template getReference<Bundle>();
}
/*
Bundle lifecycle events
*/
virtual void linkReferences(BundlesRealm& realm) override
{
bundleReferencesSet_.linkReferences(realm);
}
virtual void createServices() override
{
bundleProvidersSet_.beforeBundleActivate(bundleInjector_);
createExportTalbe(Exports());
}
virtual AsyncActivateResult activate() override;
virtual std::shared_future<IBundleInstaceImpl*> onActiveFuture() override
{
std::lock_guard<std::mutex> lock(futureCopyMutex_);
auto onActivatedFutureCopy = onActivatedFuture_;
return onActivatedFutureCopy;
}
private:
template<class T>
std::shared_ptr<ExportRef> createExportRef()
{
return std::make_shared<ExportRefT<T>>(
bundleInjector_.template create<T>());
}
template<class... TT>
void createExportTalbe(TypeList<TT...> exports)
{
exportsTable_ = ExportsTable({ createExportRef<TT>()... });
}
private:
ExportsTable exportsTable_;
ExternalsTable externalsTable_;
BundleProvidersSet bundleProvidersSet_;
BundleReferencesSet bundleReferencesSet_;
BundleInjector bundleInjector_;
BundleActivator bundleActivator_;
std::promise<IBundleInstaceImpl*> onActivatedPromise_;
std::shared_future<IBundleInstaceImpl*> onActivatedFuture_;
std::mutex futureCopyMutex_;
};
template<class BundleActivator>
class ThisBundle
{
using BundleInstaceImpl = BundleInstaceImplT<BundleActivator>;
public:
ThisBundle(BundleInstaceImpl* bundleInstaceImpl)
: bundleInstaceImpl_(bundleInstaceImpl)
{}
template<class T>
T& getService()
{
return *bundleInstaceImpl_->template getService<T>().getUnsafe();
}
template<class External>
External& getExternal();
template<class Bundle>
std::shared_future<BundleRef<Bundle>> onActive()
{
return bundleInstaceImpl_->template getReference<Bundle>().onActive();
}
private:
BundleInstaceImpl* bundleInstaceImpl_;
};
template<class BundleActivator>
AsyncActivateResult
BundleInstaceImplT<BundleActivator>::activate()
{
return std::async(std::launch::deferred, [this]() {
bundleActivator_.activate(ThisBundle<BundleActivator>(this)).get();
bundleProvidersSet_.afterBundleActivate();
onActivatedPromise_.set_value(this);
});
}
// BundlesRegistry
} // namespace sb