00001
00002 #ifndef __Progress_h__
00003 #define __Progress_h__
00004
00005 #include <DAQ++/Thread.h>
00006 #include <unistd.h>
00007 #include <sys/time.h>
00008 #include <iosfwd>
00009
00010 namespace DAQpp
00011 {
00020 class Progress
00021 {
00022 private:
00024 unsigned int flg;
00026 double vmin;
00028 double vmax;
00030 double value;
00032 unsigned long usec;
00036 class PrgThread: public Thread
00037 {
00038 private:
00040 Progress *prg;
00041 public:
00043 PrgThread(Progress *p) : prg(p)
00044 {}
00045
00046 void run();
00047 };
00049 PrgThread thread;
00051 bool ss;
00052
00053
00055 double last_value;
00056
00058 double last_update;
00059
00062 const char *proc_file;
00063
00067 int read_file() const;
00068
00069
00070 float mm0;
00071
00072
00073 static int page_k_shift;
00074
00076 struct timeval _start;
00077
00079 bool active;
00080
00081 public:
00083 enum information
00084 {
00085 show_nothing = 0x000,
00086 show_status = 0x001,
00087 timing = 0x0f0,
00088 show_time = 0x010,
00089 show_average = 0x020,
00090 show_left = 0x040,
00091 memory = 0xf00,
00092 show_memory = 0x100
00093 };
00094
00101 Progress(double a = 0., double b = 0., int opt = show_status | show_average);
00102
00104 Progress(const Progress &p);
00105
00107 virtual ~Progress();
00108
00110 void start();
00111
00113 void stop();
00114
00120 virtual void show();
00121
00123 static void *_loop(void *);
00124
00126 void update(double x)
00127 {
00128 value = x;
00129 }
00130
00132 void reset(double a, double b);
00133
00135 double Vmin() const
00136 {
00137 return vmin;
00138 }
00139
00141 double Vmax() const
00142 {
00143 return vmax;
00144 }
00145
00147 double Value() const
00148 {
00149 return value;
00150 }
00151
00153 double Percent() const
00154 {
00155 return (value -vmin) / (vmax - vmin);
00156 }
00157
00159 void timer_start();
00161 void timer_stop();
00163 void timer_reset();
00165 double time() const;
00166
00168 double mem() const;
00169
00171 void set_update(unsigned long t)
00172 {
00173 usec = t;
00174 }
00175
00177 void set_options(unsigned int o)
00178 {
00179 flg = o;
00180 }
00181
00185 std::string str() const;
00186
00190 void update();
00191 };
00192
00193 inline void Progress::timer_start()
00194 {
00195 active = true;
00196 gettimeofday (&_start, NULL);
00197 }
00198 inline void Progress::timer_stop()
00199 {
00200 active = false;
00201 }
00202 inline void Progress::timer_reset()
00203 {
00204 gettimeofday (&_start, NULL);
00205 }
00206 inline double Progress::time() const
00207 {
00208 double total;
00209 struct timeval elapsed;
00210
00211 if ( active ) gettimeofday (&elapsed, NULL);
00212 if (_start.tv_usec > elapsed.tv_usec)
00213 {
00214 elapsed.tv_usec += 1000000;
00215 elapsed.tv_sec--;
00216 }
00217 elapsed.tv_usec -= _start.tv_usec;
00218 elapsed.tv_sec -= _start.tv_sec;
00219
00220 total = elapsed.tv_sec + ((double) elapsed.tv_usec) / 1.e6;
00221 return total;
00222 }
00223 }
00229 class progress
00230 {
00231 private:
00233 const DAQpp::Progress &p;
00234 public:
00236 progress(const DAQpp::Progress &prg) : p(prg)
00237 {}
00239 std::ostream &operator()(std::ostream &os) const;
00240 };
00241 std::ostream &operator<<(std::ostream &os, progress const &p)
00242 {
00243 return p(os);
00244 }
00245
00246 #endif