vsl_binary_loader.hxx
Go to the documentation of this file.
1 // This is core/vsl/vsl_binary_loader.hxx
2 #ifndef vsl_binary_loader_hxx_
3 #define vsl_binary_loader_hxx_
4 //:
5 // \file
6 
7 #include <iostream>
8 #include <vector>
9 #include "vsl_binary_loader.h"
10 #ifdef _MSC_VER
11 # include <vcl_msvc_warnings.h>
12 #endif
13 
14 template<class BaseClass>
16 {
17  if (instance_ == nullptr)
18  {
19  instance_ = new vsl_binary_loader<BaseClass>;
20 
21  // Register for deletion by vsl_delete_all_loaders()
22  instance_->register_this();
23  }
24  return *instance_;
25 }
26 
27 template<class BaseClass>
29 {
30  for (unsigned int i=0; i<object_.size(); ++i)
31  delete object_[i];
32  object_.resize(0);
33 }
34 
35 template<class BaseClass>
37 {
38  make_empty();
39  instance_=nullptr;
40 }
41 
42 // IO for pointers to BaseClass:
43 template<class BaseClass>
45 {
46  if (!is) return;
47 
48  // HELP ON RUN-TIME ERROR HERE
49  // If you get a run-time error here it is most-likely because you called
50  // vsl_b_read with an uninitialised null base_class pointer. The base class
51  // pointer should either point to a real object, or be set to 0 - IMS.
52  delete b; // Delete old object pointed to by b
53 
54  std::string name;
55  vsl_b_read(is,name);
56 
57  if (name=="VSL_NULL_PTR")
58  {
59  // Zero pointer
60  b=nullptr;
61  return;
62  }
63 
64  unsigned int i = 0;
65  while (i<object_.size() && !(object_[i]->is_a()==name)) ++i;
66 
67  if (i<object_.size())
68  {
69  b = object_[i]->clone(); // If you get a compile error here you are probably trying to load into a non-base class pointer.
70  b->b_read(is);
71  }
72  else
73  {
74  std::cerr << "\n I/O ERROR: " << is_a() << "::load_object: "
75  << "class name <" << name << "> not in list of loaders\n"
76  << object_.size()<<" valid loaders:\n";
77  for (unsigned int j=0; j<object_.size(); ++j)
78  std::cerr << object_[j]->is_a() << std::endl;
79  is.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
80  return;
81  }
82 }
83 
84 // For pointer to baseclass types, but *not* for char* !
85 template<class BaseClass>
86 void vsl_b_write(vsl_b_ostream& bfs, const BaseClass* b)
87 {
88  if (b)
89  {
90  vsl_b_write(bfs, b->is_a());
91  b->b_write(bfs);
92  }
93  else
94  vsl_b_write(bfs, std::string("VSL_NULL_PTR"));
95 }
96 
97 template <class BaseClass>
99 
100 
101 #undef VSL_BINARY_LOADER_INSTANTIATE
102 #define VSL_BINARY_LOADER_WITH_SPECIALIZATION_INSTANTIATE(T) \
103 template <> std::string vsl_binary_loader<T >::is_a() const \
104 { return std::string("vsl_binary_loader<" #T ">"); }\
105 template class vsl_binary_loader<T >
106 #define VSL_BINARY_LOADER_INSTANTIATE(T) \
107 VSL_BINARY_LOADER_WITH_SPECIALIZATION_INSTANTIATE(T); \
108 /*template void vsl_b_read( vsl_b_istream& bfs, (T)*& b) ; */ \
109 template void vsl_b_write(vsl_b_ostream& bfs, const T* b)
110 
111 #endif // vsl_binary_loader_hxx_
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 register_this()
Register this, so it can be deleted by vsl_delete_all_loaders();.
Loader to do Polymorphic IO.
std::istream & is() const
A reference to the adaptor's stream.
Class to load objects by baseclass pointer.
void make_empty()
Remove all example objects.
void vsl_b_write(vsl_b_ostream &bfs, const BaseClass *b)
Binary file stream output operator for pointer to class.
void vsl_b_read(vsl_b_istream &is, char &n)
Read char from vsl_b_istream.
An adaptor for any std::istream to make it suitable for binary input.
~vsl_binary_loader() override
Destructor.
void load_object(vsl_b_istream &is, BaseClass *&b)
Loads object and sets base class pointer.