40#ifndef GEOGRAM_BASIC_FACTORY
41#define GEOGRAM_BASIC_FACTORY
57#ifdef GEO_COMPILER_CLANG
58#pragma clang diagnostic push
59#pragma clang diagnostic ignored "-Wdocumentation"
92 template <
class InstanceType>
94 const std::string name =
typeid(InstanceType).name();
96 if(instance ==
nullptr) {
97 instance =
new InstanceType;
100 return *
static_cast<InstanceType*
>(instance);
109 static void add(
const std::string& name, Instance* instance);
117 static Instance* get(
const std::string& name);
143 template <
class FactoryCreator>
146 typedef typename FactoryCreator::CreatorType CreatorType;
164 template <
class ConcreteType>
167 self.registry_[name] =
168 FactoryCreator::template create<ConcreteType>;
180 auto i = self.registry_.find(name);
181 return i == self.registry_.end() ? nullptr : i->second;
192 for(
auto& it : self.registry_) {
193 names.push_back(it.first);
205 for(
auto& it : self.registry_) {
206 if(it.first == name) {
231 template <
class ConcreteType>
241 Factory::template register_creator<ConcreteType>(name);
257 static inline Factory& instance() {
258 return InstanceRepo::instance<Factory>();
264 typedef std::map<std::string, CreatorType> Registry;
274 template <
class Type>
279 typedef Type* (* CreatorType)();
285 template <
class ConcreteType>
287 return new ConcreteType;
298 template <
class Type>
313 typename BaseClass::CreatorType creator =
315 return creator ==
nullptr ? nullptr : (* creator)();
326 template <
class Type,
class Param1>
331 typedef Type* (* CreatorType)(
const Param1&);
337 template <
class ConcreteType>
338 static Type*
create(
const Param1& param1) {
339 return new ConcreteType(param1);
351 template <
class Type,
class Param1>
367 const std::string& name,
const Param1& param1
369 typename BaseClass::CreatorType creator =
371 return creator ==
nullptr ? nullptr : (* creator)(param1);
393#define geo_register_creator(FactoryType, ConcreteType, name) \
394 static FactoryType::RegisterCreator<ConcreteType> \
395 CPP_CONCAT(Factory_register_creator_, __LINE__) (name); \
396 geo_argused(CPP_CONCAT(Factory_register_creator_, __LINE__))
400#ifdef GEO_COMPILER_CLANG
401#pragma clang diagnostic pop
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.