A general factory pattern. More...
#include <mbl_cloneables_factory.h>
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 MAP & | objects () |
Get singleton instance. | |
Static Private Attributes | |
static vcl_auto_ptr< MAP > | objects_ |
Singleton array of names, and association concrete instantiations of 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.
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.
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.
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.
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.
static MAP& mbl_cloneables_factory< BASE >::objects | ( | ) | [inline, static, private] |
Get singleton instance.
Definition at line 57 of file mbl_cloneables_factory.h.
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.