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.
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  ServiceTrackerBuilder& setFilter(const std::string& f) { filter = celix::Filter{f}; return *this; }
61 
65  ServiceTrackerBuilder& setFilter(celix::Filter f) { filter = std::move(f); return *this; }
66 
75  template<typename F>
77  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>&) {
78  add(svc);
79  });
80  return *this;
81  }
82 
92  template<typename F>
94  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>&) {
95  add(svc, props);
96  });
97  return *this;
98  }
99 
108  template<typename F>
110  addCallbacks.emplace_back(std::forward<F>(add));
111  return *this;
112  }
113 
122  template<typename F>
124  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>&) {
125  remove(svc);
126  });
127  return *this;
128  }
129 
138  template<typename F>
140  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>&) {
141  remove(svc, props);
142  });
143  return *this;
144  }
145 
154  template<typename F>
156  remCallbacks.emplace_back(std::forward<F>(remove));
157  return *this;
158  }
159 
169  template<typename F>
171  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>&) {
172  set(svc);
173  });
174  return *this;
175  }
176 
186  template<typename F>
188  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>&) {
189  set(svc, props);
190  });
191  return *this;
192  }
193 
203  template<typename F>
205  setCallbacks.emplace_back(std::forward<F>(set));
206  return *this;
207  }
208 
214  std::shared_ptr<ServiceTracker<I>> build() {
215  return ServiceTracker<I>::create(cCtx, std::move(name), std::move(versionRange), std::move(filter), std::move(setCallbacks), std::move(addCallbacks), std::move(remCallbacks));
216  }
217  private:
218  const std::shared_ptr<celix_bundle_context_t> cCtx;
219  std::string name;
220  celix::Filter filter{};
221  std::string versionRange{};
222  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{};
223  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{};
224  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{};
225  };
226 
234  private:
235  friend class BundleContext;
236 
237  //NOTE private to prevent move so that a build() call cannot be forgotten
239  public:
240  explicit BundleTrackerBuilder(std::shared_ptr<celix_bundle_context_t> _cCtx) : cCtx{std::move(_cCtx)} {}
241 
242  BundleTrackerBuilder &operator=(BundleTrackerBuilder &&) = delete;
243  BundleTrackerBuilder(const BundleTrackerBuilder &) = delete;
244  BundleTrackerBuilder operator=(const BundleTrackerBuilder &) = delete;
245 
247  includeFrameworkBundle = true;
248  return *this;
249  }
250 
257  BundleTrackerBuilder& addOnInstallCallback(std::function<void(const celix::Bundle&)> callback) {
258  onInstallCallbacks.push_back(std::move(callback));
259  return *this;
260  }
261 
268  BundleTrackerBuilder& addOnStartCallback(std::function<void(const celix::Bundle&)> callback) {
269  onStartCallbacks.push_back(std::move(callback));
270  return *this;
271  }
272 
279  BundleTrackerBuilder& addOnStopCallback(std::function<void(const celix::Bundle&)> callback) {
280  onStopCallbacks.push_back(std::move(callback));
281  return *this;
282  }
283 
289  std::shared_ptr<BundleTracker> build() {
290  return BundleTracker::create(cCtx, includeFrameworkBundle, std::move(onInstallCallbacks), std::move(onStartCallbacks), std::move(onStopCallbacks));
291  }
292  private:
293  const std::shared_ptr<celix_bundle_context_t> cCtx;
294  bool includeFrameworkBundle{false};
295  std::vector<std::function<void(const celix::Bundle&)>> onInstallCallbacks{};
296  std::vector<std::function<void(const celix::Bundle&)>> onStartCallbacks{};
297  std::vector<std::function<void(const celix::Bundle&)>> onStopCallbacks{};
298  };
299 
307  private:
308  friend class BundleContext;
309 
310  //NOTE private to prevent move so that a build() call cannot be forgotten
312  public:
313  explicit MetaTrackerBuilder(std::shared_ptr<celix_bundle_context_t> _cCtx, std::string _serviceName) :
314  cCtx{std::move(_cCtx)},
315  serviceName{std::move(_serviceName)} {}
316 
317  MetaTrackerBuilder &operator=(MetaTrackerBuilder &&) = delete;
318  MetaTrackerBuilder(const MetaTrackerBuilder &) = delete;
319  MetaTrackerBuilder operator=(const MetaTrackerBuilder &) = delete;
320 
328  onTrackerCreated.emplace_back(std::move(cb));
329  return *this;
330  }
331 
339  onTrackerDestroyed.emplace_back(std::move(cb));
340  return *this;
341  }
342 
348  std::shared_ptr<MetaTracker> build() {
349  return MetaTracker::create(cCtx, std::move(serviceName), std::move(onTrackerCreated), std::move(onTrackerDestroyed));
350  }
351  private:
352  const std::shared_ptr<celix_bundle_context_t> cCtx;
353  std::string serviceName;
354  std::vector<std::function<void(const ServiceTrackerInfo&)>> onTrackerCreated{};
355  std::vector<std::function<void(const ServiceTrackerInfo&)>> onTrackerDestroyed{};
356  };
357 }
celix::ServiceTracker::create
static std::shared_ptr< ServiceTracker< I > > create(std::shared_ptr< celix_bundle_context_t > cCtx, std::string svcName, std::string 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:315
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:327
celix::BundleTrackerBuilder::includeFrameworkBundleInCallback
BundleTrackerBuilder & includeFrameworkBundleInCallback()
Definition: TrackerBuilders.h:246
celix::BundleTrackerBuilder::BundleTrackerBuilder
BundleTrackerBuilder(std::shared_ptr< celix_bundle_context_t > _cCtx)
Definition: TrackerBuilders.h:240
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:155
celix::ServiceTrackerBuilder::setFilter
ServiceTrackerBuilder & setFilter(celix::Filter f)
Set filter to be used to matching services.
Definition: TrackerBuilders.h:65
celix::ServiceTrackerInfo
A trivial struct containing information about a service tracker.
Definition: Trackers.h:791
celix
Definition: Bundle.h:28
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:204
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:76
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:338
celix::MetaTrackerBuilder::MetaTrackerBuilder
MetaTrackerBuilder(std::shared_ptr< celix_bundle_context_t > _cCtx, std::string _serviceName)
Definition: TrackerBuilders.h:313
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:257
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:93
celix::ServiceTrackerBuilder
Fluent builder API to track services.
Definition: TrackerBuilders.h:38
celix::MetaTracker::create
static std::shared_ptr< MetaTracker > create(std::shared_ptr< celix_bundle_context_t > cCtx, std::string 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:829
celix::MetaTrackerBuilder::build
std::shared_ptr< MetaTracker > build()
"Builds" the meta tracker and returns a MetaTracker.
Definition: TrackerBuilders.h:348
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:279
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:268
celix::MetaTrackerBuilder
Fluent builder API to track service trackers.
Definition: TrackerBuilders.h:306
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:187
celix::Bundle
An installed bundle in the Celix framework.
Definition: Bundle.h:49
Trackers.h
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:702
celix::ServiceTrackerBuilder::setFilter
ServiceTrackerBuilder & setFilter(const std::string &f)
Set filter to be used to matching services.
Definition: TrackerBuilders.h:60
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:123
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:139
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:289
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:109
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:170
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:214
celix::BundleTrackerBuilder
Fluent builder API to track bundles.
Definition: TrackerBuilders.h:233