Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Classes | Public Types | Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
Nektar::LibUtilities::NekFactory< tKey, tBase, > Class Template Reference

Provides a generic Factory class. More...

#include <NekFactory.hpp>

Collaboration diagram for Nektar::LibUtilities::NekFactory< tKey, tBase, >:
Collaboration graph
[legend]

Classes

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

Public Types

typedef std::string tDescription
 Description datatype. More...
 
typedef std::less< tKey > tPredicator
 Comparison predicator of key. More...
 
typedef boost::shared_ptr< tBase > tBaseSharedPtr
 Shared pointer to an object of baseclass type. More...
 
typedef tBaseSharedPtr(* CreatorFunction )(BOOST_PP_ENUM_PARAMS(MAX_PARAM, tParam))
 CreatorFunction type which takes parameter and returns base class shared pointer. More...
 
typedef std::map< tKey,
ModuleEntry, tPredicator
TMapFactory
 Factory map between key and module data. More...
 
typedef TMapFactory::iterator TMapFactoryIterator
 Iterator for factory map. More...
 

Public Member Functions

 NekFactory ()
 
tBaseSharedPtr CreateInstance (tKey idKey BOOST_PP_COMMA_IF(MAX_PARAM) BOOST_PP_ENUM_BINARY_PARAMS(MAX_PARAM, tParam, x))
 Create an instance of the class referred to by idKey. More...
 
tKey RegisterCreatorFunction (tKey idKey, CreatorFunction classCreator, tDescription 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 (tDescription 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
 
boost::shared_mutex m_mutex
 

Detailed Description

template<typename tKey, typename tBase, BOOST_PP_ENUM(MAX_PARAM, FACTORY_print, typename tParam)>
class Nektar::LibUtilities::NekFactory< tKey, tBase, >

Provides a generic Factory class.

Implements a generic object factory. Class-types which use a potentially arbitrary number of parameters may be used with specialised forms of the NekFactory. An upper limit on the number of parameters is imposed by the MAX_PARAM preprocessor definition in the NekFactory.hpp file. The specialisations are generated at compile type using Boost preprocessor by through repeated inclusion of the NekFactory.hpp file.

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 116 of file NekFactory.hpp.

Member Typedef Documentation

template<typename tKey , typename tBase , BOOST_PP_ENUM(MAX_PARAM, FACTORY_print, typename tParam) >
typedef tBaseSharedPtr(* Nektar::LibUtilities::NekFactory< tKey, tBase, >::CreatorFunction)(BOOST_PP_ENUM_PARAMS(MAX_PARAM, tParam))

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

Definition at line 127 of file NekFactory.hpp.

template<typename tKey , typename tBase , BOOST_PP_ENUM(MAX_PARAM, FACTORY_print, typename tParam) >
typedef boost::shared_ptr<tBase> Nektar::LibUtilities::NekFactory< tKey, tBase, >::tBaseSharedPtr

Shared pointer to an object of baseclass type.

Definition at line 124 of file NekFactory.hpp.

template<typename tKey , typename tBase , BOOST_PP_ENUM(MAX_PARAM, FACTORY_print, typename tParam) >
typedef std::string Nektar::LibUtilities::NekFactory< tKey, tBase, >::tDescription

Description datatype.

Definition at line 120 of file NekFactory.hpp.

template<typename tKey , typename tBase , BOOST_PP_ENUM(MAX_PARAM, FACTORY_print, typename tParam) >
typedef std::map<tKey, ModuleEntry, tPredicator> Nektar::LibUtilities::NekFactory< tKey, tBase, >::TMapFactory

Factory map between key and module data.

Definition at line 145 of file NekFactory.hpp.

template<typename tKey , typename tBase , BOOST_PP_ENUM(MAX_PARAM, FACTORY_print, typename tParam) >
typedef TMapFactory::iterator Nektar::LibUtilities::NekFactory< tKey, tBase, >::TMapFactoryIterator

Iterator for factory map.

Definition at line 147 of file NekFactory.hpp.

template<typename tKey , typename tBase , BOOST_PP_ENUM(MAX_PARAM, FACTORY_print, typename tParam) >
typedef std::less<tKey> Nektar::LibUtilities::NekFactory< tKey, tBase, >::tPredicator

Comparison predicator of key.

Definition at line 122 of file NekFactory.hpp.

Constructor & Destructor Documentation

template<typename tKey , typename tBase , BOOST_PP_ENUM(MAX_PARAM, FACTORY_print, typename tParam) >
Nektar::LibUtilities::NekFactory< tKey, tBase, >::NekFactory ( )
inline

Definition at line 151 of file NekFactory.hpp.

151 : m_mutex() {}
template<typename tKey , typename tBase , BOOST_PP_ENUM(MAX_PARAM, FACTORY_print, typename tParam) >
Nektar::LibUtilities::NekFactory< tKey, tBase, >::NekFactory ( const NekFactory< tKey, tBase, > &  rhs)
private

Member Function Documentation

template<typename tKey , typename tBase , BOOST_PP_ENUM(MAX_PARAM, FACTORY_print, typename tParam) >
tBaseSharedPtr Nektar::LibUtilities::NekFactory< tKey, tBase, >::CreateInstance ( tKey idKey   BOOST_PP_COMMA_IFMAX_PARAM) BOOST_PP_ENUM_BINARY_PARAMS(MAX_PARAM, tParam, x)
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 162 of file NekFactory.hpp.

References ASSERTL0, Nektar::LibUtilities::NekFactory< tKey, tBase, >::getMapFactory(), Nektar::LibUtilities::NekFactory< tKey, tBase, >::ModuleEntry::m_func, Nektar::LibUtilities::NekFactory< tKey, tBase, >::m_mutex, MAX_PARAM, and Nektar::LibUtilities::NekFactory< tKey, tBase, >::PrintAvailableClasses().

Referenced by Nektar::SolverUtils::Advection3DHomogeneous1D::Advection3DHomogeneous1D(), Nektar::NekMeshUtils::FaceMesh::BuildLocalMesh(), Nektar::LibUtilities::SessionReader::CreateComm(), Nektar::Thread::ThreadMaster::CreateInstance(), Nektar::MultiRegions::GlobalLinSys::CreatePrecon(), Nektar::LinearisedAdvection::DFT(), Nektar::AdjointAdvection::DFT(), Nektar::NekMeshUtils::FaceMesh::DiagonalSwap(), Nektar::SolverUtils::Diffusion3DHomogeneous1D::Diffusion3DHomogeneous1D(), Nektar::VortexWaveInteraction::ExecuteRoll(), Nektar::VortexWaveInteraction::ExecuteStreak(), Nektar::VortexWaveInteraction::ExecuteWave(), Nektar::MultiRegions::ExpListHomogeneous1D::ExpListHomogeneous1D(), Nektar::MultiRegions::ExpListHomogeneous2D::ExpListHomogeneous2D(), Nektar::Utilities::InputTec::GenElement2D(), Nektar::Utilities::InputStar::GenElement2D(), Nektar::Utilities::InputTec::GenElement3D(), Nektar::Utilities::InputStar::GenElement3D(), Nektar::MultiRegions::ExpList::GenGlobalBndLinSys(), Nektar::MultiRegions::ExpList::GenGlobalLinSys(), Nektar::NekMeshUtils::Octree::GetOctreeMesh(), Nektar::LibUtilities::Import(), Nektar::ForcingMovingBody::InitialiseCableModel(), Nektar::Utilities::InputSem::insertEdge(), Nektar::GlobalMapping::Mapping::Load(), main(), Nektar::NekMeshUtils::BLMesh::Mesh(), Nektar::NekMeshUtils::TetMesh::Mesh(), Nektar::LibUtilities::SessionReader::PartitionMesh(), Nektar::Utilities::InputCAD::Process(), Nektar::Utilities::InputSem::Process(), Nektar::Utilities::InputFld::Process(), Nektar::Utilities::InputGmsh::Process(), Nektar::Utilities::InputNekpp::Process(), Nektar::Utilities::InputPts::Process(), Nektar::Utilities::OutputInfo::Process(), Nektar::Utilities::ProcessOptiExtract::Process(), Nektar::Utilities::InputSwan::Process(), Nektar::Utilities::InputVtk::Process(), Nektar::Utilities::ProcessSpherigon::Process(), Nektar::Utilities::InputNek::Process(), Nektar::Utilities::ProcessExtractSurf::Process(), Nektar::Utilities::ProcessExtractTetPrismInterface::Process(), Nektar::Utilities::ProcessBL::Process(), Nektar::Utilities::ProcessTetSplit::Process(), Nektar::Utilities::ProcessDetectSurf::Process(), Nektar::Utilities::InputPly::ReadPly(), Nektar::Utilities::Module::ReorderPrisms(), Nektar::PulseWavePropagation::SetPulseWaveBoundaryConditions(), Nektar::UnsteadyAdvectionDiffusion::SetUpSubSteppingTimeIntegration(), Nektar::Stimulus::Stimulus(), Nektar::VelocityCorrectionScheme::v_InitObject(), Nektar::VCSMapping::v_InitObject(), Nektar::LinearSWE::v_InitObject(), Nektar::NonlinearSWE::v_InitObject(), Nektar::BidomainRoth::v_InitObject(), Nektar::Bidomain::v_InitObject(), Nektar::Monodomain::v_InitObject(), Nektar::MultiRegions::PreconditionerLinearWithBlock::v_InitObject(), Nektar::PulseWavePropagation::v_InitObject(), Nektar::MultiRegions::PreconditionerLinearWithLowEnergy::v_InitObject(), Nektar::MultiRegions::PreconditionerLinearWithDiag::v_InitObject(), Nektar::APE::v_InitObject(), Nektar::NonlinearPeregrine::v_InitObject(), Nektar::UnsteadyInviscidBurger::v_InitObject(), Nektar::UnsteadyAdvection::v_InitObject(), Nektar::SolverUtils::Driver::v_InitObject(), Nektar::IncNavierStokes::v_InitObject(), Nektar::CompressibleFlowSystem::v_InitObject(), Nektar::CoupledLinearNS::v_InitObject(), Nektar::SubSteppingExtrapolate::v_SubSteppingTimeIntegration(), Nektar::VortexWaveInteraction::VortexWaveInteraction(), and Nektar::LibUtilities::Write().

164  {
165 
166  ReadLock vReadLock(m_mutex);
167 
168  // Now try and find the key in the map.
169  TMapFactoryIterator it = getMapFactory()->find(idKey);
170 
171  // If successful, check the CreatorFunction is defined and
172  // create a new instance of the class.
173  if (it != getMapFactory()->end())
174  {
175  ModuleEntry *tmp = &(it->second);
176  vReadLock.unlock();
177 
178  if (tmp->m_func)
179  {
180  try
181  {
182  return tmp->m_func(BOOST_PP_ENUM_PARAMS(MAX_PARAM, x));
183  }
184  catch (const std::string& s)
185  {
186  std::stringstream errstr;
187  errstr << "Unable to create module: " << idKey << "\n";
188  errstr << s;
189  ASSERTL0(false, errstr.str());
190  }
191  }
192  }
193 
194  // If we get this far, the key doesn't exist, so throw an error.
195  std::stringstream errstr;
196  errstr << "No such module: " << idKey << std::endl;
197  PrintAvailableClasses(errstr);
198  ASSERTL0(false, errstr.str());
199  return tBaseSharedPtr();
200  }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:161
void PrintAvailableClasses(std::ostream &pOut=std::cout)
Prints the available classes to stdout.
Definition: NekFactory.hpp:247
TMapFactory * getMapFactory()
Ensure the factory's map is created.
Definition: NekFactory.hpp:317
#define MAX_PARAM
Definition: NekFactory.hpp:58
boost::shared_ptr< tBase > tBaseSharedPtr
Shared pointer to an object of baseclass type.
Definition: NekFactory.hpp:124
TMapFactory::iterator TMapFactoryIterator
Iterator for factory map.
Definition: NekFactory.hpp:147
boost::shared_lock< boost::shared_mutex > ReadLock
Definition: NekFactory.hpp:71
template<typename tKey , typename tBase , BOOST_PP_ENUM(MAX_PARAM, FACTORY_print, typename tParam) >
std::string Nektar::LibUtilities::NekFactory< tKey, tBase, >::GetClassDescription ( tKey  idKey)
inline

Returns the description of a class.

Definition at line 299 of file NekFactory.hpp.

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

300  {
301  ReadLock vReadLock(m_mutex);
302 
303  // Now try and find the key in the map.
304  TMapFactoryIterator it = getMapFactory()->find(idKey);
305 
306  std::stringstream errstr;
307  errstr << "No such module: " << idKey << std::endl;
308  ASSERTL0 (it != getMapFactory()->end(), errstr.str());
309  return it->second.m_desc;
310  }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:161
TMapFactory * getMapFactory()
Ensure the factory's map is created.
Definition: NekFactory.hpp:317
TMapFactory::iterator TMapFactoryIterator
Iterator for factory map.
Definition: NekFactory.hpp:147
boost::shared_lock< boost::shared_mutex > ReadLock
Definition: NekFactory.hpp:71
template<typename tKey , typename tBase , BOOST_PP_ENUM(MAX_PARAM, FACTORY_print, typename tParam) >
tKey Nektar::LibUtilities::NekFactory< tKey, tBase, >::GetKey ( tDescription  pDesc)
inline

Retrieves a key, given a description.

Definition at line 277 of file NekFactory.hpp.

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

278  {
279  ReadLock vReadLock(m_mutex);
280 
282  for (it = getMapFactory()->begin(); it != getMapFactory()->end(); ++it)
283  {
284  if (it->second.m_desc == pDesc)
285  {
286  return it->first;
287  }
288  }
289  std::string errstr = "Module '"
290  + boost::lexical_cast<std::string>(pDesc)
291  + "' is not known.";
292  ASSERTL0(false, errstr);
293  }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:161
TMapFactory * getMapFactory()
Ensure the factory's map is created.
Definition: NekFactory.hpp:317
TMapFactory::iterator TMapFactoryIterator
Iterator for factory map.
Definition: NekFactory.hpp:147
boost::shared_lock< boost::shared_mutex > ReadLock
Definition: NekFactory.hpp:71
template<typename tKey , typename tBase , BOOST_PP_ENUM(MAX_PARAM, FACTORY_print, typename tParam) >
TMapFactory* Nektar::LibUtilities::NekFactory< tKey, tBase, >::getMapFactory ( )
inlineprotected
template<typename tKey , typename tBase , BOOST_PP_ENUM(MAX_PARAM, FACTORY_print, typename tParam) >
bool Nektar::LibUtilities::NekFactory< tKey, tBase, >::ModuleExists ( tKey  idKey)
inline

Checks if a particular module is available.

Definition at line 229 of file NekFactory.hpp.

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

Referenced by main().

230  {
231  ReadLock vReadLock(m_mutex);
232 
233  // Now try and find the key in the map.
234  TMapFactoryIterator it = getMapFactory()->find(idKey);
235 
236  if (it != getMapFactory()->end())
237  {
238  return true;
239  }
240  return false;
241  }
TMapFactory * getMapFactory()
Ensure the factory's map is created.
Definition: NekFactory.hpp:317
TMapFactory::iterator TMapFactoryIterator
Iterator for factory map.
Definition: NekFactory.hpp:147
boost::shared_lock< boost::shared_mutex > ReadLock
Definition: NekFactory.hpp:71
template<typename tKey , typename tBase , BOOST_PP_ENUM(MAX_PARAM, FACTORY_print, typename tParam) >
NekFactory& Nektar::LibUtilities::NekFactory< tKey, tBase, >::operator= ( const NekFactory< tKey, tBase, > &  rhs)
private
template<typename tKey , typename tBase , BOOST_PP_ENUM(MAX_PARAM, FACTORY_print, typename tParam) >
void Nektar::LibUtilities::NekFactory< tKey, tBase, >::PrintAvailableClasses ( std::ostream &  pOut = std::cout)
inline

Prints the available classes to stdout.

Definition at line 247 of file NekFactory.hpp.

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

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

248  {
249  ReadLock vReadLock(m_mutex);
250 
251  pOut << std::endl << "Available classes: " << std::endl;
253  for (it = getMapFactory()->begin(); it != getMapFactory()->end(); ++it)
254  {
255  pOut << std::endl << "Available classes: " << std::endl;
257  for (it = getMapFactory()->begin(); it != getMapFactory()->end(); ++it)
258  {
259  pOut << " " << it->first;
260  if (it->second.m_desc != "")
261  {
262  pOut << ":" << std::endl << " "
263  << it->second.m_desc << std::endl;
264  }
265  else
266  {
267  pOut << std::endl;
268  }
269  }
270  }
271  }
TMapFactory * getMapFactory()
Ensure the factory's map is created.
Definition: NekFactory.hpp:317
TMapFactory::iterator TMapFactoryIterator
Iterator for factory map.
Definition: NekFactory.hpp:147
boost::shared_lock< boost::shared_mutex > ReadLock
Definition: NekFactory.hpp:71
template<typename tKey , typename tBase , BOOST_PP_ENUM(MAX_PARAM, FACTORY_print, typename tParam) >
tKey Nektar::LibUtilities::NekFactory< tKey, tBase, >::RegisterCreatorFunction ( tKey  idKey,
CreatorFunction  classCreator,
tDescription  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 215 of file NekFactory.hpp.

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

217  {
218  WriteLock vWriteLock(m_mutex);
219 
220  ModuleEntry e(classCreator, pDesc);
221  getMapFactory()->insert(std::pair<tKey,ModuleEntry>(idKey, e));
222  return idKey;
223  }
boost::unique_lock< boost::shared_mutex > WriteLock
Definition: NekFactory.hpp:70
TMapFactory * getMapFactory()
Ensure the factory's map is created.
Definition: NekFactory.hpp:317

Member Data Documentation

template<typename tKey , typename tBase , BOOST_PP_ENUM(MAX_PARAM, FACTORY_print, typename tParam) >
boost::shared_mutex Nektar::LibUtilities::NekFactory< tKey, tBase, >::m_mutex
private
template<typename tKey , typename tBase , BOOST_PP_ENUM(MAX_PARAM, FACTORY_print, typename tParam) >
TMapFactory Nektar::LibUtilities::NekFactory< tKey, tBase, >::mMapFactory
private