vsl_clipon_binary_loader.hxx
Go to the documentation of this file.
1 // This is core/vsl/vsl_clipon_binary_loader.hxx
2 #ifndef vsl_clipon_binary_loader_hxx_
3 #define vsl_clipon_binary_loader_hxx_
4 //:
5 // \file
6 
7 #include <iostream>
8 #include <cstdlib>
9 #include <vector>
11 #ifdef _MSC_VER
12 # include <vcl_msvc_warnings.h>
13 #endif
14 
15 template<class BaseClass, class BaseClassIO>
17 {
18  if (instance_ == nullptr)
19  {
21 
22  // Register for deletion by vsl_delete_all_loaders()
23  instance_->register_this();
24  }
25  return *instance_;
26 }
27 
28 //: Add example object to list of those that can be loaded
29 template<class BaseClass, class BaseClassIO>
31 {
32  object_io_.push_back(b.clone());
33 }
34 
35 //: Return index associated with given object name
36 template<class BaseClass, class BaseClassIO>
38 {
39  unsigned int i=0;
40  while ((i<object_io_.size()) && !(object_io_[i]->target_classname()==name))
41  ++i;
42 
43  if (i>=object_io_.size())
44  {
45  std::cerr << "vsl_clipon_binary_loader<BaseClass>::index_for_name: "
46  << "class name <" << name << "> not in list of loaders\n"
47  << object_io_.size()<<" valid loaders available for\n";
48  for (unsigned int j=0; j<object_io_.size(); ++j)
49  std::cerr << object_io_[j]->target_classname() << std::endl;
50  std::abort();
51  }
52 
53  return i;
54 }
55 
56 //: Return IO object that can deal with given class
57 template<class BaseClass, class BaseClassIO>
58 const BaseClassIO& vsl_clipon_binary_loader<BaseClass,BaseClassIO>::io_for_class(const BaseClass& b) const
59 {
60  unsigned int i;
61  for (i=0; (i<object_io_.size()) && !(object_io_[i]->is_io_for(b)); ++i)
62  /*nothing*/;
63 
64  if (i>=object_io_.size())
65  {
66  std::cerr << "vsl_clipon_binary_loader<BaseClass>::io_for_class: "
67  << "Unable to determine suitable loader.\n"
68  << object_io_.size()<<" valid loaders available for\n";
69  for (unsigned int j=0; j<object_io_.size(); ++j)
70  std::cerr << object_io_[j]->target_classname() << std::endl;
71  std::abort();
72  }
73 
74  return *object_io_[i];
75 }
76 
77 //: Return IO object for given named class
78 template<class BaseClass, class BaseClassIO>
79 const BaseClassIO& vsl_clipon_binary_loader<BaseClass,BaseClassIO>::object_io(const std::string& name) const
80 {
81  return *object_io_[index_for_name(name)];
82 }
83 
84 
85 template<class BaseClass, class BaseClassIO>
87 {
88  for (unsigned int i=0; i<object_io_.size(); ++i)
89  delete object_io_[i];
90  object_io_.resize(0);
91 }
92 
93 template<class BaseClass, class BaseClassIO>
95 {
96  make_empty();
97 }
98 
99 // IO for pointers to BaseClass:
100 template<class BaseClass, class BaseClassIO>
102 {
103  // Delete old object pointed to by b
104  delete b;
105 
106  std::string name;
107  vsl_b_read(is,name);
108 
109  if (name=="VSL_NULL_PTR")
110  {
111  // Zero pointer
112  b=nullptr;
113  return;
114  }
115 
116  const BaseClassIO& io = object_io(name);
117  b = io.new_object();
118  io.b_read_by_base(is,*b);
119 }
120 
121 // IO for pointers to BaseClass:
122 template<class BaseClass, class BaseClassIO>
124 {
125  if (b==nullptr)
126  {
127  vsl_b_write(os,std::string("VSL_NULL_PTR"));
128  return;
129  }
130 
131  const BaseClassIO& io = io_for_class(*b);
132  vsl_b_write(os,io.target_classname());
133  io.b_write_by_base(os,*b);
134 }
135 
136 // IO for pointers to BaseClass:
137 template<class BaseClass, class BaseClassIO>
139 {
140  if (b==nullptr)
141  {
142  os<<"No object defined.";
143  return;
144  }
145 
146  const BaseClassIO& io = io_for_class(*b);
147  io.print_summary_by_base(os,*b);
148 }
149 
150 template <class B, class IO>
152 
153 #define VSL_CLIPON_BINARY_LOADER_INSTANTIATE(B,IO) \
154 template class vsl_clipon_binary_loader<B, IO >;
155 
156 #endif // vsl_clipon_binary_loader_hxx_
A binary output adaptor for any std::ostream.
Definition: vsl_binary_io.h:37
void vsl_b_write(vsl_b_ostream &os, char n)
Write char to vsl_b_ostream.
void register_this()
Register this, so it can be deleted by vsl_delete_all_loaders();.
static vsl_clipon_binary_loader< BaseClass, BaseClassIO > & instance()
Returns the instance variable for the singleton.
void write_object(vsl_b_ostream &is, const BaseClass *b)
Writes object to stream given base class pointer.
const std::vector< BaseClassIO * > & object_io() const
Return current list of individual IO objects.
void print_object_summary(std::ostream &os, const BaseClass *b)
Prints summary of object state to stream given base class pointer.
~vsl_clipon_binary_loader() override
Destructor.
void add(const BaseClassIO &b)
Add example object to list of those that can be loaded.
int index_for_name(const std::string &name) const
Return index associated with given object name.
const BaseClassIO & io_for_class(const BaseClass &b) const
Return IO object that can deal with given class.
void make_empty()
Remove all example objects.
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.
void read_object(vsl_b_istream &is, BaseClass *&b)
Reads object from stream and sets base class pointer.
Class to load objects by baseclass pointer using ‘clipon’ classes.