vil_io_smart_ptr.hxx
Go to the documentation of this file.
1 // This is core/vil/io/vil_io_smart_ptr.hxx
2 #ifndef vil_io_smart_ptr_hxx_
3 #define vil_io_smart_ptr_hxx_
4 //:
5 // \file
6 // \brief Serialised binary IO functions for vil_smart_ptr<T>
7 // \author Tim Cootes/Ian Scott (Manchester)
8 
9 #include <iostream>
10 #include "vil_io_smart_ptr.h"
11 #include <vsl/vsl_binary_io.h>
12 #include <vil/vil_smart_ptr.h>
13 
14 //=========================================================================
15 //: Binary save self to stream.
16 template<class T>
17 void vsl_b_write(vsl_b_ostream & os, const vil_smart_ptr<T> &p)
18 {
19  // write version number
20  constexpr short io_version_no = 2;
21  vsl_b_write(os, io_version_no);
22 
23  if (p.ptr() == nullptr) // Deal with Null pointers first.
24  {
25  vsl_b_write(os, true);
26  vsl_b_write(os, 0ul); // Use 0 to indicate a null pointer.
27  // True serialisation IDs are always 1 or more.
28  return;
29  }
30 
31  // Get a serial_number for object being pointed to
32  unsigned long id = os.get_serial_number(p.ptr());
33  // Find out if this is the first time the object being pointed to is
34  // being saved
35  if (id == 0)
36  {
37  id = os.add_serialisation_record(p.ptr());
38 
39  // Say that this is the first time
40  // that this object is being saved.
41  // This isn't really necessary but
42  // it is useful as an error check
43  vsl_b_write(os, true);
44  vsl_b_write(os, id); // Save the serial number
45 // If you get an error in the next line, it could be because your type T
46 // has no vsl_b_write(vsl_b_ostream &,const T*) defined on it.
47 // See the documentation in the .h file to see how to add it.
48  vsl_b_write(os, p.ptr()); // Only save the actual object if it
49  //hasn't been saved before to this stream
50  }
51  else
52  {
53  // Say that this is not the first time
54  // that this object is being saved.
55  // This isn't really necessary but
56  // it is useful as an error check
57 
58  vsl_b_write(os, false);
59  vsl_b_write(os, id); // Save the serial number
60  }
61 }
62 
63 //=====================================================================
64 //: Binary load self from stream.
65 template<class T>
66 void vsl_b_read(vsl_b_istream &is, vil_smart_ptr<T> &p)
67 {
68  if (!is) return;
69 
70  short ver;
71  vsl_b_read(is, ver);
72  switch (ver)
73  {
74  case 1:
75  case 2:
76  {
77  bool first_time; // true if the object is about to be loaded
78  vsl_b_read(is, first_time);
79 
80  unsigned long id; // Unique serial number indentifying object
81  vsl_b_read(is, id);
82 
83  if (id == 0) // Deal with Null pointers first.
84  {
85  p = nullptr;
86  return;
87  }
88 
89  T * pointer = static_cast<T *>( is.get_serialisation_pointer(id));
90  if (first_time != (pointer == nullptr))
91  {
92  // This checks that the saving stream and reading stream
93  // both agree on whether or not this is the first time they
94  // have seen this object.
95  std::cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vil_smart_ptr<T>&)\n"
96  << " De-serialisation failure\n";
97  is.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
98  return;
99  }
100 
101  if (pointer == nullptr)
102  {
103  // If you get an error in the next line, it could be because your type T
104  // has no vsl_b_read(vsl_b_ostream&,T*&) defined on it.
105  // See the documentation in the .h file to see how to add it.
106  vsl_b_read(is, pointer);
107  is.add_serialisation_record(id, pointer);
108  }
109 
110  p = pointer; // This operator method will set the internal
111  //pointer in vil_smart_ptr.
112  break;
113  }
114  default:
115  std::cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vil_smart_ptr<T>&)\n"
116  << " Unknown version number "<< ver << '\n';
117  is.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
118  return;
119  }
120 }
121 
122 //=====================================================================
123 //: Output a human readable summary to the stream
124 template<class T>
125 void vsl_print_summary(std::ostream & os,const vil_smart_ptr<T> & p)
126 {
127  os << "Smart ptr to ";
128  if (p.ptr())
129  {
130  // If you get an error in the next line, it could be because your type T
131  // has no vsl_print_summary(vsl_b_ostream &, const T*) defined on it.
132  // See the documentation in the .h file to see how to add it.
133  vsl_print_summary(os, (p.ptr()));
134  }
135  else
136  os << "NULL";
137 }
138 
139 
140 #if 0 // commented out
141 //===========================================
142 // Deal with base class pointers
143 template<class T>
144 void vsl_b_read(vsl_b_istream& is, vil_smart_ptr<T> * &p)
145 {
146  delete p;
147  bool not_null_ptr;
148  vsl_b_read(is, not_null_ptr);
149  if (not_null_ptr)
150  {
151  p = new vil_smart_ptr<T>;
152  vsl_b_read(is, *p);
153  }
154  else
155  p = 0;
156 }
157 
158 template<class T>
159 void vsl_b_write(vsl_b_ostream& os, const vil_smart_ptr<T> *p)
160 {
161  if (p==0)
162  {
163  vsl_b_write(os, false); // Indicate null pointer stored
164  }
165  else
166  {
167  vsl_b_write(os,true); // Indicate non-null pointer stored
168  vsl_b_write(os,*p);
169  }
170 }
171 
172 template<class T>
173 void vsl_print_summary(std::ostream, const vil_smart_ptr<T> *p)
174 {
175  if (p==0)
176  os << "NULL PTR";
177  else
178  {
179  os << "vil_smart_ptr: ";
180  vsl_print_summary(*p);
181  }
182 }
183 #endif
184 
185 #undef VIL_IO_SMART_PTR_INSTANTIATE
186 #define VIL_IO_SMART_PTR_INSTANTIATE(T) \
187 template void vsl_print_summary(std::ostream &, const vil_smart_ptr<T > &); \
188 template void vsl_b_read(vsl_b_istream &, vil_smart_ptr<T > &); \
189 template void vsl_b_write(vsl_b_ostream &, const vil_smart_ptr<T > &)
190 
191 #endif // vil_io_smart_ptr_hxx_
A templated smart pointer class.
Definition: vil_fwd.h:16
Serialised binary IO functions for vil_smart_ptr<T>
void vsl_print_summary(std::ostream &os, const vil_image_view< T > &image)
Print human readable summary of a vil_image_view<T> object to a stream.
void vsl_b_write(vsl_b_ostream &os, const vil_image_view< T > &image)
Binary save vil_image_view<T> to stream.
Contains a templated smart pointer class.
void vsl_b_read(vsl_b_istream &is, vil_image_view< T > &image)
Binary load vil_image_view<T> from stream.
T * ptr() const
These methods all return the raw/dumb pointer.