29 #include "celix/Properties.h"
43 class ServiceRegistration;
70 #if __cplusplus >= 201703L //C++17 or higher
71 static std::shared_ptr<ServiceRegistration>
create(std::shared_ptr<celix_bundle_context_t> cCtx,
72 std::shared_ptr<void> svc,
73 std::string_view name,
74 std::string_view version,
80 return createInternal(std::move(cCtx), std::move(svc), name.data(),
81 version.data(), std::move(properties), registerAsync,
82 unregisterAsync, std::move(onRegisteredCallbacks), std::move(onUnregisteredCallbacks));
85 static std::shared_ptr<ServiceRegistration>
create(std::shared_ptr<celix_bundle_context_t> cCtx,
86 std::shared_ptr<void> svc,
87 const std::string& name,
88 const std::string& version,
94 return createInternal(std::move(cCtx), std::move(svc), name.c_str(),
95 version.c_str(), std::move(properties), registerAsync,
96 unregisterAsync, std::move(onRegisteredCallbacks), std::move(onUnregisteredCallbacks));
121 std::lock_guard<std::mutex> lck{mutex};
129 std::lock_guard<std::mutex> lck{mutex};
144 bool needWaitUnregistering =
false;
145 bool needWaitRegistering =
false;
148 std::lock_guard<std::mutex> lck{mutex};
151 needWaitRegistering =
true;
153 needWaitUnregistering =
true;
156 if (needWaitRegistering) {
159 if (needWaitUnregistering) {
171 if (unregisterAsync) {
172 std::lock_guard<std::mutex> lck{mutex};
179 auto reg = static_cast<ServiceRegistration*>(data);
181 std::lock_guard<std::mutex> lck{reg->mutex};
182 reg->state = ServiceRegistrationState::UNREGISTERED;
185 for (
const auto &cb : reg->onUnregisteredCallbacks) {
191 long localSvcId = -1;
193 std::lock_guard<std::mutex> lck{mutex};
200 if (localSvcId >= 0) {
203 std::lock_guard<std::mutex> lck{mutex};
207 for (
const auto& cb: onUnregisteredCallbacks) {
219 std::shared_ptr<ServiceRegistration>
getSelf()
const {
220 std::lock_guard<std::mutex> lck{mutex};
229 std::shared_ptr<celix_bundle_context_t> _cCtx,
230 std::shared_ptr<void> _svc,
232 const char* _version,
235 bool _unregisterAsync,
238 cCtx{std::move(_cCtx)},
241 properties{std::move(_properties)},
242 registerAsync{_registerAsync},
243 unregisterAsync{_unregisterAsync},
244 onRegisteredCallbacks{std::move(_onRegisteredCallbacks)},
245 onUnregisteredCallbacks{std::move(_onUnregisteredCallbacks)},
246 svc{std::move(_svc)} {}
248 static std::shared_ptr<ServiceRegistration> createInternal(
249 std::shared_ptr<celix_bundle_context_t> cCtx,
250 std::shared_ptr<void> svc,
255 bool unregisterAsync,
256 std::vector<std::function<
void(ServiceRegistration&)>> onRegisteredCallbacks,
257 std::vector<std::function<
void(ServiceRegistration&)>> onUnregisteredCallbacks) {
258 auto delCallback = [](ServiceRegistration* reg) {
278 "celix::ServiceRegistration delete callback",
281 auto* r =
static_cast<ServiceRegistration*
>(data);
289 auto reg = std::shared_ptr<ServiceRegistration>{
290 new ServiceRegistration{
295 std::move(properties),
298 std::move(onRegisteredCallbacks),
299 std::move(onUnregisteredCallbacks)},
303 reg->registerService();
315 void registerService() {
317 auto* cProps = celix_properties_copy(properties.getCProperties());
319 opts.svc = svc.get();
320 opts.serviceName = name.c_str();
321 opts.properties = cProps;
322 if (!version.empty()) {
323 opts.serviceVersion = version.c_str();
327 opts.asyncData =
static_cast<void*
>(
this);
329 auto *reg =
static_cast<ServiceRegistration *
>(data);
331 std::lock_guard<std::mutex> lck{reg->mutex};
334 for (
const auto &cb : reg->onRegisteredCallbacks) {
338 std::lock_guard<std::mutex> lck{mutex};
345 if (localSvcId < 0) {
349 std::lock_guard<std::mutex> lck{mutex};
353 for (
const auto& cb: onRegisteredCallbacks) {
362 void setSelf(
const std::shared_ptr<ServiceRegistration>& s) {
363 std::lock_guard<std::mutex> lck{mutex};
367 const std::shared_ptr<celix_bundle_context_t> cCtx;
368 const std::string name;
369 const std::string version;
371 const bool registerAsync;
372 const bool unregisterAsync;
373 const std::vector<std::function<void(ServiceRegistration&)>> onRegisteredCallbacks;
374 const std::vector<std::function<void(ServiceRegistration&)>> onUnregisteredCallbacks;
376 mutable std::mutex mutex{};
378 std::shared_ptr<void> svc;
380 std::weak_ptr<ServiceRegistration>
self{};