Class to load objects by baseclass pointer using `clipon' classes. More...
#include <vsl_clipon_binary_loader.h>
Public Member Functions | |
vsl_clipon_binary_loader () | |
Constructor. | |
~vsl_clipon_binary_loader () | |
Destructor. | |
void | make_empty () |
Remove all example objects. | |
void | add (const BaseClassIO &b) |
Add example object to list of those that can be loaded. | |
const vcl_vector< BaseClassIO * > & | object_io () const |
Return current list of individual IO objects. | |
const BaseClassIO & | object_io (const vcl_string &name) const |
Return IO object for given named class. | |
const BaseClassIO & | io_for_class (const BaseClass &b) const |
Return IO object that can deal with given class. | |
void | read_object (vsl_b_istream &is, BaseClass *&b) |
Reads object from stream and sets base class pointer. | |
void | write_object (vsl_b_ostream &is, const BaseClass *b) |
Writes object to stream given base class pointer. | |
void | print_object_summary (vcl_ostream &os, const BaseClass *b) |
Prints summary of object state to stream given base class pointer. | |
Static Public Member Functions | |
static vsl_clipon_binary_loader < BaseClass, BaseClassIO > & | 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 Member Functions | |
int | index_for_name (const vcl_string &name) const |
Return index associated with given object name. | |
Private Attributes | |
vcl_vector< BaseClassIO * > | object_io_ |
List of object loaders. | |
Static Private Attributes | |
static vsl_clipon_binary_loader < BaseClass, BaseClassIO > * | instance_ = 0 |
the singleton object. |
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 vcl_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.
vsl_clipon_binary_loader< BaseClass, BaseClassIO >::vsl_clipon_binary_loader | ( | ) | [inline] |
Constructor.
Definition at line 91 of file vsl_clipon_binary_loader.h.
vsl_clipon_binary_loader< BaseClass, BaseClassIO >::~vsl_clipon_binary_loader | ( | ) |
Destructor.
Definition at line 90 of file vsl_clipon_binary_loader.txx.
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 26 of file vsl_clipon_binary_loader.txx.
int vsl_clipon_binary_loader< BaseClass, BaseClassIO >::index_for_name | ( | const vcl_string & | name | ) | const [private] |
Return index associated with given object name.
Definition at line 33 of file vsl_clipon_binary_loader.txx.
vsl_clipon_binary_loader< BaseClass, BaseClassIO > & vsl_clipon_binary_loader< BaseClass, BaseClassIO >::instance | ( | ) | [static] |
Returns the instance variable for the singleton.
Definition at line 12 of file vsl_clipon_binary_loader.txx.
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 54 of file vsl_clipon_binary_loader.txx.
void vsl_clipon_binary_loader< BaseClass, BaseClassIO >::make_empty | ( | ) |
Remove all example objects.
Definition at line 82 of file vsl_clipon_binary_loader.txx.
const vcl_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.
const BaseClassIO & vsl_clipon_binary_loader< BaseClass, BaseClassIO >::object_io | ( | const vcl_string & | name | ) | const |
Return IO object for given named class.
Aborts if not available
Definition at line 75 of file vsl_clipon_binary_loader.txx.
void vsl_clipon_binary_loader< BaseClass, BaseClassIO >::print_object_summary | ( | vcl_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 134 of file vsl_clipon_binary_loader.txx.
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 97 of file vsl_clipon_binary_loader.txx.
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.
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 119 of file vsl_clipon_binary_loader.txx.
vsl_clipon_binary_loader< B, IO > * vsl_clipon_binary_loader< B, IO >::instance_ = 0 [static, private] |
the singleton object.
Definition at line 81 of file vsl_clipon_binary_loader.h.
vcl_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.