Nektar++
Classes | Public Types | Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
Nektar::LibUtilities::NekFactory< tKey, tBase, tParam > Class Template Reference

Provides a generic Factory class. More...

#include <NekFactory.hpp>

Classes

struct  ModuleEntry
 Define a struct to hold the information about a module. More...
 

Public Types

typedef std::less< tKey > tPredicator
 Comparison predicator of key. More...
 
typedef std::shared_ptr< tBase > tBaseSharedPtr
 Shared pointer to an object of baseclass type. More...
 
typedef std::function< tBaseSharedPtr(tParam...)> CreatorFunction
 CreatorFunction type which takes parameter and returns base class shared pointer. More...
 
typedef std::map< tKey, ModuleEntry, tPredicatorTMapFactory
 Factory map between key and module data. More...
 

Public Member Functions

 NekFactory ()=default
 
tBaseSharedPtr CreateInstance (tKey idKey, tParam... args)
 Create an instance of the class referred to by idKey. More...
 
tKey RegisterCreatorFunction (tKey idKey, CreatorFunction classCreator, std::string pDesc="")
 Register a class with the factory. More...
 
bool ModuleExists (tKey idKey)
 Checks if a particular module is available. More...
 
void PrintAvailableClasses (std::ostream &pOut=std::cout)
 Prints the available classes to stdout. More...
 
tKey GetKey (std::string pDesc)
 Retrieves a key, given a description. More...
 
std::string GetClassDescription (tKey idKey)
 Returns the description of a class. More...
 

Protected Member Functions

TMapFactorygetMapFactory ()
 Ensure the factory's map is created. More...
 

Private Member Functions

 NekFactory (const NekFactory &rhs)
 
NekFactoryoperator= (const NekFactory &rhs)
 

Private Attributes

TMapFactory mMapFactory
 

Detailed Description

template<typename tKey, typename tBase, typename... tParam>
class Nektar::LibUtilities::NekFactory< tKey, tBase, tParam >

Provides a generic Factory class.

Implements a generic object factory. Class-types which use an arbitrary number of parameters may be used via C++ variadic templating.

To allow a class to be instantiated by the factory, the following are required in each class definition (in the case of a single parameter):

static [baseclass]* create([paramtype1] &P) {
return new [derivedclass](P);
}
static std::string className;

and outside the class definition in the implementation:

std::string [derivedclass]::className
= Factory<std::string,[baseclass],[paramtype1]>::
RegisterCreatorFunction("[derivedclass]",
[derivedclass]::create,"Description");

The assignment of the static variable className is done through the call to RegisterCreatorFunction, which registers the class with the factory prior to the start of the main() routine.

To create an instance of a derived class, for instance:

[baseclass]* var_name =
Factory<std::string,[baseclass],[paramtype1]>
::CreateInstance("[derivedclass]",Param1);

Definition at line 104 of file NekFactory.hpp.

Member Typedef Documentation

◆ CreatorFunction

template<typename tKey , typename tBase , typename... tParam>
typedef std::function<tBaseSharedPtr(tParam...)> Nektar::LibUtilities::NekFactory< tKey, tBase, tParam >::CreatorFunction

CreatorFunction type which takes parameter and returns base class shared pointer.

Definition at line 113 of file NekFactory.hpp.

◆ tBaseSharedPtr

template<typename tKey , typename tBase , typename... tParam>
typedef std::shared_ptr<tBase> Nektar::LibUtilities::NekFactory< tKey, tBase, tParam >::tBaseSharedPtr

Shared pointer to an object of baseclass type.

Definition at line 110 of file NekFactory.hpp.

◆ TMapFactory

template<typename tKey , typename tBase , typename... tParam>
typedef std::map<tKey, ModuleEntry, tPredicator> Nektar::LibUtilities::NekFactory< tKey, tBase, tParam >::TMapFactory

Factory map between key and module data.

Definition at line 130 of file NekFactory.hpp.

◆ tPredicator

template<typename tKey , typename tBase , typename... tParam>
typedef std::less<tKey> Nektar::LibUtilities::NekFactory< tKey, tBase, tParam >::tPredicator

Comparison predicator of key.

Definition at line 108 of file NekFactory.hpp.

Constructor & Destructor Documentation

◆ NekFactory() [1/2]

template<typename tKey , typename tBase , typename... tParam>
Nektar::LibUtilities::NekFactory< tKey, tBase, tParam >::NekFactory ( )
default

◆ NekFactory() [2/2]

template<typename tKey , typename tBase , typename... tParam>
Nektar::LibUtilities::NekFactory< tKey, tBase, tParam >::NekFactory ( const NekFactory< tKey, tBase, tParam > &  rhs)
private

Member Function Documentation

◆ CreateInstance()

template<typename tKey , typename tBase , typename... tParam>
tBaseSharedPtr Nektar::LibUtilities::NekFactory< tKey, tBase, tParam >::CreateInstance ( tKey  idKey,
tParam...  args 
)
inline

Create an instance of the class referred to by idKey.

Searches the factory's map for the given key and returns a shared base class pointer to a new instance of the associated class.

Parameters
idKeyKey of class to create.
xParameter to pass to class constructor.
Returns
Base class pointer to new instance.

Definition at line 144 of file NekFactory.hpp.

145  {
146 #ifdef NEKTAR_USE_THREAD_SAFETY
147  ReadLock vReadLock(m_mutex);
148 #endif
149 
150  // Now try and find the key in the map.
151  auto it = getMapFactory()->find(idKey);
152 
153  // If successful, check the CreatorFunction is defined and
154  // create a new instance of the class.
155  if (it != getMapFactory()->end())
156  {
157  ModuleEntry *tmp = &(it->second);
158 #ifdef NEKTAR_USE_THREAD_SAFETY
159  vReadLock.unlock();
160 #endif
161 
162  if (tmp->m_func)
163  {
164  try
165  {
166  return tmp->m_func(args...);
167  }
168  catch (const std::string &s)
169  {
170  std::stringstream errstr;
171  errstr << "Unable to create module: " << idKey << "\n";
172  errstr << s;
173  NEKERROR(ErrorUtil::efatal, errstr.str());
174  }
175  }
176  }
177 
178  // If we get this far, the key doesn't exist, so throw an error.
179  std::stringstream errstr;
180  errstr << "No such module: " << idKey << std::endl;
181  PrintAvailableClasses(errstr);
182  NEKERROR(ErrorUtil::efatal, errstr.str());
183  return tBaseSharedPtr();
184  }
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mode...
Definition: ErrorUtil.hpp:209
std::shared_ptr< tBase > tBaseSharedPtr
Shared pointer to an object of baseclass type.
Definition: NekFactory.hpp:110
TMapFactory * getMapFactory()
Ensure the factory's map is created.
Definition: NekFactory.hpp:297
void PrintAvailableClasses(std::ostream &pOut=std::cout)
Prints the available classes to stdout.
Definition: NekFactory.hpp:232
boost::shared_lock< boost::shared_mutex > ReadLock
Definition: Thread.h:380

References Nektar::ErrorUtil::efatal, Nektar::LibUtilities::NekFactory< tKey, tBase, tParam >::getMapFactory(), Nektar::LibUtilities::NekFactory< tKey, tBase, tParam >::ModuleEntry::m_func, NEKERROR, and Nektar::LibUtilities::NekFactory< tKey, tBase, tParam >::PrintAvailableClasses().

Referenced by Nektar::SolverUtils::Advection3DHomogeneous1D::Advection3DHomogeneous1D(), Nektar::ArtificialDiffusion::ArtificialDiffusion(), Nektar::Collections::BwdTrans_MatrixFree::BwdTrans_MatrixFree(), Nektar::CompressibleSolver::CompressibleSolver(), Nektar::LibUtilities::SessionReader::CreateComm(), Nektar::LibUtilities::FieldIO::CreateDefault(), Nektar::LibUtilities::FieldIO::CreateForFile(), Nektar::Thread::ThreadMaster::CreateInstance(), Nektar::SolverUtils::FilterFieldConvert::CreateModules(), Nektar::MultiRegions::GlobalLinSys::CreatePrecon(), Nektar::LinearisedAdvection::DFT(), Nektar::SolverUtils::Diffusion3DHomogeneous1D::Diffusion3DHomogeneous1D(), Nektar::VortexWaveInteraction::ExecuteRoll(), Nektar::VortexWaveInteraction::ExecuteStreak(), Nektar::VortexWaveInteraction::ExecuteWave(), Nektar::MultiRegions::ExpListHomogeneous1D::ExpListHomogeneous1D(), Nektar::MultiRegions::ExpListHomogeneous2D::ExpListHomogeneous2D(), Nektar::MultiRegions::ExpList::ExtractCoeffsFromFile(), Field_Init(), Nektar::FieldUtils::Field::FieldIOForFile(), Nektar::MultiRegions::ExpList::GenGlobalBndLinSys(), Nektar::MultiRegions::ExpList::GenGlobalLinSys(), Nektar::Collections::Helmholtz_MatrixFree::Helmholtz_MatrixFree(), Nektar::LibUtilities::Import(), Nektar::CompressibleFlowSystem::InitAdvection(), Nektar::Collections::Collection::Initialise(), Nektar::ForcingMovingBody::InitialiseCableModel(), Nektar::CFSImplicit::InitialiseNonlinSysSolver(), Nektar::NavierStokesCFE::InitObject_Explicit(), Nektar::Collections::IProductWRTBase_MatrixFree::IProductWRTBase_MatrixFree(), Nektar::Collections::IProductWRTDerivBase_MatrixFree::IProductWRTDerivBase_MatrixFree(), Nektar::GlobalMapping::Mapping::Load(), main(), Module_Create(), Nektar::LibUtilities::NekNonlinSys::NekNonlinSys(), Nektar::Collections::PhysDeriv_MatrixFree::PhysDeriv_MatrixFree(), Nektar::SpatialDomains::MeshGraph::Read(), Nektar::SpatialDomains::MeshPartition::ReadExpansions(), Nektar::UpwindPulseSolver::RiemannSolverUpwind(), SessionReader_CreateInstance(), Nektar::PulseWavePropagation::SetPulseWaveBoundaryConditions(), Nektar::VelocityCorrectionScheme::SetUpExtrapolation(), Nektar::UnsteadyAdvectionDiffusion::SetUpSubSteppingTimeIntegration(), Nektar::Stimulus::Stimulus(), Diffusion::TimeIntegrate(), Nektar::MultiRegions::PreconditionerLinearWithBlock::v_InitObject(), Nektar::MultiRegions::PreconditionerLinearWithDiag::v_InitObject(), Nektar::MultiRegions::PreconditionerLinearWithLowEnergy::v_InitObject(), Nektar::PulseWavePropagation::v_InitObject(), Nektar::PulseWaveSystem::v_InitObject(), Nektar::SolverUtils::UnsteadySystem::v_InitObject(), Nektar::Bidomain::v_InitObject(), Nektar::BidomainRoth::v_InitObject(), Nektar::Monodomain::v_InitObject(), Nektar::ImageWarpingSystem::v_InitObject(), Nektar::CoupledLinearNS::v_InitObject(), Nektar::IncNavierStokes::v_InitObject(), Nektar::VCSMapping::v_InitObject(), Nektar::APE::v_InitObject(), Nektar::LEE::v_InitObject(), Nektar::CFLtester::v_InitObject(), Nektar::EigenValuesAdvection::v_InitObject(), Nektar::MMFAdvection::v_InitObject(), Nektar::UnsteadyAdvection::v_InitObject(), Nektar::UnsteadyAdvectionDiffusion::v_InitObject(), Nektar::UnsteadyDiffusion::v_InitObject(), Nektar::UnsteadyInviscidBurger::v_InitObject(), Nektar::UnsteadyViscousBurgers::v_InitObject(), Nektar::CompressibleFlowSystem::v_InitObject(), Nektar::LinearSWE::v_InitObject(), Nektar::NonlinearPeregrine::v_InitObject(), Nektar::NonlinearSWE::v_InitObject(), Nektar::DiffusionLDGNS::v_InitObject(), Nektar::SolverUtils::Driver::v_InitObject(), Nektar::SolverUtils::DriverParareal::v_InitObject(), Nektar::FieldUtils::OutputFld::v_OutputFromData(), Nektar::FieldUtils::OutputFld::v_OutputFromExp(), Nektar::SpatialDomains::MeshGraphHDF5::v_PartitionMesh(), Nektar::SpatialDomains::MeshGraphXml::v_PartitionMesh(), Nektar::FieldUtils::ProcessPointDataToFld::v_Process(), Nektar::MultiRegions::GlobalLinSysIterative::v_SolveLinearSystem(), Nektar::SubSteppingExtrapolate::v_SubSteppingTimeIntegration(), Nektar::VariableConverter::VariableConverter(), Nektar::VortexWaveInteraction::VortexWaveInteraction(), Nektar::LibUtilities::Write(), and Nektar::FieldUtils::OutputFileBase::WriteFile().

◆ GetClassDescription()

template<typename tKey , typename tBase , typename... tParam>
std::string Nektar::LibUtilities::NekFactory< tKey, tBase, tParam >::GetClassDescription ( tKey  idKey)
inline

Returns the description of a class.

Definition at line 277 of file NekFactory.hpp.

278  {
279 #ifdef NEKTAR_USE_THREAD_SAFETY
280  ReadLock vReadLock(m_mutex);
281 #endif
282 
283  // Now try and find the key in the map.
284  auto it = getMapFactory()->find(idKey);
285 
286  std::stringstream errstr;
287  errstr << "No such module: " << idKey << std::endl;
288  ASSERTL0(it != getMapFactory()->end(), errstr.str());
289  return it->second.m_desc;
290  }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:215

References ASSERTL0, and Nektar::LibUtilities::NekFactory< tKey, tBase, tParam >::getMapFactory().

◆ GetKey()

template<typename tKey , typename tBase , typename... tParam>
tKey Nektar::LibUtilities::NekFactory< tKey, tBase, tParam >::GetKey ( std::string  pDesc)
inline

Retrieves a key, given a description.

Definition at line 257 of file NekFactory.hpp.

258  {
259 #ifdef NEKTAR_USE_THREAD_SAFETY
260  ReadLock vReadLock(m_mutex);
261 #endif
262 
263  for (auto &it : *getMapFactory())
264  {
265  if (it.second.m_desc == pDesc)
266  {
267  return it.first;
268  }
269  }
270  std::string errstr = "Module '" + pDesc + "' is not known.";
271  ASSERTL0(false, errstr);
272  }

References ASSERTL0, and Nektar::LibUtilities::NekFactory< tKey, tBase, tParam >::getMapFactory().

◆ getMapFactory()

template<typename tKey , typename tBase , typename... tParam>
TMapFactory* Nektar::LibUtilities::NekFactory< tKey, tBase, tParam >::getMapFactory ( )
inlineprotected

◆ ModuleExists()

template<typename tKey , typename tBase , typename... tParam>
bool Nektar::LibUtilities::NekFactory< tKey, tBase, tParam >::ModuleExists ( tKey  idKey)
inline

Checks if a particular module is available.

Definition at line 213 of file NekFactory.hpp.

214  {
215 #ifdef NEKTAR_USE_THREAD_SAFETY
216  ReadLock vReadLock(m_mutex);
217 #endif
218 
219  // Now try and find the key in the map.
220  auto it = getMapFactory()->find(idKey);
221 
222  if (it != getMapFactory()->end())
223  {
224  return true;
225  }
226  return false;
227  }

References Nektar::LibUtilities::NekFactory< tKey, tBase, tParam >::getMapFactory().

◆ operator=()

template<typename tKey , typename tBase , typename... tParam>
NekFactory& Nektar::LibUtilities::NekFactory< tKey, tBase, tParam >::operator= ( const NekFactory< tKey, tBase, tParam > &  rhs)
private

◆ PrintAvailableClasses()

template<typename tKey , typename tBase , typename... tParam>
void Nektar::LibUtilities::NekFactory< tKey, tBase, tParam >::PrintAvailableClasses ( std::ostream &  pOut = std::cout)
inline

Prints the available classes to stdout.

Definition at line 232 of file NekFactory.hpp.

233  {
234 #ifdef NEKTAR_USE_THREAD_SAFETY
235  ReadLock vReadLock(m_mutex);
236 #endif
237 
238  pOut << std::endl << "Available classes: " << std::endl;
239  for (auto &it : *getMapFactory())
240  {
241  pOut << " " << it.first;
242  if (it.second.m_desc != "")
243  {
244  pOut << ":" << std::endl
245  << " " << it.second.m_desc << std::endl;
246  }
247  else
248  {
249  pOut << std::endl;
250  }
251  }
252  }

References Nektar::LibUtilities::NekFactory< tKey, tBase, tParam >::getMapFactory().

Referenced by Nektar::LibUtilities::NekFactory< tKey, tBase, tParam >::CreateInstance(), and main().

◆ RegisterCreatorFunction()

template<typename tKey , typename tBase , typename... tParam>
tKey Nektar::LibUtilities::NekFactory< tKey, tBase, tParam >::RegisterCreatorFunction ( tKey  idKey,
CreatorFunction  classCreator,
std::string  pDesc = "" 
)
inline

Register a class with the factory.

This function is called by each class in a static context (prior to the execution of main()) and creates an entry for the class in the factory's map.

Parameters
idKeyKey used to reference the class.
classCreatorFunction to call to create an instance of this class.
pDescOptional description of class.
Returns
The given key idKey.

Definition at line 198 of file NekFactory.hpp.

200  {
201 #ifdef NEKTAR_USE_THREAD_SAFETY
202  WriteLock vWriteLock(m_mutex);
203 #endif
204 
205  ModuleEntry e(classCreator, pDesc);
206  getMapFactory()->insert(std::pair<tKey, ModuleEntry>(idKey, e));
207  return idKey;
208  }
boost::unique_lock< boost::shared_mutex > WriteLock
Definition: Thread.h:379

References Nektar::LibUtilities::NekFactory< tKey, tBase, tParam >::getMapFactory().

Referenced by Module_Register().

Member Data Documentation

◆ mMapFactory

template<typename tKey , typename tBase , typename... tParam>
TMapFactory Nektar::LibUtilities::NekFactory< tKey, tBase, tParam >::mMapFactory
private