diff options
author | tsepez <tsepez@chromium.org> | 2016-03-29 15:04:21 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-03-29 15:04:21 -0700 |
commit | 602aebc11a615971800d38f8d427a4e4f95c1134 (patch) | |
tree | ec995b42d2811f00064195e5ffe4ebf4feb4af81 /core/fxcrt/include | |
parent | bd56755ba86f2d87e24a3cee5cb92aa14a81bb27 (diff) | |
download | pdfium-602aebc11a615971800d38f8d427a4e4f95c1134.tar.xz |
Add a Retained Pointer smart class.
It's going to be hard to fix some XFA object leaks without
this. Put this in fxcrt/ as we should be able to make the
FX strings take advantage of this.
BUG=pdfium:55
Review URL: https://codereview.chromium.org/1805683002
Diffstat (limited to 'core/fxcrt/include')
-rw-r--r-- | core/fxcrt/include/cfx_retain_ptr.h | 57 | ||||
-rw-r--r-- | core/fxcrt/include/fx_basic.h | 26 | ||||
-rw-r--r-- | core/fxcrt/include/fx_memory.h | 26 |
3 files changed, 83 insertions, 26 deletions
diff --git a/core/fxcrt/include/cfx_retain_ptr.h b/core/fxcrt/include/cfx_retain_ptr.h new file mode 100644 index 0000000000..25edd585cb --- /dev/null +++ b/core/fxcrt/include/cfx_retain_ptr.h @@ -0,0 +1,57 @@ +// 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_INCLUDE_CFX_RETAIN_PTR_H_ +#define CORE_FXCRT_INCLUDE_CFX_RETAIN_PTR_H_ + +#include <memory> + +#include "core/fxcrt/include/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); } + + 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_INCLUDE_CFX_RETAIN_PTR_H_ diff --git a/core/fxcrt/include/fx_basic.h b/core/fxcrt/include/fx_basic.h index 8a7afda137..feeb6e756d 100644 --- a/core/fxcrt/include/fx_basic.h +++ b/core/fxcrt/include/fx_basic.h @@ -15,32 +15,6 @@ #include "core/fxcrt/include/fx_string.h" #include "core/fxcrt/include/fx_system.h" -// The FX_ArraySize(arr) macro returns the # of elements in an array arr. -// The expression is a compile-time constant, and therefore can be -// used in defining new arrays, for example. If you use FX_ArraySize on -// a pointer by mistake, you will get a compile-time error. -// -// One caveat is that FX_ArraySize() doesn't accept any array of an -// anonymous type or a type defined inside a function. -#define FX_ArraySize(array) (sizeof(ArraySizeHelper(array))) - -// This template function declaration is used in defining FX_ArraySize. -// Note that the function doesn't need an implementation, as we only -// use its type. -template <typename T, size_t N> -char(&ArraySizeHelper(T(&array)[N]))[N]; - -// Used with std::unique_ptr to FX_Free raw memory. -struct FxFreeDeleter { - inline void operator()(void* ptr) const { FX_Free(ptr); } -}; - -// Used with std::unique_ptr to Release() objects that can't be deleted. -template <class T> -struct ReleaseDeleter { - inline void operator()(T* ptr) const { ptr->Release(); } -}; - class CFX_BinaryBuf { public: CFX_BinaryBuf(); diff --git a/core/fxcrt/include/fx_memory.h b/core/fxcrt/include/fx_memory.h index c3dafc8d5e..2614016550 100644 --- a/core/fxcrt/include/fx_memory.h +++ b/core/fxcrt/include/fx_memory.h @@ -74,6 +74,32 @@ inline void* FX_ReallocOrDie(void* ptr, #define FX_Free(ptr) free(ptr) +// The FX_ArraySize(arr) macro returns the # of elements in an array arr. +// The expression is a compile-time constant, and therefore can be +// used in defining new arrays, for example. If you use FX_ArraySize on +// a pointer by mistake, you will get a compile-time error. +// +// One caveat is that FX_ArraySize() doesn't accept any array of an +// anonymous type or a type defined inside a function. +#define FX_ArraySize(array) (sizeof(ArraySizeHelper(array))) + +// This template function declaration is used in defining FX_ArraySize. +// Note that the function doesn't need an implementation, as we only +// use its type. +template <typename T, size_t N> +char(&ArraySizeHelper(T(&array)[N]))[N]; + +// Used with std::unique_ptr to FX_Free raw memory. +struct FxFreeDeleter { + inline void operator()(void* ptr) const { FX_Free(ptr); } +}; + +// Used with std::unique_ptr to Release() objects that can't be deleted. +template <class T> +struct ReleaseDeleter { + inline void operator()(T* ptr) const { ptr->Release(); } +}; + class CFX_DestructObject { public: virtual ~CFX_DestructObject() {} |