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.
UseServiceBuilder.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/Filter.h"
27 #include "celix/Bundle.h"
28 #include "celix/Properties.h"
29 
30 namespace celix {
49  template<typename I>
51  private:
52  friend class BundleContext;
53 
54  //NOTE private to prevent move so that a build() call cannot be forgotten
56  public:
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} {}
61 
62  UseServiceBuilder& operator=(UseServiceBuilder&&) = delete;
63  UseServiceBuilder(const UseServiceBuilder&) = delete;
64  UseServiceBuilder operator=(const UseServiceBuilder&) = delete;
65 
73 #if __cplusplus >= 201703L //C++17 or higher
74  UseServiceBuilder& setFilter(std::string_view f) { filter = celix::Filter{f}; return *this; }
75 #else
76  UseServiceBuilder& setFilter(const std::string& f) { filter = celix::Filter{f}; return *this; }
77 #endif
78 
82  UseServiceBuilder& setFilter(celix::Filter f) { filter = std::move(f); return *this; }
83 
93  template <typename Rep, typename Period>
94  UseServiceBuilder& setTimeout(std::chrono::duration<Rep, Period> duration) {
95  auto micro = std::chrono::duration_cast<std::chrono::microseconds>(duration).count();
96  timeoutInSeconds = micro / 1000000.0;
97  return *this;
98  }
99 
106  UseServiceBuilder& addUseCallback(std::function<void(I&)> cb) {
107  callbacks.emplace_back(std::move(cb));
108  return *this;
109  }
110 
119  UseServiceBuilder& addUseCallback(std::function<void(I&, const celix::Properties&)> cb) {
120  callbacksWithProperties.emplace_back(std::move(cb));
121  return *this;
122  }
123 
133  UseServiceBuilder& addUseCallback(std::function<void(I&, const celix::Properties&, const celix::Bundle&)> cb) {
134  callbacksWithOwner.emplace_back(std::move(cb));
135  return *this;
136  }
137 
150  std::size_t build() {
152  opts.filter.serviceName = name.empty() ? nullptr : name.c_str();
153  opts.filter.ignoreServiceLanguage = true;
154  opts.filter.filter = filter.empty() ? nullptr : filter.getFilterCString();
155  opts.filter.versionRange = versionRange.empty() ? nullptr : versionRange.c_str();
156  opts.waitTimeoutInSeconds = timeoutInSeconds;
157  opts.callbackHandle = this;
158  opts.useWithOwner = [](void* data, void *voidSvc, const celix_properties_t* cProps, const celix_bundle_t* cBnd) {
159  auto* builder = static_cast<UseServiceBuilder<I>*>(data);
160  auto* svc = static_cast<I*>(voidSvc);
161  const Bundle bnd{const_cast<celix_bundle_t*>(cBnd)};
162  auto props = celix::Properties::wrap(cProps);
163  for (const auto& func : builder->callbacks) {
164  func(*svc);
165  }
166  for (const auto& func : builder->callbacksWithProperties) {
167  func(*svc, *props);
168  }
169  for (const auto& func : builder->callbacksWithOwner) {
170  func(*svc, *props, bnd);
171  }
172  };
173 
174  if (useSingleService) {
175  bool called = celix_bundleContext_useServiceWithOptions(cCtx.get(), &opts);
176  return called ? 1 : 0;
177  } else {
178  return celix_bundleContext_useServicesWithOptions(cCtx.get(), &opts);
179  }
180  }
181  private:
182  const std::shared_ptr<celix_bundle_context_t> cCtx;
183  const std::string name;
184  const bool useSingleService;
185  double timeoutInSeconds{0};
186  celix::Filter filter{};
187  std::string versionRange{};
188  std::vector<std::function<void(I&)>> callbacks{};
189  std::vector<std::function<void(I&, const celix::Properties&)>> callbacksWithProperties{};
190  std::vector<std::function<void(I&, const celix::Properties&, const celix::Bundle&)>> callbacksWithOwner{};
191  };
192 }
celix_bundleContext_useServicesWithOptions
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.
Bundle.h
celix::BundleContext
The bundle context is used to interact with the Celix framework.
Definition: BundleContext.h:53
celix_bundleContext_useServiceWithOptions
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...
celix::UseServiceBuilder
Fluent builder API to use a service or services.
Definition: UseServiceBuilder.h:50
celix
Definition: Bundle.h:27
celix::UseServiceBuilder::build
std::size_t build()
"Builds" the UseServiceBuild.
Definition: UseServiceBuilder.h:150
celix::UseServiceBuilder::setTimeout
UseServiceBuilder & setTimeout(std::chrono::duration< Rep, Period > duration)
Sets a optional timeout.
Definition: UseServiceBuilder.h:94
celix_service_use_options
Service Use Options used to fine tune which services to use and which callbacks to use.
Definition: celix_bundle_context.h:765
celix::UseServiceBuilder::UseServiceBuilder
UseServiceBuilder(std::shared_ptr< celix_bundle_context_t > _cCtx, std::string _name, bool _useSingleService=true)
Definition: UseServiceBuilder.h:57
celix::UseServiceBuilder::setFilter
UseServiceBuilder & setFilter(std::string_view f)
Set filter to be used to select a service.
Definition: UseServiceBuilder.h:74
celix::UseServiceBuilder::setFilter
UseServiceBuilder & setFilter(celix::Filter f)
Set filter to be used to matching services.
Definition: UseServiceBuilder.h:82
celix::Bundle
An installed bundle in the Celix framework.
Definition: Bundle.h:48
celix::UseServiceBuilder::addUseCallback
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:133
celix::UseServiceBuilder::addUseCallback
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:119
celix::dm::Properties
celix::Properties Properties
Definition: Properties.h:25
celix::UseServiceBuilder::operator=
UseServiceBuilder & operator=(UseServiceBuilder &&)=delete
celix::UseServiceBuilder::addUseCallback
UseServiceBuilder & addUseCallback(std::function< void(I &)> cb)
Adds a use callback function which will be called when the UseServiceBuilder is "build".
Definition: UseServiceBuilder.h:106