IRC_SERVER
By @hyunjunk (hyunjun2372@gmail.com)
Loading...
Searching...
No Matches
WeakPtr.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "Core/SharedPtr.hpp"
4
5namespace IRCCore
6{
7
8/** Weak pointer custom implementation for C++98 standard
9 *
10 * @tparam T Type of the object to be managed by the weak pointer.
11 *
12 * @warning Not thread-safe
13 *
14 * @see SharedPtr
15 */
16template <typename T>
18{
19public:
21 : mControlBlock(NULL)
22 {
23 }
24
26 : mControlBlock(sharedPtr.mControlBlock)
27 {
28 if (mControlBlock != NULL)
29 {
30 mControlBlock->WeakRefCount += 1;
31 }
32 }
33
36 {
37 if (mControlBlock != NULL)
38 {
39 if (mControlBlock->StrongRefCount == 0)
40 {
41 mControlBlock = NULL;
42 }
43 else
44 {
45 mControlBlock->WeakRefCount += 1;
46 }
47 }
48 }
49
51 {
52 Reset();
53 }
54
56 {
57 Reset();
58
60 if (mControlBlock != NULL)
61 {
62 if (mControlBlock->StrongRefCount == 0)
63 {
64 mControlBlock = NULL;
65 }
66 else
67 {
68 mControlBlock->WeakRefCount += 1;
69 }
70 }
71
72 return *this;
73 }
74
76 {
77 this->Reset();
78
79 mControlBlock = sharedPtr.mControlBlock;
80 if (mControlBlock != NULL)
81 {
82 mControlBlock->WeakRefCount += 1;
83 }
84
85 return *this;
86 }
87
88 FORCEINLINE bool operator==(const WeakPtr<T>& rhs) const
89 {
90 return mControlBlock == rhs.mControlBlock;
91 }
92
93 FORCEINLINE bool operator!=(const WeakPtr<T>& rhs) const
94 {
95 return mControlBlock != rhs.mControlBlock;
96 }
97
99 {
100 if (mControlBlock != NULL)
101 {
102 if (mControlBlock->StrongRefCount > 0)
103 {
105 }
106 }
107 return SharedPtr<T>();
108 }
109
110 FORCEINLINE bool Expired() const
111 {
112 if (mControlBlock != NULL)
113 {
114 return mControlBlock->StrongRefCount == 0;
115 }
116 return true;
117 }
118
120 {
121 if (mControlBlock != NULL)
122 {
123 Assert(mControlBlock->WeakRefCount > 0);
124
125 mControlBlock->WeakRefCount -= 1;
126 if (mControlBlock->WeakRefCount == 0)
127 {
128 if (mControlBlock->StrongRefCount == 0)
129 {
130 delete mControlBlock;
131 }
132 }
133 }
134
135 mControlBlock = NULL;
136 }
137
138 FORCEINLINE size_t UseCount() const
139 {
140 if (mControlBlock != NULL)
141 {
142 return mControlBlock->StrongRefCount;
143 }
144 return 0;
145 }
146
148 {
151 rhs.mControlBlock = temp;
152 }
153
154private:
156};
157
158} // namespace IRCCore
#define FORCEINLINE
Definition AttributeDefines.hpp:55
#define Assert(exp)
Implemented as direct interrupt to avoid dirtying the call stack with assert function when debugging.
Definition MacroDefines.hpp:30
Shared pointer custom implementation for C++98 standard.
Definition SharedPtr.hpp:164
detail::ControlBlock< T > * mControlBlock
Definition SharedPtr.hpp:346
Weak pointer custom implementation for C++98 standard.
Definition WeakPtr.hpp:18
WeakPtr< T > & operator=(const SharedPtr< T > &sharedPtr)
Definition WeakPtr.hpp:75
WeakPtr(const SharedPtr< T > &sharedPtr)
Definition WeakPtr.hpp:25
size_t UseCount() const
Definition WeakPtr.hpp:138
bool Expired() const
Definition WeakPtr.hpp:110
WeakPtr()
Definition WeakPtr.hpp:20
bool operator==(const WeakPtr< T > &rhs) const
Definition WeakPtr.hpp:88
SharedPtr< T > Lock() const
Definition WeakPtr.hpp:98
void Reset()
Definition WeakPtr.hpp:119
~WeakPtr()
Definition WeakPtr.hpp:50
WeakPtr(const WeakPtr< T > &rhs)
Definition WeakPtr.hpp:34
WeakPtr< T > & operator=(const WeakPtr< T > &rhs)
Definition WeakPtr.hpp:55
bool operator!=(const WeakPtr< T > &rhs) const
Definition WeakPtr.hpp:93
void Swap(WeakPtr< T > &rhs)
Definition WeakPtr.hpp:147
detail::ControlBlock< T > * mControlBlock
Definition WeakPtr.hpp:155
Definition ControlBlock.hpp:7
Do not use this class directly.
Definition SharedPtr.hpp:31