| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/common/extensions/value_counter.h" | 5 #include "extensions/common/value_counter.h" |
| 6 |
| 7 #include <algorithm> |
| 6 | 8 |
| 7 #include "base/values.h" | 9 #include "base/values.h" |
| 8 | 10 |
| 9 #include <algorithm> | |
| 10 | |
| 11 namespace extensions { | 11 namespace extensions { |
| 12 | 12 |
| 13 ValueCounter::ValueCounter() { | 13 ValueCounter::ValueCounter() {} |
| 14 } | |
| 15 | 14 |
| 16 ValueCounter::~ValueCounter() { | 15 ValueCounter::~ValueCounter() {} |
| 17 } | |
| 18 | 16 |
| 19 ValueCounter::Entry::Entry(const base::Value& value) | 17 ValueCounter::Entry::Entry(const base::Value& value) |
| 20 : value_(value.DeepCopy()), | 18 : value_(value.DeepCopy()), count_(1) {} |
| 21 count_(1) { | |
| 22 } | |
| 23 | 19 |
| 24 ValueCounter::Entry::~Entry() { | 20 ValueCounter::Entry::~Entry() {} |
| 25 } | |
| 26 | 21 |
| 27 int ValueCounter::Entry::Increment() { | 22 int ValueCounter::Entry::Increment() { return ++count_; } |
| 28 return ++count_; | |
| 29 } | |
| 30 | 23 |
| 31 int ValueCounter::Entry::Decrement() { | 24 int ValueCounter::Entry::Decrement() { return --count_; } |
| 32 return --count_; | |
| 33 } | |
| 34 | 25 |
| 35 int ValueCounter::Add(const base::Value& value) { | 26 int ValueCounter::Add(const base::Value& value) { return AddImpl(value, true); } |
| 36 return AddImpl(value, true); | |
| 37 } | |
| 38 | 27 |
| 39 int ValueCounter::Remove(const base::Value& value) { | 28 int ValueCounter::Remove(const base::Value& value) { |
| 40 for (EntryList::iterator it = entries_.begin(); it != entries_.end(); it++) { | 29 for (EntryList::iterator it = entries_.begin(); it != entries_.end(); it++) { |
| 41 (*it)->value()->GetType(); | 30 (*it)->value()->GetType(); |
| 42 if ((*it)->value()->Equals(&value)) { | 31 if ((*it)->value()->Equals(&value)) { |
| 43 int remaining = (*it)->Decrement(); | 32 int remaining = (*it)->Decrement(); |
| 44 if (remaining == 0) { | 33 if (remaining == 0) { |
| 45 std::swap(*it, entries_.back()); | 34 std::swap(*it, entries_.back()); |
| 46 entries_.pop_back(); | 35 entries_.pop_back(); |
| 47 } | 36 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 58 int ValueCounter::AddImpl(const base::Value& value, bool increment) { | 47 int ValueCounter::AddImpl(const base::Value& value, bool increment) { |
| 59 for (EntryList::iterator it = entries_.begin(); it != entries_.end(); it++) { | 48 for (EntryList::iterator it = entries_.begin(); it != entries_.end(); it++) { |
| 60 if ((*it)->value()->Equals(&value)) | 49 if ((*it)->value()->Equals(&value)) |
| 61 return increment ? (*it)->Increment() : (*it)->count(); | 50 return increment ? (*it)->Increment() : (*it)->count(); |
| 62 } | 51 } |
| 63 entries_.push_back(linked_ptr<Entry>(new Entry(value))); | 52 entries_.push_back(linked_ptr<Entry>(new Entry(value))); |
| 64 return 1; | 53 return 1; |
| 65 } | 54 } |
| 66 | 55 |
| 67 } // namespace extensions | 56 } // namespace extensions |
| OLD | NEW |