ComponentManager-inl.h
Go to the documentation of this file.
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) << __FUNCTION__ << " Added component " << component->getName();
42  container->push_back(typedComponent);
43  }
44  else
45  {
46  SURGSIM_LOG_INFO(m_logger) << __FUNCTION__ << " component " << component->getName() <<
47  " already added to " << getName();
48  typedComponent = nullptr;
49  }
50  }
51  return typedComponent;
52 };
53 
54 template<class T>
55 bool ComponentManager::tryRemoveComponent(std::shared_ptr<SurgSim::Framework::Component> component,
56  std::vector<std::shared_ptr<T>>* container)
57 {
58  SURGSIM_ASSERT(container != nullptr) << "Trying to use a component container that is null";
59  bool result = false;
60  std::shared_ptr<T> typedComponent = std::dynamic_pointer_cast<T>(component);
61  if (typedComponent != nullptr && container->size() != 0)
62  {
63  auto found = std::find(container->begin(), container->end(), typedComponent);
64  if (found != container->end())
65  {
66  container->erase(found);
67  SURGSIM_LOG_DEBUG(m_logger) << __FUNCTION__ << " Removed component " << typedComponent->getName();
68  result = true;
69  }
70  else
71  {
72  SURGSIM_LOG_INFO(m_logger) << SURGSIM_CURRENT_FUNCTION << " Unable to remove component " <<
73  typedComponent->getName() << ". Not found.";
74  }
75  }
76  return result;
77 };
78 
79 }; // namespace Framework
80 }; // namespace SurgSim
81 
82 #endif
83 
Definition: DriveElementFromInputBehavior.cpp:27
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:55
#define SURGSIM_LOG_DEBUG(logger)
Logs a message to the specified logger at the DEBUG level.
Definition: LogMacros.h:76
std::string getName() const
Definition: BasicThread.cpp:186
#define SURGSIM_ASSERT(condition)
Assert that condition is true.
Definition: Assert.h:77
#define SURGSIM_CURRENT_FUNCTION
Helper macro to determine the function name currently being compiled.
Definition: Assert.h:61
std::shared_ptr< SurgSim::Framework::Logger > m_logger
Logger for this class.
Definition: ComponentManager.h:126
#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