00001 #ifndef _ots_Utilities_ProgressBar_h_ 00002 #define _ots_Utilities_ProgressBar_h_ 00003 00004 #include "otsdaq-core/MessageFacility/MessageFacility.h" 00005 #include "otsdaq-core/Macros/CoutMacros.h" 00006 00007 #include <string> 00008 00009 namespace ots 00010 { 00011 00012 //ProgressBar 00013 //class by Ryan Rivera ( rrivera @ fnal.gov ), July 2013 00014 // 00015 // The are 4 public member function that should matter to the user: 00016 // 00017 // resetProgressBar(int id) ~~ 00018 // Resets progress bar to 0% complete, id does not have to be unique in your code, read NOTE 2 00019 // 00020 // step() ~~ 00021 // Marks that incremental progress has been made. user should place step() 00022 // freely throughout the code each time a jump in the progress bar is desired. 00023 // 00024 // complete() ~~ 00025 // Indicate the progress bar is to be 100% at this point 00026 // 00027 // read() ~~ 00028 // Returns the % complete 00029 // 00030 // USE: User places resetProgressBar(id) at the start of sequence of events, as many step()'s as desired 00031 // within the sequence, and complete() at the end of the sequence of events. 00032 // The first time the code is run ever the class determines the total number of steps, so the 00033 // progress % will be defined to be 0% with no steps complete, 50% with at least one step complete, 00034 // and 100% once complete. Each run thereafter will always use the number of steps 00035 // in the previous sequence to calculate the proper %. 00036 // 00037 // NOTE 1: Since the code uses the previous execution's number of steps to calculate the %, avoid 00038 // placing step() calls in code that is run conditionally. It is best practice to place them 00039 // at checkpoints that must always be reached. 00040 // 00041 // NOTE 2: The class uses the filename and line # of the resetProgressBar(id) call to store the baseline number 00042 // of steps for a given sequence. So if using a single resetProgressBar(id) call to govern more than one 00043 // sequence of events, you must assign an integer to the sequence you want to reset. Below is 00044 // an example for two sequences sharing a resetProgressBar(id): 00045 // 00046 // ProgressBar myProgressBar; 00047 // void setupProgress(int i) 00048 // { 00049 // myProgressBar.resetProgressBar(i); //this reset is shared by sequence one and 00050 // //two so must identify with integer 00051 // 00052 // if(i==0) sequenceOne(); 00053 // else if(i==1) sequenceTwo(); 00054 // myProgressBar.complete(); 00055 // } 00056 // 00057 // void sequenceOne() 00058 // { 00059 // //do things 00060 // myProgressBar.step(); 00061 // //do more things 00062 // myProgressBar.step(); 00063 // } 00064 // 00065 // void sequenceTwo() 00066 // { 00067 // //do other things 00068 // myProgressBar.step(); 00069 // } 00070 // 00071 00072 class ProgressBar 00073 { 00074 public: 00075 ProgressBar(); 00076 00077 //********************************************************************// 00078 //NOTE!!! don't call reset. Call resetProgressBar(id) as though 00079 // the function was this: 00080 //void resetProgressBar(int id) 00081 // 00082 // then the pre-compiler directive: 00083 #define resetProgressBar(x) reset(__FILE__,S_(__LINE__),x) 00084 //will call this reset: 00085 void reset(std::string file, std::string lineNumber, int id=0); 00086 //********************************************************************// 00087 00088 //remaining member functions are called normal 00089 void step(); 00090 void complete(); 00091 int read(); //if stepsToComplete==0, then define any progress as 50% 00092 std::string readPercentageString(); //if stepsToComplete==0, then define any progress as 50% 00093 00094 private: 00095 const std::string cProgressBarFilePath_; 00096 const std::string cProgressBarFileExtension_; 00097 std::string totalStepsFileName_; 00098 int stepCount_; 00099 int stepsToComplete_; 00100 bool started_; 00101 }; 00102 00103 00104 } 00105 00106 00107 #endif