aboutsummaryrefslogtreecommitdiff
path: root/include/cru/platform/gui/TimerHelper.h
blob: 91d436a46b94e288f0ec61f40d410776de7c014e (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
#pragma once
#include "UiApplication.h"

namespace cru::platform::gui {

class TimerAutoCanceler {
 public:
  TimerAutoCanceler() : id_(0) {}
  explicit TimerAutoCanceler(long long id) : id_(id) {}

  CRU_DELETE_COPY(TimerAutoCanceler)

  TimerAutoCanceler(TimerAutoCanceler&& other) : id_(other.id_) {
    other.id_ = 0;
  }

  TimerAutoCanceler& operator=(TimerAutoCanceler&& other) {
    if (&other == this) {
      return *this;
    }
    Reset(other.id_);
    other.id_ = 0;
    return *this;
  }

  TimerAutoCanceler& operator=(long long other) {
    return this->operator=(TimerAutoCanceler(other));
  }

  ~TimerAutoCanceler() { Reset(); }

  long long Release() {
    auto temp = id_;
    id_ = 0;
    return temp;
  }

  void Reset(long long id = 0) {
    if (id_ > 0) IUiApplication::GetInstance()->CancelTimer(id_);
    id_ = id;
  }

  explicit operator bool() const { return id_; }

 private:
  long long id_;
};

class TimerListAutoCanceler {
 public:
  TimerListAutoCanceler() = default;
  CRU_DELETE_COPY(TimerListAutoCanceler)
  CRU_DEFAULT_MOVE(TimerListAutoCanceler)
  ~TimerListAutoCanceler() = default;

  TimerListAutoCanceler& operator+=(long long id) {
    list_.push_back(TimerAutoCanceler(id));
    return *this;
  }

  void Clear() { list_.clear(); }

  bool IsEmpty() const { return list_.empty(); }

 private:
  std::vector<TimerAutoCanceler> list_;
};

}  // namespace cru::platform::gui