aboutsummaryrefslogtreecommitdiff
path: root/third_party/chromium/base/memory
diff options
context:
space:
mode:
Diffstat (limited to 'third_party/chromium/base/memory')
-rw-r--r--third_party/chromium/base/memory/ptr_util.h74
-rw-r--r--third_party/chromium/base/memory/scoped_ptr.h135
-rw-r--r--third_party/chromium/base/memory/weak_ptr.h24
-rw-r--r--third_party/chromium/base/memory/weak_ptr_unittest.cc27
4 files changed, 104 insertions, 156 deletions
diff --git a/third_party/chromium/base/memory/ptr_util.h b/third_party/chromium/base/memory/ptr_util.h
new file mode 100644
index 0000000..8747ac9
--- /dev/null
+++ b/third_party/chromium/base/memory/ptr_util.h
@@ -0,0 +1,74 @@
+// Copyright 2015 The Chromium 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 BASE_MEMORY_PTR_UTIL_H_
+#define BASE_MEMORY_PTR_UTIL_H_
+
+#include <memory>
+#include <utility>
+
+namespace base {
+
+// Helper to transfer ownership of a raw pointer to a std::unique_ptr<T>.
+// Note that std::unique_ptr<T> has very different semantics from
+// std::unique_ptr<T[]>: do not use this helper for array allocations.
+template <typename T>
+std::unique_ptr<T> WrapUnique(T* ptr) {
+ return std::unique_ptr<T>(ptr);
+}
+
+namespace internal {
+
+template <typename T>
+struct MakeUniqueResult {
+ using Scalar = std::unique_ptr<T>;
+};
+
+template <typename T>
+struct MakeUniqueResult<T[]> {
+ using Array = std::unique_ptr<T[]>;
+};
+
+template <typename T, size_t N>
+struct MakeUniqueResult<T[N]> {
+ using Invalid = void;
+};
+
+} // namespace internal
+
+// Helper to construct an object wrapped in a std::unique_ptr. This is an
+// implementation of C++14's std::make_unique that can be used in Chrome.
+//
+// MakeUnique<T>(args) should be preferred over WrapUnique(new T(args)): bare
+// calls to `new` should be treated with scrutiny.
+//
+// Usage:
+// // ptr is a std::unique_ptr<std::string>
+// auto ptr = MakeUnique<std::string>("hello world!");
+//
+// // arr is a std::unique_ptr<int[]>
+// auto arr = MakeUnique<int[]>(5);
+
+// Overload for non-array types. Arguments are forwarded to T's constructor.
+template <typename T, typename... Args>
+typename internal::MakeUniqueResult<T>::Scalar MakeUnique(Args&&... args) {
+ return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
+}
+
+// Overload for array types of unknown bound, e.g. T[]. The array is allocated
+// with `new T[n]()` and value-initialized: note that this is distinct from
+// `new T[n]`, which default-initializes.
+template <typename T>
+typename internal::MakeUniqueResult<T>::Array MakeUnique(size_t size) {
+ return std::unique_ptr<T>(new typename std::remove_extent<T>::type[size]());
+}
+
+// Overload to reject array types of known bound, e.g. T[n].
+template <typename T, typename... Args>
+typename internal::MakeUniqueResult<T>::Invalid MakeUnique(Args&&... args) =
+ delete;
+
+} // namespace base
+
+#endif // BASE_MEMORY_PTR_UTIL_H_
diff --git a/third_party/chromium/base/memory/scoped_ptr.h b/third_party/chromium/base/memory/scoped_ptr.h
deleted file mode 100644
index 2d2c0ec..0000000
--- a/third_party/chromium/base/memory/scoped_ptr.h
+++ /dev/null
@@ -1,135 +0,0 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Scopers help you manage ownership of a pointer, helping you easily manage a
-// pointer within a scope, and automatically destroying the pointer at the end
-// of a scope. There are two main classes you will use, which correspond to the
-// operators new/delete and new[]/delete[].
-//
-// Example usage (scoped_ptr<T>):
-// {
-// scoped_ptr<Foo> foo(new Foo("wee"));
-// } // foo goes out of scope, releasing the pointer with it.
-//
-// {
-// scoped_ptr<Foo> foo; // No pointer managed.
-// foo.reset(new Foo("wee")); // Now a pointer is managed.
-// foo.reset(new Foo("wee2")); // Foo("wee") was destroyed.
-// foo.reset(new Foo("wee3")); // Foo("wee2") was destroyed.
-// foo->Method(); // Foo::Method() called.
-// foo.get()->Method(); // Foo::Method() called.
-// SomeFunc(foo.release()); // SomeFunc takes ownership, foo no longer
-// // manages a pointer.
-// foo.reset(new Foo("wee4")); // foo manages a pointer again.
-// foo.reset(); // Foo("wee4") destroyed, foo no longer
-// // manages a pointer.
-// } // foo wasn't managing a pointer, so nothing was destroyed.
-//
-// Example usage (scoped_ptr<T[]>):
-// {
-// scoped_ptr<Foo[]> foo(new Foo[100]);
-// foo.get()->Method(); // Foo::Method on the 0th element.
-// foo[10].Method(); // Foo::Method on the 10th element.
-// }
-//
-// Scopers are testable as booleans:
-// {
-// scoped_ptr<Foo> foo;
-// if (!foo)
-// foo.reset(new Foo());
-// if (foo)
-// LOG(INFO) << "This code is reached."
-// }
-//
-// These scopers also implement part of the functionality of C++11 unique_ptr
-// in that they are "movable but not copyable." You can use the scopers in
-// the parameter and return types of functions to signify ownership transfer
-// in to and out of a function. When calling a function that has a scoper
-// as the argument type, it must be called with an rvalue of a scoper, which
-// can be created by using std::move(), or the result of another function that
-// generates a temporary; passing by copy will NOT work. Here is an example
-// using scoped_ptr:
-//
-// void TakesOwnership(scoped_ptr<Foo> arg) {
-// // Do something with arg.
-// }
-// scoped_ptr<Foo> CreateFoo() {
-// // No need for calling std::move() for returning a move-only value, or
-// // when you already have an rvalue as we do here.
-// return scoped_ptr<Foo>(new Foo("new"));
-// }
-// scoped_ptr<Foo> PassThru(scoped_ptr<Foo> arg) {
-// return arg;
-// }
-//
-// {
-// scoped_ptr<Foo> ptr(new Foo("yay")); // ptr manages Foo("yay").
-// TakesOwnership(std::move(ptr)); // ptr no longer owns Foo("yay").
-// scoped_ptr<Foo> ptr2 = CreateFoo(); // ptr2 owns the return Foo.
-// scoped_ptr<Foo> ptr3 = // ptr3 now owns what was in ptr2.
-// PassThru(std::move(ptr2)); // ptr2 is correspondingly nullptr.
-// }
-//
-// Notice that if you do not call std::move() when returning from PassThru(), or
-// when invoking TakesOwnership(), the code will not compile because scopers
-// are not copyable; they only implement move semantics which require calling
-// the std::move() function to signify a destructive transfer of state.
-// CreateFoo() is different though because we are constructing a temporary on
-// the return line and thus can avoid needing to call std::move().
-//
-// The conversion move-constructor properly handles upcast in initialization,
-// i.e. you can use a scoped_ptr<Child> to initialize a scoped_ptr<Parent>:
-//
-// scoped_ptr<Foo> foo(new Foo());
-// scoped_ptr<FooParent> parent(std::move(foo));
-
-#ifndef BASE_MEMORY_SCOPED_PTR_H_
-#define BASE_MEMORY_SCOPED_PTR_H_
-
-// This is an implementation designed to match the anticipated future TR2
-// implementation of the scoped_ptr class.
-
-// TODO(dcheng): Clean up these headers, but there are likely lots of existing
-// IWYU violations.
-#include <stddef.h>
-#include <stdlib.h>
-
-#include <iosfwd>
-#include <memory>
-#include <type_traits>
-#include <utility>
-
-#include "base/compiler_specific.h"
-#include "base/logging.h"
-#include "base/macros.h"
-#include "base/move.h"
-#include "build/build_config.h"
-
-namespace base {
-
-// Function object which invokes 'free' on its parameter, which must be
-// a pointer. Can be used to store malloc-allocated pointers in scoped_ptr:
-//
-// scoped_ptr<int, base::FreeDeleter> foo_ptr(
-// static_cast<int*>(malloc(sizeof(int))));
-struct FreeDeleter {
- inline void operator()(void* ptr) const {
- free(ptr);
- }
-};
-
-} // namespace base
-
-template <typename T, typename D = std::default_delete<T>>
-using scoped_ptr = std::unique_ptr<T, D>;
-
-// A function to convert T* into scoped_ptr<T>
-// Doing e.g. make_scoped_ptr(new FooBarBaz<type>(arg)) is a shorter notation
-// for scoped_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg))
-template <typename T>
-scoped_ptr<T> make_scoped_ptr(T* ptr) {
- return scoped_ptr<T>(ptr);
-}
-
-#endif // BASE_MEMORY_SCOPED_PTR_H_
diff --git a/third_party/chromium/base/memory/weak_ptr.h b/third_party/chromium/base/memory/weak_ptr.h
index 601c379..2efb024 100644
--- a/third_party/chromium/base/memory/weak_ptr.h
+++ b/third_party/chromium/base/memory/weak_ptr.h
@@ -3,7 +3,7 @@
// found in the LICENSE file.
// Weak pointers are pointers to an object that do not affect its lifetime,
-// and which may be invalidated (i.e. reset to NULL) by the object, or its
+// and which may be invalidated (i.e. reset to nullptr) by the object, or its
// owner, at any time, most commonly when the object is about to be deleted.
// Weak pointers are useful when an object needs to be accessed safely by one
@@ -70,6 +70,7 @@
#ifndef BASE_MEMORY_WEAK_PTR_H_
#define BASE_MEMORY_WEAK_PTR_H_
+#include <cstddef>
#include <type_traits>
#include "base/base_export.h"
@@ -197,8 +198,9 @@ template <typename T> class WeakPtrFactory;
template <typename T>
class WeakPtr : public internal::WeakPtrBase {
public:
- WeakPtr() : ptr_(NULL) {
- }
+ WeakPtr() : ptr_(nullptr) {}
+
+ WeakPtr(std::nullptr_t) : ptr_(nullptr) {}
// Allow conversion from U to T provided U "is a" T. Note that this
// is separate from the (implicit) copy constructor.
@@ -206,20 +208,20 @@ class WeakPtr : public internal::WeakPtrBase {
WeakPtr(const WeakPtr<U>& other) : WeakPtrBase(other), ptr_(other.ptr_) {
}
- T* get() const { return ref_.is_valid() ? ptr_ : NULL; }
+ T* get() const { return ref_.is_valid() ? ptr_ : nullptr; }
T& operator*() const {
- DCHECK(get() != NULL);
+ DCHECK(get() != nullptr);
return *get();
}
T* operator->() const {
- DCHECK(get() != NULL);
+ DCHECK(get() != nullptr);
return get();
}
void reset() {
ref_ = internal::WeakReference();
- ptr_ = NULL;
+ ptr_ = nullptr;
}
// Implement "Safe Bool Idiom"
@@ -244,7 +246,7 @@ class WeakPtr : public internal::WeakPtrBase {
typedef T* WeakPtr::*Testable;
public:
- operator Testable() const { return get() ? &WeakPtr::ptr_ : NULL; }
+ operator Testable() const { return get() ? &WeakPtr::ptr_ : nullptr; }
private:
// Explicitly declare comparison operators as required by the "Safe Bool
@@ -263,7 +265,7 @@ class WeakPtr : public internal::WeakPtrBase {
}
// This pointer is only valid when ref_.is_valid() is true. Otherwise, its
- // value is undefined (as opposed to NULL).
+ // value is undefined (as opposed to nullptr).
T* ptr_;
};
@@ -278,9 +280,7 @@ class WeakPtrFactory {
explicit WeakPtrFactory(T* ptr) : ptr_(ptr) {
}
- ~WeakPtrFactory() {
- ptr_ = NULL;
- }
+ ~WeakPtrFactory() { ptr_ = nullptr; }
WeakPtr<T> GetWeakPtr() {
DCHECK(ptr_);
diff --git a/third_party/chromium/base/memory/weak_ptr_unittest.cc b/third_party/chromium/base/memory/weak_ptr_unittest.cc
index fdbb280..982becd 100644
--- a/third_party/chromium/base/memory/weak_ptr_unittest.cc
+++ b/third_party/chromium/base/memory/weak_ptr_unittest.cc
@@ -4,17 +4,21 @@
#include "base/memory/weak_ptr.h"
+#include <memory>
#include <string>
#include <gtest/gtest.h>
#include "base/bind.h"
#include "base/location.h"
-#include "base/memory/scoped_ptr.h"
namespace base {
namespace {
+WeakPtr<int> PassThru(WeakPtr<int> ptr) {
+ return ptr;
+}
+
struct Base {
std::string member;
};
@@ -52,13 +56,13 @@ TEST(WeakPtrFactoryTest, Comparison) {
TEST(WeakPtrFactoryTest, OutOfScope) {
WeakPtr<int> ptr;
- EXPECT_EQ(NULL, ptr.get());
+ EXPECT_EQ(nullptr, ptr.get());
{
int data;
WeakPtrFactory<int> factory(&data);
ptr = factory.GetWeakPtr();
}
- EXPECT_EQ(NULL, ptr.get());
+ EXPECT_EQ(nullptr, ptr.get());
}
TEST(WeakPtrFactoryTest, Multiple) {
@@ -71,8 +75,8 @@ TEST(WeakPtrFactoryTest, Multiple) {
EXPECT_EQ(&data, a.get());
EXPECT_EQ(&data, b.get());
}
- EXPECT_EQ(NULL, a.get());
- EXPECT_EQ(NULL, b.get());
+ EXPECT_EQ(nullptr, a.get());
+ EXPECT_EQ(nullptr, b.get());
}
TEST(WeakPtrFactoryTest, MultipleStaged) {
@@ -84,9 +88,9 @@ TEST(WeakPtrFactoryTest, MultipleStaged) {
{
WeakPtr<int> b = factory.GetWeakPtr();
}
- EXPECT_TRUE(NULL != a.get());
+ EXPECT_NE(nullptr, a.get());
}
- EXPECT_EQ(NULL, a.get());
+ EXPECT_EQ(nullptr, a.get());
}
TEST(WeakPtrFactoryTest, Dereference) {
@@ -107,6 +111,11 @@ TEST(WeakPtrFactoryTest, UpCast) {
EXPECT_EQ(ptr.get(), &data);
}
+TEST(WeakPtrTest, ConstructFromNullptr) {
+ WeakPtr<int> ptr = PassThru(nullptr);
+ EXPECT_EQ(nullptr, ptr.get());
+}
+
TEST(WeakPtrTest, SupportsWeakPtr) {
Target target;
WeakPtr<Target> ptr = target.AsWeakPtr();
@@ -157,7 +166,7 @@ TEST(WeakPtrTest, InvalidateWeakPtrs) {
EXPECT_EQ(&data, ptr.get());
EXPECT_TRUE(factory.HasWeakPtrs());
factory.InvalidateWeakPtrs();
- EXPECT_EQ(NULL, ptr.get());
+ EXPECT_EQ(nullptr, ptr.get());
EXPECT_FALSE(factory.HasWeakPtrs());
// Test that the factory can create new weak pointers after a
@@ -167,7 +176,7 @@ TEST(WeakPtrTest, InvalidateWeakPtrs) {
EXPECT_EQ(&data, ptr2.get());
EXPECT_TRUE(factory.HasWeakPtrs());
factory.InvalidateWeakPtrs();
- EXPECT_EQ(NULL, ptr2.get());
+ EXPECT_EQ(nullptr, ptr2.get());
EXPECT_FALSE(factory.HasWeakPtrs());
}