contrib/mul/mbl/mbl_file_data_wrapper.h
Go to the documentation of this file.
00001 // This is mul/mbl/mbl_file_data_wrapper.h
00002 #ifndef mbl_file_data_wrapper_h
00003 #define mbl_file_data_wrapper_h
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \author Tim Cootes
00010 // \brief A wrapper to provide access to sets of objects
00011 
00012 #include <vsl/vsl_binary_io.h>
00013 #include <mbl/mbl_data_wrapper.h>
00014 #include <vcl_string.h>
00015 
00016 //: Base class for objects which can return a set of objects, one at a time
00017 //  This is an iterator-like object.  However, unlike STL, generality is
00018 //  obtained by deriving from the class rather than providing a similar
00019 //  interface.
00020 //
00021 //  In particular one can query the object to find out how many examples
00022 //  to expect.
00023 //
00024 //  A typical use is to allow clients to build
00025 //  models from sets of data without having to hold all the data in memory
00026 //  at once.
00027 //
00028 //  Example:
00029 //  \code
00030 //  vnl_vector<double> my_sum(mbl_file_data_wrapper<vnl_vector<double> >& data)
00031 //  {
00032 //    data.reset();
00033 //    vnl_vector<double> sum = data.current();
00034 //    while (data.next())
00035 //      sum += data.current();
00036 //  }
00037 //  \endcode
00038 //
00039 //  Note: It should be fairly simple to provide an iterator type object
00040 //  which steps through the examples in a standard STL way by using this class.
00041 
00042 template<class T>
00043 class mbl_file_data_wrapper : public mbl_data_wrapper<T>
00044 {
00045   // file stream
00046   vsl_b_ifstream bfs_;
00047 
00048   // path to file holding all data
00049   vcl_string path_;
00050 
00051   // data
00052   T d_;
00053 
00054   // current index of data
00055   unsigned long index_;
00056 
00057   // size of data
00058     unsigned long size_;
00059 
00060  public:
00061 #if 0
00062   //: Default constructor
00063   mbl_file_data_wrapper();
00064 #endif
00065 
00066   // constructors
00067   mbl_file_data_wrapper(const vcl_string path);
00068 
00069   //: Default destructor
00070   virtual ~mbl_file_data_wrapper();
00071 
00072   //: count number of data items
00073   void calc_data_size();
00074 
00075   //: Number of objects available
00076   virtual unsigned long size() const;
00077 
00078   //: Reset so that current() returns first object
00079   virtual void reset();
00080 
00081   //: Return current object
00082   virtual const T& current();
00083 
00084   //: Move to next object, returning true if is valid
00085   virtual bool next();
00086 
00087   //: Return current index
00088   //  First example has index 0
00089   virtual unsigned long index() const;
00090 
00091 #if 0
00092   //: Move to element n
00093   //  First example has index 0
00094   virtual void set_index(unsigned long n);
00095 #endif
00096 
00097   //: Create copy on heap and return base pointer
00098   // This will create an independent iterator on the underlying data.
00099   // The original data is not copied.
00100   // Be careful of destruction of underlying data.
00101   virtual mbl_data_wrapper< T >* clone() const;
00102 
00103   //: Name of the class
00104   virtual vcl_string is_a() const;
00105 
00106   //: True if this is (or is derived from) class named s
00107   virtual bool is_class(vcl_string const& s) const;
00108 };
00109 
00110 #endif // mbl_file_data_wrapper_h