Skip to content

Commit 616ad52

Browse files
committed
Add move constructor and assignment support to ComPtr.
1 parent 4b60767 commit 616ad52

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

ComPtr.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#ifndef IME_COM_PTR_H
2121
#define IME_COM_PTR_H
2222

23+
#include <utility>
24+
2325
// ATL-indepdent smart pointers for COM objects
2426
// very similar to the ones provided by ATL (CComPtr & CComQIPtr).
2527

@@ -30,14 +32,18 @@ namespace Ime {
3032
template <class T>
3133
class ComPtr {
3234
public:
33-
ComPtr(void): p_(NULL) {
35+
ComPtr(void): p_(nullptr) {
3436
}
3537

3638
ComPtr(T* p, bool ref = true): p_(p) {
3739
if(p_ && ref)
3840
p_->AddRef();
3941
}
4042

43+
ComPtr(ComPtr&& other) : p_(other.p_) {
44+
other.p_ = nullptr;
45+
}
46+
4147
ComPtr(const ComPtr& other): p_(other.p_) {
4248
if(p_)
4349
p_->AddRef();
@@ -81,6 +87,12 @@ class ComPtr {
8187
return p_ < p;
8288
}
8389

90+
ComPtr& operator = (ComPtr&& other) {
91+
p_ = other.p_;
92+
other.p_ = nullptr;
93+
return *this;
94+
}
95+
8496
ComPtr& operator = (const ComPtr& other) {
8597
return operator = (other.p_);
8698
}
@@ -118,6 +130,9 @@ class ComQIPtr: public ComPtr<T> {
118130
ComQIPtr(const ComQIPtr& other): ComPtr<T>(other) {
119131
}
120132

133+
ComQIPtr(ComQIPtr&& other) : ComPtr<T>(std::move(other)) {
134+
}
135+
121136
ComQIPtr(IUnknown* p) {
122137
if(p) {
123138
p->QueryInterface(__uuidof(T), (void**)&p_);

0 commit comments

Comments
 (0)