Main Lemma Repository
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ProgressBar.h 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #ifndef PROGRESS_BAR_H
  2. #define PROGRESS_BAR_H
  3. #include <string>
  4. /**
  5. * RAII implementation of a progress bar.
  6. * available from https://stackoverflow.com/questions/46717885/make-boostprogress-display-work-when-more-information-is-being-printed-to-the
  7. */
  8. class ProgressBar
  9. {
  10. public:
  11. /**
  12. * Constructor.
  13. * It takes two values: the expected number of iterations whose progress we
  14. * want to monitor and an initial message to be displayed on top of the bar
  15. * (which can be updated with updateLastPrintedMessage()).
  16. */
  17. ProgressBar(
  18. uint32_t expectedIterations, const std::string& initialMessage="");
  19. /**
  20. * Destructor to guarantee RAII.
  21. */
  22. ~ProgressBar();
  23. // Make the object non-copyable
  24. ProgressBar(const ProgressBar& o) = delete;
  25. ProgressBar& operator=(const ProgressBar& o) = delete;
  26. /**
  27. * Must be invoked when the progress bar is no longer needed to restore the
  28. * position of the cursor to the end of the output.
  29. * It is automatically invoked when the object is destroyed.
  30. */
  31. void endProgressBar();
  32. /**
  33. * Prints a new message under the last printed message, without overwriting
  34. * it. This moves the progress bar down to be placed under the newly
  35. * written message.
  36. */
  37. void printNewMessage(const std::string& message);
  38. /**
  39. * Prints a message while the progress bar is on the screen on top on the
  40. * last printed message. Since the cursor is right at the beginning of the
  41. * progress bar, it moves the cursor up by one line before printing, and
  42. * then returns it to its original position.
  43. */
  44. void updateLastPrintedMessage(const std::string& message);
  45. /**
  46. * Overloaded prefix operator, used to indicate that the has been a new
  47. * iteration.
  48. */
  49. void operator++();
  50. private:
  51. unsigned int mTotalIterations;
  52. unsigned int mNumberOfTicks;
  53. bool mEnded;
  54. size_t mLengthOfLastPrintedMessage;
  55. };
  56. #endif /* PROGRESS_BAR_H */