Commit ae5e2d28 authored by sdefresne's avatar sdefresne Committed by Commit bot

Update SupportsUserData API to accept std::unique_ptr<>.

SupportsUserData takes ownership of the SupportsUserData::Data
passed to SetUserData. Add an overload taking a std::unique_ptr
so that client code can be converted incrementally.

BUG=690937

Review-Url: https://codereview.chromium.org/2680403004
Cr-Commit-Position: refs/heads/master@{#449933}
parent 128197bb
......@@ -23,8 +23,13 @@ SupportsUserData::Data* SupportsUserData::GetUserData(const void* key) const {
}
void SupportsUserData::SetUserData(const void* key, Data* data) {
SetUserData(key, WrapUnique(data));
}
void SupportsUserData::SetUserData(const void* key,
std::unique_ptr<Data> data) {
DCHECK(sequence_checker_.CalledOnValidSequence());
user_data_[key] = WrapUnique(data);
user_data_[key] = std::move(data);
}
void SupportsUserData::RemoveUserData(const void* key) {
......
......@@ -37,8 +37,11 @@ class BASE_EXPORT SupportsUserData {
// Multiple user data values can be stored under different keys.
// This object will TAKE OWNERSHIP of the given data pointer, and will
// delete the object if it is changed or the object is destroyed.
// TODO: remove the raw ptr version of SetUserData once everything uses
// the unique_ptr version, see crbug.com/690937.
Data* GetUserData(const void* key) const;
void SetUserData(const void* key, Data* data);
void SetUserData(const void* key, std::unique_ptr<Data> data);
void RemoveUserData(const void* key);
// SupportsUserData is not thread-safe, and on debug build will assert it is
......
......@@ -6,6 +6,7 @@
#include <vector>
#include "base/memory/ptr_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
......@@ -30,8 +31,8 @@ struct UsesItself : public SupportsUserData::Data {
TEST(SupportsUserDataTest, ClearWorksRecursively) {
TestSupportsUserData supports_user_data;
char key = 0;
supports_user_data.SetUserData(&key,
new UsesItself(&supports_user_data, &key));
supports_user_data.SetUserData(
&key, base::MakeUnique<UsesItself>(&supports_user_data, &key));
// Destruction of supports_user_data runs the actual test.
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment