Geogram  Version 1.9.1-rc
A programming library of geometric algorithms
GEO::Factory< FactoryCreator > Class Template Reference

Factory of typed objects. More...

#include <geogram/basic/factory.h>

Inheritance diagram for GEO::Factory< FactoryCreator >:
GEO::Counted

Classes

struct  RegisterCreator
 Helper class to register a creator. More...
 

Public Types

typedef FactoryCreator::CreatorType CreatorType
 

Static Public Member Functions

template<class ConcreteType >
static void register_creator (const std::string &name)
 Registers a creator. More...
 
static CreatorType find_creator (const std::string &name)
 Finds a creator by name. More...
 
static void list_creators (std::vector< std::string > &names)
 Lists all registered creators. More...
 
static bool has_creator (const std::string &name)
 Tests whether the factory has a creator. More...
 
- Static Public Member Functions inherited from GEO::Counted
static void ref (const Counted *counted)
 Increments the reference count. More...
 
static void unref (const Counted *counted)
 Decrements the reference count. More...
 

Protected Member Functions

 ~Factory () override
 Factory destructor.
 
- Protected Member Functions inherited from GEO::Counted
 Counted ()
 Creates a reference counted object. More...
 
virtual ~Counted ()
 Destroys a reference counted object. More...
 

Additional Inherited Members

- Public Member Functions inherited from GEO::Counted
void ref () const
 Increments the reference count. More...
 
void unref () const
 Decrements the reference count. More...
 
bool is_shared () const
 Check if the object is shared. More...
 
int nb_refs () const
 Gets the number of references that point to this object. More...
 

Detailed Description

template<class FactoryCreator>
class GEO::Factory< FactoryCreator >

Factory of typed objects.

A Factory is a mechanism to create objects without knowing their types in advance. The object types that a Factory can instantiate belong to a type hierarchy whose base type is defined by Type. The Factory uses a registry of creator functions bound to user-defined names. The user-defined names can be used to:

  • register a creator function (see register_creator())
  • create an object, using the creator function bound to the name (see create_object())
    Template Parameters
    FactoryCreatorthe type of the creator used to create objects in this Factory. FactoryCreator must define:
  • the type CreatorType type of the creation function
  • a static template function create(...) used to actually create objects of a given concrete type

Definition at line 137 of file factory.h.

Member Function Documentation

◆ find_creator()

template<class FactoryCreator >
static CreatorType GEO::Factory< FactoryCreator >::find_creator ( const std::string &  name)
inlinestatic

Finds a creator by name.

Parameters
[in]namea user-defined name identifying a creator in the Factory
Return values
thecreator associated to name if name exists
nullpointer otherwise

Definition at line 171 of file factory.h.

◆ has_creator()

template<class FactoryCreator >
static bool GEO::Factory< FactoryCreator >::has_creator ( const std::string &  name)
inlinestatic

Tests whether the factory has a creator.

Parameters
[in]namename of the creator
Return values
trueif creator name is registered in the factory
falseotherwise

Definition at line 196 of file factory.h.

◆ list_creators()

template<class FactoryCreator >
static void GEO::Factory< FactoryCreator >::list_creators ( std::vector< std::string > &  names)
inlinestatic

Lists all registered creators.

This stores the names of the registered creators to output vector names.

Parameters
[out]namesoutput list of registered names

Definition at line 183 of file factory.h.

◆ register_creator()

template<class FactoryCreator >
template<class ConcreteType >
static void GEO::Factory< FactoryCreator >::register_creator ( const std::string &  name)
inlinestatic

Registers a creator.

This creates a new creator for objects of type ConcreteType and registers it with the user-defined name name.

Parameters
[in]namename of the ConcreteType creator in the Factory
Template Parameters
ConcreteTypethe type of the objects to create
Usage example:
struct MyBaseClass { ... };
typedef Factory<MyBaseClass> MyFactory;
struct MyDerivedClass : MyBaseClass { ... };
MyFactory::register_creator<MyDerivedClass>("my_derived_class");
See also
RegisterCreator()

Definition at line 158 of file factory.h.


The documentation for this class was generated from the following file: