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.
TrackerBuilders.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 "Trackers.h"
27 
28 namespace celix {
29 
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
43  ServiceTrackerBuilder(ServiceTrackerBuilder&&) noexcept = default;
44  public:
45  explicit ServiceTrackerBuilder(std::shared_ptr<celix_bundle_context_t> _cCtx, std::string _name) :
46  cCtx{std::move(_cCtx)},
47  name{std::move(_name)} {}
48 
49  ServiceTrackerBuilder& operator=(ServiceTrackerBuilder&&) = delete;
50  ServiceTrackerBuilder(const ServiceTrackerBuilder&) = delete;
51  ServiceTrackerBuilder operator=(const ServiceTrackerBuilder&) = delete;
52 
60 #if __cplusplus >= 201703L //C++17 or higher
61  ServiceTrackerBuilder& setFilter(std::string_view f) { filter = celix::Filter{f}; return *this; }
62 #else
63  ServiceTrackerBuilder& setFilter(const std::string& f) { filter = celix::Filter{f}; return *this; }
64 #endif
65 
69  ServiceTrackerBuilder& setFilter(celix::Filter f) { filter = std::move(f); return *this; }
70 
79  template<typename F>
81  addCallbacks.emplace_back([add = std::forward<F>(add)](const std::shared_ptr<I>& svc, const std::shared_ptr<const celix::Properties>&, const std::shared_ptr<const celix::Bundle>&) {
82  add(svc);
83  });
84  return *this;
85  }
86 
96  template<typename F>
98  addCallbacks.emplace_back([add = std::forward<F>(add)](const std::shared_ptr<I>& svc, const std::shared_ptr<const celix::Properties>& props, const std::shared_ptr<const celix::Bundle>&) {
99  add(svc, props);
100  });
101  return *this;
102  }
103 
112  template<typename F>
114  addCallbacks.emplace_back(std::forward<F>(add));
115  return *this;
116  }
117 
126  template<typename F>
128  remCallbacks.emplace_back([remove = std::forward<F>(remove)](const std::shared_ptr<I>& svc, const std::shared_ptr<const celix::Properties>&, const std::shared_ptr<const celix::Bundle>&) {
129  remove(svc);
130  });
131  return *this;
132  }
133 
142  template<typename F>
144  remCallbacks.emplace_back([remove = std::forward<F>(remove)](const std::shared_ptr<I>& svc, const std::shared_ptr<const celix::Properties>& props, const std::shared_ptr<const celix::Bundle>&) {
145  remove(svc, props);
146  });
147  return *this;
148  }
149 
158  template<typename F>
160  remCallbacks.emplace_back(std::forward<F>(remove));
161  return *this;
162  }
163 
173  template<typename F>
175  setCallbacks.emplace_back([set = std::forward<F>(set)](const std::shared_ptr<I>& svc, const std::shared_ptr<const celix::Properties>&, const std::shared_ptr<const celix::Bundle>&) {
176  set(svc);
177  });
178  return *this;
179  }
180 
190  template<typename F>
192  setCallbacks.emplace_back([set = std::forward<F>(set)](const std::shared_ptr<I>& svc, const std::shared_ptr<const celix::Properties>& props, const std::shared_ptr<const celix::Bundle>&) {
193  set(svc, props);
194  });
195  return *this;
196  }
197 
207  template<typename F>
209  setCallbacks.emplace_back(std::forward<F>(set));
210  return *this;
211  }
212 
218  std::shared_ptr<ServiceTracker<I>> build() {
219  return ServiceTracker<I>::create(cCtx, std::move(name), std::move(versionRange), std::move(filter), std::move(setCallbacks), std::move(addCallbacks), std::move(remCallbacks));
220  }
221  private:
222  const std::shared_ptr<celix_bundle_context_t> cCtx;
223  std::string name;
224  celix::Filter filter{};
225  std::string versionRange{};
226  std::vector<std::function<void(const std::shared_ptr<I>&, const std::shared_ptr<const celix::Properties>&, const std::shared_ptr<const celix::Bundle>&)>> setCallbacks{};
227  std::vector<std::function<void(const std::shared_ptr<I>&, const std::shared_ptr<const celix::Properties>&, const std::shared_ptr<const celix::Bundle>&)>> addCallbacks{};
228  std::vector<std::function<void(const std::shared_ptr<I>&, const std::shared_ptr<const celix::Properties>&, const std::shared_ptr<const celix::Bundle>&)>> remCallbacks{};
229  };
230 
238  private:
239  friend class BundleContext;
240 
241  //NOTE private to prevent move so that a build() call cannot be forgotten
243  public:
244  explicit BundleTrackerBuilder(std::shared_ptr<celix_bundle_context_t> _cCtx) : cCtx{std::move(_cCtx)} {}
245 
246  BundleTrackerBuilder &operator=(BundleTrackerBuilder &&) = delete;
247  BundleTrackerBuilder(const BundleTrackerBuilder &) = delete;
248  BundleTrackerBuilder operator=(const BundleTrackerBuilder &) = delete;
249 
251  includeFrameworkBundle = true;
252  return *this;
253  }
254 
261  BundleTrackerBuilder& addOnInstallCallback(std::function<void(const celix::Bundle&)> callback) {
262  onInstallCallbacks.push_back(std::move(callback));
263  return *this;
264  }
265 
272  BundleTrackerBuilder& addOnStartCallback(std::function<void(const celix::Bundle&)> callback) {
273  onStartCallbacks.push_back(std::move(callback));
274  return *this;
275  }
276 
283  BundleTrackerBuilder& addOnStopCallback(std::function<void(const celix::Bundle&)> callback) {
284  onStopCallbacks.push_back(std::move(callback));
285  return *this;
286  }
287 
293  std::shared_ptr<BundleTracker> build() {
294  return BundleTracker::create(cCtx, includeFrameworkBundle, std::move(onInstallCallbacks), std::move(onStartCallbacks), std::move(onStopCallbacks));
295  }
296  private:
297  const std::shared_ptr<celix_bundle_context_t> cCtx;
298  bool includeFrameworkBundle{false};
299  std::vector<std::function<void(const celix::Bundle&)>> onInstallCallbacks{};
300  std::vector<std::function<void(const celix::Bundle&)>> onStartCallbacks{};
301  std::vector<std::function<void(const celix::Bundle&)>> onStopCallbacks{};
302  };
303 
311  private:
312  friend class BundleContext;
313 
314  //NOTE private to prevent move so that a build() call cannot be forgotten
316  public:
317  explicit MetaTrackerBuilder(std::shared_ptr<celix_bundle_context_t> _cCtx, std::string _serviceName) :
318  cCtx{std::move(_cCtx)},
319  serviceName{std::move(_serviceName)} {}
320 
321  MetaTrackerBuilder &operator=(MetaTrackerBuilder &&) = delete;
322  MetaTrackerBuilder(const MetaTrackerBuilder &) = delete;
323  MetaTrackerBuilder operator=(const MetaTrackerBuilder &) = delete;
324 
332  onTrackerCreated.emplace_back(std::move(cb));
333  return *this;
334  }
335 
343  onTrackerDestroyed.emplace_back(std::move(cb));
344  return *this;
345  }
346 
352  std::shared_ptr<MetaTracker> build() {
353  return MetaTracker::create(cCtx, std::move(serviceName), std::move(onTrackerCreated), std::move(onTrackerDestroyed));
354  }
355  private:
356  const std::shared_ptr<celix_bundle_context_t> cCtx;
357  std::string serviceName;
358  std::vector<std::function<void(const ServiceTrackerInfo&)>> onTrackerCreated{};
359  std::vector<std::function<void(const ServiceTrackerInfo&)>> onTrackerDestroyed{};
360  };
361 }
celix::MetaTrackerBuilder::addOnTrackerCreatedCallback
MetaTrackerBuilder & addOnTrackerCreatedCallback(std::function< void(const ServiceTrackerInfo &)> cb)
Adds a "on tracker created" callback function, which will be called - on the Celix event thread - whe...
Definition: TrackerBuilders.h:331
celix::BundleTrackerBuilder::includeFrameworkBundleInCallback
BundleTrackerBuilder & includeFrameworkBundleInCallback()
Definition: TrackerBuilders.h:250
celix::BundleTrackerBuilder::BundleTrackerBuilder
BundleTrackerBuilder(std::shared_ptr< celix_bundle_context_t > _cCtx)
Definition: TrackerBuilders.h:244
celix::BundleContext
The bundle context is used to interact with the Celix framework.
Definition: BundleContext.h:53
celix::ServiceTrackerBuilder::addRemWithOwnerCallback
ServiceTrackerBuilder & addRemWithOwnerCallback(F &&remove)
Adds a remove callback function, which will be called - on the Celix event thread - when a service ma...
Definition: TrackerBuilders.h:159
celix::ServiceTrackerBuilder::setFilter
ServiceTrackerBuilder & setFilter(std::string_view f)
Set filter to be used to matching services.
Definition: TrackerBuilders.h:61
celix::ServiceTrackerBuilder::setFilter
ServiceTrackerBuilder & setFilter(celix::Filter f)
Set filter to be used to matching services.
Definition: TrackerBuilders.h:69
celix::ServiceTrackerInfo
A trivial struct containing information about a service tracker.
Definition: Trackers.h:716
celix
Definition: Bundle.h:27
celix::ServiceTrackerBuilder::addSetWithOwner
ServiceTrackerBuilder & addSetWithOwner(F &&set)
Adds a set callback function, which will be called - on the Celix event thread - when there is a new ...
Definition: TrackerBuilders.h:208
celix::BundleTrackerBuilder::operator=
BundleTrackerBuilder & operator=(BundleTrackerBuilder &&)=delete
celix::ServiceTrackerBuilder::addAddCallback
ServiceTrackerBuilder & addAddCallback(F &&add)
Adds a add callback function, which will be called - on the Celix event thread - when a new service m...
Definition: TrackerBuilders.h:80
celix::MetaTracker::create
static std::shared_ptr< MetaTracker > create(std::shared_ptr< celix_bundle_context_t > cCtx, std::string_view serviceName, std::vector< std::function< void(const ServiceTrackerInfo &)>> onTrackerCreated, std::vector< std::function< void(const ServiceTrackerInfo &)>> onTrackerDestroyed)
Creates a new meta tracker and opens the tracker.
Definition: Trackers.h:754
celix::MetaTrackerBuilder::addOnTrackerDestroyedCallback
MetaTrackerBuilder & addOnTrackerDestroyedCallback(std::function< void(const ServiceTrackerInfo &)> cb)
Adds a "on tracker destroyed" callback function, which will be called - on the Celix event thread - w...
Definition: TrackerBuilders.h:342
celix::MetaTrackerBuilder::MetaTrackerBuilder
MetaTrackerBuilder(std::shared_ptr< celix_bundle_context_t > _cCtx, std::string _serviceName)
Definition: TrackerBuilders.h:317
celix::BundleTrackerBuilder::addOnInstallCallback
BundleTrackerBuilder & addOnInstallCallback(std::function< void(const celix::Bundle &)> callback)
Adds a "on install" callback function, which will be called - on the Celix event thread - when a new ...
Definition: TrackerBuilders.h:261
celix::ServiceTrackerBuilder::addAddWithPropertiesCallback
ServiceTrackerBuilder & addAddWithPropertiesCallback(F &&add)
Adds a add callback function, which will be called - on the Celix event thread - when a new service m...
Definition: TrackerBuilders.h:97
celix::ServiceTrackerBuilder
Fluent builder API to track services.
Definition: TrackerBuilders.h:38
celix::MetaTrackerBuilder::build
std::shared_ptr< MetaTracker > build()
"Builds" the meta tracker and returns a MetaTracker.
Definition: TrackerBuilders.h:352
celix::BundleTrackerBuilder::addOnStopCallback
BundleTrackerBuilder & addOnStopCallback(std::function< void(const celix::Bundle &)> callback)
Adds a "on stop" callback function, which will be called - on the Celix event thread - when a new bun...
Definition: TrackerBuilders.h:283
celix::BundleTrackerBuilder::addOnStartCallback
BundleTrackerBuilder & addOnStartCallback(std::function< void(const celix::Bundle &)> callback)
Adds a "on start" callback function, which will be called - on the Celix event thread - when a new bu...
Definition: TrackerBuilders.h:272
celix::MetaTrackerBuilder
Fluent builder API to track service trackers.
Definition: TrackerBuilders.h:310
celix::ServiceTrackerBuilder::addSetWithPropertiesCallback
ServiceTrackerBuilder & addSetWithPropertiesCallback(F &&set)
Adds a set callback function, which will be called - on the Celix event thread - when there is a new ...
Definition: TrackerBuilders.h:191
celix::Bundle
An installed bundle in the Celix framework.
Definition: Bundle.h:48
Trackers.h
celix::ServiceTracker::create
static std::shared_ptr< ServiceTracker< I > > create(std::shared_ptr< celix_bundle_context_t > cCtx, std::string_view svcName, std::string_view svcVersionRange, celix::Filter filter, std::vector< std::function< void(const std::shared_ptr< I > &, const std::shared_ptr< const celix::Properties > &, const std::shared_ptr< const celix::Bundle > &)>> setCallbacks, std::vector< std::function< void(const std::shared_ptr< I > &, const std::shared_ptr< const celix::Properties > &, const std::shared_ptr< const celix::Bundle > &)>> addCallbacks, std::vector< std::function< void(const std::shared_ptr< I > &, const std::shared_ptr< const celix::Properties > &, const std::shared_ptr< const celix::Bundle > &)>> remCallbacks)
Creates a new service tracker and opens the tracker.
Definition: Trackers.h:323
celix::BundleTracker::create
static std::shared_ptr< BundleTracker > create(std::shared_ptr< celix_bundle_context_t > cCtx, bool includeFrameworkBundle, std::vector< std::function< void(const celix::Bundle &)>> onInstallCallbacks, std::vector< std::function< void(const celix::Bundle &)>> onStartCallbacks, std::vector< std::function< void(const celix::Bundle &)>> onStopCallbacks)
Creates a new bundle tracker and opens the tracker.
Definition: Trackers.h:627
celix::ServiceTrackerBuilder::addRemCallback
ServiceTrackerBuilder & addRemCallback(F &&remove)
Adds a remove callback function, which will be called - on the Celix event thread - when a service ma...
Definition: TrackerBuilders.h:127
celix::ServiceTrackerBuilder::addRemWithPropertiesCallback
ServiceTrackerBuilder & addRemWithPropertiesCallback(F &&remove)
Adds a remove callback function, which will be called - on the Celix event thread - when a service ma...
Definition: TrackerBuilders.h:143
celix::MetaTrackerBuilder::operator=
MetaTrackerBuilder & operator=(MetaTrackerBuilder &&)=delete
celix::BundleTrackerBuilder::build
std::shared_ptr< BundleTracker > build()
"Builds" the bundle tracker and returns a BundleTracker.
Definition: TrackerBuilders.h:293
celix::ServiceTrackerBuilder::addAddWithOwnerCallback
ServiceTrackerBuilder & addAddWithOwnerCallback(F &&add)
Adds a add callback function, which will be called - on the Celix event thread - when a new service m...
Definition: TrackerBuilders.h:113
celix::ServiceTrackerBuilder::addSetCallback
ServiceTrackerBuilder & addSetCallback(F &&set)
Adds a set callback function, which will be called - on the Celix event thread - when there is a new ...
Definition: TrackerBuilders.h:174
celix::ServiceTrackerBuilder::operator=
ServiceTrackerBuilder & operator=(ServiceTrackerBuilder &&)=delete
celix::ServiceTrackerBuilder::build
std::shared_ptr< ServiceTracker< I > > build()
"Builds" the service tracker and returns a ServiceTracker.
Definition: TrackerBuilders.h:218
celix::BundleTrackerBuilder
Fluent builder API to track bundles.
Definition: TrackerBuilders.h:237