contrib/mul/mbl/mbl_file_data_collector.txx
Go to the documentation of this file.
00001 // This is mul/mbl/mbl_file_data_collector.txx
00002 #ifndef mbl_file_data_collector_txx_
00003 #define mbl_file_data_collector_txx_
00004 //:
00005 // \file
00006 
00007 #include "mbl_file_data_collector.h"
00008 
00009 #include <vcl_cstdlib.h>
00010 #include <vcl_cassert.h>
00011 
00012 #include <vsl/vsl_binary_loader.h>
00013 #include <vsl/vsl_vector_io.h>
00014 
00015 #include <vul/vul_file.h>
00016 
00017 //=======================================================================
00018 // Constructor
00019 //=======================================================================
00020 
00021 template<class T>
00022 mbl_file_data_collector<T>::mbl_file_data_collector( const vcl_string & path )
00023 //: bfs_( path ),
00024 : bfs_( 0 ),
00025   wrapper_( 0 )
00026 {
00027   path_ = path;
00028   while ( vul_file::exists( path_ ) )
00029   {
00030     path_ = vul_file::strip_extension( path_ ) + vcl_string( "a" ) + vul_file::extension( path_ );
00031   }
00032 
00033   short vn= 1;
00034 
00035   bfs_ = new vsl_b_ofstream( path_ );
00036   if ( !( *bfs_ ) )
00037   {
00038     vcl_cerr<<"ERROR: mbl_file_data_collector::constructor\n"
00039             <<"file stream failed\n";
00040     vcl_abort();
00041   }
00042 
00043   vsl_b_write( *bfs_, vn );
00044 }
00045 
00046 //: Constructor from mbl_data_collector_base
00047 template<class T>
00048 mbl_file_data_collector<T>::mbl_file_data_collector(const mbl_data_collector_base& c)
00049 : bfs_( 0 ),
00050   wrapper_( 0 )
00051 {
00052   assert( c.is_class("mbl_file_data_collector<T>") );
00053   *this= dynamic_cast< const mbl_file_data_collector<T>& > ( c );
00054 }
00055 
00056 //: Copy constructor
00057 template<class T>
00058 mbl_file_data_collector<T>::mbl_file_data_collector(const mbl_file_data_collector & c)
00059 : bfs_( 0 ),
00060   wrapper_( 0 )
00061 {
00062   assert( c.is_class( is_a() ) );
00063   *this = dynamic_cast< const mbl_file_data_collector<T>& > ( c );
00064 }
00065 
00066 #if 0 // not yet fully tested
00067 //: Assignment operator from mbl_data_collector_base
00068 template<class T>
00069 mbl_file_data_collector<T>& mbl_file_data_collector<T>::operator=( const mbl_data_collector_base& c) {...}
00070   assert( c.is_class( is_a() ) );
00071   const mbl_file_data_collector<T> & cref = dynamic_cast< const mbl_file_data_collector<T>& > ( c );
00072   return operator=(cref);
00073 #endif
00074 
00075 //: Assignment operator
00076 template<class T>
00077 mbl_file_data_collector<T>& mbl_file_data_collector<T>::operator=( const mbl_file_data_collector & c)
00078 {
00079   // I think I just need to clone the wrapper
00080   delete_stuff();
00081 
00082   // set up a new filename for each new set
00083   path_ = c.path_;
00084   while ( vul_file::exists( path_ ) )
00085   {
00086     path_ = vul_file::strip_extension( path_ ) + vcl_string( "a" ) + vul_file::extension( path_ );
00087   }
00088 
00089   delete bfs_;
00090   bfs_ = new vsl_b_ofstream( path_ );
00091 
00092   // need to file the new file with the version number...
00093   short vn= 1;
00094   vsl_b_write( *bfs_, vn );
00095 
00096 #if 0
00097   // ...and then the data
00098   const mbl_file_data_wrapper & test_copy_wrapper = c.data_wrapper();
00099 #endif
00100 
00101   // have to say (like Jim Morrison) "this is the end" - you can tell Dave wrote this!
00102   vsl_b_write( *( c.bfs_ ), true);
00103 
00104   // flush the file (to make sure it exists on disk - i.e. override buffering)
00105   ( *(c.bfs_) ).os().flush();
00106 
00107   mbl_file_data_wrapper<T> copy_wrapper( c.path_ );
00108 
00109   if ( copy_wrapper.size() > 0 )
00110   {
00111     copy_wrapper.reset();
00112     vsl_b_write( *bfs_, copy_wrapper.current() );
00113 
00114     while ( copy_wrapper.next() )
00115     {
00116       vsl_b_write( *bfs_, copy_wrapper.current() );
00117     }
00118   }
00119 #if 0
00120   bfs_= c.bfs_;
00121   path_ = c.path_;
00122   wrapper_ = c.wrapper_->clone();
00123 #endif
00124   return *this;
00125 }
00126 
00127 //=======================================================================
00128 // Destructor
00129 //=======================================================================
00130 
00131 template<class T>
00132 mbl_file_data_collector<T>::~mbl_file_data_collector()
00133 {
00134   delete_stuff();
00135   vul_file::delete_file_glob( path_.c_str() );
00136 #if 0
00137   bfs_.close();
00138   delete wrapper_;
00139 #endif
00140 }
00141 
00142 //: Delete stuff
00143 template<class T>
00144 void mbl_file_data_collector<T>::delete_stuff()
00145 {
00146   if ( bfs_ )
00147   {
00148     ( *bfs_ ).close();
00149     delete bfs_;
00150     bfs_ = 0;
00151   }
00152 
00153   if ( wrapper_ )
00154   {
00155     delete wrapper_;
00156     wrapper_ = 0;
00157   }
00158 }
00159 
00160 //: Clear any stored data
00161 template<class T>
00162 void mbl_file_data_collector<T>::clear()
00163 {
00164   // can't clear (need to wipe data file to do a proper clear ???)
00165   vcl_cout<<"mbl_file_data_collector<T>::clear - no action taken\n"
00166           <<"can't delete data file\n";
00167 }
00168 
00169 //: Hint about how many examples to expect
00170 template<class T>
00171 void mbl_file_data_collector<T>::set_n_samples(int /*n*/)
00172 {
00173   vcl_cerr << "mbl_file_data_collector::set_n_samples() is not useful\n";
00174 }
00175 
00176 //: Record given object
00177 template<class T>
00178 void mbl_file_data_collector<T>::record(const T& d)
00179 {
00180   if (!bfs_)
00181   {
00182     vcl_cerr<<"ERROR: mbl_file_data_collector::record()\n"
00183             <<"file stream failed\n";
00184     vcl_abort();
00185   }
00186   else
00187   {
00188     vsl_b_write( *bfs_, false );
00189     vsl_b_write( *bfs_, d );
00190   }
00191 }
00192 
00193 //: Return object describing the stored data
00194 template<class T>
00195 mbl_data_wrapper<T >& mbl_file_data_collector<T>::data_wrapper()
00196 {
00197   // have to say (like Jim Morrison) "this is the end"
00198   vsl_b_write( *bfs_, true);
00199 
00200   // flush the file (to make sure it exists on disk - i.e. override buffering)
00201   ( *bfs_ ).os().flush();
00202 
00203   if (!wrapper_)
00204     wrapper_ = new mbl_file_data_wrapper<T>(path_) ;
00205 
00206   wrapper_->reset();  // make sure points to start!
00207 
00208   return *wrapper_;
00209 }
00210 
00211 template <class T>
00212 bool mbl_file_data_collector<T>::is_class(vcl_string const& s) const
00213 {
00214   return s==mbl_file_data_collector<T>::is_a() || mbl_data_collector<T>::is_class(s);
00215 }
00216 
00217 //=======================================================================
00218 
00219 template <class T>
00220 short mbl_file_data_collector<T>::version_no() const
00221 {
00222   return 1;
00223 }
00224 
00225 //=======================================================================
00226 
00227 template <class T>
00228 mbl_data_collector_base* mbl_file_data_collector<T>::clone() const
00229 {
00230   return new mbl_file_data_collector<T>(*this);
00231 }
00232 
00233 template <class T>
00234 void mbl_file_data_collector<T>::print_summary(vcl_ostream& os) const
00235 {
00236   os<<"Data saved to: "<<path_<<'\n';
00237 }
00238 
00239 template <class T>
00240 void mbl_file_data_collector<T>::b_write(vsl_b_ostream& /*bfs*/) const
00241 {
00242   vcl_cout<<"mbl_file_data_collector<T>::b_write - Can't save collector!\n";
00243 }
00244 
00245 template <class T>
00246 void mbl_file_data_collector<T>::b_read(vsl_b_istream& /*bfs*/)
00247 {
00248   vcl_cout<<"mbl_file_data_collector<T>::b_read - Can't load collector!\n";
00249 }
00250 
00251 
00252 #define MBL_FILE_DATA_COLLECTOR_INSTANTIATE(T) \
00253 VCL_DEFINE_SPECIALIZATION vcl_string mbl_file_data_collector<T >::is_a() const \
00254 { return vcl_string("mbl_file_data_collector<" #T ">"); } \
00255 template class mbl_file_data_collector<T >
00256 
00257 #endif // mbl_file_data_collector_txx_