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 #ifndef CHROME_COMMON_EXTENSIONS_VALUE_COUNTER_H_
6 #define CHROME_COMMON_EXTENSIONS_VALUE_COUNTER_H_
8 #include "base/memory/linked_ptr.h"
16 namespace extensions {
18 // Keeps a running count of Values, like map<Value, int>. Adding / removing
19 // values increments / decrements the count associated with a given Value.
21 // Add() and Remove() are linear in the number of Values in the ValueCounter,
22 // because there is no operator<() defined on Value, so we must iterate to find
23 // whether a Value is equal to an existing one.
29 // Adds |value| to the set and returns how many equal values are in the set
30 // after. Does not take ownership of |value|. In the case where a Value equal
31 // to |value| doesn't already exist in this map, this function makes a
32 // DeepCopy() of |value|.
33 int Add(const base::Value& value);
35 // Removes |value| from the set and returns how many equal values are in
37 int Remove(const base::Value& value);
39 // Same as Add() but only performs the add if the value isn't present.
40 int AddIfMissing(const base::Value& value);
45 explicit Entry(const base::Value& value);
51 const base::Value* value() const { return value_.get(); }
52 int count() const { return count_; }
55 linked_ptr<base::Value> value_;
58 DISALLOW_COPY_AND_ASSIGN(Entry);
60 typedef std::vector<linked_ptr<Entry> > EntryList;
62 int AddImpl(const base::Value& value, bool increment);
66 DISALLOW_COPY_AND_ASSIGN(ValueCounter);
69 } // namespace extensions
71 #endif // CHROME_COMMON_EXTENSIONS_VALUE_COUNTER_H_