40 #ifndef GEOGRAM_BASIC_FACTORY
41 #define GEOGRAM_BASIC_FACTORY
85 template <
class InstanceType>
87 const std::string name =
typeid(InstanceType).name();
89 if(instance ==
nullptr) {
90 instance =
new InstanceType;
93 return *
static_cast<InstanceType*
>(instance);
102 static void add(
const std::string& name, Instance* instance);
110 static Instance* get(
const std::string& name);
136 template <
class FactoryCreator>
139 typedef typename FactoryCreator::CreatorType CreatorType;
157 template <
class ConcreteType>
160 self.registry_[name] =
161 FactoryCreator::template create<ConcreteType>;
173 auto i =
self.registry_.find(name);
174 return i ==
self.registry_.end() ? nullptr : i->second;
185 for(
auto& it :
self.registry_) {
186 names.push_back(it.first);
198 for(
auto& it :
self.registry_) {
199 if(it.first == name) {
224 template <
class ConcreteType>
234 Factory::template register_creator<ConcreteType>(name);
250 static inline Factory& instance() {
251 return InstanceRepo::instance<Factory>();
257 typedef std::map<std::string, CreatorType> Registry;
267 template <
class Type>
272 typedef Type* (* CreatorType)();
278 template <
class ConcreteType>
280 return new ConcreteType;
291 template <
class Type>
306 typename BaseClass::CreatorType creator =
308 return creator ==
nullptr ? nullptr : (* creator)();
319 template <
class Type,
class Param1>
324 typedef Type* (* CreatorType)(
const Param1&);
330 template <
class ConcreteType>
331 static Type*
create(
const Param1& param1) {
332 return new ConcreteType(param1);
344 template <
class Type,
class Param1>
360 typename BaseClass::CreatorType creator =
362 return creator ==
nullptr ? nullptr : (* creator)(param1);
384 #define geo_register_creator(FactoryType, ConcreteType, name) \
385 static FactoryType::RegisterCreator<ConcreteType> \
386 CPP_CONCAT(Factory_register_creator_, __LINE__) (name); \
387 geo_argused(CPP_CONCAT(Factory_register_creator_, __LINE__))
Base class for reference-counted objects.
Factory for types without constructor arguments.
static Type * create_object(const std::string &name)
Creates a new object.
Factory for types with one constructor argument.
static Type * create_object(const std::string &name, const Param1 ¶m1)
Creates a new object with parameter(s).
Factory of typed objects.
static bool has_creator(const std::string &name)
Tests whether the factory has a creator.
static void list_creators(std::vector< std::string > &names)
Lists all registered creators.
static void register_creator(const std::string &name)
Registers a creator.
~Factory() override
Factory destructor.
static CreatorType find_creator(const std::string &name)
Finds a creator by name.
Repository of unique instances.
Counted Instance
Type of the Instances stored in the repository.
static InstanceType & instance()
Gets unique instance from the repository.
Base class of reference-counted objects, to be used with smart pointers.
Types and functions for memory manipulation.
Global Vorpaline namespace.
Factory creator without constructor arguments.
static Type * create()
Creation function.
Factory creator with one argument.
static Type * create(const Param1 ¶m1)
Creation function.
Helper class to register a creator.
RegisterCreator(const std::string &name)
Constructs a registration object.