Apache Celix  2.3.0
An implementation of the OSGi specification adapted to C and C++
DependencyManager.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 <cstdio>
23 #include <iostream>
24 #include <vector>
25 
26 #include "celix/dm/types.h"
27 #include "celix/dm/Component.h"
30 
31 #include "bundle_context.h"
32 #include "celix_bundle_context.h"
33 #include "celix_dependency_manager.h"
34 
35 namespace celix { namespace dm {
36 
49  public:
50  DependencyManager(celix_bundle_context_t *ctx);
51 
52  virtual ~DependencyManager();
53 
54  DependencyManager(DependencyManager&& mgr) = delete;
56 
57  DependencyManager(const DependencyManager&) = delete;
59 
60  celix_bundle_context_t* bundleContext() const { return context.get(); }
61  celix_dependency_manager_t *cDependencyManager() const { return cDepMan.get(); }
62 
63 
69  template<class T>
70  typename std::enable_if<std::is_default_constructible<T>::value, Component<T>&>::type
71  createComponent(std::string name = std::string{}, std::string uuid = {});
72 
79  template<class T>
80  Component<T>& createComponent(std::unique_ptr<T>&& rhs, std::string name = std::string{}, std::string uuid = {});
81 
88  template<class T>
89  Component<T>& createComponent(std::shared_ptr<T> rhs, std::string name = std::string{}, std::string uuid = {});
90 
97  template<class T>
98  Component<T>& createComponent(T rhs, std::string name = std::string{}, std::string uuid = {});
99 
116  void build();
117 
123  void buildAsync();
124 
129  void start();
130 
139  void wait() const;
140 
147  void waitIfAble() const;
148 
154  void destroyComponent(BaseComponent &component);
155 
156 
163  void clear();
164 
170  void clearAsync();
171 
176  void stop();
177 
181  std::size_t getNrOfComponents() const;
182 
188  template<typename T>
189  std::shared_ptr<Component<T>> findComponent(const std::string& uuid) const;
190 
198  bool removeComponent(const std::string& uuid);
199 
207  bool removeComponentAsync(const std::string& uuid);
208 
214 
219  std::vector<celix::dm::DependencyManagerInfo> getInfos() const;
220 
221  friend std::ostream& operator<<(std::ostream& out, const DependencyManager& mng);
222  private:
223  template<class T>
224  Component<T>& createComponentInternal(std::string name, std::string uuid);
225 
226  std::shared_ptr<celix_bundle_context_t> context;
227  std::shared_ptr<celix_dependency_manager_t> cDepMan;
228  mutable std::mutex mutex{}; //protects below
229  std::vector<std::shared_ptr<BaseComponent>> components{};
230  };
231 
235  inline std::ostream& operator<<(std::ostream& out, const DependencyManager& mng);
236 }}
237 
celix::dm::DependencyManager::removeComponent
bool removeComponent(const std::string &uuid)
Removes component with provided UUID from the dependency manager and wail until the component is dest...
Definition: DependencyManager_Impl.h:87
celix::dm::DependencyManager::bundleContext
celix_bundle_context_t * bundleContext() const
Definition: DependencyManager.h:60
celix::dm::DependencyManager
Definition: DependencyManager.h:48
DependencyManager_Impl.h
celix::dm::DependencyManager::wait
void wait() const
Wait for an empty Celix event queue.
Definition: DependencyManager_Impl.h:123
types.h
celix::dm::DependencyManager::start
void start()
Definition: DependencyManager_Impl.h:67
celix
Definition: Bundle.h:26
celix::dm::DependencyManager::operator=
DependencyManager & operator=(DependencyManager &&rhs)=delete
celix::dm::operator<<
std::ostream & operator<<(std::ostream &out, const BaseComponent &cmp)
Definition: Component_Impl.h:54
DependencyManagerInfo.h
celix::dm::DependencyManager::waitIfAble
void waitIfAble() const
Wait (if not called on the Celix event thread) for an empty Celix event queue.
Definition: DependencyManager_Impl.h:127
celix::dm::DependencyManager::getInfo
celix::dm::DependencyManagerInfo getInfo() const
Definition: DependencyManager_Impl.h:202
celix::dm::DependencyManager::stop
void stop()
Definition: DependencyManager_Impl.h:134
celix::dm::DependencyManager::build
void build()
Definition: DependencyManager_Impl.h:71
celix::dm::DependencyManager::buildAsync
void buildAsync()
Definition: DependencyManager_Impl.h:76
celix::dm::DependencyManager::destroyComponent
void destroyComponent(BaseComponent &component)
Removes a component from the Dependency Manager and wait until the component is destroyed.
Definition: DependencyManager_Impl.h:83
celix::dm::DependencyManager::operator<<
friend std::ostream & operator<<(std::ostream &out, const DependencyManager &mng)
celix::dm::DependencyManager::findComponent
std::shared_ptr< Component< T > > findComponent(const std::string &uuid) const
Definition: DependencyManager_Impl.h:143
celix::dm::Component
Definition: Component.h:163
celix::dm::DependencyManager::~DependencyManager
virtual ~DependencyManager()
Definition: DependencyManager_Impl.h:29
Component.h
celix::dm::DependencyManager::getInfos
std::vector< celix::dm::DependencyManagerInfo > getInfos() const
Definition: DependencyManager_Impl.h:210
celix::dm::DependencyManager::getNrOfComponents
std::size_t getNrOfComponents() const
Definition: DependencyManager_Impl.h:138
celix::dm::DependencyManager::clearAsync
void clearAsync()
Definition: DependencyManager_Impl.h:114
celix::dm::DependencyManager::removeComponentAsync
bool removeComponentAsync(const std::string &uuid)
Removes a component from the Dependency Manager and destroys it async.
Definition: DependencyManager_Impl.h:93
celix::dm::DependencyManager::cDependencyManager
celix_dependency_manager_t * cDependencyManager() const
Definition: DependencyManager.h:61
celix::dm::DependencyManager::clear
void clear()
Clears the dependency manager, which removes all configured components and waits until all components...
Definition: DependencyManager_Impl.h:109
celix::dm::DependencyManager::createComponent
std::enable_if< std::is_default_constructible< T >::value, Component< T > & >::type createComponent(std::string name=std::string{}, std::string uuid={})
Definition: DependencyManager_Impl.h:48
ServiceDependency.h
celix::dm::DependencyManager::DependencyManager
DependencyManager(celix_bundle_context_t *ctx)
Definition: DependencyManager_Impl.h:25
celix::dm::BaseComponent
Definition: Component.h:57
celix::dm::DependencyManagerInfo
Definition: DependencyManagerInfo.h:126
celix_bundle_context.h