Classes | Public Types | Static Public Member Functions | List of all members
minxlib::factory_map< K, F, D > Class Template Reference

A registry mapping keys to factory functions. More...

#include <factory.hh>

Classes

struct  bad_key
 Exception to indicate nonexistent key. More...
 
class  iterator
 Iterator for accessing client-supplied arbitrary data. More...
 

Public Types

typedef iterator const_iterator
 Read-only iterators. More...
 

Static Public Member Functions

static bool add (K k, F f, D d=null_type())
 Register a factory and optional data. More...
 
static F get_factory (K k)
 Retrieve the factory function for a class. More...
 
static D get_data (K k)
 Retrieve the user-supplied arbitrary data for a class. More...
 
static const_iterator begin ()
 Obtain read-only iterator to first data item. More...
 
static const_iterator end ()
 Obtain read-only iterator to one past last data item. More...
 
static std::pair
< const_iterator,
const_iterator
range ()
 Pack begin and end iterators in an STL pair. More...
 

Detailed Description

template<typename K, typename F, typename D = null_type>
class minxlib::factory_map< K, F, D >

A registry mapping keys to factory functions.

This class implements a singleton map that holds the subclass factory functions (type F) for classes in a hierarchy identified by keys of type K. In addition to the factory functions, it can also hold arbitrary data of type D.

The factory functions are used by the factory class defined in this file. However, the optional arbitrary data is only held and can be used by client classes in whatever way they see fit. In minxlib, this is usually used for the pythonize functions; see event.hh (for example).

Note
To use more than a single item of arbitrary data, you can use a boost::tuple.
As mentioned earlier, this object factory framework is designed specifically for minxlib and is not meant for wide reuse. One of the implications of this is that the types K, F, and D should be cheap to construct and copy. In minxlib, K is typically an integral type, F and D are function pointers.

Member Typedef Documentation

template<typename K, typename F, typename D = null_type>
typedef iterator minxlib::factory_map< K, F, D >::const_iterator

Read-only iterators.

This class's iterators are already read-only. Therefore, the const_iterator type is the same as the iterator type.

Member Function Documentation

template<typename K, typename F, typename D = null_type>
static bool minxlib::factory_map< K, F, D >::add ( k,
f,
d = null_type() 
)
inlinestatic

Register a factory and optional data.

Parameters
kThe key identifying a class in some hierarchy.
fThe factory function for the class identified by k.
dOptional data to be stored along with the class's factory.
Returns
True (i.e., useless return value).

This function is meant to be used by subclasses in a class hierarchy that is to be instantiated with an object factory. The typical usage pattern would be for each subclass in the hierarchy to define a static bool data member and call this function in that data member's initializer.

template<typename K , typename F , typename D >
factory_map< K, F, D >::iterator minxlib::factory_map< K, F, D >::begin ( )
static

Obtain read-only iterator to first data item.

Returns
Iterator to first item.

When some function wants to iterate over all the arbitrary data items supplied during the registration of the various subclass factories, it can request an iterator to the first item of the underlying sequence using this function.

Note
Once data is stored in a factory_map, clients may only obtain read-only access to it. That is, the iterator returned by this function is a const_iterator.
template<typename K , typename F , typename D >
factory_map< K, F, D >::iterator minxlib::factory_map< K, F, D >::end ( )
static

Obtain read-only iterator to one past last data item.

Returns
Iterator pointing to one-past last item.

When some function wants to iterate over all the arbitrary data items supplied during the registration of the various subclass factories, it can request an iterator to one-past the last item of the underlying sequence using this function.

Note
Once data is stored in a factory_map, clients may only obtain read-only access to it. That is, the iterator returned by this function is a const_iterator.
template<typename K, typename F, typename D = null_type>
static D minxlib::factory_map< K, F, D >::get_data ( k)
inlinestatic

Retrieve the user-supplied arbitrary data for a class.

Parameters
kThey key identifying the class whose user data we want.
Returns
The user data for the specified class.

This function is meant to be called by clients so they can get the arbitrary data they supplied when registering their factories. Typically, however, clients will iterate over the map and use the data values through that process.

template<typename K, typename F, typename D = null_type>
static F minxlib::factory_map< K, F, D >::get_factory ( k)
inlinestatic

Retrieve the factory function for a class.

Parameters
kThey key identifying the class whose factory we want.
Returns
The factory function for the specified class.

This function is meant to be called by the minxlib::factory class. Other clients should have little use for it.

template<typename K, typename F, typename D = null_type>
static std::pair<const_iterator, const_iterator> minxlib::factory_map< K, F, D >::range ( )
inlinestatic

Pack begin and end iterators in an STL pair.

Returns
STL pair containing begin and end iterators.

This function is meant to be used in conjunction with BOOST_FOREACH. Here is an illustrative (albeit non-buildable) example:

class foo { ... } ;
class bar { ... } ;
typedef foo* (*create_foo)() ;
typedef factory_map<int, create_foo, bar> registry ;
// code to register various factory functions and bar
// objects in registry
void f()
{
BOOST_FOREACH(const bar& b, registry::range())
b.do_something() ;
}