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

Side by Side Diff: base/timer.h

Issue 7995: Move Time, TimeDelta and TimeTicks into namespace base. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 12 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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 // OneShotTimer and RepeatingTimer provide a simple timer API. As the names 5 // OneShotTimer and RepeatingTimer provide a simple timer API. As the names
6 // suggest, OneShotTimer calls you back once after a time delay expires. 6 // suggest, OneShotTimer calls you back once after a time delay expires.
7 // RepeatingTimer on the other hand calls you back periodically with the 7 // RepeatingTimer on the other hand calls you back periodically with the
8 // prescribed time interval. 8 // prescribed time interval.
9 // 9 //
10 // OneShotTimer and RepeatingTimer both cancel the timer when they go out of 10 // OneShotTimer and RepeatingTimer both cancel the timer when they go out of
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 OrphanDelayedTask(); 61 OrphanDelayedTask();
62 } 62 }
63 63
64 // Returns true if the timer is running (i.e., not stopped). 64 // Returns true if the timer is running (i.e., not stopped).
65 bool IsRunning() const { 65 bool IsRunning() const {
66 return delayed_task_ != NULL; 66 return delayed_task_ != NULL;
67 } 67 }
68 68
69 // Returns the current delay for this timer. May only call this method when 69 // Returns the current delay for this timer. May only call this method when
70 // the timer is running! 70 // the timer is running!
71 TimeDelta GetCurrentDelay() const { 71 base::TimeDelta GetCurrentDelay() const {
72 DCHECK(IsRunning()); 72 DCHECK(IsRunning());
73 return delayed_task_->delay_; 73 return delayed_task_->delay_;
74 } 74 }
75 75
76 protected: 76 protected:
77 BaseTimer_Helper() : delayed_task_(NULL) {} 77 BaseTimer_Helper() : delayed_task_(NULL) {}
78 78
79 // We have access to the timer_ member so we can orphan this task. 79 // We have access to the timer_ member so we can orphan this task.
80 class TimerTask : public Task { 80 class TimerTask : public Task {
81 public: 81 public:
82 TimerTask(TimeDelta delay) : delay_(delay) { 82 TimerTask(base::TimeDelta delay) : delay_(delay) {
83 // timer_ is set in InitiateDelayedTask. 83 // timer_ is set in InitiateDelayedTask.
84 } 84 }
85 BaseTimer_Helper* timer_; 85 BaseTimer_Helper* timer_;
86 TimeDelta delay_; 86 base::TimeDelta delay_;
87 }; 87 };
88 88
89 // Used to orphan delayed_task_ so that when it runs it does nothing. 89 // Used to orphan delayed_task_ so that when it runs it does nothing.
90 void OrphanDelayedTask(); 90 void OrphanDelayedTask();
91 91
92 // Used to initiated a new delayed task. This has the side-effect of 92 // Used to initiated a new delayed task. This has the side-effect of
93 // orphaning delayed_task_ if it is non-null. 93 // orphaning delayed_task_ if it is non-null.
94 void InitiateDelayedTask(TimerTask* timer_task); 94 void InitiateDelayedTask(TimerTask* timer_task);
95 95
96 TimerTask* delayed_task_; 96 TimerTask* delayed_task_;
97 97
98 DISALLOW_COPY_AND_ASSIGN(BaseTimer_Helper); 98 DISALLOW_COPY_AND_ASSIGN(BaseTimer_Helper);
99 }; 99 };
100 100
101 //----------------------------------------------------------------------------- 101 //-----------------------------------------------------------------------------
102 // This class is an implementation detail of OneShotTimer and RepeatingTimer. 102 // This class is an implementation detail of OneShotTimer and RepeatingTimer.
103 // Please do not use this class directly. 103 // Please do not use this class directly.
104 template <class Receiver, bool kIsRepeating> 104 template <class Receiver, bool kIsRepeating>
105 class BaseTimer : public BaseTimer_Helper { 105 class BaseTimer : public BaseTimer_Helper {
106 public: 106 public:
107 typedef void (Receiver::*ReceiverMethod)(); 107 typedef void (Receiver::*ReceiverMethod)();
108 108
109 // Call this method to start the timer. It is an error to call this method 109 // Call this method to start the timer. It is an error to call this method
110 // while the timer is already running. 110 // while the timer is already running.
111 void Start(TimeDelta delay, Receiver* receiver, ReceiverMethod method) { 111 void Start(base::TimeDelta delay, Receiver* receiver, ReceiverMethod method) {
112 DCHECK(!IsRunning()); 112 DCHECK(!IsRunning());
113 InitiateDelayedTask(new TimerTask(delay, receiver, method)); 113 InitiateDelayedTask(new TimerTask(delay, receiver, method));
114 } 114 }
115 115
116 // Call this method to stop the timer. It is a no-op if the timer is not 116 // Call this method to stop the timer. It is a no-op if the timer is not
117 // running. 117 // running.
118 void Stop() { 118 void Stop() {
119 OrphanDelayedTask(); 119 OrphanDelayedTask();
120 } 120 }
121 121
122 // Call this method to reset the timer delay of an already running timer. 122 // Call this method to reset the timer delay of an already running timer.
123 void Reset() { 123 void Reset() {
124 DCHECK(IsRunning()); 124 DCHECK(IsRunning());
125 InitiateDelayedTask(static_cast<TimerTask*>(delayed_task_)->Clone()); 125 InitiateDelayedTask(static_cast<TimerTask*>(delayed_task_)->Clone());
126 } 126 }
127 127
128 private: 128 private:
129 typedef BaseTimer<Receiver, kIsRepeating> SelfType; 129 typedef BaseTimer<Receiver, kIsRepeating> SelfType;
130 130
131 class TimerTask : public BaseTimer_Helper::TimerTask { 131 class TimerTask : public BaseTimer_Helper::TimerTask {
132 public: 132 public:
133 TimerTask(TimeDelta delay, Receiver* receiver, ReceiverMethod method) 133 TimerTask(base::TimeDelta delay, Receiver* receiver, ReceiverMethod method)
134 : BaseTimer_Helper::TimerTask(delay), 134 : BaseTimer_Helper::TimerTask(delay),
135 receiver_(receiver), 135 receiver_(receiver),
136 method_(method) { 136 method_(method) {
137 } 137 }
138 virtual void Run() { 138 virtual void Run() {
139 if (!timer_) // timer_ is null if we were orphaned. 139 if (!timer_) // timer_ is null if we were orphaned.
140 return; 140 return;
141 SelfType* self = static_cast<SelfType*>(timer_); 141 SelfType* self = static_cast<SelfType*>(timer_);
142 if (kIsRepeating) { 142 if (kIsRepeating) {
143 self->Reset(); 143 self->Reset();
(...skipping 17 matching lines...) Expand all
161 class OneShotTimer : public BaseTimer<Receiver, false> {}; 161 class OneShotTimer : public BaseTimer<Receiver, false> {};
162 162
163 //----------------------------------------------------------------------------- 163 //-----------------------------------------------------------------------------
164 // A simple, repeating timer. See usage notes at the top of the file. 164 // A simple, repeating timer. See usage notes at the top of the file.
165 template <class Receiver> 165 template <class Receiver>
166 class RepeatingTimer : public BaseTimer<Receiver, true> {}; 166 class RepeatingTimer : public BaseTimer<Receiver, true> {};
167 167
168 } // namespace base 168 } // namespace base
169 169
170 #endif // BASE_TIMER_H_ 170 #endif // BASE_TIMER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698