contrib/mul/mbl/mbl_data_wrapper.h
Go to the documentation of this file.
00001 // This is mul/mbl/mbl_data_wrapper.h
00002 #ifndef mbl_data_wrapper_h
00003 #define mbl_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 //: Base class for objects which can return a set of objects, one at a time
00013 //  This is an iterator-like object.  However, unlike STL, generality is
00014 //  obtained by deriving from the class rather than providing a similar
00015 //  interface.
00016 //
00017 //  In particular one can query the object to find out how many examples
00018 //  to expect.
00019 //
00020 //  A typical use is to allow clients to build
00021 //  models from sets of data without having to hold all the data in memory
00022 //  at once.
00023 //
00024 //  Example:
00025 //  \code
00026 //  vnl_vector<double> my_sum(mbl_data_wrapper<vnl_vector<double> >& data)
00027 //  {
00028 //    data.reset();
00029 //    vnl_vector<double> sum = data.current();
00030 //    while (data.next())
00031 //      sum += data.current();
00032 //  }
00033 //  \endcode
00034 //
00035 //  Note: It should be fairly simple to provide an iterator type object
00036 //  which steps through the examples in a standard STL way by using this class.
00037 
00038 #include <vcl_string.h>
00039 
00040 template<class T>
00041 class mbl_data_wrapper
00042 {
00043  public:
00044   //: Default constructor
00045   mbl_data_wrapper();
00046 
00047   //: Default destructor
00048   virtual ~mbl_data_wrapper();
00049 
00050   //: Number of objects available
00051   virtual unsigned long size() const = 0;
00052 
00053   //: Reset so that current() returns first object
00054   virtual void reset() = 0;
00055 
00056   //: Return current object
00057   virtual const T& current() = 0;
00058 
00059   //: Move to next object, returning true if is valid
00060   virtual bool next() = 0;
00061 
00062   //: Return current index
00063   //  First example has index 0
00064   virtual unsigned long index() const =0;
00065 
00066   //: Move to element n
00067   //  First example has index 0
00068   virtual void set_index(unsigned long n);
00069 
00070   //: Create copy on heap and return base pointer
00071   // This will create an independent iterator on the underlying data.
00072   // The original data is not copied.
00073   // Be careful of destruction of underlying data.
00074   virtual mbl_data_wrapper< T >* clone() const = 0;
00075 
00076   //: Name of the class
00077   virtual vcl_string is_a() const =0;
00078 
00079   //: True if this is (or is derived from) class named s
00080   virtual bool is_class(vcl_string const& s) const =0;
00081 };
00082 
00083 #endif // mbl_data_wrapper_h