LoadProgress.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * LoadProgress.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "StdInc.h"
  12. #include <atomic>
  13. namespace Load
  14. {
  15. using Type = unsigned char;
  16. /*
  17. * Purpose of that class is to track progress of computations
  18. * Derive from this class if you want to translate user or system
  19. * remaining amount of work needed.
  20. * Tracking of the progress should be from another thread.
  21. */
  22. class DLL_LINKAGE Progress
  23. {
  24. public:
  25. //Sets current state to 0.
  26. //Amount of steps to finish progress will be equal to 100
  27. Progress();
  28. virtual ~Progress() = default;
  29. //Returns current state of the progress
  30. //To translate it into percentage (to float, for example):
  31. //float progress = <>.get() / std::numeric_limits<Load::Type>::max();
  32. Type get() const;
  33. //Returns true if current state equal to final state, false otherwise
  34. bool finished() const;
  35. //Sets current state equal to the argument
  36. void set(Type);
  37. //Sets current state to 0
  38. //steps - amount of steps needed to reach final state
  39. void reset(int steps = 100);
  40. //Immediately sets state to final
  41. //finished() will return true after calling this method
  42. void finish();
  43. //Sets amount of steps needed to reach final state
  44. //doesn't modify current state
  45. void setupSteps(int steps);
  46. //Sets amount of steps needed to reach state specified
  47. //doesn't modify current state
  48. void setupStepsTill(int steps, Type state);
  49. //Increases current state by steps count
  50. //if current state reaches final state, returns immediately
  51. void step(int count = 1);
  52. private:
  53. std::atomic<Type> _progress, _target;
  54. std::atomic<int> _step, _maxSteps;
  55. };
  56. }