00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00020 #ifndef _TaskProgressManager_hpp
00021 #define _TaskProgressManager_hpp
00022 
00023 #include<imlib3dconfig.h>
00024 #ifdef    HAVE_LIBPTHREAD
00025 #include<pthread.h>
00026 #else  // HAVE_LIBPTHREAD
00027 typedef int pthread_t;
00028 typedef int pthread_mutex_t;
00029 #endif // HAVE_LIBPTHREAD
00030 
00031 #include<ImLib3D/CppTools.hpp>
00032 #include<algorithm>
00033 
00034 void ttprintf(const char *fmt, ...);
00035 
00036 
00037 class TaskProgressManager;
00038 extern TaskProgressManager *defaultTaskProgressManager;
00039 
00041 
00044 class TaskInProgress
00045 {
00046     friend class TaskProgressManager;
00047     static int idcount;
00048     int id;
00049 protected:
00050     TaskProgressManager *manager;
00051 
00052     double refTime;
00053     string name;
00054     double startTime;
00055     double lastReport;
00056     void AutoKill();
00057 public:
00058     virtual double Progress() const {return DTime(refTime)-startTime;}
00059     virtual string ProgressLabel() const {return SPrintf("%2.1fs",Progress());}
00060     virtual ~TaskInProgress(){AutoKill();}
00061     TaskInProgress(const string &_name,TaskProgressManager *_manager=defaultTaskProgressManager);
00062 };
00063 
00065 template<class TType>
00066 class ProgressTracker : public TaskInProgress
00067 {
00068     TType *progressAwareObj;
00069 public:
00070     virtual double Progress() const {return progressAwareObj->Progress();}
00071     virtual string ProgressLabel() const {return SPrintf("%2.1f%%",100*Progress());}
00072     ProgressTracker(const string &_name,TType *_progressAwareObj,TaskProgressManager *_manager=defaultTaskProgressManager):
00073         TaskInProgress(_name,_manager),
00074         progressAwareObj(_progressAwareObj)
00075     {       
00076     }
00077 };
00078 
00080 
00081 template<class TType>
00082 class TaskTracker : public TaskInProgress
00083 {
00084     TType startValue;
00085     TType endValue;
00086     TType *currentValue;
00087 public:
00088     virtual double Progress() const {return (*currentValue-startValue)/(float)(endValue-startValue);}
00089     virtual string ProgressLabel() const {return SPrintf("%2.1f%%",100*Progress());}
00090     
00091     TaskTracker(const string &_name,TType _startValue,TType _endValue,TType *_currentValue,TaskProgressManager *_manager=defaultTaskProgressManager):
00092         TaskInProgress(_name,_manager),
00093         startValue(_startValue),endValue(_endValue),currentValue(_currentValue)
00094     {
00095 
00096     }
00097 };
00098 
00100 
00101 class AlternateReportManager
00102 {
00103 public:
00104     virtual void Report(const vector<TaskInProgress *> &tasks)=0;
00105 };
00106 
00107 
00109 
00112 class TaskProgressManager
00113 {
00114     typedef TaskInProgress Task;
00115     pthread_t thread;
00116     pthread_mutex_t mutex;
00117     double refTime;
00118     double sampleDelay;
00119     double reportDelay;
00120     bool running;
00121 protected:
00122     int dirtyLine;
00123     vector<Task *> tasks;
00124     void Report(int taskNum);
00125     AlternateReportManager *alternateReportManager;
00126 public:
00127     void CleanLine();
00128 protected:
00129     void Lock();
00130     void UnLock();
00131     void CheckTasks();
00132 public:
00133     void NewTask(Task *task);
00134     void EndTask(const Task *task);
00135 protected:
00136     void Run();
00137     static void *EntryPoint(void *arg);
00138 private:
00139     bool reportTotalTime;
00140 public:
00141     void ReportTotalTime(bool _reportTotalTime=true){reportTotalTime=_reportTotalTime;}
00142     void SetAlternateReportManager(AlternateReportManager *_alternateReportManager)
00143     {alternateReportManager=_alternateReportManager;}
00144     void Start();
00145     void Stop();
00146     TaskProgressManager(double sampleDelay,double reportDelay);
00147     ~TaskProgressManager();
00148 };
00149 
00150 #endif // _TaskProgressManager_hpp