Public Member Functions | Static Public Member Functions | Protected Member Functions | Private Attributes | Static Private Attributes | List of all members
vsl_binary_loader< BaseClass > Class Template Reference

Class to load objects by baseclass pointer. More...

#include <vsl_binary_loader.h>

Inheritance diagram for vsl_binary_loader< BaseClass >:
Inheritance graph
[legend]

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...
 

Detailed Description

template<class BaseClass>
class vsl_binary_loader< BaseClass >

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.

Constructor & Destructor Documentation

◆ vsl_binary_loader()

template<class BaseClass>
vsl_binary_loader< BaseClass >::vsl_binary_loader ( )
default

Constructor.

◆ ~vsl_binary_loader()

template<class BaseClass >
vsl_binary_loader< BaseClass >::~vsl_binary_loader ( )
override

Destructor.

Definition at line 36 of file vsl_binary_loader.hxx.

Member Function Documentation

◆ add()

template<class BaseClass>
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.

◆ instance()

template<class BaseClass >
vsl_binary_loader< BaseClass > & vsl_binary_loader< BaseClass >::instance ( )
static

Returns the instance variable for the singleton.

Definition at line 15 of file vsl_binary_loader.hxx.

◆ is_a()

template<class BaseClass>
std::string vsl_binary_loader< BaseClass >::is_a ( ) const

◆ load_object()

template<class BaseClass >
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.

◆ make_empty()

template<class BaseClass >
void vsl_binary_loader< BaseClass >::make_empty ( )

Remove all example objects.

Definition at line 28 of file vsl_binary_loader.hxx.

◆ object()

template<class BaseClass>
const std::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.

◆ register_this()

void vsl_binary_loader_base::register_this ( )
protectedinherited

Register this, so it can be deleted by vsl_delete_all_loaders();.

Definition at line 37 of file vsl_binary_loader_base.cxx.

Member Data Documentation

◆ instance_

template<class BaseClass>
vsl_binary_loader< BaseClass > * vsl_binary_loader< BaseClass >::instance_ = nullptr
staticprivate

the singleton object.

Definition at line 52 of file vsl_binary_loader.h.

◆ object_

template<class BaseClass>
std::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.


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