-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqevtUniquePtr.hpp
83 lines (67 loc) · 2.61 KB
/
qevtUniquePtr.hpp
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
71
72
73
74
75
76
77
78
79
80
81
82
83
/// @brief An RAII unique_ptr style class for QP::QEvt objects.
/// @ingroup
/// @cond
///***************************************************************************
///
/// Copyright (C) 2022 Matthew Eshleman. All rights reserved.
///
/// This program is open source software: you can redistribute it and/or
/// modify it under the terms of the GNU General Public License as published
/// by the Free Software Foundation, either version 3 of the License, or
/// (at your option) any later version.
///
/// Alternatively, upon written permission from Matthew Eshleman, this program
/// may be distributed and modified under the terms of a Commercial
/// License. For further details, see the Contact Information below.
///
/// Contact Information:
/// Matthew Eshleman
/// https://covemountainsoftware.com
///***************************************************************************
/// @endcond
#ifndef CMS_QEVT_UNIQUE_PTR_HPP
#define CMS_QEVT_UNIQUE_PTR_HPP
#include "qpcpp.hpp"
#include <utility>
namespace cms {
/// I love a nice RAII pattern, such as std::unique_ptr.
/// A QEvt may have been allocated from an event pool, which must be
/// garbage collected. This is easy to forget, and is therefore more error
/// prone, especially when deferring events or recording events. This
/// class helps guarantee that the event is appropriately garbage collected.
///
/// https://covemountainsoftware.com/2019/11/26/why-i-prefer-c-raii-all-the-things/
///
/// \tparam EvtT - a class/struct derived from QP::QEvt.
template <class EvtT> class QEvtUniquePtr {
public:
static_assert(std::is_base_of<QP::QEvt, EvtT>::value,
"template param 'EvtT' must be a derived class of QP::QEvt");
constexpr QEvtUniquePtr() noexcept : m_evt(nullptr) { }
explicit QEvtUniquePtr(QP::QEvt const* const evt) :
m_evt(static_cast<EvtT const* const>(evt))
{
}
~QEvtUniquePtr()
{
if (m_evt != nullptr) {
QP::QF::gc(m_evt);
}
}
QEvtUniquePtr(QEvtUniquePtr&& o) noexcept : m_evt(std::move(o.m_evt))
{
o.m_evt = nullptr;
}
QEvtUniquePtr(QEvtUniquePtr const& rhs) = delete;
QEvtUniquePtr& operator=(QEvtUniquePtr const& rhs) = delete;
EvtT const* operator->() const { return m_evt; }
explicit operator bool() const { return m_evt != nullptr; }
bool operator==(void* ptr) const { return ptr == m_evt; }
bool operator!=(void* ptr) const { return ptr != m_evt; }
const EvtT* get() noexcept { return m_evt; }
private:
EvtT const* m_evt;
};
} // namespace cms
#endif // CMS_QEVT_UNIQUE_PTR_HPP