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 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
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
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
00076 template<class T>
00077 mbl_file_data_collector<T>& mbl_file_data_collector<T>::operator=( const mbl_file_data_collector & c)
00078 {
00079
00080 delete_stuff();
00081
00082
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
00093 short vn= 1;
00094 vsl_b_write( *bfs_, vn );
00095
00096 #if 0
00097
00098 const mbl_file_data_wrapper & test_copy_wrapper = c.data_wrapper();
00099 #endif
00100
00101
00102 vsl_b_write( *( c.bfs_ ), true);
00103
00104
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
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
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
00161 template<class T>
00162 void mbl_file_data_collector<T>::clear()
00163 {
00164
00165 vcl_cout<<"mbl_file_data_collector<T>::clear - no action taken\n"
00166 <<"can't delete data file\n";
00167 }
00168
00169
00170 template<class T>
00171 void mbl_file_data_collector<T>::set_n_samples(int )
00172 {
00173 vcl_cerr << "mbl_file_data_collector::set_n_samples() is not useful\n";
00174 }
00175
00176
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
00194 template<class T>
00195 mbl_data_wrapper<T >& mbl_file_data_collector<T>::data_wrapper()
00196 {
00197
00198 vsl_b_write( *bfs_, true);
00199
00200
00201 ( *bfs_ ).os().flush();
00202
00203 if (!wrapper_)
00204 wrapper_ = new mbl_file_data_wrapper<T>(path_) ;
00205
00206 wrapper_->reset();
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& ) 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& )
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_