Factory< Base, Args > Struct Template Reference

Kynema-SGF API: kynema_sgf::Factory< Base, Args > Struct Template Reference
Kynema-SGF API v0.1.0
CFD solver for wind plant simulations
Loading...
Searching...
No Matches

#include <Factory.H>

Inheritance diagram for kynema_sgf::Factory< Base, Args >:
[legend]

Classes

struct  Register
 Class to handle registration of subclass for runtime selection. More...

Public Member Functions

virtual ~Factory ()=default

Static Public Member Functions

static std::unique_ptr< Basecreate (const std::string &key, Args... args)
static void print (std::ostream &os)

Public Attributes

friend Base

Private Types

using CreatorFunc = std::unique_ptr<Base> (*)(Args...)
using LookupTable = std::unordered_map<std::string, CreatorFunc>

Private Member Functions

 Factory ()=default

Static Private Member Functions

static void key_exists_or_error (const std::string &key)
 Check if the keyword exists in table or print diagnostic message and abort.
static LookupTabletable ()
 Lookup table containing all registered instances.

Detailed Description

template<class Base, class... Args>
struct kynema_sgf::Factory< Base, Args >

Runtime selection of options based on string keyword lookup

Factory provides an automated way to register subclasses that can be instantiated during runtime using string based lookups. The facility mimics the runTimeSelectionTable in OpenFOAM and the current structure is adapted from http://www.nirfriedman.com/2018/04/29/unforgettable-factory/

The usage is described using the following example from code. We would like to support creation of a turbulence model based on user inputs that are read from a file. We will create a base class TurbulenceModel that defines the API for the turbulence model and how it interacts with the rest of the code. Then new models, e.g., Smagorinsky, k-Omega SST/SAS, etc., are added as subclasses to this instance.

// Create a base class that can be used to register and create instances
//
// The template arguments are the classname for CRTP and types of additional
// arguments to be passed to the constructor of the object
class TurbulenceModel : public Factory<TurbulenceModel, const CFDSim&>
{
// Define a base identifier string for debugging purposes
static const std::string base_identifier() { return "TurbulenceModel";
}
// Define API as appropriate here
};
// Define a subclass
// Instead of using the base class we use Base::Register to allow
registration class Smagorinsky : public
TurbulenceModel::Register<Smagorinsky>
{
// This is the keyword that is used to lookup and create this instance
static const std::string identifier() { return "Smagorinsky"; }
// Model implementation here
};
// To create a turbulence model instance
auto sim = CFDSim(mesh);
// tmodel is a std::unique_ptr<TurbulenceModel> instance
auto tmodel = TurbulenceModel::create("Smagorinsky", sim);
Template Parameters
Basebase class (e.g., Physics)
ArgsArguments types to be passed to the constructor during instantiation

Member Typedef Documentation

◆ CreatorFunc

template<class Base, class... Args>
using kynema_sgf::Factory< Base, Args >::CreatorFunc = std::unique_ptr<Base> (*)(Args...)
private

◆ LookupTable

template<class Base, class... Args>
using kynema_sgf::Factory< Base, Args >::LookupTable = std::unordered_map<std::string, CreatorFunc>
private

Constructor & Destructor Documentation

◆ ~Factory()

template<class Base, class... Args>
virtual kynema_sgf::Factory< Base, Args >::~Factory ( )
virtualdefault

◆ Factory()

template<class Base, class... Args>
kynema_sgf::Factory< Base, Args >::Factory ( )
privatedefault

Member Function Documentation

◆ create()

template<class Base, class... Args>
std::unique_ptr< Base > kynema_sgf::Factory< Base, Args >::create ( const std::string & key,
Args... args )
inlinestatic

Create an instance of the concrete subclass based on the runtime keyword

Parameters
keyUnique identifier used to lookup subclass instance
argsArguments to the constructor of the subclass

◆ key_exists_or_error()

template<class Base, class... Args>
void kynema_sgf::Factory< Base, Args >::key_exists_or_error ( const std::string & key)
inlinestaticprivate

Check if the keyword exists in table or print diagnostic message and abort.

◆ print()

template<class Base, class... Args>
void kynema_sgf::Factory< Base, Args >::print ( std::ostream & os)
inlinestatic

Print a list of the valid subclasses registered to this base instance

Parameters
osValid output stream

◆ table()

template<class Base, class... Args>
LookupTable & kynema_sgf::Factory< Base, Args >::table ( )
inlinestaticprivate

Lookup table containing all registered instances.

Member Data Documentation

◆ Base

template<class Base, class... Args>
friend kynema_sgf::Factory< Base, Args >::Base

The documentation for this struct was generated from the following file:
  • /home/runner/work/kynema-sgf/kynema-sgf/src/core/Factory.H