ComponentManager.h
Go to the documentation of this file.
// This file is a part of the OpenSurgSim project.
// Copyright 2013-2016, SimQuest Solutions Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SURGSIM_FRAMEWORK_COMPONENTMANAGER_H
#define SURGSIM_FRAMEWORK_COMPONENTMANAGER_H
#include <memory>
#include <string>
#include <vector>
#include <boost/thread/mutex.hpp>
#include "SurgSim/Framework/BasicThread.h"
#include "SurgSim/Framework/Behavior.h"
#include "SurgSim/Framework/Log.h"
#include "SurgSim/Framework/Component.h"
namespace SurgSim
{
namespace Framework
{
class Runtime;
class Logger;
class ComponentManager : public BasicThread
{
public:
explicit ComponentManager(const std::string& name = "Unknown Component Manager");
virtual ~ComponentManager();
bool enqueueAddComponent(const std::shared_ptr<Component>& component);
bool enqueueRemoveComponent(const std::shared_ptr<Component>& component);
std::shared_ptr<Runtime> getRuntime() const;
void setRuntime(std::shared_ptr<Runtime> val);
protected:
template<class T>
std::shared_ptr<T> tryAddComponent(std::shared_ptr<SurgSim::Framework::Component> component,
std::vector<std::shared_ptr<T>>* container);
template<class T>
bool tryRemoveComponent(std::shared_ptr<SurgSim::Framework::Component> component,
std::vector<std::shared_ptr<T>>* container);
void processComponents();
void processBehaviors(const double dt);
// Enum is defined in the beginning of this file
virtual int getType() const = 0;
void copyScheduledComponents(std::vector<std::shared_ptr<Component>>* inflightAdditions,
std::vector<std::shared_ptr<Component>>* inflightRemovals,
std::vector<std::shared_ptr<SceneElement>>* inflightElements);
std::shared_ptr<SurgSim::Framework::Logger> getLogger() const;
boost::mutex m_componentMutex;
std::vector<std::shared_ptr<Component>> m_componentAdditions;
std::vector<std::shared_ptr<Component>> m_componentRemovals;
std::vector<std::shared_ptr<SceneElement>> m_elementCache;
// Each behavior will have a type to be matched with the corresponding manager
// Managers will only handle matching behaviors
std::vector<std::shared_ptr<SurgSim::Framework::Behavior>> m_behaviors;
void doBeforeStop() override;
template<class T>
void retireComponents(const std::vector<std::shared_ptr<T>>& container);
private:
virtual bool executeAdditions(const std::shared_ptr<Component>& component) = 0;
virtual bool executeRemovals(const std::shared_ptr<Component>& component) = 0;
bool executeInitialization() override;
void removeComponents(const std::vector<std::shared_ptr<Component>>::const_iterator& beginIt,
const std::vector<std::shared_ptr<Component>>::const_iterator& endIt);
void addComponents(
const std::vector<std::shared_ptr<Component>>::const_iterator& beginIt,
const std::vector<std::shared_ptr<Component>>::const_iterator& endIt,
std::vector<std::shared_ptr<Component>>* actualAdditions);
void wakeUpComponents(const std::vector<std::shared_ptr<Component>>::const_iterator& beginIt,
const std::vector<std::shared_ptr<Component>>::const_iterator& endIt);
std::weak_ptr<Runtime> m_runtime;
};
}; // namespace Framework
}; // namespace SurgSim
#include "SurgSim/Framework/ComponentManager-inl.h"
#endif // SURGSIM_FRAMEWORK_COMPONENTMANAGER_H
SurgSim
Definition: CompoundShapeToGraphics.cpp:29
SurgSim::Framework::ComponentManager::getLogger
std::shared_ptr< SurgSim::Framework::Logger > getLogger() const
Returns this manager&#39;s logger.
Definition: ComponentManager.cpp:226
SurgSim::Framework::ComponentManager::m_behaviors
std::vector< std::shared_ptr< SurgSim::Framework::Behavior > > m_behaviors
Collection of behaviors.
Definition: ComponentManager.h:131
SurgSim::Framework::ComponentManager::wakeUpComponents
void wakeUpComponents(const std::vector< std::shared_ptr< Component >>::const_iterator &beginIt, const std::vector< std::shared_ptr< Component >>::const_iterator &endIt)
Wake all the components up, only the components that were successfully initialized get the wakeup cal...
Definition: ComponentManager.cpp:200
SurgSim::Framework::ComponentManager::tryRemoveComponent
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
SurgSim::Framework::ComponentManager::m_componentMutex
boost::mutex m_componentMutex
Blocks protects addition and removal queues.
Definition: ComponentManager.h:118
SurgSim::Framework::ComponentManager::retireComponents
void retireComponents(const std::vector< std::shared_ptr< T >> &container)
Definition: ComponentManager-inl.h:80
SurgSim::Framework::ComponentManager::executeRemovals
virtual bool executeRemovals(const std::shared_ptr< Component > &component)=0
Handle representations, override for each thread.
Component.h
SurgSim::Framework::ComponentManager
Base Component Manager class.
Definition: ComponentManager.h:49
SurgSim::Framework::ComponentManager::m_componentRemovals
std::vector< std::shared_ptr< Component > > m_componentRemovals
Definition: ComponentManager.h:124
SurgSim::Framework::ComponentManager::executeInitialization
bool executeInitialization() override
Overridden from BasicThread, extends the initialization to contain component initialization including...
Definition: ComponentManager.cpp:100
Log.h
The convenience header that provides the entirety of the logging API.
SurgSim::Framework::ComponentManager::processBehaviors
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
SurgSim::Framework::ComponentManager::getRuntime
std::shared_ptr< Runtime > getRuntime() const
Definition: ComponentManager.cpp:58
SurgSim::Framework::ComponentManager::m_elementCache
std::vector< std::shared_ptr< SceneElement > > m_elementCache
Definition: ComponentManager.h:125
SurgSim::Framework::ComponentManager::enqueueAddComponent
bool enqueueAddComponent(const std::shared_ptr< Component > &component)
Queues a component to be added later.
Definition: ComponentManager.cpp:43
SurgSim::Framework::ComponentManager::doBeforeStop
void doBeforeStop() override
Prepares the thread for its execution to be stopped.
Definition: ComponentManager.cpp:217
Behavior.h
SurgSim::Framework::ComponentManager::processComponents
void processComponents()
Processes all the components that are scheduled for addition or removal, this needs to be called insi...
Definition: ComponentManager.cpp:63
SurgSim::Framework::ComponentManager::ComponentManager
ComponentManager(const std::string &name="Unknown Component Manager")
Definition: ComponentManager.cpp:28
SurgSim::Framework::ComponentManager::enqueueRemoveComponent
bool enqueueRemoveComponent(const std::shared_ptr< Component > &component)
Queues a component to be removed.
Definition: ComponentManager.cpp:50
ComponentManager-inl.h
SurgSim::Framework::ComponentManager::setRuntime
void setRuntime(std::shared_ptr< Runtime > val)
Definition: ComponentManager.cpp:38
SurgSim::Framework::ComponentManager::m_runtime
std::weak_ptr< Runtime > m_runtime
Definition: ComponentManager.h:179
SurgSim::Framework::ComponentManager::~ComponentManager
virtual ~ComponentManager()
Definition: ComponentManager.cpp:33
SurgSim::Framework::BasicThread
Basic thread implementation, tries to maintain a constant rate, supplies startup an initialization...
Definition: BasicThread.h:48
SurgSim::Framework::ComponentManager::executeAdditions
virtual bool executeAdditions(const std::shared_ptr< Component > &component)=0
Adds a component.
SurgSim::Framework::ComponentManager::removeComponents
void removeComponents(const std::vector< std::shared_ptr< Component >>::const_iterator &beginIt, const std::vector< std::shared_ptr< Component >>::const_iterator &endIt)
Delegates to doRemoveComponent to remove all the components in the indicated array.
Definition: ComponentManager.cpp:165
SurgSim::Framework::ComponentManager::m_componentAdditions
std::vector< std::shared_ptr< Component > > m_componentAdditions
Definition: ComponentManager.h:123
SurgSim::Framework::ComponentManager::tryAddComponent
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
BasicThread.h
SurgSim::Framework::ComponentManager::copyScheduledComponents
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
SurgSim::Framework::ComponentManager::getType
virtual int getType() const =0
Returns the type of Manager.
SurgSim::Framework::ComponentManager::addComponents
void addComponents(const std::vector< std::shared_ptr< Component >>::const_iterator &beginIt, const std::vector< std::shared_ptr< Component >>::const_iterator &endIt, std::vector< std::shared_ptr< Component >> *actualAdditions)
Delegates to doAddComponent and calls initialize on all the components.
Definition: ComponentManager.cpp:177