35#ifndef NEKTAR_LIBUTILITIES_BASICUTILS_NEKFACTORY
36#define NEKTAR_LIBUTILITIES_BASICUTILS_NEKFACTORY
43#include <unordered_map>
45#ifdef NEKTAR_USE_THREAD_SAFETY
47#include <shared_mutex>
57#ifdef NEKTAR_USE_THREAD_SAFETY
59typedef std::unique_lock<std::shared_mutex>
WriteLock;
60typedef std::shared_lock<std::shared_mutex>
ReadLock;
101template <
typename tKey,
128 typedef std::unordered_map<tKey, ModuleEntry, HashOp>
tMapFactory;
144#ifdef NEKTAR_USE_THREAD_SAFETY
145 ReadLock vReadLock(m_mutex);
156#ifdef NEKTAR_USE_THREAD_SAFETY
164 return tmp->
m_func(args...);
166 catch (
const std::string &s)
168 std::stringstream errstr;
169 errstr <<
"Unable to create module: " << idKey <<
"\n";
177 std::stringstream errstr;
178 errstr <<
"No such module: " << idKey << std::endl;
197 std::string pDesc =
"")
199#ifdef NEKTAR_USE_THREAD_SAFETY
200 WriteLock vWriteLock(m_mutex);
204 getMapFactory()->insert(std::pair<tKey, ModuleEntry>(idKey, e));
213#ifdef NEKTAR_USE_THREAD_SAFETY
214 ReadLock vReadLock(m_mutex);
232#ifdef NEKTAR_USE_THREAD_SAFETY
233 ReadLock vReadLock(m_mutex);
236 pOut << std::endl <<
"Available classes: " << std::endl;
239 pOut <<
" " << it.first;
240 if (it.second.m_desc !=
"")
242 pOut <<
":" << std::endl
243 <<
" " << it.second.m_desc << std::endl;
257#ifdef NEKTAR_USE_THREAD_SAFETY
258 ReadLock vReadLock(m_mutex);
264 std::stringstream errstr;
265 errstr <<
"No such module: " << idKey << std::endl;
267 return it->second.m_desc;
286#ifdef NEKTAR_USE_THREAD_SAFETY
287 std::shared_mutex m_mutex;
#define ASSERTL0(condition, msg)
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mode...
Provides a generic Factory class.
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
NekFactory(const NekFactory &rhs)=delete
std::unordered_map< tKey, ModuleEntry, HashOp > tMapFactory
Factory map between key and module data.
std::shared_ptr< tBase > tBaseSharedPtr
Shared pointer to an object of baseclass type.
NekFactory & operator=(const NekFactory &rhs)=delete
tBaseSharedPtr CreateInstance(tKey idKey, tParam... args)
Create an instance of the class referred to by idKey.
tMapFactory * getMapFactory()
Ensure the factory's map is created.
void PrintAvailableClasses(std::ostream &pOut=std::cout)
Prints the available classes to stdout.
std::function< tBaseSharedPtr(tParam...)> CreatorFunction
CreatorFunction type which takes parameter and returns base class shared pointer.
bool ModuleExists(tKey idKey)
Checks if a particular module is available.
std::string GetClassDescription(tKey idKey)
Returns the description of a class.
std::shared_lock< std::shared_mutex > ReadLock
std::unique_lock< std::shared_mutex > WriteLock
Define a struct to hold the information about a module.
ModuleEntry(CreatorFunction pFunc, const std::string pDesc)
std::string m_desc
Description of class for use in listing available classes.
CreatorFunction m_func
Function used to create instance of class.