Apache Celix  2.4.0
Apache Celix is a framework for C, C++14 and C++17 to develop dynamic modular software applications using component and in-process service-oriented programming.
ServiceRegistrationBuilder.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #pragma once
21 
22 #include <string>
23 #include <vector>
24 #include <memory>
25 
26 #include "celix/Utils.h"
28 
29 namespace celix {
37  template<typename I>
39  private:
40  friend class BundleContext;
41 
42  //NOTE private to prevent move so that a build() call cannot be forgotten
44  public:
46  std::shared_ptr<celix_bundle_context_t> _cCtx,
47  std::shared_ptr<I> _svc,
48  std::string _name,
49  bool _registerAsync = true,
50  bool _unregisterAsync = true) :
51  cCtx{std::move(_cCtx)},
52  svc{std::move(_svc)},
53  name{std::move(_name)},
54  version{celix::typeVersion<I>()},
55  registerAsync{_registerAsync},
56  unregisterAsync{_unregisterAsync} {}
57 
58  ServiceRegistrationBuilder& operator=(ServiceRegistrationBuilder&&) = delete;
59  ServiceRegistrationBuilder(const ServiceRegistrationBuilder&) = delete;
60  ServiceRegistrationBuilder operator=(const ServiceRegistrationBuilder&) = delete;
61 
67 #if __cplusplus >= 201703L //C++17 or higher
68  ServiceRegistrationBuilder& setVersion(std::string_view v) { version = v; return *this; }
69 #else
70  ServiceRegistrationBuilder& setVersion(std::string v) { version = std::move(v); return *this; }
71 #endif
72 
78 #if __cplusplus >= 201703L //C++17 or higher
79  template<typename T>
80  ServiceRegistrationBuilder& addProperty(std::string_view key, T&& value) { properties.template set(key, std::forward<T>(value)); return *this; }
81 #else
82  template<typename T>
83  ServiceRegistrationBuilder& addProperty(const std::string& key, T&& value) { properties.set(key, std::forward<T>(value)); return *this; }
84 #endif
85 
91  ServiceRegistrationBuilder& setProperties(celix::Properties p) { properties = std::move(p); return *this; }
92 
100  for (const auto& pair : props) {
101  properties.set(pair.first, pair.second);
102  }
103  return *this;
104  }
105 
112  onRegisteredCallbacks.emplace_back(std::move(callback));
113  return *this;
114  }
115 
122  onUnregisteredCallbacks.emplace_back(std::move(callback));
123  return *this;
124  }
125 
142  registerAsync = async;
143  return *this;
144  }
145 
165  unregisterAsync = async;
166  return *this;
167  }
168 
176  std::shared_ptr<ServiceRegistration> build() {
178  cCtx,
179  std::move(svc),
180  std::move(name),
181  std::move(version),
182  std::move(properties),
183  registerAsync,
184  unregisterAsync,
185  std::move(onRegisteredCallbacks),
186  std::move(onUnregisteredCallbacks));
187  }
188  private:
189  const std::shared_ptr<celix_bundle_context_t> cCtx;
190  std::shared_ptr<I> svc;
191  std::string name;
192  std::string version;
193  bool registerAsync;
194  bool unregisterAsync;
195  celix::Properties properties{};
196  std::vector<std::function<void(ServiceRegistration&)>> onRegisteredCallbacks{};
197  std::vector<std::function<void(ServiceRegistration&)>> onUnregisteredCallbacks{};
198  };
199 }
celix::BundleContext
The bundle context is used to interact with the Celix framework.
Definition: BundleContext.h:53
celix::ServiceRegistrationBuilder::setUnregisterAsync
ServiceRegistrationBuilder & setUnregisterAsync(bool async)
Configure if the service un-registration will be done synchronized or asynchronized.
Definition: ServiceRegistrationBuilder.h:164
celix::ServiceRegistrationBuilder::addProperty
ServiceRegistrationBuilder & addProperty(std::string_view key, T &&value)
Add a property to the service properties.
Definition: ServiceRegistrationBuilder.h:80
celix
Definition: Bundle.h:27
celix::ServiceRegistrationBuilder::addOnRegistered
ServiceRegistrationBuilder & addOnRegistered(std::function< void(ServiceRegistration &)> callback)
Adds an on registered callback for the service registration.
Definition: ServiceRegistrationBuilder.h:111
celix::ServiceRegistration::create
static std::shared_ptr< ServiceRegistration > create(std::shared_ptr< celix_bundle_context_t > cCtx, std::shared_ptr< void > svc, std::string_view name, std::string_view version, celix::Properties properties, bool registerAsync, bool unregisterAsync, std::vector< std::function< void(ServiceRegistration &)>> onRegisteredCallbacks, std::vector< std::function< void(ServiceRegistration &)>> onUnregisteredCallbacks)
Definition: ServiceRegistration.h:71
celix::ServiceRegistration
A registered service.
Definition: ServiceRegistration.h:53
celix::ServiceRegistrationBuilder::addProperties
ServiceRegistrationBuilder & addProperties(const celix::Properties &props)
Add the properties to the service properties.
Definition: ServiceRegistrationBuilder.h:99
celix::ServiceRegistrationBuilder
Fluent builder API to build a new service registration for a service.
Definition: ServiceRegistrationBuilder.h:38
celix::ServiceRegistrationBuilder::operator=
ServiceRegistrationBuilder & operator=(ServiceRegistrationBuilder &&)=delete
celix::ServiceRegistrationBuilder::addOnUnregistered
ServiceRegistrationBuilder & addOnUnregistered(std::function< void(ServiceRegistration &)> callback)
Adds an on unregistered callback for the service registration.
Definition: ServiceRegistrationBuilder.h:121
celix::ServiceRegistrationBuilder::setProperties
ServiceRegistrationBuilder & setProperties(celix::Properties p)
Set the service properties.
Definition: ServiceRegistrationBuilder.h:91
celix::ServiceRegistrationBuilder::setRegisterAsync
ServiceRegistrationBuilder & setRegisterAsync(bool async)
Configure if the service registration will be done synchronized or asynchronized.
Definition: ServiceRegistrationBuilder.h:141
celix::ServiceRegistrationBuilder::build
std::shared_ptr< ServiceRegistration > build()
"Builds" the service registration and return a ServiceRegistration.
Definition: ServiceRegistrationBuilder.h:176
celix::ServiceRegistrationBuilder::setVersion
ServiceRegistrationBuilder & setVersion(std::string_view v)
Set the service version.
Definition: ServiceRegistrationBuilder.h:68
celix::dm::Properties
celix::Properties Properties
Definition: Properties.h:25
ServiceRegistration.h