Skip to content

Commit

Permalink
[base] Allow movable types to derive from SupportsUserData.
Browse files Browse the repository at this point in the history
Making SupportsUserData movable allows it to be used to allow additional
data to be attached to movable data objects, e.g. we'll use this to allow
CertVerifyResult instances to have debug information attached by
different verifier implementations

Bug: 991247
Change-Id: Iba8c9000750559e7edbafac8e5ebefddf8a38e10
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1739147
Reviewed-by: Wez <wez@chromium.org>
Commit-Queue: Matt Mueller <mattm@chromium.org>
Cr-Commit-Position: refs/heads/master@{#685843}
  • Loading branch information
matt-mueller authored and Commit Bot committed Aug 10, 2019
1 parent c757549 commit 39fc01f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
3 changes: 3 additions & 0 deletions base/supports_user_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ SupportsUserData::SupportsUserData() {
sequence_checker_.DetachFromSequence();
}

SupportsUserData::SupportsUserData(SupportsUserData&&) = default;
SupportsUserData& SupportsUserData::operator=(SupportsUserData&&) = default;

SupportsUserData::Data* SupportsUserData::GetUserData(const void* key) const {
DCHECK(sequence_checker_.CalledOnValidSequence());
// Avoid null keys; they are too vulnerable to collision.
Expand Down
2 changes: 2 additions & 0 deletions base/supports_user_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ namespace base {
class BASE_EXPORT SupportsUserData {
public:
SupportsUserData();
SupportsUserData(SupportsUserData&&);
SupportsUserData& operator=(SupportsUserData&&);

// Derive from this class and add your own data members to associate extra
// information with this object. Alternatively, add this as a public base
Expand Down
18 changes: 18 additions & 0 deletions base/supports_user_data_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,23 @@ TEST(SupportsUserDataTest, ClearWorksRecursively) {
// Destruction of supports_user_data runs the actual test.
}

struct TestData : public SupportsUserData::Data {};

TEST(SupportsUserDataTest, Movable) {
TestSupportsUserData supports_user_data_1;
char key1 = 0;
supports_user_data_1.SetUserData(&key1, std::make_unique<TestData>());
void* data_1_ptr = supports_user_data_1.GetUserData(&key1);

TestSupportsUserData supports_user_data_2;
char key2 = 0;
supports_user_data_2.SetUserData(&key2, std::make_unique<TestData>());

supports_user_data_2 = std::move(supports_user_data_1);

EXPECT_EQ(data_1_ptr, supports_user_data_2.GetUserData(&key1));
EXPECT_EQ(nullptr, supports_user_data_2.GetUserData(&key2));
}

} // namespace
} // namespace base

0 comments on commit 39fc01f

Please sign in to comment.