opensurgsim
ComponentManager-inl.h
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013, 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_INL_H
17 #define SURGSIM_FRAMEWORK_COMPONENTMANAGER_INL_H
18 
19 namespace SurgSim
20 {
21 namespace Framework
22 {
23 
29 template<class T>
30 std::shared_ptr<T> ComponentManager::tryAddComponent(std::shared_ptr<SurgSim::Framework::Component> component,
31  std::vector<std::shared_ptr<T>>* container)
32 {
33  SURGSIM_ASSERT(component != nullptr) << "Trying to add a component that is null";
34  SURGSIM_ASSERT(container != nullptr) << "Trying to use a component container that is null";
35  std::shared_ptr<T> typedComponent = std::dynamic_pointer_cast<T>(component);
36  if (typedComponent != nullptr)
37  {
38  auto found = std::find(container->cbegin(), container->cend(), typedComponent);
39  if (found == container->cend())
40  {
41  SURGSIM_LOG_INFO(m_logger) << "Added component " << component->getFullName();
42  container->push_back(typedComponent);
43  }
44  else
45  {
46  SURGSIM_LOG_INFO(m_logger) << "Component " << component->getFullName() << " already added to " << getName();
47  typedComponent = nullptr;
48  }
49  }
50  return typedComponent;
51 };
52 
53 template<class T>
54 bool ComponentManager::tryRemoveComponent(std::shared_ptr<SurgSim::Framework::Component> component,
55  std::vector<std::shared_ptr<T>>* container)
56 {
57  SURGSIM_ASSERT(container != nullptr) << "Trying to use a component container that is null";
58  bool result = false;
59  std::shared_ptr<T> typedComponent = std::dynamic_pointer_cast<T>(component);
60  if (typedComponent != nullptr && container->size() != 0)
61  {
62  auto found = std::find(container->begin(), container->end(), typedComponent);
63  if (found != container->end())
64  {
65  container->erase(found);
66  SURGSIM_LOG_DEBUG(m_logger) << __FUNCTION__ << " Removed component " << typedComponent->getName();
67  result = true;
68  }
69  else
70  {
72  << SURGSIM_CURRENT_FUNCTION << " Unable to remove component "
73  << typedComponent->getName() << ". Not found.";
74  }
75  }
76  return result;
77 };
78 
79 template<class T>
80 void ComponentManager::retireComponents(const std::vector<std::shared_ptr<T>>& container)
81 {
82  static_assert(std::is_base_of<Component, T>::value == true, "Class has to be of type component");
83  for (const auto& component : container)
84  {
85  component->retire();
86  }
87 }
88 
89 
90 }; // namespace Framework
91 }; // namespace SurgSim
92 
93 #endif
94 
Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
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
#define SURGSIM_LOG_DEBUG(logger)
Logs a message to the specified logger at the DEBUG level.
Definition: LogMacros.h:76
#define SURGSIM_ASSERT(condition)
Assert that condition is true.
Definition: Assert.h:77
std::shared_ptr< SurgSim::Framework::Logger > m_logger
Logger for this thread.
Definition: BasicThread.h:160
#define SURGSIM_CURRENT_FUNCTION
Helper macro to determine the function name currently being compiled.
Definition: Assert.h:61
std::string getName() const
Definition: BasicThread.cpp:233
#define SURGSIM_LOG_INFO(logger)
Logs a message to the specified logger at the INFO level.
Definition: LogMacros.h:86
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