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

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

Issue 813553002: Adding support for pre-sent messages (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@state
Patch Set: Created 5 years, 11 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
« no previous file with comments | « components/copresence/rpc/rpc_handler_unittest.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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>
11 #include <vector> 11 #include <vector>
12 12
13 #include "base/macros.h" 13 #include "base/macros.h"
14 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/scoped_ptr.h"
15 #include "base/time/default_tick_clock.h" 15 #include "base/time/default_tick_clock.h"
16 #include "base/time/tick_clock.h" 16 #include "base/time/tick_clock.h"
17 #include "base/time/time.h" 17 #include "base/time/time.h"
18 #include "base/timer/timer.h" 18 #include "base/timer/timer.h"
19 19
20 namespace copresence { 20 namespace copresence {
21 21
22 // TimedMap is a map with the added functionality of clearing any 22 // TimedMap is a map with the added functionality of clearing any
23 // key/value pair after its specified lifetime is over. 23 // key/value pair after its specified lifetime is over.
24 // TODO(ckehoe): Why is this interface so different from std::map?
24 template <typename KeyType, typename ValueType> 25 template <typename KeyType, typename ValueType>
25 class TimedMap { 26 class TimedMap {
26 public: 27 public:
27 TimedMap(const base::TimeDelta& lifetime, size_t max_elements) 28 TimedMap(const base::TimeDelta& lifetime, size_t max_elements)
28 : kEmptyValue(ValueType()), 29 : kEmptyValue(ValueType()),
29 clock_(new base::DefaultTickClock()), 30 clock_(new base::DefaultTickClock()),
30 lifetime_(lifetime), 31 lifetime_(lifetime),
31 max_elements_(max_elements) { 32 max_elements_(max_elements) {
32 timer_.Start(FROM_HERE, lifetime_, this, &TimedMap::ClearExpiredTokens); 33 timer_.Start(FROM_HERE, lifetime_, this, &TimedMap::ClearExpiredTokens);
33 } 34 }
(...skipping 11 matching lines...) Expand all
45 ClearExpiredTokens(); 46 ClearExpiredTokens();
46 return map_.find(key) != map_.end(); 47 return map_.find(key) != map_.end();
47 } 48 }
48 49
49 const ValueType& GetValue(const KeyType& key) { 50 const ValueType& GetValue(const KeyType& key) {
50 ClearExpiredTokens(); 51 ClearExpiredTokens();
51 auto elt = map_.find(key); 52 auto elt = map_.find(key);
52 return elt == map_.end() ? kEmptyValue : elt->second; 53 return elt == map_.end() ? kEmptyValue : elt->second;
53 } 54 }
54 55
56 ValueType* GetMutableValue(const KeyType& key) {
57 ClearExpiredTokens();
58 auto elt = map_.find(key);
59 return elt == map_.end() ? nullptr : &(elt->second);
60 }
61
62 // TODO(ckehoe): Add a unit test for this.
63 size_t Erase(const KeyType& key) {
64 return map_.erase(key);
65 }
66
55 void set_clock_for_testing(scoped_ptr<base::TickClock> clock) { 67 void set_clock_for_testing(scoped_ptr<base::TickClock> clock) {
56 clock_ = clock.Pass(); 68 clock_ = clock.Pass();
57 } 69 }
58 70
59 private: 71 private:
60 void ClearExpiredTokens() { 72 void ClearExpiredTokens() {
61 while (!expiry_queue_.empty() && 73 while (!expiry_queue_.empty() &&
62 expiry_queue_.top().second <= clock_->NowTicks()) 74 expiry_queue_.top().second <= clock_->NowTicks())
63 ClearOldestToken(); 75 ClearOldestToken();
64 } 76 }
(...skipping 26 matching lines...) Expand all
91 // Priority queue with our element keys ordered by the earliest expiring keys 103 // Priority queue with our element keys ordered by the earliest expiring keys
92 // first. 104 // first.
93 ExpiryQueue expiry_queue_; 105 ExpiryQueue expiry_queue_;
94 106
95 DISALLOW_COPY_AND_ASSIGN(TimedMap); 107 DISALLOW_COPY_AND_ASSIGN(TimedMap);
96 }; 108 };
97 109
98 } // namespace copresence 110 } // namespace copresence
99 111
100 #endif // COMPONENTS_COPRESENCE_TIMED_MAP_H_ 112 #endif // COMPONENTS_COPRESENCE_TIMED_MAP_H_
OLDNEW
« no previous file with comments | « components/copresence/rpc/rpc_handler_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698