contrib/mul/mbl/mbl_progress.cxx
Go to the documentation of this file.
00001 #include "mbl_progress.h"
00002 // This is mul/mbl/mbl_progress.cxx
00003 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00004 #pragma implementation
00005 #endif
00006 
00007 #include <vcl_config_compiler.h>
00008 
00009 
00010 //=======================================================================
00011 void mbl_progress::set_estimated_iterations(const vcl_string& identifier,
00012                                             const int iterations,
00013                                             const vcl_string& display_text)
00014 {
00015   identifier2estimatediterations_[identifier] = iterations;
00016   identifier2progress_[identifier] = 0;
00017   identifier2cancel_[identifier] = false;
00018   identifier2displaytext_[identifier] = display_text;
00019   on_set_estimated_iterations(identifier, iterations);
00020 }
00021 
00022 
00023 //=======================================================================
00024 void mbl_progress::set_progress(const vcl_string& identifier,
00025                                 const int progress)
00026 {
00027   if (is_cancelled(identifier))
00028   {
00029     end_progress(identifier);
00030 #if VCL_HAS_EXCEPTIONS
00031     if (throw_exception_on_cancel_)
00032     {
00033       throw mbl_progress_cancel_exception();
00034     }
00035 #endif
00036   }
00037 
00038   vcl_map<vcl_string, int>::iterator it = identifier2progress_.find(identifier);
00039   if (it != identifier2progress_.end())
00040   {
00041     it->second = progress;
00042     on_set_progress(identifier, progress);
00043   }
00044 }
00045 
00046 
00047 //=======================================================================
00048 void mbl_progress::increment_progress(const vcl_string& identifier,
00049                                       const int n)
00050 {
00051   vcl_map<vcl_string, int>::const_iterator it =
00052     identifier2progress_.find(identifier);
00053   if (it != identifier2progress_.end())
00054   {
00055     int progress = it->second + n;
00056     set_progress(identifier, progress);
00057   }
00058 }
00059 
00060 
00061 //=======================================================================
00062 void mbl_progress::end_progress(const vcl_string& identifier)
00063 {
00064   on_end_progress(identifier);
00065 }
00066 
00067 
00068 //=======================================================================
00069 int mbl_progress::progress(const vcl_string& identifier) const
00070 {
00071   int p = -1;
00072   vcl_map<vcl_string, int>::const_iterator it =
00073     identifier2progress_.find(identifier);
00074   if (it != identifier2progress_.end())
00075   {
00076     p = it->second;
00077   }
00078   return p;
00079 }
00080 
00081 
00082 //=======================================================================
00083 vcl_string mbl_progress::display_text(const vcl_string& identifier) const
00084 {
00085   vcl_string dt;
00086   vcl_map<vcl_string, vcl_string>::const_iterator it =
00087     identifier2displaytext_.find(identifier);
00088   if (it != identifier2displaytext_.end())
00089   {
00090     dt = it->second;
00091   }
00092   return dt;
00093 }
00094 
00095 
00096 //=======================================================================
00097 int mbl_progress::estimated_iterations(const vcl_string& identifier) const
00098 {
00099   int n = -1;
00100   vcl_map<vcl_string, int>::const_iterator it =
00101     identifier2estimatediterations_.find(identifier);
00102   if (it != identifier2estimatediterations_.end())
00103   {
00104     n = it->second;
00105   }
00106   return n;
00107 }
00108 
00109 
00110 //=======================================================================
00111 // Modify the flag to cancel the current process.
00112 // identifier: Progress object to cancel.
00113 //=======================================================================
00114 void mbl_progress::set_cancelled(const vcl_string& identifier,
00115                                  const bool cancel)
00116 {
00117   vcl_map<vcl_string, bool>::iterator it = identifier2cancel_.find(identifier);
00118   if (it != identifier2cancel_.end())
00119   {
00120     it->second = cancel;
00121   }
00122 }
00123 
00124 
00125 //=======================================================================
00126 // Check whether a cancel request has been registered.
00127 // identifier:  Progress object to check for a cancel request.
00128 // Returns True if a cancel request has been received for this progress object.
00129 //=======================================================================
00130 bool mbl_progress::is_cancelled(const vcl_string &identifier) const
00131 {
00132   bool retval = false;
00133   vcl_map<vcl_string, bool>::const_iterator it = identifier2cancel_.find(identifier);
00134   if (it != identifier2cancel_.end())
00135   {
00136     retval = it->second;
00137   }
00138   return retval;
00139 }
00140