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

Class to load objects by baseclass pointer using ‘clipon’ classes. More...

#include <vsl_clipon_binary_loader.h>

Inheritance diagram for vsl_clipon_binary_loader< BaseClass, BaseClassIO >:
Inheritance graph
[legend]

Public Member Functions

 vsl_clipon_binary_loader ()=default
 Constructor. More...
 
 ~vsl_clipon_binary_loader () override
 Destructor. More...
 
void make_empty ()
 Remove all example objects. More...
 
void add (const BaseClassIO &b)
 Add example object to list of those that can be loaded. More...
 
const std::vector< BaseClassIO * > & object_io () const
 Return current list of individual IO objects. More...
 
const BaseClassIO & object_io (const std::string &name) const
 Return IO object for given named class. More...
 
const BaseClassIO & io_for_class (const BaseClass &b) const
 Return IO object that can deal with given class. More...
 
void read_object (vsl_b_istream &is, BaseClass *&b)
 Reads object from stream and sets base class pointer. More...
 
void write_object (vsl_b_ostream &is, const BaseClass *b)
 Writes object to stream given base class pointer. More...
 
void print_object_summary (std::ostream &os, const BaseClass *b)
 Prints summary of object state to stream given base class pointer. More...
 

Static Public Member Functions

static vsl_clipon_binary_loader< BaseClass, BaseClassIO > & 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 Member Functions

int index_for_name (const std::string &name) const
 Return index associated with given object name. More...
 

Private Attributes

std::vector< BaseClassIO * > object_io_
 List of object loaders. More...
 

Static Private Attributes

static vsl_clipon_binary_loader< BaseClass, BaseClassIO > * instance_ = nullptr
 the singleton object. More...
 

Detailed Description

template<class BaseClass, class BaseClassIO>
class vsl_clipon_binary_loader< BaseClass, BaseClassIO >

Class to load objects by baseclass pointer using ‘clipon’ classes.

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 name information stored in the stream. We assume the existence of a polymorphic hierarchy of ‘clipon’ loader objects, eg BaseClassIO and DerivedClassIO, one per class in the hierarchy derived from BaseClass. Each XXXXIO class is able to read and write the corresponding XXXX class.

BaseClassIO must implement the following functions:

//: Base for objects which provide IO for classes derived from BaseClass
class BaseClassIO
{
public:
//: Create new object of type BaseClass on heap
virtual BaseClass* new_object() const;
//: Write derived class to os using baseclass reference
virtual void b_write_by_base(vsl_b_ostream& os,
const BaseClass& base) const;
//: Write derived class to os using baseclass reference
virtual void b_read_by_base(vsl_b_istream& is, BaseClass& base) const;
//: Print summary to stream by BaseClass pointer
void vsl_print_summary(vsl_b_ostream &os, const BaseClass * b);
//: Copy this object onto the heap and return a pointer
virtual BaseClassIO* clone() const;
//: Return name of class for which this object provides IO
virtual std::string target_classname() const;
//: Return true if b is of class target_classname()
// Typically this will just be "return b.is_a()==target_classname()"
// However, third party libraries may use a different system
virtual bool is_io_for(const BaseClass& b) const;
};

To handle the actual IO we define a loader which has a list of BaseClassIO pointers, and the ChainOfResponsibility (Design Pattern) approach is used to load the object i.e. each io->target_classname() 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 use it to create an object on the heap (using io->new_object()) and then load the data into that from the stream (using io->b_read_by_base())

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_clipon_binary_loader<B,IO>::instance().add(my_object)

For examples of usage please see vsl/tests/test_clipon_polymorphic_io.cxx or look in the Binary IO chapter of the VXL book.

To indicate a null pointer (0), the string "VSL_NULL_PTR" is saved to the stream. All loader singletons can be deleted using vsl_delete_all_loaders()

Definition at line 78 of file vsl_clipon_binary_loader.h.

Constructor & Destructor Documentation

◆ vsl_clipon_binary_loader()

template<class BaseClass, class BaseClassIO>
vsl_clipon_binary_loader< BaseClass, BaseClassIO >::vsl_clipon_binary_loader ( )
default

Constructor.

◆ ~vsl_clipon_binary_loader()

template<class BaseClass , class BaseClassIO >
vsl_clipon_binary_loader< BaseClass, BaseClassIO >::~vsl_clipon_binary_loader ( )
override

Destructor.

Definition at line 94 of file vsl_clipon_binary_loader.hxx.

Member Function Documentation

◆ add()

template<class BaseClass , class BaseClassIO >
void vsl_clipon_binary_loader< BaseClass, BaseClassIO >::add ( const BaseClassIO &  b)

Add example object to list of those that can be loaded.

Definition at line 30 of file vsl_clipon_binary_loader.hxx.

◆ index_for_name()

template<class BaseClass , class BaseClassIO >
int vsl_clipon_binary_loader< BaseClass, BaseClassIO >::index_for_name ( const std::string &  name) const
private

Return index associated with given object name.

Definition at line 37 of file vsl_clipon_binary_loader.hxx.

◆ instance()

template<class BaseClass , class BaseClassIO >
vsl_clipon_binary_loader< BaseClass, BaseClassIO > & vsl_clipon_binary_loader< BaseClass, BaseClassIO >::instance ( )
static

Returns the instance variable for the singleton.

Definition at line 16 of file vsl_clipon_binary_loader.hxx.

◆ io_for_class()

template<class BaseClass , class BaseClassIO >
const BaseClassIO & vsl_clipon_binary_loader< BaseClass, BaseClassIO >::io_for_class ( const BaseClass &  b) const

Return IO object that can deal with given class.

Definition at line 58 of file vsl_clipon_binary_loader.hxx.

◆ make_empty()

template<class BaseClass , class BaseClassIO >
void vsl_clipon_binary_loader< BaseClass, BaseClassIO >::make_empty ( )

Remove all example objects.

Definition at line 86 of file vsl_clipon_binary_loader.hxx.

◆ object_io() [1/2]

template<class BaseClass, class BaseClassIO>
const std::vector<BaseClassIO*>& vsl_clipon_binary_loader< BaseClass, BaseClassIO >::object_io ( ) const
inline

Return current list of individual IO objects.

Definition at line 106 of file vsl_clipon_binary_loader.h.

◆ object_io() [2/2]

template<class BaseClass , class BaseClassIO >
const BaseClassIO & vsl_clipon_binary_loader< BaseClass, BaseClassIO >::object_io ( const std::string &  name) const

Return IO object for given named class.

Aborts if not available

Definition at line 79 of file vsl_clipon_binary_loader.hxx.

◆ print_object_summary()

template<class BaseClass , class BaseClassIO >
void vsl_clipon_binary_loader< BaseClass, BaseClassIO >::print_object_summary ( std::ostream &  os,
const BaseClass *  b 
)

Prints summary of object state to stream given base class pointer.

Determines which derived class object is and calls the appropriate print summary function. (Class must be one given to Loader by the add method). If b==0, a suitable string will be saved

Definition at line 138 of file vsl_clipon_binary_loader.hxx.

◆ read_object()

template<class BaseClass , class BaseClassIO >
void vsl_clipon_binary_loader< BaseClass, BaseClassIO >::read_object ( vsl_b_istream is,
BaseClass *&  b 
)

Reads object from stream and sets base class pointer.

Determines which derived class object on stream 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 101 of file vsl_clipon_binary_loader.hxx.

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

◆ write_object()

template<class BaseClass , class BaseClassIO >
void vsl_clipon_binary_loader< BaseClass, BaseClassIO >::write_object ( vsl_b_ostream is,
const BaseClass *  b 
)

Writes object to stream given base class pointer.

Determines which derived class object is and calls the appropriate write function. (Class must be one given to Loader by the add method). If b==0, a suitable string will be saved

Definition at line 123 of file vsl_clipon_binary_loader.hxx.

Member Data Documentation

◆ instance_

template<class BaseClass, class BaseClassIO>
vsl_clipon_binary_loader< B, IO > * vsl_clipon_binary_loader< B, IO >::instance_ = nullptr
staticprivate

the singleton object.

Definition at line 81 of file vsl_clipon_binary_loader.h.

◆ object_io_

template<class BaseClass, class BaseClassIO>
std::vector<BaseClassIO*> vsl_clipon_binary_loader< BaseClass, BaseClassIO >::object_io_
private

List of object loaders.

Definition at line 84 of file vsl_clipon_binary_loader.h.


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