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

Side by Side Diff: base/timer/timer.h

Issue 1355063004: Template methods on Timer classes instead of the classes themselves. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: timer: fixcaller Created 5 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
« no previous file with comments | « base/test/launcher/test_launcher.h ('k') | base/timer/timer_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 11 matching lines...) Expand all
22 // this, &MyClass::DoStuff); 22 // this, &MyClass::DoStuff);
23 // } 23 // }
24 // void StopDoingStuff() { 24 // void StopDoingStuff() {
25 // timer_.Stop(); 25 // timer_.Stop();
26 // } 26 // }
27 // private: 27 // private:
28 // void DoStuff() { 28 // void DoStuff() {
29 // // This method is called every second to do stuff. 29 // // This method is called every second to do stuff.
30 // ... 30 // ...
31 // } 31 // }
32 // base::RepeatingTimer<MyClass> timer_; 32 // base::RepeatingTimer timer_;
33 // }; 33 // };
34 // 34 //
35 // Both OneShotTimer and RepeatingTimer also support a Reset method, which 35 // Both OneShotTimer and RepeatingTimer also support a Reset method, which
36 // allows you to easily defer the timer event until the timer delay passes once 36 // allows you to easily defer the timer event until the timer delay passes once
37 // again. So, in the above example, if 0.5 seconds have already passed, 37 // again. So, in the above example, if 0.5 seconds have already passed,
38 // calling Reset on timer_ would postpone DoStuff by another 1 second. In 38 // calling Reset on timer_ would postpone DoStuff by another 1 second. In
39 // other words, Reset is shorthand for calling Stop and then Start again with 39 // other words, Reset is shorthand for calling Stop and then Start again with
40 // the same arguments. 40 // the same arguments.
41 // 41 //
42 // NOTE: These APIs are not thread safe. Always call from the same thread. 42 // NOTE: These APIs are not thread safe. Always call from the same thread.
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 193
194 // If true, user_task_ is scheduled to run sometime in the future. 194 // If true, user_task_ is scheduled to run sometime in the future.
195 bool is_running_; 195 bool is_running_;
196 196
197 DISALLOW_COPY_AND_ASSIGN(Timer); 197 DISALLOW_COPY_AND_ASSIGN(Timer);
198 }; 198 };
199 199
200 //----------------------------------------------------------------------------- 200 //-----------------------------------------------------------------------------
201 // This class is an implementation detail of OneShotTimer and RepeatingTimer. 201 // This class is an implementation detail of OneShotTimer and RepeatingTimer.
202 // Please do not use this class directly. 202 // Please do not use this class directly.
203 template <class Receiver, bool kIsRepeating>
204 class BaseTimerMethodPointer : public Timer { 203 class BaseTimerMethodPointer : public Timer {
205 public: 204 public:
206 typedef void (Receiver::*ReceiverMethod)();
207
208 // This is here to work around the fact that Timer::Start is "hidden" by the 205 // This is here to work around the fact that Timer::Start is "hidden" by the
209 // Start definition below, rather than being overloaded. 206 // Start definition below, rather than being overloaded.
210 // TODO(tim): We should remove uses of BaseTimerMethodPointer::Start below 207 // TODO(tim): We should remove uses of BaseTimerMethodPointer::Start below
211 // and convert callers to use the base::Closure version in Timer::Start, 208 // and convert callers to use the base::Closure version in Timer::Start,
212 // see bug 148832. 209 // see bug 148832.
213 using Timer::Start; 210 using Timer::Start;
214 211
215 BaseTimerMethodPointer() : Timer(kIsRepeating, kIsRepeating) {} 212 enum RepeatMode { ONE_SHOT, REPEATING };
213 BaseTimerMethodPointer(RepeatMode mode)
214 : Timer(mode == REPEATING, mode == REPEATING) {}
216 215
217 // Start the timer to run at the given |delay| from now. If the timer is 216 // Start the timer to run at the given |delay| from now. If the timer is
218 // already running, it will be replaced to call a task formed from 217 // already running, it will be replaced to call a task formed from
219 // |reviewer->*method|. 218 // |reviewer->*method|.
220 virtual void Start(const tracked_objects::Location& posted_from, 219 template <class Receiver>
221 TimeDelta delay, 220 void Start(const tracked_objects::Location& posted_from,
222 Receiver* receiver, 221 TimeDelta delay,
223 ReceiverMethod method) { 222 Receiver* receiver,
223 void (Receiver::*method)()) {
224 Timer::Start(posted_from, delay, 224 Timer::Start(posted_from, delay,
225 base::Bind(method, base::Unretained(receiver))); 225 base::Bind(method, base::Unretained(receiver)));
226 } 226 }
227 }; 227 };
228 228
229 //----------------------------------------------------------------------------- 229 //-----------------------------------------------------------------------------
230 // A simple, one-shot timer. See usage notes at the top of the file. 230 // A simple, one-shot timer. See usage notes at the top of the file.
231 template <class Receiver> 231 class OneShotTimer : public BaseTimerMethodPointer {
232 class OneShotTimer : public BaseTimerMethodPointer<Receiver, false> {}; 232 public:
233 OneShotTimer() : BaseTimerMethodPointer(ONE_SHOT) {}
234 };
233 235
234 //----------------------------------------------------------------------------- 236 //-----------------------------------------------------------------------------
235 // A simple, repeating timer. See usage notes at the top of the file. 237 // A simple, repeating timer. See usage notes at the top of the file.
236 template <class Receiver> 238 class RepeatingTimer : public BaseTimerMethodPointer {
237 class RepeatingTimer : public BaseTimerMethodPointer<Receiver, true> {}; 239 public:
240 RepeatingTimer() : BaseTimerMethodPointer(REPEATING) {}
241 };
238 242
239 //----------------------------------------------------------------------------- 243 //-----------------------------------------------------------------------------
240 // A Delay timer is like The Button from Lost. Once started, you have to keep 244 // A Delay timer is like The Button from Lost. Once started, you have to keep
241 // calling Reset otherwise it will call the given method in the MessageLoop 245 // calling Reset otherwise it will call the given method in the MessageLoop
242 // thread. 246 // thread.
243 // 247 //
244 // Once created, it is inactive until Reset is called. Once |delay| seconds have 248 // Once created, it is inactive until Reset is called. Once |delay| seconds have
245 // passed since the last call to Reset, the callback is made. Once the callback 249 // passed since the last call to Reset, the callback is made. Once the callback
246 // has been made, it's inactive until Reset is called again. 250 // has been made, it's inactive until Reset is called again.
247 // 251 //
248 // If destroyed, the timeout is canceled and will not occur even if already 252 // If destroyed, the timeout is canceled and will not occur even if already
249 // inflight. 253 // inflight.
250 template <class Receiver>
251 class DelayTimer : protected Timer { 254 class DelayTimer : protected Timer {
252 public: 255 public:
253 typedef void (Receiver::*ReceiverMethod)(); 256 template <class Receiver>
254
255 DelayTimer(const tracked_objects::Location& posted_from, 257 DelayTimer(const tracked_objects::Location& posted_from,
256 TimeDelta delay, 258 TimeDelta delay,
257 Receiver* receiver, 259 Receiver* receiver,
258 ReceiverMethod method) 260 void (Receiver::*method)())
259 : Timer(posted_from, delay, 261 : Timer(posted_from,
262 delay,
260 base::Bind(method, base::Unretained(receiver)), 263 base::Bind(method, base::Unretained(receiver)),
261 false) {} 264 false) {}
262 265
263 void Reset() override { Timer::Reset(); } 266 void Reset() override;
264 }; 267 };
265 268
269 // This class has a templated method so it can not be exported without failing
270 // to link in MSVC. But clang-plugin does not allow inline definitions of
271 // virtual methods, so the inline definition lives in the header file here
272 // to satisfy both.
273 inline void DelayTimer::Reset() {
274 Timer::Reset();
275 }
276
266 } // namespace base 277 } // namespace base
267 278
268 #endif // BASE_TIMER_TIMER_H_ 279 #endif // BASE_TIMER_TIMER_H_
OLDNEW
« no previous file with comments | « base/test/launcher/test_launcher.h ('k') | base/timer/timer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698