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_INCLUDE_FXCRT_CFX_RETAIN_PTR_H_ | |
6 #define CORE_INCLUDE_FXCRT_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<T>& that) : CFX_RetainPtr(that.Get()) {} | |
22 CFX_RetainPtr(CFX_RetainPtr<T>&& 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 = NULL) { | |
dsinclair
2016/03/16 13:47:45
nit: nullptr
Tom Sepez
2016/03/16 19:12:55
Done.
| |
29 CFX_RetainPtr<T> tmp(obj); | |
30 Swap(tmp); | |
31 } | |
32 | |
33 void Swap(CFX_RetainPtr<T>& that) { m_pObj.swap(that.m_pObj); } | |
34 | |
35 T* Get() const { return m_pObj.get(); } | |
36 | |
37 CFX_RetainPtr& operator=(const CFX_RetainPtr<T>& that) { | |
38 if (*this != that) | |
39 Reset(that.Get()); | |
40 return *this; | |
41 } | |
42 | |
43 bool operator==(const CFX_RetainPtr<T>& that) const { | |
44 return Get() == that.Get(); | |
45 } | |
46 | |
47 bool operator!=(const CFX_RetainPtr<T>& that) const { | |
48 return !(*this == that); | |
49 } | |
50 | |
51 operator bool() const { return !!m_pObj; } | |
52 T& operator*() const { return *m_pObj.get(); } | |
53 T* operator->() const { return m_pObj.get(); } | |
54 | |
55 protected: | |
dsinclair
2016/03/16 13:47:45
Why not private?
Tom Sepez
2016/03/16 19:12:55
Done.
| |
56 std::unique_ptr<T, ReleaseDeleter<T>> m_pObj; | |
57 }; | |
58 | |
59 #endif // CORE_INCLUDE_FXCRT_CFX_RETAIN_PTR_H_ | |
OLD | NEW |