summaryrefslogtreecommitdiff
path: root/core/fxcrt/maybe_owned.h
diff options
context:
space:
mode:
authorDan Sinclair <dsinclair@chromium.org>2017-09-21 14:52:41 -0400
committerChromium commit bot <commit-bot@chromium.org>2017-09-21 19:22:00 +0000
commitde44d154f6c61af75f149e965a7f483f0b30dd98 (patch)
treecb33c824a73bb017d4403fba898b1d8c4e0c1566 /core/fxcrt/maybe_owned.h
parentb89669975f6156fce4ced5c8998125a845f8e7dc (diff)
downloadpdfium-de44d154f6c61af75f149e965a7f483f0b30dd98.tar.xz
Move CFX_MaybeOwned to fxcrt::MaybeOwned
This CL moves CFX_MaybeOwned into the fxcrt namespace and removes the CFX_ prefix. The test names for maybe owned were updated to be in the MaybeOned test suite instead of the fxcrt suite. Bug: pdfium:898 Change-Id: I0c07057d66c8610e7b19133094b4507fff725e76 Reviewed-on: https://pdfium-review.googlesource.com/14470 Commit-Queue: dsinclair <dsinclair@chromium.org> Reviewed-by: Tom Sepez <tsepez@chromium.org>
Diffstat (limited to 'core/fxcrt/maybe_owned.h')
-rw-r--r--core/fxcrt/maybe_owned.h92
1 files changed, 92 insertions, 0 deletions
diff --git a/core/fxcrt/maybe_owned.h b/core/fxcrt/maybe_owned.h
new file mode 100644
index 0000000000..11dd68642d
--- /dev/null
+++ b/core/fxcrt/maybe_owned.h
@@ -0,0 +1,92 @@
+// Copyright 2016 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CORE_FXCRT_MAYBE_OWNED_H_
+#define CORE_FXCRT_MAYBE_OWNED_H_
+
+#include <algorithm>
+#include <memory>
+#include <utility>
+
+#include "core/fxcrt/fx_memory.h"
+#include "core/fxcrt/fx_system.h"
+
+namespace fxcrt {
+
+// A template that can hold either owned or unowned references, and cleans up
+// appropriately. Possibly the most pernicious anti-pattern imaginable, but
+// it crops up throughout the codebase due to a desire to avoid copying-in
+// objects or data.
+template <typename T, typename D = std::default_delete<T>>
+class MaybeOwned {
+ public:
+ MaybeOwned() : m_pObj(nullptr) {}
+ explicit MaybeOwned(T* ptr) : m_pObj(ptr) {}
+ explicit MaybeOwned(std::unique_ptr<T, D> ptr)
+ : m_pOwnedObj(std::move(ptr)), m_pObj(m_pOwnedObj.get()) {}
+
+ MaybeOwned(const MaybeOwned& that) = delete;
+ MaybeOwned(MaybeOwned&& that) noexcept
+ : m_pOwnedObj(that.m_pOwnedObj.release()), m_pObj(that.m_pObj) {
+ that.m_pObj = nullptr;
+ }
+
+ void Reset(std::unique_ptr<T, D> ptr) {
+ m_pOwnedObj = std::move(ptr);
+ m_pObj = m_pOwnedObj.get();
+ }
+ void Reset(T* ptr = nullptr) {
+ m_pOwnedObj.reset();
+ m_pObj = ptr;
+ }
+
+ bool IsOwned() const { return !!m_pOwnedObj; }
+ T* Get() const { return m_pObj; }
+ std::unique_ptr<T, D> Release() {
+ ASSERT(IsOwned());
+ return std::move(m_pOwnedObj);
+ }
+
+ MaybeOwned& operator=(const MaybeOwned& that) = delete;
+ MaybeOwned& operator=(MaybeOwned&& that) {
+ m_pOwnedObj = std::move(that.m_pOwnedObj);
+ m_pObj = that.m_pObj;
+ that.m_pObj = nullptr;
+ return *this;
+ }
+ MaybeOwned& operator=(T* ptr) {
+ Reset(ptr);
+ return *this;
+ }
+ MaybeOwned& operator=(std::unique_ptr<T, D> ptr) {
+ Reset(std::move(ptr));
+ return *this;
+ }
+
+ bool operator==(const MaybeOwned& that) const { return Get() == that.Get(); }
+ bool operator==(const std::unique_ptr<T, D>& ptr) const {
+ return Get() == ptr.get();
+ }
+ bool operator==(T* ptr) const { return Get() == ptr; }
+
+ bool operator!=(const MaybeOwned& that) const { return !(*this == that); }
+ bool operator!=(const std::unique_ptr<T, D> ptr) const {
+ return !(*this == ptr);
+ }
+ bool operator!=(T* ptr) const { return !(*this == ptr); }
+
+ explicit operator bool() const { return !!m_pObj; }
+ T& operator*() const { return *m_pObj; }
+ T* operator->() const { return m_pObj; }
+
+ private:
+ std::unique_ptr<T, D> m_pOwnedObj;
+ T* m_pObj;
+};
+
+} // namespace fxcrt
+
+using fxcrt::MaybeOwned;
+
+#endif // CORE_FXCRT_MAYBE_OWNED_H_