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_set.h" |
| 6 |
| 7 #include "base/values.h" |
| 8 |
| 9 namespace extensions { |
| 10 |
| 11 ValueSet::ValueSet() { |
| 12 } |
| 13 |
| 14 ValueSet::~ValueSet() { |
| 15 } |
| 16 |
| 17 ValueSet::Entry::Entry(const base::Value* value) |
| 18 : value(value->DeepCopy()), |
| 19 count(1) { |
| 20 } |
| 21 |
| 22 ValueSet::Entry::~Entry() { |
| 23 } |
| 24 |
| 25 int ValueSet::Entry::Increment() { |
| 26 return ++count; |
| 27 } |
| 28 |
| 29 int ValueSet::Entry::Decrement() { |
| 30 return --count; |
| 31 } |
| 32 |
| 33 int ValueSet::Add(const base::Value* value) { |
| 34 return AddImpl(value, true); |
| 35 } |
| 36 |
| 37 int ValueSet::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)->count; |
| 42 if (remaining == 0) { |
| 43 entries_.erase(it); |
| 44 } |
| 45 return remaining; |
| 46 } |
| 47 } |
| 48 return 0; |
| 49 } |
| 50 |
| 51 int ValueSet::AddIfMissing(const base::Value* value) { |
| 52 return AddImpl(value, false); |
| 53 } |
| 54 |
| 55 int ValueSet::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->DeepCopy()))); |
| 61 return 1; |
| 62 } |
| 63 |
| 64 } // namespace extensions |
OLD | NEW |