GGS(GenericGEANT4Simulation)Software  2.6.3
 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 30 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 123 of file GGSFactory.h.

124  {
125  const std::string &routineName(_factoryName + "::CreateObject");
126 
127  // 1. Search for a suitable builder
128  typename BuildersMap::iterator builderIter = _builders.find(builderName);
129  if (builderIter == _builders.end()) {
130  COUT(ERROR) << "No available class builder with name " << builderName << "." << ENDL;
131  return std::unique_ptr<T>((T*)(NULL));
132  }
133 
134  // 2. Build the object and return it
135  return std::unique_ptr<T>((*(builderIter->second))(arguments...));
136 
137 }
#define ENDL
Definition: GGSSmartLog.h:104
#define COUT(level)
Smart log macro. It writes on stdout only if the specified verbosity level is lesser than the maximum...
Definition: GGSSmartLog.h:77
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 75 of file GGSFactory.h.

75  {
76  static GGSFactory<T, ConstructorArgs...> instance;
77  return instance;
78 }
Template factory class.
Definition: GGSFactory.h:30
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 118 of file GGSFactory.h.

118  {
119  return _builderNames;
120 }
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 99 of file GGSFactory.h.

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

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