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::NekFactoryBase< tKey, tBase, tBasePtr, tParam > Class Template Reference

#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< tBasePtr(tParam...)> CreatorFunction
 CreatorFunction type which takes parameter and returns base class shared pointer.
 
typedef std::unordered_map< tKey, ModuleEntry, HashOp > tMapFactory
 Factory map between key and module data.
 

Public Member Functions

 NekFactoryBase ()=default
 
tBasePtr 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

tMapFactory * getMapFactory ()
 Ensure the factory's map is created.
 

Private Member Functions

 NekFactoryBase (const NekFactoryBase &rhs)=delete
 
NekFactoryBase & operator= (const NekFactoryBase &rhs)=delete
 

Private Attributes

tMapFactory m_mapFactory
 

Detailed Description

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

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

Member Typedef Documentation

◆ CreatorFunction

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

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

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

◆ tBaseSharedPtr

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

Shared pointer to an object of baseclass type.

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

◆ tMapFactory

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

Factory map between key and module data.

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

Constructor & Destructor Documentation

◆ NekFactoryBase() [1/2]

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

◆ NekFactoryBase() [2/2]

template<typename tKey , typename tBase , typename tBasePtr , typename... tParam>
Nektar::LibUtilities::NekFactoryBase< tKey, tBase, tBasePtr, tParam >::NekFactoryBase ( const NekFactoryBase< tKey, tBase, tBasePtr, tParam > &  rhs)
privatedelete

Member Function Documentation

◆ CreateInstance()

template<typename tKey , typename tBase , typename tBasePtr , typename... tParam>
tBasePtr Nektar::LibUtilities::NekFactoryBase< tKey, tBase, tBasePtr, 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 143 of file BasicUtils/NekFactory.hpp.

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

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

Referenced by Nektar::NekMesh::CADSystemCFI::AddCurve(), Nektar::SpatialDomains::CADSystemOCE::AddCurve(), Nektar::NekMesh::CADSystemCFI::AddSurf(), Nektar::SpatialDomains::CADSystemOCE::AddSurf(), Nektar::NekMesh::CADSystemCFI::AddVert(), Nektar::SpatialDomains::CADSystemOCE::AddVert(), Nektar::SolverUtils::Advection3DHomogeneous1D::Advection3DHomogeneous1D(), Nektar::ArtificialDiffusion::ArtificialDiffusion(), BOOST_AUTO_TEST_CASE(), Nektar::ForcingMovingBodyTests::BOOST_AUTO_TEST_CASE(), Nektar::CompressibleSolver::CompressibleSolver(), TestComm::Configure(), Nektar::BndCondPressureOutflowCFEOp< TData >::Create(), Nektar::BndCondWallCFEOp< TData >::Create(), Nektar::CFLVelocityCFEOp< TData >::Create(), Nektar::MultiRegions::Operator< TData >::Create(), Nektar::MultiRegions::BlockOperator< TData >::Create(), Nektar::MultiRegions::ElmtBlockOp< TFieldIn, TFieldOut, TData >::Create(), 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::NekLinSysIterEigenvalueUnitTests::CreateTestSession(), Nektar::LinearisedAdvection::DFT(), Nektar::SolverUtils::FileFieldInterpolator::DFT(), Nektar::SolverUtils::Diffusion3DHomogeneous1D::Diffusion3DHomogeneous1D(), EquationSystem_Create(), Nektar::VortexWaveInteraction::ExecuteRoll(), Nektar::VortexWaveInteraction::ExecuteStreak(), TestAdvectionWeakDG< TData >::ExpectedSolution(), TestDiffusionIP< TData >::ExpectedSolution(), TestPreconOps< TData >::ExpectedSolution(), Field_Init(), Nektar::FieldUtils::Field::FieldIOForFile(), Filter_Create(), Nektar::SolverUtils::FilterPython::FilterPython(), Nektar::LibUtilities::Import(), Nektar::CompressibleFlowSystem::InitAdvection(), Nektar::MultiRegions::GlobalLinSysIterativeFull::Initialise(), Nektar::Collections::Collection::Initialise(), Nektar::ForcingMovingBody::InitialiseCableModel(), Nektar::CFSImplicit::InitialiseNonlinSysSolver(), Nektar::ShallowWaterSystem::InitialiseNonlinSysSolver(), Nektar::IncBoundaryConditions::Initialize(), Nektar::PerforatedPlate::InitObject(), Nektar::NavierStokesCFE::InitObject_Explicit(), Nektar::GlobalMapping::Mapping::Load(), Nektar::SolverUtils::Forcing::Load(), Nektar::Stimulus::LoadStimuli(), main(), 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::GlobalLinSysIterativeStaticCond::v_SolveLinearSystem(), Nektar::SubSteppingExtrapolate::v_SubSteppingTimeIntegration(), Nektar::SpatialDomains::MeshGraphIOHDF5::v_WriteGeometry(), Nektar::VariableConverter::VariableConverter(), Nektar::VortexWaveInteraction::VortexWaveInteraction(), Nektar::LibUtilities::Write(), Nektar::FieldUtils::OutputFileBase::WriteFile(), and Nektar::IterativeElasticSystem::WriteGeometry().

◆ GetClassDescription()

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

Returns the description of a class.

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

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

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

◆ getMapFactory()

template<typename tKey , typename tBase , typename tBasePtr , typename... tParam>
tMapFactory * Nektar::LibUtilities::NekFactoryBase< tKey, tBase, tBasePtr, tParam >::getMapFactory ( )
inlineprotected

◆ ModuleExists()

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

◆ operator=()

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

◆ PrintAvailableClasses()

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

Prints the available classes to stdout.

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

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

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

Referenced by Nektar::BndCondPressureOutflowCFEOp< TData >::Create(), Nektar::BndCondWallCFEOp< TData >::Create(), Nektar::CFLVelocityCFEOp< TData >::Create(), Nektar::MultiRegions::Operator< TData >::Create(), Nektar::MultiRegions::BlockOperator< TData >::Create(), Nektar::MultiRegions::ElmtBlockOp< TFieldIn, TFieldOut, TData >::Create(), Nektar::LibUtilities::NekFactoryBase< tKey, tBase, tBasePtr, tParam >::CreateInstance(), and main().

◆ RegisterCreatorFunction()

template<typename tKey , typename tBase , typename tBasePtr , typename... tParam>
tKey Nektar::LibUtilities::NekFactoryBase< tKey, tBase, tBasePtr, 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 197 of file BasicUtils/NekFactory.hpp.

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

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

Member Data Documentation

◆ m_mapFactory

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