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
3 // found in the LICENSE file.
5 #include "extensions/common/value_counter.h"
9 #include "base/values.h"
11 namespace extensions
{
13 struct ValueCounter::Entry
{
14 explicit Entry(scoped_ptr
<base::Value
> value
)
15 : value(value
.Pass()), count(1) {}
17 scoped_ptr
<base::Value
> value
;
21 ValueCounter::ValueCounter() {
24 ValueCounter::~ValueCounter() {
27 bool ValueCounter::Add(const base::Value
& value
) {
28 for (Entry
* entry
: entries_
) {
29 if (entry
->value
->Equals(&value
)) {
34 entries_
.push_back(new Entry(value
.CreateDeepCopy()));
38 bool ValueCounter::Remove(const base::Value
& value
) {
39 for (ScopedVector
<Entry
>::iterator it
= entries_
.begin();
40 it
!= entries_
.end(); ++it
) {
41 if ((*it
)->value
->Equals(&value
)) {
42 if (--(*it
)->count
== 0) {
43 std::swap(*it
, entries_
.back());
45 return true; // Removed the last entry.
47 return false; // Removed, but no the last entry.
50 return false; // Nothing to remove.
53 } // namespace extensions