STIR  6.2.0
Public Member Functions | List of all members
stir::FactoryRegistry< Key, Factory, Compare > Class Template Reference

This class can be used to store 'factories' and their corresponding keys. It is essentially a map, but with some extra embelishments. More...

#include "stir/FactoryRegistry.h"

Public Member Functions

 FactoryRegistry ()
 Default constructor without defaults (see find_factory())
 
 FactoryRegistry (const Key &default_key, const Factory &default_factory)
 constructor with default values which will be returned when no match is found (see find_factory())
 
void add_to_registry (const Key &key, Factory const &factory)
 Add a pair to the registry. More...
 
void remove_from_registry (const Key &key)
 Remove a pair from the registry.
 
void list_keys (std::ostream &s) const
 List all keys to an ostream, separated by newlines.
 
Factory const & find_factory (const Key &key) const
 Find a factory corresponding to a key. More...
 

Detailed Description

template<typename Key, typename Factory, typename Compare = std::less<Key>>
class stir::FactoryRegistry< Key, Factory, Compare >

This class can be used to store 'factories' and their corresponding keys. It is essentially a map, but with some extra embelishments.

A factory is supposed to be an object which can create another object, although this is not really enfored by the implementation of FactoryRegistry.

Type requirements

Key,Factory,Compare must be suitable as defined for std::map. In addition, FactoryRegistry::list_keys() requires that operator<<(ostream&, const Key&) is defined.

Todo:
Probably it would be better to store pointers to factories. However, in that case, the destructor of FactoryRegistry would have to deallocate these factory objects. This would mean that factories have to be allocated with new, and hence would prevent using simple function pointers.

Member Function Documentation

◆ add_to_registry()

template<class Key , class Factory , class Compare >
void stir::FactoryRegistry< Key, Factory, Compare >::add_to_registry ( const Key &  key,
Factory const &  factory 
)
inline

Add a pair to the registry.

Adding the same key twice will overwrite the first value.

References stir::warning().

Referenced by stir::FactoryRegistry< Key, Factory, Compare >::FactoryRegistry().

◆ find_factory()

template<class Key , class Factory , class Compare >
Factory const & stir::FactoryRegistry< Key, Factory, Compare >::find_factory ( const Key &  key) const
inline

Find a factory corresponding to a key.

If the key is not found, the behaviour depends on which constructor was used. If the (default) no-argument constructor is used, an error message is printed, and the program aborts. If the 2nd constructor with default values, the default_factory is returned.

References stir::error(), and stir::FactoryRegistry< Key, Factory, Compare >::list_keys().


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