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 ValueSet::ValueSet() { |
| 10 } |
| 11 |
| 12 ValueSet::~ValueSet() { |
| 13 } |
| 14 |
| 15 ValueSet::Entry::Entry(const base::Value* value) |
| 16 : value(value->DeepCopy()), |
| 17 count(1) { |
| 18 } |
| 19 |
| 20 ValueSet::Entry::~Entry() { |
| 21 } |
| 22 |
| 23 int ValueSet::Entry::Increment() { |
| 24 return ++count; |
| 25 } |
| 26 |
| 27 int ValueSet::Entry::Decrement() { |
| 28 return --count; |
| 29 } |
| 30 |
| 31 int ValueSet::Add(const base::Value* value) { |
| 32 return AddImpl(value, true); |
| 33 } |
| 34 |
| 35 int ValueSet::Remove(const base::Value* value) { |
| 36 for (EntryList::iterator it = entries_.begin(); it != entries_.end(); it++) { |
| 37 (*it)->value->GetType(); |
| 38 if ((*it)->value->Equals(value)) { |
| 39 int remaining = --(*it)->count; |
| 40 if (remaining == 0) { |
| 41 entries_.erase(it); |
| 42 } |
| 43 return remaining; |
| 44 } |
| 45 } |
| 46 return 0; |
| 47 } |
| 48 |
| 49 int ValueSet::AddIfMissing(const base::Value* value) { |
| 50 return AddImpl(value, false); |
| 51 } |
| 52 |
| 53 int ValueSet::AddImpl(const base::Value* value, bool increment) { |
| 54 for (EntryList::iterator it = entries_.begin(); it != entries_.end(); it++) { |
| 55 if ((*it)->value->Equals(value)) |
| 56 return increment ? (*it)->Increment() : (*it)->count; |
| 57 } |
| 58 entries_.push_back(linked_ptr<Entry>(new Entry(value->DeepCopy()))); |
| 59 return 1; |
| 60 } |
OLD | NEW |