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

Unified Diff: base/timer/timer.h

Issue 2850093003: Simplify base::Timer implementation
Patch Set: . Created 3 years, 8 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | base/timer/timer.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/timer/timer.h
diff --git a/base/timer/timer.h b/base/timer/timer.h
index 8aac279def647771f7cead12876c2ea06e4fddcf..1fedafd4fca6db39206791246dfb0ac3bdd27fee 100644
--- a/base/timer/timer.h
+++ b/base/timer/timer.h
@@ -77,20 +77,17 @@ class BASE_EXPORT Timer {
// user_task is retained or reset when it runs or stops. If |tick_clock| is
// provided, it is used instead of TimeTicks::Now() to get TimeTicks when
// scheduling tasks.
- Timer(bool retain_user_task, bool is_repeating);
- Timer(bool retain_user_task, bool is_repeating, TickClock* tick_clock);
+ Timer(bool retain_user_task,
+ bool is_repeating,
+ TickClock* tick_clock = nullptr);
// Construct a timer with retained task info. If |tick_clock| is provided, it
// is used instead of TimeTicks::Now() to get TimeTicks when scheduling tasks.
Timer(const tracked_objects::Location& posted_from,
TimeDelta delay,
const base::Closure& user_task,
- bool is_repeating);
- Timer(const tracked_objects::Location& posted_from,
- TimeDelta delay,
- const base::Closure& user_task,
bool is_repeating,
- TickClock* tick_clock);
+ TickClock* tick_clock = nullptr);
virtual ~Timer();
@@ -111,6 +108,18 @@ class BASE_EXPORT Timer {
TimeDelta delay,
const base::Closure& user_task);
+ // Start the timer to run at the given |delay| from now. If the timer is
+ // already running, it will be replaced to call a task formed from
+ // |reviewer->*method|.
+ template <class Receiver>
+ void Start(const tracked_objects::Location& posted_from,
+ TimeDelta delay,
+ Receiver* receiver,
+ void (Receiver::*method)()) {
+ Timer::Start(posted_from, delay,
+ base::Bind(method, base::Unretained(receiver)));
+ }
+
// Call this method to stop and cancel the timer. It is a no-op if the timer
// is not running.
virtual void Stop();
@@ -218,50 +227,19 @@ class BASE_EXPORT Timer {
};
//-----------------------------------------------------------------------------
-// This class is an implementation detail of OneShotTimer and RepeatingTimer.
-// Please do not use this class directly.
-class BaseTimerMethodPointer : public Timer {
- public:
- // This is here to work around the fact that Timer::Start is "hidden" by the
- // Start definition below, rather than being overloaded.
- // TODO(tim): We should remove uses of BaseTimerMethodPointer::Start below
- // and convert callers to use the base::Closure version in Timer::Start,
- // see bug 148832.
- using Timer::Start;
-
- enum RepeatMode { ONE_SHOT, REPEATING };
- BaseTimerMethodPointer(RepeatMode mode, TickClock* tick_clock)
- : Timer(mode == REPEATING, mode == REPEATING, tick_clock) {}
-
- // Start the timer to run at the given |delay| from now. If the timer is
- // already running, it will be replaced to call a task formed from
- // |reviewer->*method|.
- template <class Receiver>
- void Start(const tracked_objects::Location& posted_from,
- TimeDelta delay,
- Receiver* receiver,
- void (Receiver::*method)()) {
- Timer::Start(posted_from, delay,
- base::Bind(method, base::Unretained(receiver)));
- }
-};
-
-//-----------------------------------------------------------------------------
// A simple, one-shot timer. See usage notes at the top of the file.
-class OneShotTimer : public BaseTimerMethodPointer {
+class OneShotTimer : public Timer {
public:
- OneShotTimer() : OneShotTimer(nullptr) {}
- explicit OneShotTimer(TickClock* tick_clock)
- : BaseTimerMethodPointer(ONE_SHOT, tick_clock) {}
+ explicit OneShotTimer(TickClock* tick_clock = nullptr)
+ : Timer(false, false, tick_clock) {}
};
//-----------------------------------------------------------------------------
// A simple, repeating timer. See usage notes at the top of the file.
-class RepeatingTimer : public BaseTimerMethodPointer {
+class RepeatingTimer : public Timer {
public:
- RepeatingTimer() : RepeatingTimer(nullptr) {}
- explicit RepeatingTimer(TickClock* tick_clock)
- : BaseTimerMethodPointer(REPEATING, tick_clock) {}
+ explicit RepeatingTimer(TickClock* tick_clock = nullptr)
+ : Timer(true, true, tick_clock) {}
};
//-----------------------------------------------------------------------------
@@ -275,37 +253,25 @@ class RepeatingTimer : public BaseTimerMethodPointer {
//
// If destroyed, the timeout is canceled and will not occur even if already
// inflight.
-class DelayTimer : protected Timer {
+class DelayTimer {
public:
template <class Receiver>
DelayTimer(const tracked_objects::Location& posted_from,
TimeDelta delay,
Receiver* receiver,
- void (Receiver::*method)())
- : DelayTimer(posted_from, delay, receiver, method, nullptr) {}
-
- template <class Receiver>
- DelayTimer(const tracked_objects::Location& posted_from,
- TimeDelta delay,
- Receiver* receiver,
void (Receiver::*method)(),
- TickClock* tick_clock)
- : Timer(posted_from,
- delay,
- base::Bind(method, base::Unretained(receiver)),
- false,
- tick_clock) {}
-
- void Reset() override;
-};
+ TickClock* tick_clock = nullptr)
+ : timer_(posted_from,
+ delay,
+ base::Bind(method, base::Unretained(receiver)),
+ false,
+ tick_clock) {}
+
+ void Reset() { timer_.Reset(); }
-// This class has a templated method so it can not be exported without failing
-// to link in MSVC. But clang-plugin does not allow inline definitions of
-// virtual methods, so the inline definition lives in the header file here
-// to satisfy both.
-inline void DelayTimer::Reset() {
- Timer::Reset();
-}
+ private:
+ Timer timer_;
+};
} // namespace base
« no previous file with comments | « no previous file | base/timer/timer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698