Go to the documentation of this file.00001
00002 #ifndef mbl_file_data_collector_txx_
00003 #define mbl_file_data_collector_txx_
00004
00005
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
00019
00020
00021 template<class T>
00022 mbl_file_data_collector<T>::mbl_file_data_collector( const vcl_string & path )
00023
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
00047
00048
00049
00050
00051
00052 template<class T>
00053 mbl_file_data_collector<T>::mbl_file_data_collector(const mbl_data_collector_base& c)
00054 : bfs_( 0 ),
00055 wrapper_( 0 )
00056 {
00057 assert( c.is_class("mbl_file_data_collector<T>") );
00058 *this= dynamic_cast< const mbl_file_data_collector<T>& > ( c );
00059 }
00060
00061
00062 template<class T>
00063 mbl_file_data_collector<T>::mbl_file_data_collector(const mbl_file_data_collector & c)
00064 : bfs_( 0 ),
00065 wrapper_( 0 )
00066 {
00067
00068 assert( c.is_class( is_a() ) );
00069 *this = dynamic_cast< const mbl_file_data_collector<T>& > ( c );
00070 }
00071
00072
00073
00074
00075
00076
00077 template<class T>
00078 mbl_file_data_collector<T>& mbl_file_data_collector<T>::operator=( const mbl_file_data_collector & c)
00079 {
00080
00081
00082 const mbl_file_data_collector<T> & cref = ( c );
00083
00084
00085 delete_stuff();
00086
00087
00088 path_ = cref.path_;
00089 while ( vul_file::exists( path_ ) )
00090 {
00091 path_ = vul_file::strip_extension( path_ ) + vcl_string( "a" ) + vul_file::extension( path_ );
00092 }
00093
00094 delete( bfs_ );
00095 bfs_ = new vsl_b_ofstream( path_ );
00096
00097
00098 short vn= 1;
00099 vsl_b_write( *bfs_, vn );
00100
00101
00102
00103
00104
00105 vsl_b_write( *( cref.bfs_ ), true);
00106
00107
00108 ( *(cref.bfs_) ).os().flush();
00109
00110 mbl_file_data_wrapper<T> copy_wrapper( cref.path_ );
00111
00112 if ( copy_wrapper.size() > 0 )
00113 {
00114 copy_wrapper.reset();
00115 vsl_b_write( *bfs_, copy_wrapper.current() );
00116
00117 while( copy_wrapper.next() )
00118 {
00119 vsl_b_write( *bfs_, copy_wrapper.current() );
00120 }
00121 }
00122
00123
00124
00125
00126
00127
00128 return *this;
00129 }
00130
00131
00132
00133
00134
00135
00136
00137 template<class T>
00138 mbl_file_data_collector<T>::~mbl_file_data_collector()
00139 {
00140 delete_stuff();
00141
00142 vul_file::delete_file_glob( path_.c_str() );
00143
00144
00145
00146 }
00147
00148
00149 template<class T>
00150 void mbl_file_data_collector<T>::delete_stuff()
00151 {
00152 if ( bfs_ )
00153 {
00154 ( *bfs_ ).close();
00155 delete bfs_;
00156 bfs_ = 0;
00157 }
00158
00159 if ( wrapper_ )
00160 {
00161 delete wrapper_;
00162 wrapper_ = 0;
00163 }
00164 }
00165
00166
00167 template<class T>
00168 void mbl_file_data_collector<T>::clear()
00169 {
00170
00171 vcl_cout<<"mbl_file_data_collector<T>::clear - no action taken\n"
00172 <<"can't delete data file\n";
00173 }
00174
00175
00176 template<class T>
00177 void mbl_file_data_collector<T>::set_n_samples(int )
00178 {
00179 vcl_cerr << "mbl_file_data_collector::set_n_samples() is not useful\n";
00180 }
00181
00182
00183 template<class T>
00184 void mbl_file_data_collector<T>::record(const T& d)
00185 {
00186 if (!bfs_)
00187 {
00188 vcl_cerr<<"ERROR: mbl_file_data_collector::record()\n"
00189 <<"file stream failed\n";
00190 vcl_abort();
00191 }
00192 else
00193 {
00194 vsl_b_write( *bfs_, false );
00195 vsl_b_write( *bfs_, d );
00196 }
00197 }
00198
00199
00200 template<class T>
00201 mbl_data_wrapper<T >& mbl_file_data_collector<T>::data_wrapper()
00202 {
00203
00204 vsl_b_write( *bfs_, true);
00205
00206
00207 ( *bfs_ ).os().flush();
00208
00209 if (!wrapper_)
00210 wrapper_ = new mbl_file_data_wrapper<T>(path_) ;
00211
00212 wrapper_->reset();
00213
00214 return *wrapper_;
00215 }
00216
00217 template <class T>
00218 bool mbl_file_data_collector<T>::is_class(vcl_string const& s) const
00219 {
00220 return s==mbl_file_data_collector<T>::is_a() || mbl_data_collector<T>::is_class(s);
00221 }
00222
00223
00224
00225 template <class T>
00226 short mbl_file_data_collector<T>::version_no() const
00227 {
00228 return 1;
00229 }
00230
00231
00232
00233 template <class T>
00234 mbl_data_collector_base* mbl_file_data_collector<T>::clone() const
00235 {
00236
00237
00238
00239
00240
00241 return new mbl_file_data_collector<T>(*this);
00242
00243 }
00244
00245 template <class T>
00246 void mbl_file_data_collector<T>::print_summary(vcl_ostream& os) const
00247 {
00248 os<<"Data saved to: "<<path_<<'\n';
00249 }
00250
00251 template <class T>
00252 void mbl_file_data_collector<T>::b_write(vsl_b_ostream& ) const
00253 {
00254 vcl_cout<<"mbl_file_data_collector<T>::b_write - Can't save collector!\n";
00255 }
00256
00257 template <class T>
00258 void mbl_file_data_collector<T>::b_read(vsl_b_istream& )
00259 {
00260 vcl_cout<<"mbl_file_data_collector<T>::b_read - Can't load collector!\n";
00261 }
00262
00263
00264 #define MBL_FILE_DATA_COLLECTOR_INSTANTIATE(T) \
00265 VCL_DEFINE_SPECIALIZATION vcl_string mbl_file_data_collector<T >::is_a() const \
00266 { return vcl_string("mbl_file_data_collector<" #T ">"); } \
00267 template class mbl_file_data_collector<T >
00268
00269 #endif // mbl_file_data_collector_txx_