summaryrefslogtreecommitdiff
path: root/core/fxcrt
diff options
context:
space:
mode:
Diffstat (limited to 'core/fxcrt')
-rw-r--r--core/fxcrt/cfx_retain_ptr_unittest.cpp215
-rw-r--r--core/fxcrt/include/cfx_retain_ptr.h57
-rw-r--r--core/fxcrt/include/fx_basic.h26
-rw-r--r--core/fxcrt/include/fx_memory.h26
4 files changed, 298 insertions, 26 deletions
diff --git a/core/fxcrt/cfx_retain_ptr_unittest.cpp b/core/fxcrt/cfx_retain_ptr_unittest.cpp
new file mode 100644
index 0000000000..4e74e52768
--- /dev/null
+++ b/core/fxcrt/cfx_retain_ptr_unittest.cpp
@@ -0,0 +1,215 @@
+// 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.
+
+#include "core/fxcrt/include/cfx_retain_ptr.h"
+
+#include <utility>
+
+#include "testing/fx_string_testhelpers.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+
+class PseudoRetainable {
+ public:
+ PseudoRetainable() : retain_count_(0), release_count_(0) {}
+ void Retain() { ++retain_count_; }
+ void Release() { ++release_count_; }
+ int retain_count() const { return retain_count_; }
+ int release_count() const { return release_count_; }
+
+ private:
+ int retain_count_;
+ int release_count_;
+};
+
+} // namespace
+
+TEST(fxcrt, RetainPtrNull) {
+ CFX_RetainPtr<PseudoRetainable> ptr;
+ EXPECT_EQ(nullptr, ptr.Get());
+}
+
+TEST(fxcrt, RetainPtrNormal) {
+ PseudoRetainable obj;
+ {
+ CFX_RetainPtr<PseudoRetainable> ptr(&obj);
+ EXPECT_EQ(&obj, ptr.Get());
+ EXPECT_EQ(1, obj.retain_count());
+ EXPECT_EQ(0, obj.release_count());
+ }
+ EXPECT_EQ(1, obj.retain_count());
+ EXPECT_EQ(1, obj.release_count());
+}
+
+TEST(fxcrt, RetainPtrCopyCtor) {
+ PseudoRetainable obj;
+ {
+ CFX_RetainPtr<PseudoRetainable> ptr1(&obj);
+ {
+ CFX_RetainPtr<PseudoRetainable> ptr2(ptr1);
+ EXPECT_EQ(2, obj.retain_count());
+ EXPECT_EQ(0, obj.release_count());
+ }
+ EXPECT_EQ(2, obj.retain_count());
+ EXPECT_EQ(1, obj.release_count());
+ }
+ EXPECT_EQ(2, obj.retain_count());
+ EXPECT_EQ(2, obj.release_count());
+}
+
+TEST(fxcrt, RetainPtrMoveCtor) {
+ PseudoRetainable obj;
+ {
+ CFX_RetainPtr<PseudoRetainable> ptr1(&obj);
+ {
+ CFX_RetainPtr<PseudoRetainable> ptr2(std::move(ptr1));
+ EXPECT_EQ(1, obj.retain_count());
+ EXPECT_EQ(0, obj.release_count());
+ }
+ EXPECT_EQ(1, obj.retain_count());
+ EXPECT_EQ(1, obj.release_count());
+ }
+ EXPECT_EQ(1, obj.retain_count());
+ EXPECT_EQ(1, obj.release_count());
+}
+
+TEST(fxcrt, RetainPtrResetNull) {
+ PseudoRetainable obj;
+ {
+ CFX_RetainPtr<PseudoRetainable> ptr(&obj);
+ ptr.Reset();
+ EXPECT_EQ(1, obj.retain_count());
+ EXPECT_EQ(1, obj.release_count());
+ }
+ EXPECT_EQ(1, obj.retain_count());
+ EXPECT_EQ(1, obj.release_count());
+}
+
+TEST(fxcrt, RetainPtrReset) {
+ PseudoRetainable obj1;
+ PseudoRetainable obj2;
+ {
+ CFX_RetainPtr<PseudoRetainable> ptr(&obj1);
+ ptr.Reset(&obj2);
+ EXPECT_EQ(1, obj1.retain_count());
+ EXPECT_EQ(1, obj1.release_count());
+ EXPECT_EQ(1, obj2.retain_count());
+ EXPECT_EQ(0, obj2.release_count());
+ }
+ EXPECT_EQ(1, obj1.retain_count());
+ EXPECT_EQ(1, obj1.release_count());
+ EXPECT_EQ(1, obj2.retain_count());
+ EXPECT_EQ(1, obj2.release_count());
+}
+
+TEST(fxcrt, RetainPtrSwap) {
+ PseudoRetainable obj1;
+ PseudoRetainable obj2;
+ {
+ CFX_RetainPtr<PseudoRetainable> ptr1(&obj1);
+ {
+ CFX_RetainPtr<PseudoRetainable> ptr2(&obj2);
+ ptr1.Swap(ptr2);
+ EXPECT_EQ(1, obj1.retain_count());
+ EXPECT_EQ(0, obj1.release_count());
+ EXPECT_EQ(1, obj2.retain_count());
+ EXPECT_EQ(0, obj2.release_count());
+ }
+ EXPECT_EQ(1, obj1.retain_count());
+ EXPECT_EQ(1, obj1.release_count());
+ EXPECT_EQ(1, obj2.retain_count());
+ EXPECT_EQ(0, obj2.release_count());
+ }
+ EXPECT_EQ(1, obj1.retain_count());
+ EXPECT_EQ(1, obj1.release_count());
+ EXPECT_EQ(1, obj2.retain_count());
+ EXPECT_EQ(1, obj2.release_count());
+}
+
+TEST(fxcrt, RetainPtrSwapNull) {
+ PseudoRetainable obj1;
+ {
+ CFX_RetainPtr<PseudoRetainable> ptr1(&obj1);
+ {
+ CFX_RetainPtr<PseudoRetainable> ptr2;
+ ptr1.Swap(ptr2);
+ EXPECT_FALSE(ptr1);
+ EXPECT_TRUE(ptr2);
+ EXPECT_EQ(1, obj1.retain_count());
+ EXPECT_EQ(0, obj1.release_count());
+ }
+ EXPECT_EQ(1, obj1.retain_count());
+ EXPECT_EQ(1, obj1.release_count());
+ }
+ EXPECT_EQ(1, obj1.retain_count());
+ EXPECT_EQ(1, obj1.release_count());
+}
+
+TEST(fxcrt, RetainPtrAssign) {
+ PseudoRetainable obj;
+ {
+ CFX_RetainPtr<PseudoRetainable> ptr(&obj);
+ {
+ CFX_RetainPtr<PseudoRetainable> ptr2;
+ ptr2 = ptr;
+ EXPECT_EQ(2, obj.retain_count());
+ EXPECT_EQ(0, obj.release_count());
+ }
+ EXPECT_EQ(2, obj.retain_count());
+ EXPECT_EQ(1, obj.release_count());
+ }
+ EXPECT_EQ(2, obj.retain_count());
+ EXPECT_EQ(2, obj.release_count());
+}
+
+TEST(fxcrt, RetainPtrEquals) {
+ PseudoRetainable obj1;
+ PseudoRetainable obj2;
+ CFX_RetainPtr<PseudoRetainable> null_ptr1;
+ CFX_RetainPtr<PseudoRetainable> obj1_ptr1(&obj1);
+ CFX_RetainPtr<PseudoRetainable> obj2_ptr1(&obj2);
+ {
+ CFX_RetainPtr<PseudoRetainable> null_ptr2;
+ EXPECT_TRUE(null_ptr1 == null_ptr2);
+
+ CFX_RetainPtr<PseudoRetainable> obj1_ptr2(&obj1);
+ EXPECT_TRUE(obj1_ptr1 == obj1_ptr2);
+
+ CFX_RetainPtr<PseudoRetainable> obj2_ptr2(&obj2);
+ EXPECT_TRUE(obj2_ptr1 == obj2_ptr2);
+ }
+ EXPECT_FALSE(null_ptr1 == obj1_ptr1);
+ EXPECT_FALSE(null_ptr1 == obj2_ptr1);
+ EXPECT_FALSE(obj1_ptr1 == obj2_ptr1);
+}
+
+TEST(fxcrt, RetainPtrNotEquals) {
+ PseudoRetainable obj1;
+ PseudoRetainable obj2;
+ CFX_RetainPtr<PseudoRetainable> null_ptr1;
+ CFX_RetainPtr<PseudoRetainable> obj1_ptr1(&obj1);
+ CFX_RetainPtr<PseudoRetainable> obj2_ptr1(&obj2);
+ {
+ CFX_RetainPtr<PseudoRetainable> null_ptr2;
+ CFX_RetainPtr<PseudoRetainable> obj1_ptr2(&obj1);
+ CFX_RetainPtr<PseudoRetainable> obj2_ptr2(&obj2);
+ EXPECT_FALSE(null_ptr1 != null_ptr2);
+ EXPECT_FALSE(obj1_ptr1 != obj1_ptr2);
+ EXPECT_FALSE(obj2_ptr1 != obj2_ptr2);
+ }
+ EXPECT_TRUE(null_ptr1 != obj1_ptr1);
+ EXPECT_TRUE(null_ptr1 != obj2_ptr1);
+ EXPECT_TRUE(obj1_ptr1 != obj2_ptr1);
+}
+
+TEST(fxcrt, RetainPtrBool) {
+ PseudoRetainable obj1;
+ CFX_RetainPtr<PseudoRetainable> null_ptr;
+ CFX_RetainPtr<PseudoRetainable> obj1_ptr(&obj1);
+ bool null_bool = null_ptr;
+ bool obj1_bool = obj1_ptr;
+ EXPECT_FALSE(null_bool);
+ EXPECT_TRUE(obj1_bool);
+}
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() {}