summaryrefslogtreecommitdiff
path: root/logbook/WorkedBeforeLoadState.hpp
blob: 02578cd284b11c3132d723f4a57d318ab7fab833 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#ifndef WORKED_BEFORE_LOAD_STATE_HPP
#define WORKED_BEFORE_LOAD_STATE_HPP

#include <QtGlobal>

class WorkedBeforeLoadState final
{
public:
  enum class ReloadResult
  {
    Start,
    Coalesced
  };

  enum class CompletionResult
  {
    Idle,
    Restart
  };

  ReloadResult request_reload ()
  {
    if (Phase::Idle == phase_)
      {
        phase_ = Phase::Loading;
        return ReloadResult::Start;
      }

    reload_pending_ = true;
    return ReloadResult::Coalesced;
  }

  void begin_completion ()
  {
    Q_ASSERT (Phase::Loading == phase_);
    phase_ = Phase::Completing;
  }

  CompletionResult finish_completion ()
  {
    Q_ASSERT (Phase::Completing == phase_);
    if (reload_pending_)
      {
        reload_pending_ = false;
        phase_ = Phase::Loading;
        return CompletionResult::Restart;
      }

    phase_ = Phase::Idle;
    return CompletionResult::Idle;
  }

  bool active () const
  {
    return Phase::Idle != phase_;
  }

private:
  enum class Phase
  {
    Idle,
    Loading,
    Completing
  };

  Phase phase_ {Phase::Idle};
  bool reload_pending_ {false};
};

#endif // WORKED_BEFORE_LOAD_STATE_HPP