Dakota  Version 7.0
Explore and Predict with Confidence
Public Types | Public Member Functions | Private Attributes | List of all members
GenericFactory< Key, CreateFun, ErrorPolicy > Class Template Reference
Inheritance diagram for GenericFactory< Key, CreateFun, ErrorPolicy >:
DefaultGenericFactoryErrorPolicy< Key, CreateFun >

Public Types

using return_type = typename FunctionInfo< CreateFun >::return_type
 
- Public Types inherited from DefaultGenericFactoryErrorPolicy< Key, CreateFun >
using return_type = typename FunctionInfo< CreateFun >::return_type
 

Public Member Functions

template<typename Fun >
bool register_key (const Key &key, Fun &&fun)
 
bool key_is_registered (const Key &key) const noexcept
 
template<typename... Args>
auto create (const Key &key, Args &&... args)
 
- Public Member Functions inherited from DefaultGenericFactoryErrorPolicy< Key, CreateFun >
template<typename... CreateArgs>
return_type on_unknown_key (const Key &, CreateArgs &&...) const
 

Private Attributes

std::unordered_map< Key, std::function< CreateFun > > m_registrar
 

Detailed Description

template<typename Key, typename CreateFun, typename ErrorPolicy = DefaultGenericFactoryErrorPolicy<Key, CreateFun>>
class Dakota::Util::GenericFactory< Key, CreateFun, ErrorPolicy >

A class for registering factory functions and calling them by providing a key. Factory functions are added to the registry via register_key. Providing a registered key to create will return the result of invoking the factory function registered with the key.

Precondition
ErrorPolicy::on_unknown_key must have the same return type as CreateFun
Template Parameters
Keythe lookup key; any hashable type is allowed
CreateFunthe type of function for creating factory items
ErrorPolicya policy type that implements on_unknown_key

Member Function Documentation

◆ register_key()

bool register_key ( const Key &  key,
Fun &&  fun 
)
inline

Register a factory function with the given key. Future calls to create will invoke Fun when provided the same value for key. If the key is already registered, Fun will not be inserted and this method will return false.

Parameters
keythe key to associate the given function with
funthe function, functor, or function pointer associated with the key
Returns
true if fun was successfully inserted for the given key; false if the key is already present in the registry

◆ key_is_registered()

bool key_is_registered ( const Key &  key) const
inlinenoexcept

Returns whether a given key is registered.

Parameters
keythe key to query
Returns
true if the key is present in the registry; false otherwise

◆ create()

auto create ( const Key &  key,
Args &&...  args 
)
inline

Invoke the factory function registered to the given key, returning its result if the key exists; otherwise returns ErrorPolicy::on_unknown_key(std::forward<Args>(args)...).

Parameters
keythe key for looking up the factory function
argsthe arguments to forward to the factory function for the key if it exists; ErrorPolicy::on_unknown_key otherwise
Returns
the result of invoking the factory function associated with key if it exists; ErrorPolicy::on_unknown_key otherwise

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