rutz::factory< T > Class Template Reference

Create objects base on 'key' strings. More...

#include <rutz/factory.h>

Inheritance diagram for rutz::factory< T >:

Inheritance graph
[legend]
Collaboration diagram for rutz::factory< T >:

Collaboration graph
[legend]

List of all members.

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.


Detailed Description

template<class T>
class rutz::factory< T >

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.


Constructor & Destructor Documentation

template<class T>
rutz::factory< T >::factory ( const char *  keydescr = "object type",
bool  nocase = false 
) [inline]

Default constructor.

Parameters:
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)

Definition at line 210 of file factory.h.


Member Function Documentation

template<class T>
const char* rutz::factory< T >::register_creator ( rutz::creator_base< base_t > *  creator,
const char *  name 
) [inline]

Registers a creation object with the factory.

The function returns the actual key that was paired with the creation function.

Definition at line 225 of file factory.h.

template<class T>
template<class derived_t>
const char* rutz::factory< T >::register_creator ( derived_t(*)()  func,
const char *  key = 0 
) [inline]

Registers a creation function with the factory.

The default key associated with the creation function will be given by the type's actual C++ name. The function returns the actual key that was paired with the creation function.

Definition at line 240 of file factory.h.

template<class T>
void rutz::factory< T >::register_alias ( const char *  orig_key,
const char *  alias_key 
) [inline]

Introduces an alternate key for an existing key.

There must already have been a creation function registered for the original key.

Definition at line 258 of file factory.h.

template<class T>
base_t rutz::factory< T >::new_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, a null pointer is returned.

Definition at line 288 of file factory.h.

template<class T>
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().


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

The software described here is Copyright (c) 1998-2005, Rob Peters.
This page was generated Wed Dec 3 06:56:34 2008 by Doxygen version 1.5.5.