opensurgsim
ComponentManager.h
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013-2016, SimQuest Solutions Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef SURGSIM_FRAMEWORK_COMPONENTMANAGER_H
17 #define SURGSIM_FRAMEWORK_COMPONENTMANAGER_H
18 
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
23 #include <boost/thread/mutex.hpp>
24 
25 #include "SurgSim/Framework/BasicThread.h"
26 #include "SurgSim/Framework/Behavior.h"
27 #include "SurgSim/Framework/Log.h"
28 #include "SurgSim/Framework/Component.h"
29 
30 namespace SurgSim
31 {
32 namespace Framework
33 {
34 
35 class Runtime;
36 class Logger;
37 
50 {
51 public:
52 
53  explicit ComponentManager(const std::string& name = "Unknown Component Manager");
54  virtual ~ComponentManager();
55 
60  bool enqueueAddComponent(const std::shared_ptr<Component>& component);
61 
66  bool enqueueRemoveComponent(const std::shared_ptr<Component>& component);
67 
70  std::shared_ptr<Runtime> getRuntime() const;
71  void setRuntime(std::shared_ptr<Runtime> val);
73 
74 protected:
81  template<class T>
82  std::shared_ptr<T> tryAddComponent(std::shared_ptr<SurgSim::Framework::Component> component,
83  std::vector<std::shared_ptr<T>>* container);
84 
90  template<class T>
91  bool tryRemoveComponent(std::shared_ptr<SurgSim::Framework::Component> component,
92  std::vector<std::shared_ptr<T>>* container);
93 
94 
97  void processComponents();
98 
101  void processBehaviors(const double dt);
102 
104  // Enum is defined in the beginning of this file
105  virtual int getType() const = 0;
106 
110  void copyScheduledComponents(std::vector<std::shared_ptr<Component>>* inflightAdditions,
111  std::vector<std::shared_ptr<Component>>* inflightRemovals,
112  std::vector<std::shared_ptr<SceneElement>>* inflightElements);
113 
115  std::shared_ptr<SurgSim::Framework::Logger> getLogger() const;
116 
118  boost::mutex m_componentMutex;
119 
123  std::vector<std::shared_ptr<Component>> m_componentAdditions;
124  std::vector<std::shared_ptr<Component>> m_componentRemovals;
125  std::vector<std::shared_ptr<SceneElement>> m_elementCache;
127 
129  // Each behavior will have a type to be matched with the corresponding manager
130  // Managers will only handle matching behaviors
131  std::vector<std::shared_ptr<SurgSim::Framework::Behavior>> m_behaviors;
132 
133  void doBeforeStop() override;
134 
135  template<class T>
136  void retireComponents(const std::vector<std::shared_ptr<T>>& container);
137 
138 private:
143  virtual bool executeAdditions(const std::shared_ptr<Component>& component) = 0;
144 
149  virtual bool executeRemovals(const std::shared_ptr<Component>& component) = 0;
150 
153  bool executeInitialization() override;
154 
158  void removeComponents(const std::vector<std::shared_ptr<Component>>::const_iterator& beginIt,
159  const std::vector<std::shared_ptr<Component>>::const_iterator& endIt);
160 
165  void addComponents(
166  const std::vector<std::shared_ptr<Component>>::const_iterator& beginIt,
167  const std::vector<std::shared_ptr<Component>>::const_iterator& endIt,
168  std::vector<std::shared_ptr<Component>>* actualAdditions);
169 
176  void wakeUpComponents(const std::vector<std::shared_ptr<Component>>::const_iterator& beginIt,
177  const std::vector<std::shared_ptr<Component>>::const_iterator& endIt);
178 
179  std::weak_ptr<Runtime> m_runtime;
180 };
181 
182 }; // namespace Framework
183 }; // namespace SurgSim
184 
185 #include "SurgSim/Framework/ComponentManager-inl.h"
186 
187 #endif // SURGSIM_FRAMEWORK_COMPONENTMANAGER_H
Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
std::shared_ptr< SurgSim::Framework::Logger > getLogger() const
Returns this manager&#39;s logger.
Definition: ComponentManager.cpp:226
std::vector< std::shared_ptr< SurgSim::Framework::Behavior > > m_behaviors
Collection of behaviors.
Definition: ComponentManager.h:131
bool tryRemoveComponent(std::shared_ptr< SurgSim::Framework::Component > component, std::vector< std::shared_ptr< T >> *container)
Template version of the removeComponent method.
Definition: ComponentManager-inl.h:54
boost::mutex m_componentMutex
Blocks protects addition and removal queues.
Definition: ComponentManager.h:118
Base Component Manager class.
Definition: ComponentManager.h:49
The convenience header that provides the entirety of the logging API.
void processBehaviors(const double dt)
Processes behaviors This needs to be called inside doUpdate() function in each &#39;sub&#39; manager...
Definition: ComponentManager.cpp:87
std::shared_ptr< Runtime > getRuntime() const
Definition: ComponentManager.cpp:58
bool enqueueAddComponent(const std::shared_ptr< Component > &component)
Queues a component to be added later.
Definition: ComponentManager.cpp:43
void doBeforeStop() override
Prepares the thread for its execution to be stopped.
Definition: ComponentManager.cpp:217
void processComponents()
Processes all the components that are scheduled for addition or removal, this needs to be called insi...
Definition: ComponentManager.cpp:63
bool enqueueRemoveComponent(const std::shared_ptr< Component > &component)
Queues a component to be removed.
Definition: ComponentManager.cpp:50
Basic thread implementation, tries to maintain a constant rate, supplies startup an initialization...
Definition: BasicThread.h:48
std::vector< std::shared_ptr< Component > > m_componentAdditions
Definition: ComponentManager.h:123
std::shared_ptr< T > tryAddComponent(std::shared_ptr< SurgSim::Framework::Component > component, std::vector< std::shared_ptr< T >> *container)
Template version of the addComponent method.
Definition: ComponentManager-inl.h:30
void copyScheduledComponents(std::vector< std::shared_ptr< Component >> *inflightAdditions, std::vector< std::shared_ptr< Component >> *inflightRemovals, std::vector< std::shared_ptr< SceneElement >> *inflightElements)
Helper, blocks access to the additions and removal queue and copies the components from there to the ...
Definition: ComponentManager.cpp:147
virtual int getType() const =0
Returns the type of Manager.