Commit 927382fa authored by Istiaque Ahmed's avatar Istiaque Ahmed Committed by Commit Bot

Make ValueCounter::Entry non-copyable and move-able

This CL also makes its member to be stack allocated base::Value
instead of dynamic allocated one.

Bug: None
Test: Minor cleanup, no behavior changes expected.
Change-Id: I6b97497c43e7c37b6dd5481049834c3b54b9b1a6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2133207
Commit-Queue: Istiaque Ahmed <lazyboy@chromium.org>
Reviewed-by: default avatarDevlin <rdevlin.cronin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#756056}
parent f56e090c
...@@ -13,10 +13,15 @@ ...@@ -13,10 +13,15 @@
namespace extensions { namespace extensions {
struct ValueCounter::Entry { struct ValueCounter::Entry {
explicit Entry(std::unique_ptr<base::Value> value) explicit Entry(base::Value value) : value(std::move(value)), count(1) {}
: value(std::move(value)), count(1) {}
std::unique_ptr<base::Value> value; Entry(Entry&) = delete;
Entry& operator=(Entry&) = delete;
Entry(Entry&&) = default;
Entry& operator=(Entry&&) = default;
base::Value value;
int count; int count;
}; };
...@@ -27,20 +32,20 @@ ValueCounter::~ValueCounter() { ...@@ -27,20 +32,20 @@ ValueCounter::~ValueCounter() {
} }
bool ValueCounter::Add(const base::Value& value) { bool ValueCounter::Add(const base::Value& value) {
for (const auto& entry : entries_) { for (auto& entry : entries_) {
if (entry->value->Equals(&value)) { if (entry.value.Equals(&value)) {
++entry->count; ++entry.count;
return false; return false;
} }
} }
entries_.push_back(std::make_unique<Entry>(value.CreateDeepCopy())); entries_.emplace_back(value.Clone());
return true; return true;
} }
bool ValueCounter::Remove(const base::Value& value) { bool ValueCounter::Remove(const base::Value& value) {
for (auto it = entries_.begin(); it != entries_.end(); ++it) { for (auto it = entries_.begin(); it != entries_.end(); ++it) {
if ((*it)->value->Equals(&value)) { if (it->value.Equals(&value)) {
if (--(*it)->count == 0) { if (--it->count == 0) {
std::swap(*it, entries_.back()); std::swap(*it, entries_.back());
entries_.pop_back(); entries_.pop_back();
return true; // Removed the last entry. return true; // Removed the last entry.
......
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
#ifndef EXTENSIONS_COMMON_VALUE_COUNTER_H_ #ifndef EXTENSIONS_COMMON_VALUE_COUNTER_H_
#define EXTENSIONS_COMMON_VALUE_COUNTER_H_ #define EXTENSIONS_COMMON_VALUE_COUNTER_H_
#include <memory>
#include <vector> #include <vector>
#include "base/macros.h" #include "base/macros.h"
...@@ -42,7 +41,7 @@ class ValueCounter { ...@@ -42,7 +41,7 @@ class ValueCounter {
private: private:
struct Entry; struct Entry;
std::vector<std::unique_ptr<Entry>> entries_; std::vector<Entry> entries_;
DISALLOW_COPY_AND_ASSIGN(ValueCounter); DISALLOW_COPY_AND_ASSIGN(ValueCounter);
}; };
......
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