PSCF v1.1
Public Member Functions | Protected Member Functions | List of all members
Util::Factory< Data > Class Template Referenceabstract

Factory template. More...

#include <Factory.h>

Public Member Functions

 Factory ()
 Constructor. More...
 
virtual ~Factory ()
 Destructor. More...
 
void addSubfactory (Factory< Data > &subfactory)
 Add a new subfactory to the list. More...
 
virtual Data * factory (const std::string &className) const =0
 Returns a pointer to a new instance of specified subclass. More...
 
Data * readObject (std::istream &in, ParamComposite &parent, std::string &className, bool &isEnd, bool isRequired=true)
 Read a class name, instantiate an object, and read its parameters. More...
 
Data * readObjectOptional (std::istream &in, ParamComposite &parent, std::string &className, bool &isEnd)
 Read an optional class name, instantiate an object, and read its parameters. More...
 
Data * loadObject (Serializable::IArchive &ar, ParamComposite &parent, std::string &className)
 Load a class name, instantiate an object, and load the object. More...
 

Protected Member Functions

Data * trySubfactories (const std::string &className) const
 Search through subfactories for match. More...
 
void setIoCommunicator (MPI::Intracomm &communicator)
 Set associated Mpi communicator. More...
 
bool hasIoCommunicator () const
 Does this factory have a param communicator? More...
 

Detailed Description

template<typename Data>
class Util::Factory< Data >

Factory template.

Definition at line 33 of file Factory.h.

Constructor & Destructor Documentation

◆ Factory()

template<typename Data >
Util::Factory< Data >::Factory

Constructor.

Definition at line 198 of file Factory.h.

◆ ~Factory()

template<typename Data >
Util::Factory< Data >::~Factory
virtual

Destructor.

Definition at line 206 of file Factory.h.

Member Function Documentation

◆ addSubfactory()

template<typename Data >
void Util::Factory< Data >::addSubfactory ( Factory< Data > &  subfactory)

Add a new subfactory to the list.

Parameters
subfactoryNew subfactory to be added

Definition at line 237 of file Factory.h.

◆ factory()

template<typename Data >
virtual Data * Util::Factory< Data >::factory ( const std::string &  className) const
pure virtual

Returns a pointer to a new instance of specified subclass.

This method takes the name className of a subclass of Data as a parameter, and attempts to instantiate an object of that class. If it recognizes the className, it creates an instance of that class and returns a Data* base class pointer to the new object. If it does not recognize the className, it returns a null pointer.

An implementation should first call "trySubfactories(className)" and immediately return if this returns a non-null pointer, before attempting to match the className against specific strings.

Parameters
classNamename of subclass
Returns
base class pointer to new object, or a null pointer.

Implemented in Pscf::Fd1d::IteratorFactory, Pscf::Pspc::IteratorFactory< D >, Pscf::Pspg::IteratorFactory< D >, Pscf::Fd1d::SweepFactory, Pscf::Pspc::SweepFactory< D >, and Pscf::Pspg::SweepFactory< D >.

◆ readObject()

template<typename Data >
Data * Util::Factory< Data >::readObject ( std::istream &  in,
ParamComposite parent,
std::string &  className,
bool &  isEnd,
bool  isRequired = true 
)

Read a class name, instantiate an object, and read its parameters.

This method:

  • reads a comment line of the form className + {
  • invokes the factory method to create an instance of className if className is recognized
  • invokes the readParam() method of the new object
  • throws an Exception if className is not recognized if isRequired is true.
  • returns a null pointer if className is not recognized if isRequired is false.

When compiled with MPI, if the parent ParamComposite has a param communicator, this method reads the comment line on the Io processor, broadcasts it to all others, and then lets each processor independently match this string.

Exceptions
Exceptionif className is not recognized but is required.
Parameters
ininput stream
parentparent ParamComposite object
className(output) name of subclass of Data
isEnd(output) is the input a closing bracket "}" ?
isRequiredis this object required? Default is true.
Returns
pointer to new instance of className

Definition at line 247 of file Factory.h.

References Util::ParamComposite::addParamComposite(), Util::bcast< std::string >(), Util::Label::buffer(), Util::Label::clear(), Util::ParamComponent::echo(), Util::Log::file(), Util::MpiFileIo::hasIoCommunicator(), Util::MpiFileIo::ioCommunicator(), Util::Label::isClear(), Util::Label::read(), Util::ParamComponent::setIndent(), Util::Label::setIsMatched(), UTIL_THROW, and Util::Begin::writeParam().

Referenced by Pscf::Fd1d::System::readParameters().

◆ readObjectOptional()

template<typename Data >
Data * Util::Factory< Data >::readObjectOptional ( std::istream &  in,
ParamComposite parent,
std::string &  className,
bool &  isEnd 
)

Read an optional class name, instantiate an object, and read its parameters.

This method:

  • reads a comment line of the form className + {
  • invokes the factory method to create an instance of className
  • invokes the readParam() method of the new object if className is recognized.
  • returns a null pointer if className is not recognized.

When compiled with MPI, if the parent ParamComposite has a param communicator, this method reads the comment line on the Io processor, broadcasts it to all others, and then lets each processor independently match this string.

Parameters
ininput stream
parentparent ParamComposite object
className(output) name of subclass of Data
isEnd(output) is the input a closing bracket "}" ?
Returns
pointer to new instance of className

Definition at line 378 of file Factory.h.

Referenced by Pscf::Fd1d::System::readParameters().

◆ loadObject()

template<typename Data >
Data * Util::Factory< Data >::loadObject ( Serializable::IArchive ar,
ParamComposite parent,
std::string &  className 
)

Load a class name, instantiate an object, and load the object.

This method:

  • loads a className from an input archive
  • invokes the factory method to create an instance of className
  • invokes the load() method of the new object

When compiled with MPI, if the parent ParamComposite has a param communicator, this method loads the comment line on the Io processor, broadcasts it to all others, and then lets each processor independently match this string.

Exceptions
Exceptionif className is not recognized.
Parameters
arinput/loading archive
parentparent ParamComposite object
className(output) name of subclass of Data
Returns
pointer to new instance of className

Definition at line 387 of file Factory.h.

References Util::bcast< std::string >(), Util::Log::file(), Util::MpiFileIo::hasIoCommunicator(), Util::MpiFileIo::ioCommunicator(), and Util::ParamComposite::loadParamComposite().

◆ trySubfactories()

template<typename Data >
Data * Util::Factory< Data >::trySubfactories ( const std::string &  className) const
protected

Search through subfactories for match.

This method iterates through all registered subfactories, calls the factory(const std::string& ) method of each, and immediately returns a pointer to a new object if any of them returns a non-null pointer. If all of them return a null pointer, this method also returns a null pointer.

Parameters
classNamename of subclass
Returns
base class pointer to new object, or a null pointer.

Definition at line 425 of file Factory.h.

◆ setIoCommunicator()

template<typename Data >
void Util::Factory< Data >::setIoCommunicator ( MPI::Intracomm &  communicator)
protected

Set associated Mpi communicator.

Is not recursive (is not applied to subfactories).

Parameters
communicatorMPI Intra-communicator to use for input

Definition at line 214 of file Factory.h.

References UTIL_THROW.

◆ hasIoCommunicator()

template<typename Data >
bool Util::Factory< Data >::hasIoCommunicator
protected

Does this factory have a param communicator?

Definition at line 229 of file Factory.h.


The documentation for this class was generated from the following file: