35 #ifndef OPM_FACTORY_HEADER
36 #define OPM_FACTORY_HEADER
38 #include <opm/common/ErrorMacros.hpp>
66 return instance().doCreateObject(type);
76 const ProductPtr original)
78 return instance().doCloneObject(type, original);
88 template<
class Derived>
91 instance().template doAddCreator<Derived>(type);
112 virtual ProductPtr create() = 0;
113 virtual ProductPtr clone(ProductPtr original) = 0;
114 virtual ~Creator() {}
118 template<
class Derived>
119 class ConcreteCreator:
public Creator
122 virtual ProductPtr create()
126 virtual ProductPtr clone(
const ProductPtr original)
128 const Derived& orig =
dynamic_cast<const Derived&
>(*original);
133 typedef std::shared_ptr<Creator> CreatorPtr;
134 typedef std::map<std::string, CreatorPtr> CreatorMap;
136 CreatorMap string_to_creator_;
139 ProductPtr doCreateObject(
const std::string& type)
141 typename CreatorMap::iterator it;
142 it = string_to_creator_.find(type);
143 if (it == string_to_creator_.end()) {
144 OPM_THROW(std::runtime_error,
"Creator type " << type
145 <<
" is not registered in the factory.");
147 return it->second->create();
151 ProductPtr doCloneObject(
const std::string& type,
152 const ProductPtr original)
154 typename CreatorMap::iterator it;
155 it = string_to_creator_.find(type);
156 if (it == string_to_creator_.end()) {
157 OPM_THROW(std::runtime_error,
"Creator type " << type
158 <<
" is not registered in the factory.");
160 return it->second->clone(original);
164 template<
class Derived>
165 void doAddCreator(
const std::string& type)
167 CreatorPtr c(
new ConcreteCreator<Derived>);
168 string_to_creator_[type] = c;
174 #endif // OPM_FACTORY_HEADER
static ProductPtr cloneObject(const std::string &type, const ProductPtr original)
Definition: Factory.hpp:75
Definition: AnisotropicEikonal.hpp:43
static void addCreator(const std::string &type)
Definition: Factory.hpp:89
Definition: Factory.hpp:53
std::shared_ptr< Base > ProductPtr
The type of pointer returned by createObject().
Definition: Factory.hpp:57
static ProductPtr createObject(const std::string &type)
Definition: Factory.hpp:64