Nektar++
Loading...
Searching...
No Matches
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::shared_ptr< tBase > tBaseSharedPtr
 Shared pointer to an object of baseclass type.
 
typedef std::function< tBaseSharedPtr(tParam...)> CreatorFunction
 CreatorFunction type which takes parameter and returns base class shared pointer.
 
typedef std::unordered_map< tKey, ModuleEntry, HashOptMapFactory
 Factory map between key and module data.
 

Public Member Functions

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

Protected Member Functions

tMapFactorygetMapFactory ()
 Ensure the factory's map is created.
 

Private Member Functions

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

Private Attributes

tMapFactory m_mapFactory
 

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;
@ P
Monomial polynomials .
Definition BasisType.h:62

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 BasicUtils/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 111 of file BasicUtils/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 108 of file BasicUtils/NekFactory.hpp.

◆ tMapFactory

template<typename tKey , typename tBase , typename... tParam>
typedef std::unordered_map<tKey, ModuleEntry, HashOp> Nektar::LibUtilities::NekFactory< tKey, tBase, tParam >::tMapFactory

Factory map between key and module data.

Definition at line 128 of file BasicUtils/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)
privatedelete

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.
argsParameter to pass to class constructor.
Returns
Base class pointer to new instance.

Definition at line 142 of file BasicUtils/NekFactory.hpp.

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

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::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::FileFieldInterpolator::DFT(), Nektar::SolverUtils::Diffusion3DHomogeneous1D::Diffusion3DHomogeneous1D(), EquationSystem_Create(), Nektar::VortexWaveInteraction::ExecuteRoll(), Nektar::VortexWaveInteraction::ExecuteStreak(), Field_Init(), Nektar::FieldUtils::Field::FieldIOForFile(), Filter_Create(), Nektar::SolverUtils::FilterPython::FilterPython(), Nektar::LibUtilities::Import(), Nektar::CompressibleFlowSystem::InitAdvection(), Nektar::Collections::Collection::Initialise(), Nektar::ForcingMovingBody::InitialiseCableModel(), Nektar::CFSImplicit::InitialiseNonlinSysSolver(), Nektar::ShallowWaterSystem::InitialiseNonlinSysSolver(), Nektar::IncBoundaryConditions::Initialize(), Nektar::NavierStokesCFE::InitObject_Explicit(), Nektar::GlobalMapping::Mapping::Load(), main(), MeshGraphIO_Create(), Module_Create(), Nektar::LibUtilities::NekNonlinSysIter::NekNonlinSysIter(), Nektar::SpatialDomains::MeshGraphIO::Read(), Nektar::SpatialDomains::MeshPartition::ReadExpansions(), Nektar::UpwindPulseSolver::RiemannSolverUpwind(), SessionReader_CreateInstance(), Nektar::SolverUtils::DriverParallelInTime::SetParallelInTimeEquationSystem(), 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::EigenValuesAdvection::v_InitObject(), Nektar::MMFAdvection::v_InitObject(), Nektar::UnsteadyAdvection::v_InitObject(), Nektar::UnsteadyAdvectionDiffusion::v_InitObject(), Nektar::UnsteadyDiffusion::v_InitObject(), Nektar::UnsteadyInviscidBurgers::v_InitObject(), Nektar::UnsteadyViscousBurgers::v_InitObject(), Nektar::CompressibleFlowSystem::v_InitObject(), Nektar::LinearSWE::v_InitObject(), Nektar::NonlinearSWE::v_InitObject(), Nektar::DiffusionLDGNS::v_InitObject(), Nektar::SolverUtils::Driver::v_InitObject(), Nektar::FieldUtils::OutputFld::v_OutputFromData(), Nektar::FieldUtils::OutputFld::v_OutputFromExp(), Nektar::SpatialDomains::MeshGraphIOHDF5::v_PartitionMesh(), Nektar::SpatialDomains::MeshGraphIOXml::v_PartitionMesh(), Nektar::FieldUtils::OutputXml::v_Process(), Nektar::FieldUtils::ProcessPointDataToFld::v_Process(), Nektar::MultiRegions::GlobalLinSysIterativeFull::v_SolveLinearSystem(), Nektar::MultiRegions::GlobalLinSysIterativeStaticCond::v_SolveLinearSystem(), Nektar::SubSteppingExtrapolate::v_SubSteppingTimeIntegration(), Nektar::VariableConverter::VariableConverter(), Nektar::VortexWaveInteraction::VortexWaveInteraction(), Nektar::LibUtilities::Write(), Nektar::FieldUtils::OutputFileBase::WriteFile(), and Nektar::IterativeElasticSystem::WriteGeometry().

◆ 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 255 of file BasicUtils/NekFactory.hpp.

256 {
257#ifdef NEKTAR_USE_THREAD_SAFETY
258 ReadLock vReadLock(m_mutex);
259#endif
260
261 // Now try and find the key in the map.
262 auto it = getMapFactory()->find(idKey);
263
264 std::stringstream errstr;
265 errstr << "No such module: " << idKey << std::endl;
266 ASSERTL0(it != getMapFactory()->end(), errstr.str());
267 return it->second.m_desc;
268 }
#define ASSERTL0(condition, msg)

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 211 of file BasicUtils/NekFactory.hpp.

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

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

Referenced by Nektar::SpatialDomains::MeshGraphIOHDF5::v_PartitionMesh(), and Nektar::SpatialDomains::MeshGraphIOXml::v_PartitionMesh().

◆ operator=()

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

◆ 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 230 of file BasicUtils/NekFactory.hpp.

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

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 196 of file BasicUtils/NekFactory.hpp.

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

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

Member Data Documentation

◆ m_mapFactory

template<typename tKey , typename tBase , typename... tParam>
tMapFactory Nektar::LibUtilities::NekFactory< tKey, tBase, tParam >::m_mapFactory
private