vsl_binary_loader.h
Go to the documentation of this file.
1 // This is core/vsl/vsl_binary_loader.h
2 #ifndef vsl_binary_loader_h_
3 #define vsl_binary_loader_h_
4 //:
5 // \file
6 // \brief Loader to do Polymorphic IO.
7 // \author Ian Scott, Tim Cootes (Manchester) March 2001
8 //
9 // You should include this file if you want to do polymorphic IO
10 // (i.e. save a class by its base-class pointer.)
11 
12 #include <vector>
13 #include <vsl/vsl_binary_io.h>
15 #ifdef _MSC_VER
16 # include <vcl_msvc_warnings.h>
17 #endif
18 
19 //: Class to load objects by baseclass pointer.
20 // An example of a singleton design pattern for loading
21 // a DerivedClass from a stream into a BaseClass*.
22 // All we are given is a BaseClass* into which
23 // the object has to be loaded but we can only tell
24 // what sort of object it is from the is_a() information
25 // stored in the stream. To handle this we define a loader
26 // which has a list of BaseClass pointers,
27 // and the ChainOfResponsibility (Design Pattern)
28 // approach is used to load the object i.e. each
29 // ptr->is_a() is matched against the string on the stream
30 // until we find a match or run out of pointers. If
31 // a pointer is found which matches the string on
32 // the stream, we clone it, use the clone to load the object
33 // from the stream, then return a pointer to the clone.
34 //
35 // We use a singleton so that there is only one list of
36 // concrete derived classes which can be added
37 // to for loading purposes. If you derive a new
38 // class you just have to append it to the list of
39 // classes of the singleton, viz:
40 // vsl_binary_loader::instance().append(my_object)
41 // The BaseClass MUST implement is_a(),is_class(),clone(),b_write() and
42 // b_read() virtual functions.
43 //
44 // vsl_binary_loader.h also provides the function templates for loading
45 // and saving by base class pointer
46 //
47 // All loader singletons can be deleted using vsl_delete_all_loaders()
48 template<class BaseClass>
50 {
51  //: the singleton object
53 
54  //: List of concrete classes that this loader can deal with
55  std::vector<BaseClass*> object_;
56 
57  public :
58  //: Constructor
59  vsl_binary_loader() = default;
60 
61  //: Destructor
62  ~vsl_binary_loader() override;
63 
64  //: Returns the instance variable for the singleton.
66 
67  //: Remove all example objects
68  void make_empty();
69 
70  //: Add example object to list of those that can be loaded
71  void add( const BaseClass& b) { object_.push_back(b.clone());}
72 
73  //: Return current list of class objects which can be loaded
74  const std::vector<BaseClass*>& object() { return object_; }
75 
76  //: Loads object and sets base class pointer
77  // Determines which derived class object on is belongs
78  // to, loads it and sets b to be a pointer to it.
79  // (Class must be one given to Loader by the add method).
80  // If is indicates a NULL pointer, b will be set to NULL.
81  // If b not initially NULL, *b will be deleted.
82  void load_object( vsl_b_istream& is, BaseClass*& b);
83 
84  std::string is_a() const;
85 };
86 
87 
88 //: Loads object and sets base class pointer
89 // Determines which derived class object on bfs belongs
90 // to, loads it and sets b to be a pointer to it.
91 // (Class must be one given to Loader by the append method).
92 // If bfs indicates a NULL pointer, b will be set to NULL.
93 // If b not initially NULL, *b will be deleted.
94 template<class BaseClass>
95 inline void vsl_b_read( vsl_b_istream& bfs, BaseClass*& b)
96 {
97  vsl_binary_loader<BaseClass>::instance().load_object(bfs,b);
98 }
99 
100 //: Binary file stream output operator for pointer to class
101 // This works correctly even if b is a NULL pointer
102 template<class BaseClass>
103 void vsl_b_write(vsl_b_ostream& bfs, const BaseClass* b);
104 
105 #endif // vsl_binary_loader_h_
A binary output adaptor for any std::ostream.
Definition: vsl_binary_io.h:37
static vsl_binary_loader< BaseClass > & instance()
Returns the instance variable for the singleton.
void vsl_b_read(vsl_b_istream &bfs, BaseClass *&b)
Loads object and sets base class pointer.
void add(const BaseClass &b)
Add example object to list of those that can be loaded.
static vsl_binary_loader< BaseClass > * instance_
the singleton object.
void vsl_b_write(vsl_b_ostream &bfs, const BaseClass *b)
Binary file stream output operator for pointer to class.
const std::vector< BaseClass * > & object()
Return current list of class objects which can be loaded.
Base class for vsl_binary_loader objects.
Class to load objects by baseclass pointer.
std::string is_a() const
void make_empty()
Remove all example objects.
vsl_binary_loader()=default
Constructor.
An adaptor for any std::istream to make it suitable for binary input.
std::vector< BaseClass * > object_
List of concrete classes that this loader can deal with.
Set of functions, and objects to perform binary IO.
~vsl_binary_loader() override
Destructor.
void load_object(vsl_b_istream &is, BaseClass *&b)
Loads object and sets base class pointer.