Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members

s11n::instantiator< BaseType, KeyType, ContextType > Class Template Reference

instantiator is essentially a static classloader, capable of loading classes by using registered factories for a given set of keys (e.g., class names). More...

#include <instantiator.h>

List of all members.

Public Types

typedef BaseType value_type
 A typedef for the BaseType used by this class.

typedef KeyType key_type
 A typedef for the KeyType used by this class.

typedef ContextType context_type
 Same as ContextType.

typedef KeyType argument_type
 Same as KeyType, for conformance with the the Adaptable Unary Functor model.

typedef BaseType * result_type
 Same as (BaseType *), for conformance with the Adaptable Unary Functor model.

typedef instantiator< BaseType,
KeyType, ContextType > 
ThisType
 Convenience typedef.

typedef value_type *(* factory_type )()
 The type of factories used by this class: a function taking void and returning (value_type ).

typedef std::map< key_type,
factory_type
object_factory_map
 Internal container type used for mapping keys to factories.


Public Member Functions

result_type operator() (const argument_type &key)
 Same as instantiate( key ).


Static Public Member Functions

value_typeinstantiate (const key_type &key)
 Tries to instantiate an instance of value_type using the given key.

void register_factory (const key_type &key, factory_type fp=0)
 Registers a factory using the given key.

template<typename SubOfBaseType> void register_subtype (const key_type &key, factory_type fp=0)
 Convenience wrapper around register_factory( key, factory_for_SubOfBaseType ).

object_factory_mapfactory_map ()
 Returns the internal key-to-factory map.

bool is_registered (const key_type &key)
 Returns true if the given key is registered.


Detailed Description

template<class BaseType, class KeyType = std::string, class ContextType = instantiator_sharing_context>
class s11n::instantiator< BaseType, KeyType, ContextType >

instantiator is essentially a static classloader, capable of loading classes by using registered factories for a given set of keys (e.g., class names).

Classloaders, at least in my experience, need to be able to load all classes which derive from some given type. Without a common base class, one can't safely attempt to cast from an arbitrary pointer to the type we want to load. That's where the BaseType parameter comes in. All objects instantiated via this loader must inherit from BaseType.

KeyType is a type which specifies the type of key used to look up classes, defaulting to std::string.

Both BaseType and KeyType must be Default Constructable on the heap (e.g., via new BaseType()).

This class holds no state, thus it can be copied quickly.

Sample usage:

typedef instantiator<MyClass> CL;
CL::register_factory( "my_key" );
MyClass *foo = CL::load( "some_key" ); // == NULL
foo = CL::instantiate( "my_key" ); // == a new object

Note that all instantiators of the same type use the same object factories. The ContextType template parameter can be used to limit the scope of the object factory registrations to a specific context: instantiators with different Contexts use different maps. ContextType is only used as a type, and is never instantiated by this class.

Definition at line 122 of file instantiator.h.


Member Typedef Documentation

template<class BaseType, class KeyType = std::string, class ContextType = instantiator_sharing_context>
typedef value_type*( * s11n::instantiator< BaseType, KeyType, ContextType >::factory_type)()
 

The type of factories used by this class: a function taking void and returning (value_type ).

See factory_map().

todo: implement proper functor support.

Definition at line 163 of file instantiator.h.

Referenced by s11n::instantiator< BaseType, KeyType, ContextType >::register_factory().


Member Function Documentation

template<class BaseType, class KeyType = std::string, class ContextType = instantiator_sharing_context>
object_factory_map& s11n::instantiator< BaseType, KeyType, ContextType >::factory_map  )  [inline, static]
 

Returns the internal key-to-factory map.

It is safe for clients to modify this except in multi-threaded environments, and then all guarantees go out the window. That said, it should never be necessary for clients to use this.

It is safe to call this post-main(), but such calls may return an empty map!

Definition at line 240 of file instantiator.h.

References s11n::instantiator< BaseType, KeyType, ContextType >::object_factory_map.

Referenced by s11n::instantiator< BaseType, KeyType, ContextType >::instantiate(), s11n::instantiator< BaseType, KeyType, ContextType >::is_registered(), and s11n::instantiator< BaseType, KeyType, ContextType >::register_factory().

template<class BaseType, class KeyType = std::string, class ContextType = instantiator_sharing_context>
value_type* s11n::instantiator< BaseType, KeyType, ContextType >::instantiate const key_type key  )  [inline, static]
 

Tries to instantiate an instance of value_type using the given key.

Returns NULL if no class could be loaded for the given key.

The caller takes responsibility for the returned pointer.

Definition at line 179 of file instantiator.h.

References s11n::instantiator< BaseType, KeyType, ContextType >::factory_map(), s11n::instantiator< BaseType, KeyType, ContextType >::instantiate(), and s11n::instantiator< BaseType, KeyType, ContextType >::value_type.

Referenced by s11n::instantiator< BaseType, KeyType, ContextType >::instantiate().

template<class BaseType, class KeyType = std::string, class ContextType = instantiator_sharing_context>
bool s11n::instantiator< BaseType, KeyType, ContextType >::is_registered const key_type key  )  [inline, static]
 

Returns true if the given key is registered.

This is sometimes useful for checking whether a factory needs to be re-registered, which is sometimes necessary post-main(), when the internal map gets hosed before clients are done using it.

Definition at line 251 of file instantiator.h.

References s11n::instantiator< BaseType, KeyType, ContextType >::factory_map(), and s11n::instantiator< BaseType, KeyType, ContextType >::is_registered().

Referenced by s11n::instantiator< BaseType, KeyType, ContextType >::is_registered().

template<class BaseType, class KeyType = std::string, class ContextType = instantiator_sharing_context>
void s11n::instantiator< BaseType, KeyType, ContextType >::register_factory const key_type key,
factory_type  fp = 0
[inline, static]
 

Registers a factory using the given key.

If fp is NULL then a default factory is used. Note that fp may not return a type other than ThisType::value_type *, but the actual object it creates may be a polymorphic subclass of value_type. See the object_factory class for a factory which does this subtype-to-base conversion.

Definition at line 207 of file instantiator.h.

References s11n::instantiator< BaseType, KeyType, ContextType >::factory_map(), s11n::instantiator< BaseType, KeyType, ContextType >::factory_type, and s11n::instantiator< BaseType, KeyType, ContextType >::register_factory().

Referenced by s11n::instantiator< BaseType, KeyType, ContextType >::register_factory().

template<class BaseType, class KeyType = std::string, class ContextType = instantiator_sharing_context>
template<typename SubOfBaseType>
void s11n::instantiator< BaseType, KeyType, ContextType >::register_subtype const key_type key,
factory_type  fp = 0
[inline, static]
 

Convenience wrapper around register_factory( key, factory_for_SubOfBaseType ).

Registers a factory for ThisType::value_type, using the given key and a default factory for producing SubOfBaseType objects. SubOfBaseType must be a subtype of ThisType::value_type.

Definition at line 223 of file instantiator.h.

References s11n::instantiator< BaseType, KeyType, ContextType >::register_subtype().

Referenced by s11n::instantiator< BaseType, KeyType, ContextType >::register_subtype().


The documentation for this class was generated from the following file:
Generated on Tue Jul 20 10:46:49 2004 for s11n by doxygen 1.3.7