Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(447)

Side by Side Diff: components/copresence/timed_map.h

Issue 670623002: Change base::TickClock to a ref counted class. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@audio_redesign
Patch Set: y Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 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 #ifndef COMPONENTS_COPRESENCE_TIMED_MAP_H_ 5 #ifndef COMPONENTS_COPRESENCE_TIMED_MAP_H_
6 #define COMPONENTS_COPRESENCE_TIMED_MAP_H_ 6 #define COMPONENTS_COPRESENCE_TIMED_MAP_H_
7 7
8 #include <map> 8 #include <map>
9 #include <queue> 9 #include <queue>
10 #include <utility> 10 #include <utility>
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 ClearExpiredTokens(); 45 ClearExpiredTokens();
46 return map_.find(key) != map_.end(); 46 return map_.find(key) != map_.end();
47 } 47 }
48 48
49 const ValueType& GetValue(const KeyType& key) { 49 const ValueType& GetValue(const KeyType& key) {
50 ClearExpiredTokens(); 50 ClearExpiredTokens();
51 auto elt = map_.find(key); 51 auto elt = map_.find(key);
52 return elt == map_.end() ? kEmptyValue : elt->second; 52 return elt == map_.end() ? kEmptyValue : elt->second;
53 } 53 }
54 54
55 void set_clock_for_testing(scoped_ptr<base::TickClock> clock) { 55 void set_clock_for_testing(const scoped_refptr<base::TickClock>& clock) {
56 clock_ = clock.Pass(); 56 clock_ = clock;
57 } 57 }
58 58
59 private: 59 private:
60 void ClearExpiredTokens() { 60 void ClearExpiredTokens() {
61 while (!expiry_queue_.empty() && 61 while (!expiry_queue_.empty() &&
62 expiry_queue_.top().second <= clock_->NowTicks()) 62 expiry_queue_.top().second <= clock_->NowTicks())
63 ClearOldestToken(); 63 ClearOldestToken();
64 } 64 }
65 65
66 void ClearOldestToken() { 66 void ClearOldestToken() {
67 map_.erase(expiry_queue_.top().first); 67 map_.erase(expiry_queue_.top().first);
68 expiry_queue_.pop(); 68 expiry_queue_.pop();
69 } 69 }
70 70
71 typedef std::pair<KeyType, base::TimeTicks> KeyTimeTuple; 71 typedef std::pair<KeyType, base::TimeTicks> KeyTimeTuple;
72 72
73 class EarliestFirstComparator { 73 class EarliestFirstComparator {
74 public: 74 public:
75 // This will sort our queue with the 'earliest' time being the top. 75 // This will sort our queue with the 'earliest' time being the top.
76 bool operator()(const KeyTimeTuple& left, const KeyTimeTuple& right) const { 76 bool operator()(const KeyTimeTuple& left, const KeyTimeTuple& right) const {
77 return left.second > right.second; 77 return left.second > right.second;
78 } 78 }
79 }; 79 };
80 80
81 typedef std::priority_queue<KeyTimeTuple, std::vector<KeyTimeTuple>, 81 typedef std::priority_queue<KeyTimeTuple, std::vector<KeyTimeTuple>,
82 EarliestFirstComparator> ExpiryQueue; 82 EarliestFirstComparator> ExpiryQueue;
83 83
84 const ValueType kEmptyValue; 84 const ValueType kEmptyValue;
85 85
86 scoped_ptr<base::TickClock> clock_; 86 scoped_refptr<base::TickClock> clock_;
87 base::RepeatingTimer<TimedMap> timer_; 87 base::RepeatingTimer<TimedMap> timer_;
88 const base::TimeDelta lifetime_; 88 const base::TimeDelta lifetime_;
89 const size_t max_elements_; 89 const size_t max_elements_;
90 std::map<KeyType, ValueType> map_; 90 std::map<KeyType, ValueType> map_;
91 // Priority queue with our element keys ordered by the earliest expiring keys 91 // Priority queue with our element keys ordered by the earliest expiring keys
92 // first. 92 // first.
93 ExpiryQueue expiry_queue_; 93 ExpiryQueue expiry_queue_;
94 94
95 DISALLOW_COPY_AND_ASSIGN(TimedMap); 95 DISALLOW_COPY_AND_ASSIGN(TimedMap);
96 }; 96 };
97 97
98 } // namespace copresence 98 } // namespace copresence
99 99
100 #endif // COMPONENTS_COPRESENCE_TIMED_MAP_H_ 100 #endif // COMPONENTS_COPRESENCE_TIMED_MAP_H_
OLDNEW
« no previous file with comments | « components/copresence/handlers/audio/audio_directive_list.cc ('k') | components/copresence/timed_map_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698