GGS(GenericGEANT4Simulation)Software  2.7.0
 All Data Structures Namespaces Files Functions Variables Typedefs Macros
Public Member Functions | Static Public Member Functions
GGSFactory< T, ConstructorArgs > Class Template Reference

Template factory class. More...

#include <GGSFactory.h>

Public Member Functions

bool RegisterBuilder (const std::string &builderName, T *(*objectBuilder)(ConstructorArgs...))
 Register a builder for a class. More...
 
std::unique_ptr< T > CreateObject (const std::string &name, ConstructorArgs...arguments)
 Create an object. More...
 
const std::vector< std::string > & GetListOfRegisteredBuilders ()
 Gets a vector containing the names of available registered builders. More...
 

Static Public Member Functions

static GGSFactoryGetInstance ()
 Getter method for singleton pointer. More...
 

Detailed Description

template<class T, typename... ConstructorArgs>
class GGSFactory< T, ConstructorArgs >

Template factory class.

This class is a generic factory, that can build all the derived classes of a given base class using constructors with any set of arguments. The first template argument is the base class of the objects built by the factory. Variadic template arguments are the constructor arguments. Actual object build is done by means of builder functions, each constructing instances of a given derived class. Builders must be registered providing a suitable builder function with a name (which can be the name of the built class itself).

Definition at line 29 of file GGSFactory.h.

Member Function Documentation

template<class T , typename... ConstructorArgs>
std::unique_ptr< T > GGSFactory< T, ConstructorArgs >::CreateObject ( const std::string &  name,
ConstructorArgs...  arguments 
)

Create an object.

The builder is fetched by its name. If the desired builder is not available, the NULL pointer will be returned.

Parameters
nameThe name of the builder for the desired object.
Returns
a pointer to an instance of the derived class created by the builder (NULL if any error occurs).

Definition at line 115 of file GGSFactory.h.

116  {
117  const std::string &routineName(_factoryName + "::CreateObject");
118 
119  // 1. Search for a suitable builder
120  typename BuildersMap::iterator builderIter = _builders.find(builderName);
121  if (builderIter == _builders.end()) {
122  COUT(ERROR) << "No available class builder with name " << builderName << "." << ENDL;
123  return std::unique_ptr<T>((T *)(NULL));
124  }
125 
126  // 2. Build the object and return it
127  return std::unique_ptr<T>((*(builderIter->second))(arguments...));
128 }
#define ENDL
Definition: GGSSmartLog.h:105
#define COUT(level)
Smart log macro. It writes on stdout only if the specified verbosity level is lesser than the maximum...
Definition: GGSSmartLog.h:76
template<class T , typename... ConstructorArgs>
GGSFactory< T, ConstructorArgs...> & GGSFactory< T, ConstructorArgs >::GetInstance ( )
static

Getter method for singleton pointer.

Returns
pointer to the singleton instance of the GGSFactory class.

Definition at line 71 of file GGSFactory.h.

71  {
72  static GGSFactory<T, ConstructorArgs...> instance;
73  return instance;
74 }
Template factory class.
Definition: GGSFactory.h:29
template<class T , typename... ConstructorArgs>
const std::vector< std::string > & GGSFactory< T, ConstructorArgs >::GetListOfRegisteredBuilders ( )

Gets a vector containing the names of available registered builders.

Returns
a vector with the names of registered builders.

Definition at line 110 of file GGSFactory.h.

110  {
111  return _builderNames;
112 }
template<class T , typename... ConstructorArgs>
bool GGSFactory< T, ConstructorArgs >::RegisterBuilder ( const std::string &  builderName,
T *(*)(ConstructorArgs...)  objectBuilder 
)

Register a builder for a class.

Parameters
builderNameThe name of the builder.
objectBuilderPointer to a builder function for the class.
Returns
true if the class has been correctly registered.

Definition at line 92 of file GGSFactory.h.

93  {
94  const std::string &routineName(_factoryName + "::RegisterBuilder");
95 
96  std::pair<typename BuildersMap::iterator, bool> insertResult =
97  _builders.insert(std::pair<std::string, T *(*)(ConstructorArgs...)>(builderName, objectBuilder));
98  if (insertResult.second) {
99  _builderNames.push_back(builderName);
100  COUT(DEBUG) << "Registered builder " << builderName << ENDL;
101  return true;
102  } else {
103  COUT(ERROR) << "Impossible to register class builder " << builderName
104  << ": a class builder with the same name is already registered." << ENDL;
105  return false;
106  }
107 }
#define ENDL
Definition: GGSSmartLog.h:105
#define COUT(level)
Smart log macro. It writes on stdout only if the specified verbosity level is lesser than the maximum...
Definition: GGSSmartLog.h:76

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