Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / extensions / common / value_counter.h
blob948cd30e587784ff2c581329d1745a8f7d15d01c
1 // Copyright 2014 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 EXTENSIONS_COMMON_VALUE_COUNTER_H_
6 #define EXTENSIONS_COMMON_VALUE_COUNTER_H_
8 #include <vector>
10 #include "base/memory/linked_ptr.h"
12 namespace base {
13 class Value;
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.
24 class ValueCounter {
25 public:
26 ValueCounter();
27 ~ValueCounter();
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
36 // the set after.
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);
42 private:
43 class Entry {
44 public:
45 explicit Entry(const base::Value& value);
46 ~Entry();
48 int Increment();
49 int Decrement();
51 const base::Value* value() const { return value_.get(); }
52 int count() const { return count_; }
54 private:
55 linked_ptr<base::Value> value_;
56 int count_;
58 DISALLOW_COPY_AND_ASSIGN(Entry);
60 typedef std::vector<linked_ptr<Entry> > EntryList;
62 int AddImpl(const base::Value& value, bool increment);
64 EntryList entries_;
66 DISALLOW_COPY_AND_ASSIGN(ValueCounter);
69 } // namespace extensions
71 #endif // EXTENSIONS_COMMON_VALUE_COUNTER_H_