Class to load objects by baseclass pointer. More...
#include <vsl_binary_loader.h>

Public Member Functions | |
| vsl_binary_loader ()=default | |
| Constructor. More... | |
| ~vsl_binary_loader () override | |
| Destructor. More... | |
| void | make_empty () |
| Remove all example objects. More... | |
| void | add (const BaseClass &b) |
| Add example object to list of those that can be loaded. More... | |
| const std::vector< BaseClass * > & | object () |
| Return current list of class objects which can be loaded. More... | |
| void | load_object (vsl_b_istream &is, BaseClass *&b) |
| Loads object and sets base class pointer. More... | |
| std::string | is_a () const |
Static Public Member Functions | |
| static vsl_binary_loader< BaseClass > & | instance () |
| Returns the instance variable for the singleton. More... | |
Protected Member Functions | |
| void | register_this () |
| Register this, so it can be deleted by vsl_delete_all_loaders();. More... | |
Private Attributes | |
| std::vector< BaseClass * > | object_ |
| List of concrete classes that this loader can deal with. More... | |
Static Private Attributes | |
| static vsl_binary_loader< BaseClass > * | instance_ = nullptr |
| the singleton object. More... | |
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.
|
default |
Constructor.
|
override |
Destructor.
Definition at line 36 of file vsl_binary_loader.hxx.
|
inline |
Add example object to list of those that can be loaded.
Definition at line 71 of file vsl_binary_loader.h.
|
static |
Returns the instance variable for the singleton.
Definition at line 15 of file vsl_binary_loader.hxx.
| std::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 44 of file vsl_binary_loader.hxx.
| void vsl_binary_loader< BaseClass >::make_empty | ( | ) |
Remove all example objects.
Definition at line 28 of file vsl_binary_loader.hxx.
|
inline |
Return current list of class objects which can be loaded.
Definition at line 74 of file vsl_binary_loader.h.
|
protectedinherited |
Register this, so it can be deleted by vsl_delete_all_loaders();.
Definition at line 37 of file vsl_binary_loader_base.cxx.
|
staticprivate |
the singleton object.
Definition at line 52 of file vsl_binary_loader.h.
|
private |
List of concrete classes that this loader can deal with.
Definition at line 55 of file vsl_binary_loader.h.
1.8.15