diff options
author | dsinclair <dsinclair@chromium.org> | 2016-09-29 13:59:29 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-09-29 13:59:29 -0700 |
commit | a52ab741019342fbca0468e43a01deb44fa5f1bd (patch) | |
tree | 9b1deef3b8deb23d65bf56348b220fe59dcd3169 /core/fxcrt/cfx_retain_ptr.h | |
parent | 8a4e286e686c3027f382afb07d3ddcec4610fdd9 (diff) | |
download | pdfium-a52ab741019342fbca0468e43a01deb44fa5f1bd.tar.xz |
Move core/fxcrt/include to core/fxcrt
BUG=pdfium:611
Review-Url: https://codereview.chromium.org/2382723003
Diffstat (limited to 'core/fxcrt/cfx_retain_ptr.h')
-rw-r--r-- | core/fxcrt/cfx_retain_ptr.h | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/core/fxcrt/cfx_retain_ptr.h b/core/fxcrt/cfx_retain_ptr.h new file mode 100644 index 0000000000..883494e7ec --- /dev/null +++ b/core/fxcrt/cfx_retain_ptr.h @@ -0,0 +1,56 @@ +// 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_CFX_RETAIN_PTR_H_ +#define CORE_FXCRT_CFX_RETAIN_PTR_H_ + +#include <memory> + +#include "core/fxcrt/fx_memory.h" + +template <class T> +class CFX_RetainPtr { + public: + explicit CFX_RetainPtr(T* pObj) : m_pObj(pObj) { + if (m_pObj) + m_pObj->Retain(); + } + + CFX_RetainPtr() : CFX_RetainPtr(nullptr) {} + CFX_RetainPtr(const CFX_RetainPtr& that) : CFX_RetainPtr(that.Get()) {} + CFX_RetainPtr(CFX_RetainPtr&& that) { Swap(that); } + + template <class U> + CFX_RetainPtr(const CFX_RetainPtr<U>& that) : CFX_RetainPtr(that.Get()) {} + + void Reset(T* obj = nullptr) { + if (obj) + obj->Retain(); + m_pObj.reset(obj); + } + + T* Get() const { return m_pObj.get(); } + void Swap(CFX_RetainPtr& that) { m_pObj.swap(that.m_pObj); } + + CFX_RetainPtr& operator=(const CFX_RetainPtr& that) { + if (*this != that) + Reset(that.Get()); + return *this; + } + + bool operator==(const CFX_RetainPtr& that) const { + return Get() == that.Get(); + } + + bool operator!=(const CFX_RetainPtr& that) const { return !(*this == that); } + + explicit operator bool() const { return !!m_pObj; } + T& operator*() const { return *m_pObj.get(); } + T* operator->() const { return m_pObj.get(); } + + private: + std::unique_ptr<T, ReleaseDeleter<T>> m_pObj; +}; + +#endif // CORE_FXCRT_CFX_RETAIN_PTR_H_ |