1 // Copyright (c) 2012 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 "chrome/common/extensions/value_counter.h"
7 #include "base/values.h"
11 namespace extensions
{
13 ValueCounter::ValueCounter() {
16 ValueCounter::~ValueCounter() {
19 ValueCounter::Entry::Entry(const base::Value
& value
)
20 : value_(value
.DeepCopy()),
24 ValueCounter::Entry::~Entry() {
27 int ValueCounter::Entry::Increment() {
31 int ValueCounter::Entry::Decrement() {
35 int ValueCounter::Add(const base::Value
& value
) {
36 return AddImpl(value
, true);
39 int ValueCounter::Remove(const base::Value
& value
) {
40 for (EntryList::iterator it
= entries_
.begin(); it
!= entries_
.end(); it
++) {
41 (*it
)->value()->GetType();
42 if ((*it
)->value()->Equals(&value
)) {
43 int remaining
= (*it
)->Decrement();
45 std::swap(*it
, entries_
.back());
54 int ValueCounter::AddIfMissing(const base::Value
& value
) {
55 return AddImpl(value
, false);
58 int ValueCounter::AddImpl(const base::Value
& value
, bool increment
) {
59 for (EntryList::iterator it
= entries_
.begin(); it
!= entries_
.end(); it
++) {
60 if ((*it
)->value()->Equals(&value
))
61 return increment
? (*it
)->Increment() : (*it
)->count();
63 entries_
.push_back(linked_ptr
<Entry
>(new Entry(value
)));
67 } // namespace extensions