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.
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 "celix_bundle_context.h"
32 #include "celix_dependency_manager.h"
33 
34 namespace celix { namespace dm {
35 
48  public:
49  DependencyManager(celix_bundle_context_t *ctx);
50 
51  virtual ~DependencyManager();
52 
53  DependencyManager(DependencyManager&& mgr) = delete;
55 
56  DependencyManager(const DependencyManager&) = delete;
58 
59  celix_bundle_context_t* bundleContext() const { return context.get(); }
60  celix_dependency_manager_t *cDependencyManager() const { return cDepMan.get(); }
61 
62 
68  template<class T>
69  typename std::enable_if<std::is_default_constructible<T>::value, Component<T>&>::type
70  createComponent(std::string name = std::string{}, std::string uuid = {});
71 
78  template<class T>
79  Component<T>& createComponent(std::unique_ptr<T>&& rhs, std::string name = std::string{}, std::string uuid = {});
80 
87  template<class T>
88  Component<T>& createComponent(std::shared_ptr<T> rhs, std::string name = std::string{}, std::string uuid = {});
89 
96  template<class T>
97  Component<T>& createComponent(T rhs, std::string name = std::string{}, std::string uuid = {});
98 
115  void build();
116 
122  void buildAsync();
123 
128  void start();
129 
139  void wait() const;
140 
149  void waitIfAble() const;
150 
156  void destroyComponent(BaseComponent &component);
157 
158 
165  void clear();
166 
172  void clearAsync();
173 
178  void stop();
179 
183  std::size_t getNrOfComponents() const;
184 
190  template<typename T>
191  std::shared_ptr<Component<T>> findComponent(const std::string& uuid) const;
192 
200  bool removeComponent(const std::string& uuid);
201 
209  bool removeComponentAsync(const std::string& uuid);
210 
216 
221  std::vector<celix::dm::DependencyManagerInfo> getInfos() const;
222 
223  friend std::ostream& operator<<(std::ostream& out, const DependencyManager& mng);
224  private:
225  template<class T>
226  Component<T>& createComponentInternal(std::string name, std::string uuid);
227 
228  std::shared_ptr<celix_bundle_context_t> context;
229  std::shared_ptr<celix_dependency_manager_t> cDepMan;
230  mutable std::mutex mutex{}; //protects below
231  std::vector<std::shared_ptr<BaseComponent>> components{};
232  };
233 
237  inline std::ostream& operator<<(std::ostream& out, const DependencyManager& mng);
238 }}
239 
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:59
celix::dm::DependencyManager
Definition: DependencyManager.h:47
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:27
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:165
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:214
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:60
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