Skip to content

Commit 5b51fe0

Browse files
committed
Fix ComPtr assign operator and add move constructor/assign operator
1 parent a97862b commit 5b51fe0

File tree

1 file changed

+23
-4
lines changed
  • patch_common/include/patch_common

1 file changed

+23
-4
lines changed

Diff for: patch_common/include/patch_common/ComPtr.h

+23-4
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,15 @@ class ComPtr
1212
ComPtr(const ComPtr& other) :
1313
m_ptr(other.m_ptr)
1414
{
15-
if (m_ptr)
15+
if (m_ptr) {
1616
m_ptr->AddRef();
17+
}
18+
}
19+
20+
ComPtr(ComPtr&& other) :
21+
m_ptr(other.m_ptr)
22+
{
23+
other.m_ptr = nullptr;
1724
}
1825

1926
~ComPtr()
@@ -23,11 +30,22 @@ class ComPtr
2330

2431
ComPtr& operator=(const ComPtr& other)
2532
{
26-
if (&other != this) {
33+
if (&other.m_ptr != &m_ptr) {
2734
release();
2835
m_ptr = other.m_ptr;
29-
if (m_ptr)
36+
if (m_ptr) {
3037
m_ptr->AddRef();
38+
}
39+
}
40+
return *this;
41+
}
42+
43+
ComPtr& operator=(ComPtr&& other)
44+
{
45+
if (&other.m_ptr != &m_ptr) {
46+
release();
47+
m_ptr = other.m_ptr;
48+
other.m_ptr = nullptr;
3149
}
3250
return *this;
3351
}
@@ -50,8 +68,9 @@ class ComPtr
5068

5169
void release()
5270
{
53-
if (m_ptr)
71+
if (m_ptr) {
5472
m_ptr->Release();
73+
}
5574
m_ptr = nullptr;
5675
}
5776
};

0 commit comments

Comments
 (0)