core/vsl/vsl_clipon_binary_loader.txx
Go to the documentation of this file.
00001 // This is core/vsl/vsl_clipon_binary_loader.txx
00002 #ifndef vsl_clipon_binary_loader_txx_
00003 #define vsl_clipon_binary_loader_txx_
00004 //:
00005 // \file
00006 
00007 #include "vsl_clipon_binary_loader.h"
00008 #include <vcl_cstdlib.h> // vcl_abort()
00009 #include <vcl_vector.txx>
00010 
00011 template<class BaseClass, class BaseClassIO>
00012 vsl_clipon_binary_loader<BaseClass,BaseClassIO>& vsl_clipon_binary_loader<BaseClass,BaseClassIO>::instance()
00013 {
00014   if (instance_ == 0)
00015   {
00016     instance_ = new vsl_clipon_binary_loader<BaseClass,BaseClassIO>;
00017 
00018     // Register for deletion by vsl_delete_all_loaders()
00019     instance_->register_this();
00020   }
00021   return *instance_;
00022 }
00023 
00024 //: Add example object to list of those that can be loaded
00025 template<class BaseClass, class BaseClassIO>
00026 void vsl_clipon_binary_loader<BaseClass,BaseClassIO>::add( const BaseClassIO& b)
00027 {
00028   object_io_.push_back(b.clone());
00029 }
00030 
00031 //: Return index associated with given object name
00032 template<class BaseClass, class BaseClassIO>
00033 int vsl_clipon_binary_loader<BaseClass,BaseClassIO>::index_for_name(const vcl_string& name) const
00034 {
00035   unsigned int i=0;
00036   while ((i<object_io_.size()) && !(object_io_[i]->target_classname()==name))
00037     ++i;
00038 
00039   if (i>=object_io_.size())
00040   {
00041     vcl_cerr << "vsl_clipon_binary_loader<BaseClass>::index_for_name: "
00042              << "class name <" << name << "> not in list of loaders\n"
00043              << object_io_.size()<<" valid loaders available for\n";
00044     for (unsigned int j=0; j<object_io_.size(); ++j)
00045       vcl_cerr << object_io_[j]->target_classname() << vcl_endl;
00046     vcl_abort();
00047   }
00048 
00049   return i;
00050 }
00051 
00052 //: Return IO object that can deal with given class
00053 template<class BaseClass, class BaseClassIO>
00054 const BaseClassIO& vsl_clipon_binary_loader<BaseClass,BaseClassIO>::io_for_class(const BaseClass& b) const
00055 {
00056   unsigned int i;
00057   for (i=0; (i<object_io_.size()) && !(object_io_[i]->is_io_for(b)); ++i)
00058     /*nothing*/;
00059 
00060   if (i>=object_io_.size())
00061   {
00062     vcl_cerr << "vsl_clipon_binary_loader<BaseClass>::io_for_class: "
00063              << "Unable to determine suitable loader.\n"
00064              << object_io_.size()<<" valid loaders available for\n";
00065     for (unsigned int j=0; j<object_io_.size(); ++j)
00066       vcl_cerr << object_io_[j]->target_classname() << vcl_endl;
00067     vcl_abort();
00068   }
00069 
00070   return *object_io_[i];
00071 }
00072 
00073 //: Return IO object for given named class
00074 template<class BaseClass, class BaseClassIO>
00075 const BaseClassIO& vsl_clipon_binary_loader<BaseClass,BaseClassIO>::object_io(const vcl_string& name) const
00076 {
00077   return *object_io_[index_for_name(name)];
00078 }
00079 
00080 
00081 template<class BaseClass, class BaseClassIO>
00082 void vsl_clipon_binary_loader<BaseClass,BaseClassIO>::make_empty()
00083 {
00084   for (unsigned int i=0; i<object_io_.size(); ++i)
00085   delete object_io_[i];
00086   object_io_.resize(0);
00087 }
00088 
00089 template<class BaseClass, class BaseClassIO>
00090 vsl_clipon_binary_loader<BaseClass,BaseClassIO>::~vsl_clipon_binary_loader()
00091 {
00092   make_empty();
00093 }
00094 
00095 // IO for  pointers to BaseClass:
00096 template<class BaseClass, class BaseClassIO>
00097 void vsl_clipon_binary_loader<BaseClass,BaseClassIO>::read_object( vsl_b_istream& is, BaseClass*& b)
00098 {
00099   // Delete old object pointed to by b
00100   delete b;
00101 
00102   vcl_string name;
00103   vsl_b_read(is,name);
00104 
00105   if (name=="VSL_NULL_PTR")
00106   {
00107     // Zero pointer
00108     b=0;
00109     return;
00110   }
00111 
00112   const BaseClassIO& io = object_io(name);
00113   b = io.new_object();
00114   io.b_read_by_base(is,*b);
00115 }
00116 
00117 // IO for  pointers to BaseClass:
00118 template<class BaseClass, class BaseClassIO>
00119 void vsl_clipon_binary_loader<BaseClass,BaseClassIO>::write_object( vsl_b_ostream& os, const BaseClass* b)
00120 {
00121   if (b==0)
00122   {
00123     vsl_b_write(os,vcl_string("VSL_NULL_PTR"));
00124     return;
00125   }
00126 
00127   const BaseClassIO& io = io_for_class(*b);
00128   vsl_b_write(os,io.target_classname());
00129   io.b_write_by_base(os,*b);
00130 }
00131 
00132 // IO for  pointers to BaseClass:
00133 template<class BaseClass, class BaseClassIO>
00134 void vsl_clipon_binary_loader<BaseClass,BaseClassIO>::print_object_summary( vcl_ostream& os, const BaseClass* b)
00135 {
00136   if (b==0)
00137   {
00138     os<<"No object defined.";
00139     return;
00140   }
00141 
00142   const BaseClassIO& io = io_for_class(*b);
00143   io.print_summary_by_base(os,*b);
00144 }
00145 
00146 template <class B, class IO>
00147 vsl_clipon_binary_loader<B, IO>* vsl_clipon_binary_loader<B, IO>::instance_ = 0;
00148 
00149 #define VSL_CLIPON_BINARY_LOADER_INSTANTIATE(B,IO) \
00150 template class vsl_clipon_binary_loader<B, IO >; \
00151 VCL_VECTOR_INSTANTIATE(IO*)
00152 
00153 #endif // vsl_clipon_binary_loader_txx_