otsdaq  v2_00_00
ProgressBar.h
1 #ifndef _ots_Utilities_ProgressBar_h_
2 #define _ots_Utilities_ProgressBar_h_
3 
4 #include "otsdaq-core/MessageFacility/MessageFacility.h"
5 #include "otsdaq-core/Macros/CoutHeaderMacros.h"
6 
7 #include <string>
8 
9 namespace ots
10 {
11 
12 //ProgressBar
13 //class by Ryan Rivera ( rrivera @ fnal.gov ), July 2013
14 //
15 // The are 4 public member function that should matter to the user:
16 //
17 // resetProgressBar(int id) ~~
18 // Resets progress bar to 0% complete, id does not have to be unique in your code, read NOTE 2
19 //
20 // step() ~~
21 // Marks that incremental progress has been made. user should place step()
22 // freely throughout the code each time a jump in the progress bar is desired.
23 //
24 // complete() ~~
25 // Indicate the progress bar is to be 100% at this point
26 //
27 // read() ~~
28 // Returns the % complete
29 //
30 // USE: User places resetProgressBar(id) at the start of sequence of events, as many step()'s as desired
31 // within the sequence, and complete() at the end of the sequence of events.
32 // The first time the code is run ever the class determines the total number of steps, so the
33 // progress % will be defined to be 0% with no steps complete, 50% with at least one step complete,
34 // and 100% once complete. Each run thereafter will always use the number of steps
35 // in the previous sequence to calculate the proper %.
36 //
37 // NOTE 1: Since the code uses the previous execution's number of steps to calculate the %, avoid
38 // placing step() calls in code that is run conditionally. It is best practice to place them
39 // at checkpoints that must always be reached.
40 //
41 // NOTE 2: The class uses the filename and line # of the resetProgressBar(id) call to store the baseline number
42 // of steps for a given sequence. So if using a single resetProgressBar(id) call to govern more than one
43 // sequence of events, you must assign an integer to the sequence you want to reset. Below is
44 // an example for two sequences sharing a resetProgressBar(id):
45 //
46 // ProgressBar myProgressBar;
47 // void setupProgress(int i)
48 // {
49 // myProgressBar.resetProgressBar(i); //this reset is shared by sequence one and
50 // //two so must identify with integer
51 //
52 // if(i==0) sequenceOne();
53 // else if(i==1) sequenceTwo();
54 // myProgressBar.complete();
55 // }
56 //
57 // void sequenceOne()
58 // {
59 // //do things
60 // myProgressBar.step();
61 // //do more things
62 // myProgressBar.step();
63 // }
64 //
65 // void sequenceTwo()
66 // {
67 // //do other things
68 // myProgressBar.step();
69 // }
70 //
71 
73 {
74 public:
75  ProgressBar();
76 
77  //********************************************************************//
78  //NOTE!!! don't call reset. Call resetProgressBar(id) as though
79  // the function was this:
80  //void resetProgressBar(int id)
81  //
82  // then the pre-compiler directive:
83  #define resetProgressBar(x) reset(__FILE__,S_(__LINE__),x)
84  //will call this reset:
85  void reset(std::string file, std::string lineNumber, int id=0);
86  //********************************************************************//
87 
88  //remaining member functions are called normal
89  void step();
90  void complete();
91  int read(); //if stepsToComplete==0, then define any progress as 50%
92  std::string readPercentageString(); //if stepsToComplete==0, then define any progress as 50%
93 
94 private:
95  const std::string cProgressBarFilePath_;
96  const std::string cProgressBarFileExtension_;
97  std::string totalStepsFileName_;
98  int stepCount_;
99  int stepsToComplete_;
100  bool started_;
101 };
102 
103 
104 }
105 
106 
107 #endif