Small footprint UI library for hardware accelerated games & applications
Révision | f366630084a4bd9d65c166613e1fb8124b3ee92b (tree) |
---|---|
l'heure | 2017-09-18 22:25:49 |
Auteur | Starg <starg@user...> |
Commiter | Starg |
Added TBUniquePointer
@@ -0,0 +1,74 @@ | ||
1 | + | |
2 | +#ifndef TB_MEMORY_H | |
3 | +#define TB_MEMORY_H | |
4 | + | |
5 | +#include "tb_core.h" | |
6 | + | |
7 | +namespace tb { | |
8 | + | |
9 | +/** TBUniquePointer is a smart pointer with unique object ownership. */ | |
10 | +template<class T> | |
11 | +class TBUniquePointer | |
12 | +{ | |
13 | +public: | |
14 | + TBUniquePointer() : m_p(nullptr) {} | |
15 | + explicit TBUniquePointer(T *p) : m_p(p) {} | |
16 | + | |
17 | + TBUniquePointer(const TBUniquePointer &) = delete; | |
18 | + TBUniquePointer &operator=(const TBUniquePointer &) = delete; | |
19 | + | |
20 | + TBUniquePointer(TBUniquePointer &&rhs) : m_p(rhs.p) { rhs.m_p = nullptr; } | |
21 | + | |
22 | + TBUniquePointer &operator=(TBUniquePointer &&rhs) | |
23 | + { | |
24 | + if (this != &rhs) | |
25 | + { | |
26 | + Set(rhs.m_p); | |
27 | + rhs.m_p = nullptr; | |
28 | + } | |
29 | + | |
30 | + return *this; | |
31 | + } | |
32 | + | |
33 | + ~TBUniquePointer() | |
34 | + { | |
35 | + Reset(); | |
36 | + } | |
37 | + | |
38 | + void Reset() | |
39 | + { | |
40 | + if (m_p) | |
41 | + { | |
42 | + delete m_p; | |
43 | + m_p = nullptr; | |
44 | + } | |
45 | + } | |
46 | + | |
47 | + T *Release() | |
48 | + { | |
49 | + T *p = m_p; | |
50 | + m_p = nullptr; | |
51 | + return p; | |
52 | + } | |
53 | + | |
54 | + void Set(T *p) | |
55 | + { | |
56 | + Reset(); | |
57 | + m_p = p; | |
58 | + } | |
59 | + | |
60 | + explicit operator bool() const { return !!m_p; } | |
61 | + T *Get() { return m_p; } | |
62 | + const T *Get() const { return m_p; } | |
63 | + T &operator*() { return *m_p; } | |
64 | + const T &operator*() const { return *m_p; } | |
65 | + T *operator->() { return m_p; } | |
66 | + const T *operator->() const { return m_p; } | |
67 | + | |
68 | +private: | |
69 | + T *m_p; | |
70 | +}; | |
71 | + | |
72 | +} // namespace tb | |
73 | + | |
74 | +#endif // !TB_MEMORY_H |