ObjectFactory.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_OBJECTFACTORY_H
17 #define SURGSIM_FRAMEWORK_OBJECTFACTORY_H
18 
20 
21 #include <string>
22 #include <map>
23 #include <boost/function.hpp>
24 #include <boost/functional/factory.hpp>
25 #include <boost/thread/mutex.hpp>
26 
27 
28 namespace SurgSim
29 {
30 namespace Framework
31 {
32 
39 template <typename Base>
41 {
42 public:
43 
48  template <typename Derived>
49  bool registerClass(const std::string& className);
50 
55  std::shared_ptr<Base> create(const std::string& className);
56 
60  bool isRegistered(const std::string& className) const;
61 
62 private:
63 
64  typedef boost::function<std::shared_ptr<Base>()> Constructor;
65 
67  std::map<std::string, Constructor> m_constructors;
68 
70  mutable boost::mutex m_mutex;
71 
72 };
73 
82 template <typename Base, typename Parameter1>
84 {
85 public:
86 
91  template <typename Derived>
92  bool registerClass(const std::string& className);
93 
99  std::shared_ptr<Base> create(const std::string& className, const Parameter1& val);
100 
104  bool isRegistered(const std::string& className) const;
105 
106 private:
107 
108  typedef boost::function<std::shared_ptr<Base>(const Parameter1&)> Constructor;
109 
111  std::map<std::string, Constructor> m_constructors;
112 
114  mutable boost::mutex m_mutex;
115 };
116 
117 
121 template <class T>
123 {
124 public:
126 
129  {
130  static FactoryType factory;
131  return factory;
132  }
133 };
134 
139 template <class T, class P>
141 {
142 public:
144 
147  {
148  static FactoryType factory;
149  return factory;
150  }
151 };
152 
153 };
154 };
155 
157 
164 #define SURGSIM_REGISTER(BaseClass, DerivedClass, ClassName) \
165  SURGSIM_USED_VARIABLE(bool SURGSIM_CONCATENATE(ClassName, Registered)) = \
166  BaseClass::getFactory().registerClass<DerivedClass>(#DerivedClass);
167 
181 #define SURGSIM_STATIC_REGISTRATION(ClassName) \
182  extern bool SURGSIM_CONCATENATE(ClassName, Registered); \
183  SURGSIM_USED_VARIABLE(static bool SURGSIM_CONCATENATE(ClassName, IsRegistered)) = \
184  SURGSIM_CONCATENATE(ClassName, Registered);
185 
186 #endif // SURGSIM_FRAMEWORK_OBJECTFACTORY_H
ObjectFactory-inl.h
SurgSim::Framework::ObjectFactory1::create
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
SurgSim::Framework::FactoryBase1::FactoryType
ObjectFactory1< T, P > FactoryType
Definition: ObjectFactory.h:143
Macros.h
SurgSim::Framework::ObjectFactory::isRegistered
bool isRegistered(const std::string &className) const
Check whether the class is registered in the factory.
Definition: ObjectFactory-inl.h:52
SurgSim::Framework::ObjectFactory
An object factory, once a class is registered with the factory it can be used to create instances of ...
Definition: ObjectFactory.h:40
SurgSim::Framework::ObjectFactory1::registerClass
bool registerClass(const std::string &className)
Register a class with the factory.
Definition: ObjectFactory-inl.h:62
SurgSim::Framework::FactoryBase1
CRTP Base class to implement Object Factory functionality on a base class, use this rather than writi...
Definition: ObjectFactory.h:140
SurgSim::Framework::ObjectFactory1::Constructor
boost::function< std::shared_ptr< Base >const Parameter1 &)> Constructor
Definition: ObjectFactory.h:108
SurgSim
Definition: CompoundShapeToGraphics.cpp:29
SurgSim::Framework::ObjectFactory1::m_mutex
boost::mutex m_mutex
Threadsafety for registration.
Definition: ObjectFactory.h:114
SurgSim::Framework::ObjectFactory1::isRegistered
bool isRegistered(const std::string &className) const
Check whether the class is registered in the factory.
Definition: ObjectFactory-inl.h:92
SurgSim::Framework::FactoryBase::getFactory
static FactoryType & getFactory()
Definition: ObjectFactory.h:128
SurgSim::Framework::ObjectFactory::Constructor
boost::function< std::shared_ptr< Base >)> Constructor
Definition: ObjectFactory.h:64
SurgSim::Framework::ObjectFactory1
An object factory, once a class is registered with the factory it can be used to create instances of ...
Definition: ObjectFactory.h:83
SurgSim::Framework::ObjectFactory::m_mutex
boost::mutex m_mutex
Threadsafety for registration.
Definition: ObjectFactory.h:70
SurgSim::Framework::FactoryBase1::getFactory
static FactoryType & getFactory()
Definition: ObjectFactory.h:146
SurgSim::Framework::FactoryBase::FactoryType
ObjectFactory< T > FactoryType
Definition: ObjectFactory.h:125
SurgSim::Framework::FactoryBase
CRTP Base class to implement Object Factory functionality on a base class, use this rather than writi...
Definition: ObjectFactory.h:122
SurgSim::Framework::ObjectFactory::registerClass
bool registerClass(const std::string &className)
Register a class with the factory.
Definition: ObjectFactory-inl.h:24
SurgSim::Framework::ObjectFactory1::m_constructors
std::map< std::string, Constructor > m_constructors
All the constructors.
Definition: ObjectFactory.h:111
SurgSim::Framework::ObjectFactory::m_constructors
std::map< std::string, Constructor > m_constructors
All the constructors.
Definition: ObjectFactory.h:67
SurgSim::Framework::ObjectFactory::create
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