30 #include "celix/Properties.h"
44 class ServiceRegistration;
71 static std::shared_ptr<ServiceRegistration>
create(std::shared_ptr<celix_bundle_context_t> cCtx,
72 std::shared_ptr<void> svc,
73 const std::string& name,
74 const std::string& version,
80 return createInternal(std::move(cCtx), std::move(svc), name.c_str(),
81 version.c_str(), std::move(properties), registerAsync,
82 unregisterAsync, std::move(onRegisteredCallbacks), std::move(onUnregisteredCallbacks));
106 std::lock_guard<std::mutex> lck{mutex};
114 std::lock_guard<std::mutex> lck{mutex};
129 bool needWaitUnregistering =
false;
130 bool needWaitRegistering =
false;
133 std::lock_guard<std::mutex> lck{mutex};
136 needWaitRegistering =
true;
138 needWaitUnregistering =
true;
141 if (needWaitRegistering) {
144 if (needWaitUnregistering) {
156 if (unregisterAsync) {
157 std::lock_guard<std::mutex> lck{mutex};
164 auto reg = static_cast<ServiceRegistration*>(data);
166 std::lock_guard<std::mutex> lck{reg->mutex};
167 reg->state = ServiceRegistrationState::UNREGISTERED;
170 for (
const auto &cb : reg->onUnregisteredCallbacks) {
176 long localSvcId = -1;
178 std::lock_guard<std::mutex> lck{mutex};
185 if (localSvcId >= 0) {
188 std::lock_guard<std::mutex> lck{mutex};
192 for (
const auto& cb: onUnregisteredCallbacks) {
204 std::shared_ptr<ServiceRegistration>
getSelf()
const {
205 std::lock_guard<std::mutex> lck{mutex};
214 std::shared_ptr<celix_bundle_context_t> _cCtx,
215 std::shared_ptr<void> _svc,
217 const char* _version,
220 bool _unregisterAsync,
223 cCtx{std::move(_cCtx)},
226 properties{std::move(_properties)},
227 registerAsync{_registerAsync},
228 unregisterAsync{_unregisterAsync},
229 onRegisteredCallbacks{std::move(_onRegisteredCallbacks)},
230 onUnregisteredCallbacks{std::move(_onUnregisteredCallbacks)},
231 svc{std::move(_svc)} {}
233 static std::shared_ptr<ServiceRegistration> createInternal(
234 std::shared_ptr<celix_bundle_context_t> cCtx,
235 std::shared_ptr<void> svc,
240 bool unregisterAsync,
241 std::vector<std::function<
void(ServiceRegistration&)>> onRegisteredCallbacks,
242 std::vector<std::function<
void(ServiceRegistration&)>> onUnregisteredCallbacks) {
243 auto delCallback = [](ServiceRegistration* reg) {
263 "celix::ServiceRegistration delete callback",
266 auto* r =
static_cast<ServiceRegistration*
>(data);
274 auto reg = std::shared_ptr<ServiceRegistration>{
275 new ServiceRegistration{
280 std::move(properties),
283 std::move(onRegisteredCallbacks),
284 std::move(onUnregisteredCallbacks)},
288 reg->registerService();
300 void registerService() {
302 auto* cProps = celix_properties_copy(properties.getCProperties());
304 opts.svc = svc.get();
305 opts.serviceName = name.c_str();
306 opts.properties = cProps;
307 if (!version.empty()) {
308 opts.serviceVersion = version.c_str();
312 opts.asyncData =
static_cast<void*
>(
this);
314 auto *reg =
static_cast<ServiceRegistration *
>(data);
316 std::lock_guard<std::mutex> lck{reg->mutex};
319 for (
const auto &cb : reg->onRegisteredCallbacks) {
323 std::lock_guard<std::mutex> lck{mutex};
330 if (localSvcId < 0) {
334 std::lock_guard<std::mutex> lck{mutex};
338 for (
const auto& cb: onRegisteredCallbacks) {
347 void setSelf(
const std::shared_ptr<ServiceRegistration>& s) {
348 std::lock_guard<std::mutex> lck{mutex};
352 const std::shared_ptr<celix_bundle_context_t> cCtx;
353 const std::string name;
354 const std::string version;
356 const bool registerAsync;
357 const bool unregisterAsync;
358 const std::vector<std::function<void(ServiceRegistration&)>> onRegisteredCallbacks;
359 const std::vector<std::function<void(ServiceRegistration&)>> onUnregisteredCallbacks;
361 mutable std::mutex mutex{};
363 std::shared_ptr<void> svc;
365 std::weak_ptr<ServiceRegistration>
self{};