Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(170)

Side by Side Diff: core/fxcrt/cfx_maybe_owned.h

Issue 2522313002: Use CFX_MaybeOwned<> in fpdf_edit_create.cpp (Closed)
Patch Set: nits Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « core/fpdfapi/parser/cpdf_stream_acc.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 PDFium Authors. All rights reserved. 1 // Copyright 2016 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CORE_FXCRT_CFX_MAYBE_OWNED_H_ 5 #ifndef CORE_FXCRT_CFX_MAYBE_OWNED_H_
6 #define CORE_FXCRT_CFX_MAYBE_OWNED_H_ 6 #define CORE_FXCRT_CFX_MAYBE_OWNED_H_
7 7
8 #include <algorithm> 8 #include <algorithm>
9 #include <memory> 9 #include <memory>
10 #include <utility> 10 #include <utility>
11 11
12 #include "core/fxcrt/fx_memory.h" 12 #include "core/fxcrt/fx_memory.h"
13 #include "core/fxcrt/fx_system.h" 13 #include "core/fxcrt/fx_system.h"
14 14
15 // A template that can hold either owned or unowned references, and cleans up 15 // A template that can hold either owned or unowned references, and cleans up
16 // appropriately. Possibly the most pernicious anti-pattern imaginable, but 16 // appropriately. Possibly the most pernicious anti-pattern imaginable, but
17 // it crops up throughout the codebase due to a desire to avoid copying-in 17 // it crops up throughout the codebase due to a desire to avoid copying-in
18 // objects or data. 18 // objects or data.
19 template <typename T, typename D = std::default_delete<T>> 19 template <typename T, typename D = std::default_delete<T>>
20 class CFX_MaybeOwned { 20 class CFX_MaybeOwned {
21 public: 21 public:
22 CFX_MaybeOwned() : m_pObj(nullptr) {} 22 CFX_MaybeOwned() : m_pObj(nullptr) {}
23 explicit CFX_MaybeOwned(T* ptr) : m_pObj(ptr) {} 23 explicit CFX_MaybeOwned(T* ptr) : m_pObj(ptr) {}
24 explicit CFX_MaybeOwned(std::unique_ptr<T> ptr) 24 explicit CFX_MaybeOwned(std::unique_ptr<T, D> ptr)
25 : m_pOwnedObj(std::move(ptr)), m_pObj(m_pOwnedObj.get()) {} 25 : m_pOwnedObj(std::move(ptr)), m_pObj(m_pOwnedObj.get()) {}
26 26
27 CFX_MaybeOwned(const CFX_MaybeOwned& that) = delete; 27 CFX_MaybeOwned(const CFX_MaybeOwned& that) = delete;
28 CFX_MaybeOwned(CFX_MaybeOwned&& that) 28 CFX_MaybeOwned(CFX_MaybeOwned&& that)
29 : m_pOwnedObj(that.m_pOwnedObj.release()), m_pObj(that.m_pObj) { 29 : m_pOwnedObj(that.m_pOwnedObj.release()), m_pObj(that.m_pObj) {
30 that.m_pObj = nullptr; 30 that.m_pObj = nullptr;
31 } 31 }
32 32
33 void Reset(std::unique_ptr<T> ptr) { 33 void Reset(std::unique_ptr<T, D> ptr) {
34 m_pOwnedObj = std::move(ptr); 34 m_pOwnedObj = std::move(ptr);
35 m_pObj = m_pOwnedObj.get(); 35 m_pObj = m_pOwnedObj.get();
36 } 36 }
37 void Reset(T* ptr = nullptr) { 37 void Reset(T* ptr = nullptr) {
38 m_pOwnedObj.reset(); 38 m_pOwnedObj.reset();
39 m_pObj = ptr; 39 m_pObj = ptr;
40 } 40 }
41 41
42 bool IsOwned() const { return !!m_pOwnedObj; } 42 bool IsOwned() const { return !!m_pOwnedObj; }
43 T* Get() const { return m_pObj; } 43 T* Get() const { return m_pObj; }
44 std::unique_ptr<T> Release() { 44 std::unique_ptr<T, D> Release() {
45 ASSERT(IsOwned()); 45 ASSERT(IsOwned());
46 return std::move(m_pOwnedObj); 46 return std::move(m_pOwnedObj);
47 } 47 }
48 48
49 CFX_MaybeOwned& operator=(const CFX_MaybeOwned& that) = delete; 49 CFX_MaybeOwned& operator=(const CFX_MaybeOwned& that) = delete;
50 CFX_MaybeOwned& operator=(CFX_MaybeOwned&& that) { 50 CFX_MaybeOwned& operator=(CFX_MaybeOwned&& that) {
51 m_pOwnedObj = std::move(that.m_pOwnedObj); 51 m_pOwnedObj = std::move(that.m_pOwnedObj);
52 m_pObj = that.m_pObj; 52 m_pObj = that.m_pObj;
53 that.m_pObj = nullptr; 53 that.m_pObj = nullptr;
54 return *this; 54 return *this;
55 } 55 }
56 CFX_MaybeOwned& operator=(T* ptr) { 56 CFX_MaybeOwned& operator=(T* ptr) {
57 Reset(ptr); 57 Reset(ptr);
58 return *this; 58 return *this;
59 } 59 }
60 CFX_MaybeOwned& operator=(std::unique_ptr<T> ptr) { 60 CFX_MaybeOwned& operator=(std::unique_ptr<T, D> ptr) {
61 Reset(std::move(ptr)); 61 Reset(std::move(ptr));
62 return *this; 62 return *this;
63 } 63 }
64 64
65 bool operator==(const CFX_MaybeOwned& that) const { 65 bool operator==(const CFX_MaybeOwned& that) const {
66 return Get() == that.Get(); 66 return Get() == that.Get();
67 } 67 }
68 bool operator==(const std::unique_ptr<T>& ptr) const { 68 bool operator==(const std::unique_ptr<T, D>& ptr) const {
69 return Get() == ptr.get(); 69 return Get() == ptr.get();
70 } 70 }
71 bool operator==(T* ptr) const { return Get() == ptr; } 71 bool operator==(T* ptr) const { return Get() == ptr; }
72 72
73 bool operator!=(const CFX_MaybeOwned& that) const { return !(*this == that); } 73 bool operator!=(const CFX_MaybeOwned& that) const { return !(*this == that); }
74 bool operator!=(const std::unique_ptr<T> ptr) const { 74 bool operator!=(const std::unique_ptr<T, D> ptr) const {
75 return !(*this == ptr); 75 return !(*this == ptr);
76 } 76 }
77 bool operator!=(T* ptr) const { return !(*this == ptr); } 77 bool operator!=(T* ptr) const { return !(*this == ptr); }
78 78
79 explicit operator bool() const { return !!m_pObj; } 79 explicit operator bool() const { return !!m_pObj; }
80 T& operator*() const { return *m_pObj; } 80 T& operator*() const { return *m_pObj; }
81 T* operator->() const { return m_pObj; } 81 T* operator->() const { return m_pObj; }
82 82
83 private: 83 private:
84 std::unique_ptr<T, D> m_pOwnedObj; 84 std::unique_ptr<T, D> m_pOwnedObj;
85 T* m_pObj; 85 T* m_pObj;
86 }; 86 };
87 87
88 #endif // CORE_FXCRT_CFX_MAYBE_OWNED_H_ 88 #endif // CORE_FXCRT_CFX_MAYBE_OWNED_H_
OLDNEW
« no previous file with comments | « core/fpdfapi/parser/cpdf_stream_acc.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698