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 @@
namespace extensions {
struct ValueCounter::Entry {
explicit Entry(std::unique_ptr<base::Value> value)
: value(std::move(value)), count(1) {}
explicit Entry(base::Value value) : 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;
};
......@@ -27,20 +32,20 @@ ValueCounter::~ValueCounter() {
}
bool ValueCounter::Add(const base::Value& value) {
for (const auto& entry : entries_) {
if (entry->value->Equals(&value)) {
++entry->count;
for (auto& entry : entries_) {
if (entry.value.Equals(&value)) {
++entry.count;
return false;
}
}
entries_.push_back(std::make_unique<Entry>(value.CreateDeepCopy()));
entries_.emplace_back(value.Clone());
return true;
}
bool ValueCounter::Remove(const base::Value& value) {
for (auto it = entries_.begin(); it != entries_.end(); ++it) {
if ((*it)->value->Equals(&value)) {
if (--(*it)->count == 0) {
if (it->value.Equals(&value)) {
if (--it->count == 0) {
std::swap(*it, entries_.back());
entries_.pop_back();
return true; // Removed the last entry.
......
......@@ -5,7 +5,6 @@
#ifndef EXTENSIONS_COMMON_VALUE_COUNTER_H_
#define EXTENSIONS_COMMON_VALUE_COUNTER_H_
#include <memory>
#include <vector>
#include "base/macros.h"
......@@ -42,7 +41,7 @@ class ValueCounter {
private:
struct Entry;
std::vector<std::unique_ptr<Entry>> entries_;
std::vector<Entry> entries_;
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