Go to the documentation of this file.00001
00002 #ifndef vbl_io_smart_ptr_txx_
00003 #define vbl_io_smart_ptr_txx_
00004
00005
00006
00007
00008
00009
00010 #include "vbl_io_smart_ptr.h"
00011 #include <vsl/vsl_binary_io.h>
00012 #include <vbl/vbl_smart_ptr.h>
00013 #include <vcl_cstdlib.h>
00014
00015
00016
00017 template<class T>
00018 void vsl_b_write(vsl_b_ostream & os, const vbl_smart_ptr<T> &p)
00019 {
00020
00021 const short io_version_no = 2;
00022 vsl_b_write(os, io_version_no);
00023 vsl_b_write(os, p.is_protected());
00024
00025 if (p.ptr() == 0)
00026 {
00027 vsl_b_write(os, true);
00028 vsl_b_write(os, 0ul);
00029
00030 return;
00031 }
00032
00033
00034 unsigned long id = os.get_serial_number(p.ptr());
00035
00036
00037 if (id == 0)
00038 {
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 if (!p.is_protected())
00051 {
00052 vcl_cerr << "vsl_b_write(vsl_b_ostream & os, const vbl_smart_ptr<T>&):"
00053 << " You cannot\nsave unprotected smart pointers before saving"
00054 << " a protected smart pointer\nto the same object. Either do"
00055 << " not save unprotected smart pointers, or\nbe very careful"
00056 << " about the order.\n";
00057 vcl_abort();
00058 }
00059
00060 id = os.add_serialisation_record(p.ptr());
00061
00062
00063
00064
00065
00066 vsl_b_write(os, true);
00067 vsl_b_write(os, id);
00068
00069
00070
00071 vsl_b_write(os, p.ptr());
00072
00073 }
00074 else
00075 {
00076
00077
00078
00079
00080
00081 vsl_b_write(os, false);
00082 vsl_b_write(os, id);
00083 }
00084 }
00085
00086
00087
00088 template<class T>
00089 void vsl_b_read(vsl_b_istream &is, vbl_smart_ptr<T> &p)
00090 {
00091 if (!is) return;
00092
00093 short ver;
00094 vsl_b_read(is, ver);
00095 switch (ver)
00096 {
00097 case 1:
00098 case 2:
00099 {
00100 bool is_protected;
00101
00102 vsl_b_read(is, is_protected);
00103
00104 bool first_time;
00105 vsl_b_read(is, first_time);
00106
00107 if (first_time && !is_protected)
00108 {
00109 vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vbl_smart_ptr<T>&)\n"
00110 << " De-serialisation failure of non-protected smart_ptr\n";
00111 is.is().clear(vcl_ios::badbit);
00112 return;
00113 }
00114 unsigned long id;
00115 vsl_b_read(is, id);
00116
00117 if (id == 0)
00118 {
00119 p = 0;
00120 return;
00121 }
00122
00123 T * pointer = static_cast<T *>( is.get_serialisation_pointer(id));
00124 if (first_time != (pointer == 0))
00125 {
00126
00127
00128
00129 vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vbl_smart_ptr<T>&)\n"
00130 << " De-serialisation failure\n";
00131 is.is().clear(vcl_ios::badbit);
00132 return;
00133 }
00134
00135 if (pointer == 0)
00136 {
00137
00138
00139
00140 vsl_b_read(is, pointer);
00141 is.add_serialisation_record(id, pointer);
00142 }
00143
00144 p = pointer;
00145
00146 if (!is_protected)
00147 p.unprotect();
00148
00149 break;
00150 }
00151 default:
00152 vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vbl_smart_ptr<T>&)\n"
00153 << " Unknown version number "<< ver << '\n';
00154 is.is().clear(vcl_ios::badbit);
00155 return;
00156 }
00157 }
00158
00159
00160
00161 template<class T>
00162 void vsl_print_summary(vcl_ostream & os,const vbl_smart_ptr<T> & p)
00163 {
00164 if (p.is_protected())
00165 os <<"Unprotected ";
00166 os << "Smart ptr to ";
00167 if (p.ptr())
00168 {
00169
00170
00171
00172 vsl_print_summary(os, p.ptr());
00173 }
00174 else
00175 os << "NULL";
00176 }
00177
00178
00179 #undef VBL_IO_SMART_PTR_INSTANTIATE
00180 #define VBL_IO_SMART_PTR_INSTANTIATE(T) \
00181 template void vsl_print_summary(vcl_ostream &, const vbl_smart_ptr<T > &); \
00182 template void vsl_b_read(vsl_b_istream &, vbl_smart_ptr<T > &); \
00183 template void vsl_b_write(vsl_b_ostream &, const vbl_smart_ptr<T > &)
00184
00185 #endif // vbl_io_smart_ptr_txx_