| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "extensions/common/value_counter.h" | 5 #include "extensions/common/value_counter.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <memory> |
| 8 #include <utility> | 9 #include <utility> |
| 9 | 10 |
| 11 #include "base/memory/ptr_util.h" |
| 10 #include "base/values.h" | 12 #include "base/values.h" |
| 11 | 13 |
| 12 namespace extensions { | 14 namespace extensions { |
| 13 | 15 |
| 14 struct ValueCounter::Entry { | 16 struct ValueCounter::Entry { |
| 15 explicit Entry(scoped_ptr<base::Value> value) | 17 explicit Entry(std::unique_ptr<base::Value> value) |
| 16 : value(std::move(value)), count(1) {} | 18 : value(std::move(value)), count(1) {} |
| 17 | 19 |
| 18 scoped_ptr<base::Value> value; | 20 std::unique_ptr<base::Value> value; |
| 19 int count; | 21 int count; |
| 20 }; | 22 }; |
| 21 | 23 |
| 22 ValueCounter::ValueCounter() { | 24 ValueCounter::ValueCounter() { |
| 23 } | 25 } |
| 24 | 26 |
| 25 ValueCounter::~ValueCounter() { | 27 ValueCounter::~ValueCounter() { |
| 26 } | 28 } |
| 27 | 29 |
| 28 bool ValueCounter::Add(const base::Value& value) { | 30 bool ValueCounter::Add(const base::Value& value) { |
| 29 for (const auto& entry : entries_) { | 31 for (const auto& entry : entries_) { |
| 30 if (entry->value->Equals(&value)) { | 32 if (entry->value->Equals(&value)) { |
| 31 ++entry->count; | 33 ++entry->count; |
| 32 return false; | 34 return false; |
| 33 } | 35 } |
| 34 } | 36 } |
| 35 entries_.push_back(make_scoped_ptr(new Entry(value.CreateDeepCopy()))); | 37 entries_.push_back(base::WrapUnique(new Entry(value.CreateDeepCopy()))); |
| 36 return true; | 38 return true; |
| 37 } | 39 } |
| 38 | 40 |
| 39 bool ValueCounter::Remove(const base::Value& value) { | 41 bool ValueCounter::Remove(const base::Value& value) { |
| 40 for (auto it = entries_.begin(); it != entries_.end(); ++it) { | 42 for (auto it = entries_.begin(); it != entries_.end(); ++it) { |
| 41 if ((*it)->value->Equals(&value)) { | 43 if ((*it)->value->Equals(&value)) { |
| 42 if (--(*it)->count == 0) { | 44 if (--(*it)->count == 0) { |
| 43 std::swap(*it, entries_.back()); | 45 std::swap(*it, entries_.back()); |
| 44 entries_.pop_back(); | 46 entries_.pop_back(); |
| 45 return true; // Removed the last entry. | 47 return true; // Removed the last entry. |
| 46 } | 48 } |
| 47 return false; // Removed, but no the last entry. | 49 return false; // Removed, but no the last entry. |
| 48 } | 50 } |
| 49 } | 51 } |
| 50 return false; // Nothing to remove. | 52 return false; // Nothing to remove. |
| 51 } | 53 } |
| 52 | 54 |
| 53 } // namespace extensions | 55 } // namespace extensions |
| OLD | NEW |