opensurgsim
ObjectFactory-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 
17 #ifndef SURGSIM_FRAMEWORK_OBJECTFACTORY_INL_H
18 #define SURGSIM_FRAMEWORK_OBJECTFACTORY_INL_H
19 
21 
22 template <class Base>
23 template <class Derived>
25 {
26  boost::mutex::scoped_lock lock(m_mutex);
27  bool result = false;
28  if (m_constructors.find(className) == m_constructors.end())
29  {
30  m_constructors[className] = boost::factory<std::shared_ptr<Derived>>();
31  result = true;
32  };
33  return result;
34 };
35 
36 template <class Base>
37 std::shared_ptr<Base> SurgSim::Framework::ObjectFactory<Base>::create(const std::string& className)
38 {
39  boost::mutex::scoped_lock lock(m_mutex);
40  auto it = m_constructors.find(className);
41  if (it == m_constructors.end())
42  {
43  SURGSIM_FAILURE() << "ObjectFactory does not know about class called " << className;
44  // gcc complains if there is no return
45  return nullptr;
46  }
47  return (it->second)();
48 };
49 
50 
51 template <typename Base>
52 bool SurgSim::Framework::ObjectFactory<Base>::isRegistered(const std::string& className) const
53 {
54  boost::mutex::scoped_lock lock(m_mutex);
55  auto it = m_constructors.find(className);
56  return (it != m_constructors.end());
57 }
58 
59 
60 template <typename Base, typename Parameter1>
61 template <typename Derived>
63 {
64  boost::mutex::scoped_lock lock(m_mutex);
65  bool result = false;
66  if (m_constructors.find(className) == m_constructors.end())
67  {
68  m_constructors[className] = boost::factory<std::shared_ptr<Derived>>();
69  result = true;
70  };
71  return result;
72 };
73 
74 template <typename Base, typename Parameter1>
76  const std::string& className,
77  const Parameter1& val)
78 {
79  boost::mutex::scoped_lock lock(m_mutex);
80  auto it = m_constructors.find(className);
81 
82  if (it == m_constructors.end())
83  {
84  SURGSIM_FAILURE() << "ObjectFactory does not know about class called " << className;
85  // gcc complains if there is no return
86  return nullptr;
87  }
88  return (it->second)(val);
89 };
90 
91 template <typename Base, typename Parameter1>
93 {
94  boost::mutex::scoped_lock lock(m_mutex);
95  auto it = m_constructors.find(className);
96  return (it != m_constructors.end());
97 }
98 
99 
100 #endif // SURGSIM_FRAMEWORK_OBJECTFACTORY_INL_H
std::shared_ptr< Base > create(const std::string &className, const Parameter1 &val)
Create an instance of a class based on the specific class name, whose constructor takes 1 parameter...
Definition: ObjectFactory-inl.h:75
#define SURGSIM_FAILURE()
Report that something very bad has happened and abort program execution.
Definition: Assert.h:95
bool registerClass(const std::string &className)
Register a class with the factory.
Definition: ObjectFactory-inl.h:62
bool isRegistered(const std::string &className) const
Check whether the class is registered in the factory.
Definition: ObjectFactory-inl.h:52
bool registerClass(const std::string &className)
Register a class with the factory.
Definition: ObjectFactory-inl.h:24
The header that provides the assertion API.
std::shared_ptr< Base > create(const std::string &className)
Create an instance of a class based on the specific class name.
Definition: ObjectFactory-inl.h:37
bool isRegistered(const std::string &className) const
Check whether the class is registered in the factory.
Definition: ObjectFactory-inl.h:92