contrib/mul/mbl/mbl_progress.h
Go to the documentation of this file.
00001 #ifndef mbl_progress_h_
00002 #define mbl_progress_h_
00003 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00004 #pragma interface
00005 #endif
00006 //:
00007 // \file
00008 // \brief  A base for classes which wish to take some action during a lengthy operation.
00009 // \author Graham Vincent and Kevin de Souza
00010 // \date 25 Feb 2005
00011 
00012 #include <vcl_string.h>
00013 #include <vcl_map.h>
00014 
00015 //========================================================================
00016 //: An exception that can be thrown by an operation when cancelled.
00017 class mbl_progress_cancel_exception
00018 {
00019  public:
00020   mbl_progress_cancel_exception() {}
00021   ~mbl_progress_cancel_exception() {}
00022 };
00023 
00024 
00025 //========================================================================
00026 //: An API for reporting progress.
00027 //
00028 // An mbl_progress object can be used to keep track of multiple operations.
00029 // Each one is identified by a unique string.
00030 //
00031 // A function implementing an algorithm may accept a mbl_progress object.
00032 // Before entering a processing loop, the function passes the progress
00033 // object an identifying string together with an estimate of how many
00034 // iterations the loop is expected to take. During the subsequent
00035 // processing, the process informs the progress object when each iteration
00036 // has completed. It is up to the derived classes to act on this information.
00037 //
00038 // For example: derive a class containing a graphical progress bar,
00039 // which is updated when a particular identifier indicates that an
00040 // iteration has completed.
00041 //
00042 // The algorithm may also query the progress object to check if it has
00043 // been cancelled (e.g. by a user pressing a cancel button in a
00044 // derived GUI class) and take appropriate action.
00045 //
00046 // Algorithms could have the identifier associated with various tasks in
00047 // their public interface.
00048 class mbl_progress
00049 {
00050  public:
00051 
00052   //: Constructor
00053   mbl_progress(): throw_exception_on_cancel_(false) {}
00054 
00055   //: Destructor
00056   virtual ~mbl_progress() {}
00057 
00058   //: Estimated number of iterations for the given identifier.
00059   void set_estimated_iterations(const vcl_string& identifier,
00060                                 const int iterations,
00061                                 const vcl_string& display_text);
00062 
00063   //: If true set_progress() will throw an exception if cancel has been set
00064   void set_throw_exception_on_cancel(bool t)
00065     { throw_exception_on_cancel_=t; }
00066 
00067   //: Sets progress for the given identifier.
00068   // Checks whether cancel has been set; if so calls end_progress(),
00069   // and throws an exception if "throw_exception_on_cancel" is true.
00070   void set_progress(const vcl_string& identifier,
00071                     const int progress);
00072 
00073   //: Increments progress for the given identifier by n.
00074   void increment_progress(const vcl_string& identifier,
00075                           const int n=1);
00076 
00077   //: Explicitly marks the end of loop for the given identifier.
00078   void end_progress(const vcl_string& identifier);
00079 
00080   //: Gets progress for given identifier.
00081   //  \param identifier to query.
00082   //  \return progress (-i if identifier is not known by this object).
00083   int progress(const vcl_string& identifier) const;
00084 
00085   //: Gets display text for given identifier.
00086   //  \param identifier to query.
00087   vcl_string display_text(const vcl_string& identifier) const;
00088 
00089   //: Gets estimated total iterations for given identifier.
00090   //  \param identifier to query.
00091   //  \return progress (-1 if identifier is not known by this object).
00092   int estimated_iterations(const vcl_string& identifier) const;
00093 
00094   //: Modify the flag to cancel the current process.
00095   //  \param identifier Progress object to cancel.
00096   void set_cancelled(const vcl_string& identifier,
00097                      const bool cancel);
00098 
00099   //: Check whether progress object is marked as cancelled.
00100   //  \param identifier Progress object to check.
00101   //  \return True if a cancel flag is set for this progress object.
00102   bool is_cancelled(const vcl_string& identifier) const;
00103 
00104 
00105  protected:
00106 
00107   //: Called when set_estimated_iterations() is called for a given identifier.
00108   //  Derived classes may take some action here.
00109   //  \param identifier The operation being monitored.
00110   virtual void on_set_estimated_iterations(const vcl_string& identifier,
00111                                            const int total_iterations) = 0;
00112 
00113   //: Called when set_progress() is called for a given identifier.
00114   //  Derived classes may take some action here.
00115   //  \param identifier The operation being monitored.
00116   //  \param progress The new progress status.
00117   virtual void on_set_progress(const vcl_string& identifier,
00118                                const int progress) = 0;
00119 
00120   //: Called when end_progress() is called for a given identifier.
00121   //  Derived classes may take some action here.
00122   //  \param identifier The operation being monitored.
00123   virtual void on_end_progress(const vcl_string& identifier) = 0;
00124 
00125  private:
00126 
00127   //: Stores display text for each identifier
00128   vcl_map<vcl_string, vcl_string> identifier2displaytext_;
00129 
00130   //: Stores estimated iterations for each identifier
00131   vcl_map<vcl_string, int> identifier2estimatediterations_;
00132 
00133   //: Stores current progress for each identifier
00134   vcl_map<vcl_string, int> identifier2progress_;
00135 
00136   //: Flags to indicate whether a request to cancel has been registered for each identifier
00137   vcl_map<vcl_string, bool> identifier2cancel_;
00138 
00139   //: If true set_progress() will throw an exception if cancel has been set.
00140   bool throw_exception_on_cancel_;
00141 };
00142 
00143 //========================================================================
00144 
00145 #endif // mbl_progress_h_