Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "chrome/common/extensions/value_counter.h" | |
| 6 | |
| 7 #include "base/values.h" | |
| 8 | |
| 9 namespace extensions { | |
| 10 | |
| 11 ValueCounter::ValueCounter() { | |
| 12 } | |
| 13 | |
| 14 ValueCounter::~ValueCounter() { | |
| 15 } | |
| 16 | |
| 17 ValueCounter::Entry::Entry(const base::Value& value) | |
| 18 : value_(value.DeepCopy()), | |
| 19 count_(1) { | |
| 20 } | |
| 21 | |
| 22 ValueCounter::Entry::~Entry() { | |
| 23 } | |
| 24 | |
| 25 int ValueCounter::Entry::Increment() { | |
| 26 return ++count_; | |
| 27 } | |
| 28 | |
| 29 int ValueCounter::Entry::Decrement() { | |
| 30 return --count_; | |
| 31 } | |
| 32 | |
| 33 int ValueCounter::Add(const base::Value& value) { | |
| 34 return AddImpl(value, true); | |
| 35 } | |
| 36 | |
| 37 int ValueCounter::Remove(const base::Value& value) { | |
| 38 for (EntryList::iterator it = entries_.begin(); it != entries_.end(); it++) { | |
| 39 (*it)->value()->GetType(); | |
| 40 if ((*it)->value()->Equals(&value)) { | |
| 41 int remaining = (*it)->Decrement(); | |
| 42 if (remaining == 0) { | |
| 43 entries_.erase(it); | |
|
battre
2012/06/20 09:50:46
swap it with entries_.back() and pop_back()?
nit:
koz (OOO until 15th September)
2012/06/21 02:23:11
Done.
| |
| 44 } | |
| 45 return remaining; | |
| 46 } | |
| 47 } | |
| 48 return 0; | |
| 49 } | |
| 50 | |
| 51 int ValueCounter::AddIfMissing(const base::Value& value) { | |
| 52 return AddImpl(value, false); | |
| 53 } | |
| 54 | |
| 55 int ValueCounter::AddImpl(const base::Value& value, bool increment) { | |
| 56 for (EntryList::iterator it = entries_.begin(); it != entries_.end(); it++) { | |
| 57 if ((*it)->value()->Equals(&value)) | |
| 58 return increment ? (*it)->Increment() : (*it)->count(); | |
| 59 } | |
| 60 entries_.push_back(linked_ptr<Entry>(new Entry(value))); | |
| 61 return 1; | |
| 62 } | |
| 63 | |
| 64 } // namespace extensions | |
| OLD | NEW |