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

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.
tKey RegisterCreatorFunction (tKey idKey, CreatorFunction classCreator, tDescription 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.
tKey GetKey (tDescription pDesc)
 Retrieves a key, given a description.
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)
NekFactoryoperator= (const NekFactory &rhs)

Private Attributes

TMapFactory mMapFactory

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
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 =
::CreateInstance("[derivedclass]",Param1);

Definition at line 112 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 123 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 120 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 116 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 141 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 143 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 118 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 147 of file NekFactory.hpp.

{}
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 158 of file NekFactory.hpp.

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

Referenced by Nektar::SolverUtils::Advection3DHomogeneous1D::Advection3DHomogeneous1D(), Nektar::LibUtilities::SessionReader::CreateComm(), Nektar::AdjointAdvection::DFT(), Nektar::LinearisedAdvection::DFT(), Nektar::SolverUtils::Diffusion3DHomogeneous1D::Diffusion3DHomogeneous1D(), Nektar::MultiRegions::GlobalLinSysIterative::DoConjugateGradient(), Nektar::VortexWaveInteraction::ExecuteRoll(), Nektar::VortexWaveInteraction::ExecuteStreak(), Nektar::VortexWaveInteraction::ExecuteWave(), Nektar::MultiRegions::ExpListHomogeneous1D::ExpListHomogeneous1D(), Nektar::MultiRegions::ExpListHomogeneous2D::ExpListHomogeneous2D(), Nektar::Utilities::InputTec::GenElement2D(), Nektar::Utilities::InputTec::GenElement3D(), Nektar::MultiRegions::ExpList::GenGlobalBndLinSys(), Nektar::MultiRegions::ExpList::GenGlobalLinSys(), Nektar::LibUtilities::Import(), main(), Nektar::LibUtilities::SessionReader::PartitionMesh(), Nektar::Utilities::InputFld::Process(), Nektar::Utilities::OutputInfo::Process(), Nektar::Utilities::Module::ReorderPrisms(), Nektar::PulseWavePropagation::SetPulseWaveBoundaryConditions(), Nektar::Stimulus::Stimulus(), Nektar::MultiRegions::GlobalLinSysIterativeStaticCond::v_AssembleSchurComplement(), Nektar::LinearSWE::v_InitObject(), Nektar::NonlinearSWE::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::UnsteadyInviscidBurger::v_InitObject(), Nektar::SolverUtils::Driver::v_InitObject(), Nektar::UnsteadyAdvection::v_InitObject(), Nektar::IncNavierStokes::v_InitObject(), Nektar::CompressibleFlowSystem::v_InitObject(), Nektar::MultiRegions::GlobalLinSysIterativeStaticCond::v_InitObject(), Nektar::SubSteppingExtrapolate::v_SubSteppingTimeIntegration(), Nektar::VortexWaveInteraction::VortexWaveInteraction(), and Nektar::LibUtilities::Write().

{
// Now try and find the key in the map.
TMapFactoryIterator it = getMapFactory()->find(idKey);
// If successful, check the CreatorFunction is defined and
// create a new instance of the class.
if (it != getMapFactory()->end())
{
if (it->second.m_func)
{
try
{
return it->second.m_func(BOOST_PP_ENUM_PARAMS(MAX_PARAM, x));
}
catch (const std::string& s)
{
std::stringstream errstr;
errstr << "Unable to create module: " << idKey << "\n";
errstr << s;
ASSERTL0(false, errstr.str());
}
}
}
// If we get this far, the key doesn't exist, so throw an error.
std::stringstream errstr;
errstr << "No such module: " << idKey << std::endl;
ASSERTL0(false, errstr.str());
return tBaseSharedPtr();
}
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 281 of file NekFactory.hpp.

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

{
// Now try and find the key in the map.
TMapFactoryIterator it = getMapFactory()->find(idKey);
std::stringstream errstr;
errstr << "No such module: " << idKey << std::endl;
ASSERTL0 (it != getMapFactory()->end(), errstr.str());
return it->second.m_desc;
}
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 261 of file NekFactory.hpp.

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

{
for (it = getMapFactory()->begin(); it != getMapFactory()->end(); ++it)
{
if (it->second.m_desc == pDesc)
{
return it->first;
}
}
std::string errstr = "Module '"
+ boost::lexical_cast<std::string>(pDesc)
+ "' is not known.";
ASSERTL0(false, errstr);
}
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 217 of file NekFactory.hpp.

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

Referenced by main(), and Nektar::LibUtilities::SessionReader::PartitionMesh().

{
// Now try and find the key in the map.
TMapFactoryIterator it = getMapFactory()->find(idKey);
if (it != getMapFactory()->end())
{
return true;
}
return false;
}
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 233 of file NekFactory.hpp.

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

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

{
pOut << std::endl << "Available classes: " << std::endl;
for (it = getMapFactory()->begin(); it != getMapFactory()->end(); ++it)
{
pOut << std::endl << "Available classes: " << std::endl;
for (it = getMapFactory()->begin(); it != getMapFactory()->end(); ++it)
{
pOut << " " << it->first;
if (it->second.m_desc != "")
{
pOut << ":" << std::endl << " "
<< it->second.m_desc << std::endl;
}
else
{
pOut << std::endl;
}
}
}
}
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 205 of file NekFactory.hpp.

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

{
ModuleEntry e(classCreator, pDesc);
getMapFactory()->insert(std::pair<tKey,ModuleEntry>(idKey, e));
return idKey;
}

Member Data Documentation

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