Class to load objects by baseclass pointer using ‘clipon’ classes. More...
#include <vsl_clipon_binary_loader.h>

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... | |
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:
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.
|
default |
Constructor.
|
override |
Destructor.
Definition at line 94 of file vsl_clipon_binary_loader.hxx.
| 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.
|
private |
Return index associated with given object name.
Definition at line 37 of file vsl_clipon_binary_loader.hxx.
|
static |
Returns the instance variable for the singleton.
Definition at line 16 of file vsl_clipon_binary_loader.hxx.
| 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.
| void vsl_clipon_binary_loader< BaseClass, BaseClassIO >::make_empty | ( | ) |
Remove all example objects.
Definition at line 86 of file vsl_clipon_binary_loader.hxx.
|
inline |
Return current list of individual IO objects.
Definition at line 106 of file vsl_clipon_binary_loader.h.
| 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.
| 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.
| 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.
|
protectedinherited |
Register this, so it can be deleted by vsl_delete_all_loaders();.
Definition at line 37 of file vsl_binary_loader_base.cxx.
| 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.
|
staticprivate |
the singleton object.
Definition at line 81 of file vsl_clipon_binary_loader.h.
|
private |
List of object loaders.
Definition at line 84 of file vsl_clipon_binary_loader.h.
1.8.15