Public Member Functions | Static Public Member Functions | Protected Member Functions | Private Attributes | Static Private Attributes
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]

List of all members.

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.

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

template<class BaseClass>
vsl_binary_loader< BaseClass >::vsl_binary_loader ( ) [inline]

Constructor.

Definition at line 59 of file vsl_binary_loader.h.

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

Destructor.

Definition at line 32 of file vsl_binary_loader.txx.


Member Function Documentation

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.

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

template<class BaseClass>
vcl_string vsl_binary_loader< BaseClass >::is_a ( ) const
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 40 of file vsl_binary_loader.txx.

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

Remove all example objects.

Definition at line 24 of file vsl_binary_loader.txx.

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


Member Data Documentation

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

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


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