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

#include <cru/base/Guard.h>
#include <memory>
#include <utility>

namespace cru::platform::gui {
template <typename TSelf>
class DeleteLaterImpl {
 private:
  constexpr static auto kLogTag = "cru::platform::gui::DeleteLaterImpl";

 public:
  virtual ~DeleteLaterImpl() {}

  void DeleteLater() {
    IUiApplication::GetInstance()->DeleteLater(static_cast<TSelf*>(this));
  }
};

namespace details {
template <typename T>
struct DeleteLaterPtrDeleter {
  void operator()(T* p) const noexcept { p->DeleteLater(); }
};
}  // namespace details

template <typename T>
using DeleteLaterPtr = std::unique_ptr<T, details::DeleteLaterPtrDeleter<T>>;

template <typename T>
DeleteLaterPtr<T> ToDeleteLaterPtr(std::unique_ptr<T>&& p) {
  return DeleteLaterPtr<T>(p.release());
}

template <typename T, typename... Args>
DeleteLaterPtr<T> MakeDeleteLater(Args&&... args) {
  return DeleteLaterPtr<T>(new T(std::forward<Args>(args)...));
}

}  // namespace cru::platform::gui