ObjectFactory.h
Go to the documentation of this file.
// This file is a part of the OpenSurgSim project.
// Copyright 2013, 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_OBJECTFACTORY_H
#define SURGSIM_FRAMEWORK_OBJECTFACTORY_H
#include "SurgSim/Framework/Macros.h"
#include <string>
#include <map>
#include <boost/function.hpp>
#include <boost/functional/factory.hpp>
#include <boost/thread/mutex.hpp>
namespace SurgSim
{
namespace Framework
{
template <typename Base>
class ObjectFactory
{
public:
template <typename Derived>
bool registerClass(const std::string& className);
std::shared_ptr<Base> create(const std::string& className);
bool isRegistered(const std::string& className) const;
private:
typedef boost::function<std::shared_ptr<Base>()> Constructor;
std::map<std::string, Constructor> m_constructors;
mutable boost::mutex m_mutex;
};
template <typename Base, typename Parameter1>
class ObjectFactory1
{
public:
template <typename Derived>
bool registerClass(const std::string& className);
std::shared_ptr<Base> create(const std::string& className, const Parameter1& val);
bool isRegistered(const std::string& className) const;
private:
typedef boost::function<std::shared_ptr<Base>(const Parameter1&)> Constructor;
std::map<std::string, Constructor> m_constructors;
mutable boost::mutex m_mutex;
};
template <class T>
class FactoryBase
{
public:
typedef ObjectFactory<T> FactoryType;
static FactoryType& getFactory()
{
static FactoryType factory;
return factory;
}
};
template <class T, class P>
class FactoryBase1
{
public:
typedef ObjectFactory1<T, P> FactoryType;
static FactoryType& getFactory()
{
static FactoryType factory;
return factory;
}
};
};
};
#include "SurgSim/Framework/ObjectFactory-inl.h"
#define SURGSIM_REGISTER(BaseClass, DerivedClass, ClassName) \
SURGSIM_USED_VARIABLE(bool SURGSIM_CONCATENATE(ClassName, Registered)) = \
BaseClass::getFactory().registerClass<DerivedClass>(#DerivedClass);
#define SURGSIM_STATIC_REGISTRATION(ClassName) \
extern bool SURGSIM_CONCATENATE(ClassName, Registered); \
SURGSIM_USED_VARIABLE(static bool SURGSIM_CONCATENATE(ClassName, IsRegistered)) = \
SURGSIM_CONCATENATE(ClassName, Registered);
#endif // SURGSIM_FRAMEWORK_OBJECTFACTORY_H
SurgSim::Framework::ObjectFactory1::m_constructors
std::map< std::string, Constructor > m_constructors
All the constructors.
Definition: ObjectFactory.h:111
SurgSim
Definition: CompoundShapeToGraphics.cpp:29
SurgSim::Framework::FactoryBase1::FactoryType
ObjectFactory1< T, P > FactoryType
Definition: ObjectFactory.h:143
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< UniformBase, std::string >::Constructor
boost::function< std::shared_ptr< UniformBase >const std::string &)> Constructor
Definition: ObjectFactory.h:108
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::ObjectFactory::registerClass
bool registerClass(const std::string &className)
Register a class with the factory.
Definition: ObjectFactory-inl.h:24
SurgSim::Framework::ObjectFactory::m_constructors
std::map< std::string, Constructor > m_constructors
All the constructors.
Definition: ObjectFactory.h:67
SurgSim::Framework::FactoryBase::getFactory
static FactoryType & getFactory()
Definition: ObjectFactory.h:128
SurgSim::Framework::FactoryBase1::getFactory
static FactoryType & getFactory()
Definition: ObjectFactory.h:146
SurgSim::Framework::FactoryBase::FactoryType
ObjectFactory< T > FactoryType
Definition: ObjectFactory.h:125
Macros.h
SurgSim::Framework::ObjectFactory1::m_mutex
boost::mutex m_mutex
Threadsafety for registration.
Definition: ObjectFactory.h:114
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::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
SurgSim::Framework::ObjectFactory::Constructor
boost::function< std::shared_ptr< Base >)> Constructor
Definition: ObjectFactory.h:64
ObjectFactory-inl.h
SurgSim::Framework::ObjectFactory::m_mutex
boost::mutex m_mutex
Threadsafety for registration.
Definition: ObjectFactory.h:70
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