Create objects base on 'key' strings. More...
#include <rutz/factory.h>
Public Types | |
typedef factory_base::fallback_t | fallback_t |
typedef T | base_t |
Public Member Functions | |
factory (const char *keydescr="object type", bool nocase=false) | |
Default constructor. | |
virtual | ~factory () throw () |
Virtual no-throw destructor. | |
const char * | register_creator (rutz::creator_base< base_t > *creator, const char *name) |
Registers a creation object with the factory. | |
template<class derived_t > | |
const char * | register_creator (derived_t(*func)(), const char *key=0) |
Registers a creation function with the factory. | |
void | register_alias (const char *orig_key, const char *alias_key) |
Introduces an alternate key for an existing key. | |
bool | is_valid_key (const char *key) const |
Query whether a given key is a valid, known key in the factory. | |
rutz::fstring | get_known_keys (const char *sep) const |
Get a list of known keys, separated by sep. | |
base_t | new_object (const rutz::fstring &type) const |
Returns a new object of a given type. | |
base_t | new_checked_object (const rutz::fstring &type) const |
Returns a new object of a given type. | |
void | set_fallback (rutz::shared_ptr< factory_fallback > f) |
Change the fallback object. | |
void | set_fallback (fallback_t *fptr) |
Change the fallback function. | |
Protected Member Functions | |
rutz::creator_base< base_t > * | find_creator (const rutz::fstring &key) const |
Find a creator for the given key; otherwise return null. |
Create objects base on 'key' strings.
rutz::factory can create objects of different types associated with different 'key' strings. rutz::factory maintains a mapping from key strings to rutz::creator_base's, and so given a key can call the create() function of the associated creator_base. All of the product types of a factory must be derived from factory::base_t. The recommended usage is for factories for specific types to be implemented as singleton classes derived from rutz::factory.
rutz::factory uses an internal mutex so that all of its member functions can safely be called in a multithreaded program without external locking.
Definition at line 155 of file factory.h.
rutz::factory< T >::factory | ( | const char * | keydescr = "object type" , |
|
bool | nocase = false | |||
) | [inline] |
Default constructor.
key_descr | a human-readable description of what this factory's keys represent; this is used in error messages, e.g. if descr is "frobnicator", then error messages would include "unknown frobnicator" | |
nocase | true if the factory should use case-insensitive string comparisons (default is false, giving normal case-sensitive string comparisons) |
virtual rutz::factory< T >::~factory | ( | ) | throw () [inline, virtual] |
rutz::creator_base<base_t>* rutz::factory< T >::find_creator | ( | const rutz::fstring & | key | ) | const [inline, protected] |
Find a creator for the given key; otherwise return null.
Definition at line 171 of file factory.h.
Referenced by rutz::factory< nub::ref< P > >::is_valid_key(), rutz::factory< nub::ref< P > >::new_checked_object(), and rutz::factory< nub::ref< P > >::new_object().
rutz::fstring rutz::factory< T >::get_known_keys | ( | const char * | sep | ) | const [inline] |
bool rutz::factory< T >::is_valid_key | ( | const char * | key | ) | const [inline] |
base_t rutz::factory< T >::new_checked_object | ( | const rutz::fstring & | type | ) | const [inline] |
Returns a new object of a given type.
If the given type has not been registered with the factory, an exception is thrown.
Definition at line 300 of file factory.h.
Referenced by nub::obj_mgr::new_obj().
base_t rutz::factory< T >::new_object | ( | const rutz::fstring & | type | ) | const [inline] |
void rutz::factory< T >::register_alias | ( | const char * | orig_key, | |
const char * | alias_key | |||
) | [inline] |
const char* rutz::factory< T >::register_creator | ( | derived_t(*)() | func, | |
const char * | key = 0 | |||
) | [inline] |
const char* rutz::factory< T >::register_creator | ( | rutz::creator_base< base_t > * | creator, | |
const char * | name | |||
) | [inline] |
void rutz::factory< T >::set_fallback | ( | fallback_t * | fptr | ) | [inline] |
void rutz::factory< T >::set_fallback | ( | rutz::shared_ptr< factory_fallback > | f | ) | [inline] |