OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 PDFium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CORE_FXCRT_INCLUDE_CFX_RETAIN_PTR_H_ | |
6 #define CORE_FXCRT_INCLUDE_CFX_RETAIN_PTR_H_ | |
7 | |
8 #include <memory> | |
9 | |
10 #include "core/include/fxcrt/fx_memory.h" | |
11 | |
12 template <class T> | |
13 class CFX_RetainPtr { | |
14 public: | |
15 explicit CFX_RetainPtr(T* pObj) : m_pObj(pObj) { | |
16 if (m_pObj) | |
17 m_pObj->Retain(); | |
18 } | |
19 | |
20 CFX_RetainPtr() : CFX_RetainPtr(nullptr) {} | |
21 CFX_RetainPtr(const CFX_RetainPtr& that) : CFX_RetainPtr(that.Get()) {} | |
22 CFX_RetainPtr(CFX_RetainPtr&& that) { Swap(that); } | |
23 | |
24 template <class U> | |
25 CFX_RetainPtr(const CFX_RetainPtr<U>& that) | |
26 : CFX_RetainPtr(that.Get()) {} | |
27 | |
28 void Reset(T* obj = nullptr) { | |
Lei Zhang
2016/03/29 18:36:36
Would it be slightly cheaper to do this instead to
Tom Sepez
2016/03/29 21:37:22
Done.
| |
29 CFX_RetainPtr<T> tmp(obj); | |
30 Swap(tmp); | |
31 } | |
32 | |
33 T* Get() const { return m_pObj.get(); } | |
34 void Swap(CFX_RetainPtr& that) { m_pObj.swap(that.m_pObj); } | |
35 | |
36 CFX_RetainPtr& operator=(const CFX_RetainPtr& that) { | |
37 if (*this != that) | |
38 Reset(that.Get()); | |
39 return *this; | |
40 } | |
41 | |
42 bool operator==(const CFX_RetainPtr& that) const { | |
43 return Get() == that.Get(); | |
44 } | |
45 | |
46 bool operator!=(const CFX_RetainPtr& that) const { return !(*this == that); } | |
47 | |
48 operator bool() const { return !!m_pObj; } | |
49 T& operator*() const { return *m_pObj.get(); } | |
50 T* operator->() const { return m_pObj.get(); } | |
51 | |
52 private: | |
53 std::unique_ptr<T, ReleaseDeleter<T>> m_pObj; | |
54 }; | |
55 | |
56 #endif // CORE_FXCRT_INCLUDE_CFX_RETAIN_PTR_H_ | |
OLD | NEW |