Static Public Member Functions | Private Types | Static Private Member Functions | Static Private Attributes
mbl_cloneables_factory< BASE > Class Template Reference

A general factory pattern. More...

#include <mbl_cloneables_factory.h>

List of all members.

Static Public Member Functions

static void add (const BASE &object)
 Add an object for later cloning by the factory.
static void add (const BASE &object, const vcl_string &name)
 Add an object for later cloning by the factory.
static vcl_auto_ptr< BASE > get_clone (const vcl_string &name)
 Get a pointer to a new copy of the object identified by name.

Private Types

typedef vcl_map< vcl_string,
mbl_cloneable_ptr< BASE > > 
MAP

Static Private Member Functions

static MAPobjects ()
 Get singleton instance.

Static Private Attributes

static vcl_auto_ptr< MAPobjects_
 Singleton array of names, and association concrete instantiations of BASE.

Detailed Description

template<class BASE>
class mbl_cloneables_factory< BASE >

A general factory pattern.

After templating this on a base class, and loading in a bunch of concrete instantiations of the derived classes, you can create any of the derived classes simply from the class's name.

BASE must define a clone() and is_a() members

 class BASE
 {
   ...
 public:
   virtual BASE* clone() const=0; // Derived classes must copy themselves.
   virtual vcl_string BASE::is_a() const; {
     return "BASE";} // Derived classes need unique names.
 }

Example:

 mbl_cloneables_factory<vimt_image>::add(vimt_image_2d());
 mbl_cloneables_factory<vimt_image>::add(vimt_image_3d());

 vcl_auto_ptr<vimt_image> p = mbl_cloneables_factory<vimt_image>::get_clone("vimt_image_2d()");
 assert(dynamic_cast<vimt_image_2d>(p));

Definition at line 46 of file mbl_cloneables_factory.h.


Member Typedef Documentation

template<class BASE >
typedef vcl_map<vcl_string, mbl_cloneable_ptr<BASE> > mbl_cloneables_factory< BASE >::MAP [private]

Definition at line 49 of file mbl_cloneables_factory.h.


Member Function Documentation

template<class BASE >
static void mbl_cloneables_factory< BASE >::add ( const BASE &  object) [inline, static]

Add an object for later cloning by the factory.

Use the object's class name via the is_a() member.

Definition at line 69 of file mbl_cloneables_factory.h.

template<class BASE >
static void mbl_cloneables_factory< BASE >::add ( const BASE &  object,
const vcl_string &  name 
) [inline, static]

Add an object for later cloning by the factory.

If there already is an object called name, it will be overwritten.

Definition at line 74 of file mbl_cloneables_factory.h.

template<class BASE >
static vcl_auto_ptr<BASE > mbl_cloneables_factory< BASE >::get_clone ( const vcl_string &  name) [inline, static]

Get a pointer to a new copy of the object identified by name.

An exception will be thrown if name does not exist.

Definition at line 81 of file mbl_cloneables_factory.h.

template<class BASE >
static MAP& mbl_cloneables_factory< BASE >::objects ( ) [inline, static, private]

Get singleton instance.

Definition at line 57 of file mbl_cloneables_factory.h.


Member Data Documentation

template<class BASE >
vcl_auto_ptr<MAP> mbl_cloneables_factory< BASE >::objects_ [static, private]

Singleton array of names, and association concrete instantiations of BASE.

Definition at line 52 of file mbl_cloneables_factory.h.


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