Apache Celix
latest
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.
|
Go to the documentation of this file.
26 #include "celix/Filter.h"
28 #include "celix/Properties.h"
57 explicit UseServiceBuilder(std::shared_ptr<celix_bundle_context_t> _cCtx, std::string _name,
bool _useSingleService =
true) :
58 cCtx{std::move(_cCtx)},
59 name{std::move(_name)},
60 useSingleService{_useSingleService} {}
62 UseServiceBuilder&
operator=(UseServiceBuilder&&) =
delete;
63 UseServiceBuilder(
const UseServiceBuilder&) =
delete;
64 UseServiceBuilder
operator=(
const UseServiceBuilder&) =
delete;
89 template <
typename Rep,
typename Period>
91 auto micro = std::chrono::duration_cast<std::chrono::microseconds>(duration).count();
92 timeoutInSeconds = micro / 1000000.0;
103 callbacks.emplace_back(std::move(cb));
116 callbacksWithProperties.emplace_back(std::move(cb));
130 callbacksWithOwner.emplace_back(std::move(cb));
148 opts.filter.serviceName = name.empty() ? nullptr : name.c_str();
149 opts.filter.filter = filter.empty() ? nullptr : filter.getFilterCString();
150 opts.filter.versionRange = versionRange.empty() ? nullptr : versionRange.c_str();
151 opts.waitTimeoutInSeconds = timeoutInSeconds;
152 opts.callbackHandle =
this;
153 opts.useWithOwner = [](
void* data,
void *voidSvc,
const celix_properties_t* cProps,
const celix_bundle_t* cBnd) {
155 auto* svc =
static_cast<I*
>(voidSvc);
156 const Bundle bnd{
const_cast<celix_bundle_t*
>(cBnd)};
157 auto props = celix::Properties::wrap(cProps);
158 for (
const auto& func : builder->callbacks) {
161 for (
const auto& func : builder->callbacksWithProperties) {
164 for (
const auto& func : builder->callbacksWithOwner) {
165 func(*svc, props, bnd);
169 if (useSingleService) {
171 return called ? 1 : 0;
177 const std::shared_ptr<celix_bundle_context_t> cCtx;
178 const std::string name;
179 const bool useSingleService;
180 double timeoutInSeconds{0};
181 celix::Filter filter{};
182 std::string versionRange{};
183 std::vector<std::function<void(I&)>> callbacks{};
184 std::vector<std::function<void(I&,
const celix::Properties&)>> callbacksWithProperties{};
CELIX_FRAMEWORK_EXPORT size_t celix_bundleContext_useServicesWithOptions(celix_bundle_context_t *ctx, const celix_service_use_options_t *opts)
Use the services with the provided service filter options using the provided callback.
UseServiceBuilder & setFilter(const std::string &f)
Set filter to be used to select a service.
Definition: UseServiceBuilder.h:73
The bundle context is used to interact with the Celix framework.
Definition: BundleContext.h:53
CELIX_FRAMEWORK_EXPORT bool celix_bundleContext_useServiceWithOptions(celix_bundle_context_t *ctx, const celix_service_use_options_t *opts)
Use the highest ranking service satisfying the provided service filter options using the provided cal...
Fluent builder API to use a service or services.
Definition: UseServiceBuilder.h:50
std::size_t build()
"Builds" the UseServiceBuild.
Definition: UseServiceBuilder.h:146
UseServiceBuilder & setTimeout(std::chrono::duration< Rep, Period > duration)
Sets a optional timeout.
Definition: UseServiceBuilder.h:90
Service Use Options used to fine tune which services to use and which callbacks to use.
Definition: celix_bundle_context.h:489
UseServiceBuilder(std::shared_ptr< celix_bundle_context_t > _cCtx, std::string _name, bool _useSingleService=true)
Definition: UseServiceBuilder.h:57
UseServiceBuilder & setFilter(celix::Filter f)
Set filter to be used to matching services.
Definition: UseServiceBuilder.h:78
An installed bundle in the Celix framework.
Definition: Bundle.h:49
UseServiceBuilder & addUseCallback(std::function< void(I &, const celix::Properties &, const celix::Bundle &)> cb)
Adds a use callback function which will be called when the UseServiceBuilder is "build".
Definition: UseServiceBuilder.h:129
UseServiceBuilder & addUseCallback(std::function< void(I &, const celix::Properties &)> cb)
Adds a use callback function which will be called when the UseServiceBuilder is "build".
Definition: UseServiceBuilder.h:115
celix::Properties Properties
Definition: Properties.h:25
UseServiceBuilder & operator=(UseServiceBuilder &&)=delete
UseServiceBuilder & addUseCallback(std::function< void(I &)> cb)
Adds a use callback function which will be called when the UseServiceBuilder is "build".
Definition: UseServiceBuilder.h:102