Class to load objects by baseclass pointer. More...
#include <vsl_binary_loader.h>
Public Member Functions | |
vsl_binary_loader () | |
Constructor. | |
~vsl_binary_loader () | |
Destructor. | |
void | make_empty () |
Remove all example objects. | |
void | add (const BaseClass &b) |
Add example object to list of those that can be loaded. | |
const vcl_vector< BaseClass * > & | object () |
Return current list of class objects which can be loaded. | |
void | load_object (vsl_b_istream &is, BaseClass *&b) |
Loads object and sets base class pointer. | |
vcl_string | is_a () const |
Static Public Member Functions | |
static vsl_binary_loader < BaseClass > & | instance () |
Returns the instance variable for the singleton. | |
Protected Member Functions | |
void | register_this () |
Register this, so it can be deleted by vsl_delete_all_loaders();. | |
Private Attributes | |
vcl_vector< BaseClass * > | object_ |
List of concrete classes that this loader can deal with. | |
Static Private Attributes | |
static vsl_binary_loader < BaseClass > * | instance_ = 0 |
the singleton object. |
Class to load objects by baseclass pointer.
An example of a singleton design pattern for loading a DerivedClass from a stream into a BaseClass*. All we are given is a BaseClass* into which the object has to be loaded but we can only tell what sort of object it is from the is_a() information stored in the stream. To handle this we define a loader which has a list of BaseClass pointers, and the ChainOfResponsibility (Design Pattern) approach is used to load the object i.e. each ptr->is_a() is matched against the string on the stream until we find a match or run out of pointers. If a pointer is found which matches the string on the stream, we clone it, use the clone to load the object from the stream, then return a pointer to the clone.
We use a singleton so that there is only one list of concrete derived classes which can be added to for loading purposes. If you derive a new class you just have to append it to the list of classes of the singleton, viz: vsl_binary_loader::instance().append(my_object) The BaseClass MUST implement is_a(),is_class(),clone(),b_write() and b_read() virtual functions.
vsl_binary_loader.h also provides the function templates for loading and saving by base class pointer
All loader singletons can be deleted using vsl_delete_all_loaders()
Definition at line 49 of file vsl_binary_loader.h.
vsl_binary_loader< BaseClass >::vsl_binary_loader | ( | ) | [inline] |
Constructor.
Definition at line 59 of file vsl_binary_loader.h.
vsl_binary_loader< BaseClass >::~vsl_binary_loader | ( | ) |
Destructor.
Definition at line 32 of file vsl_binary_loader.txx.
void vsl_binary_loader< BaseClass >::add | ( | const BaseClass & | b | ) | [inline] |
Add example object to list of those that can be loaded.
Definition at line 71 of file vsl_binary_loader.h.
vsl_binary_loader< BaseClass > & vsl_binary_loader< BaseClass >::instance | ( | ) | [static] |
Returns the instance variable for the singleton.
Definition at line 11 of file vsl_binary_loader.txx.
vcl_string vsl_binary_loader< BaseClass >::is_a | ( | ) | const |
void vsl_binary_loader< BaseClass >::load_object | ( | vsl_b_istream & | is, |
BaseClass *& | b | ||
) |
Loads object and sets base class pointer.
Determines which derived class object on is belongs to, loads it and sets b to be a pointer to it. (Class must be one given to Loader by the add method). If is indicates a NULL pointer, b will be set to NULL. If b not initially NULL, *b will be deleted.
Definition at line 40 of file vsl_binary_loader.txx.
void vsl_binary_loader< BaseClass >::make_empty | ( | ) |
Remove all example objects.
Definition at line 24 of file vsl_binary_loader.txx.
const vcl_vector<BaseClass*>& vsl_binary_loader< BaseClass >::object | ( | ) | [inline] |
Return current list of class objects which can be loaded.
Definition at line 74 of file vsl_binary_loader.h.
void vsl_binary_loader_base::register_this | ( | ) | [protected, inherited] |
Register this, so it can be deleted by vsl_delete_all_loaders();.
Definition at line 38 of file vsl_binary_loader_base.cxx.
vsl_binary_loader< BaseClass > * vsl_binary_loader< BaseClass >::instance_ = 0 [static, private] |
the singleton object.
Definition at line 52 of file vsl_binary_loader.h.
vcl_vector<BaseClass*> vsl_binary_loader< BaseClass >::object_ [private] |
List of concrete classes that this loader can deal with.
Definition at line 55 of file vsl_binary_loader.h.