Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved. | 2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 98 | 98 |
| 99 #ifndef NDEBUG | 99 #ifndef NDEBUG |
| 100 ThreadIdentifier m_thread; | 100 ThreadIdentifier m_thread; |
| 101 #endif | 101 #endif |
| 102 | 102 |
| 103 friend class ThreadTimers; | 103 friend class ThreadTimers; |
| 104 friend class TimerHeapLessThanFunction; | 104 friend class TimerHeapLessThanFunction; |
| 105 friend class TimerHeapReference; | 105 friend class TimerHeapReference; |
| 106 }; | 106 }; |
| 107 | 107 |
| 108 template <typename TimerFiredClass> class MockableTimer; | |
| 109 | |
| 108 template <typename TimerFiredClass> | 110 template <typename TimerFiredClass> |
| 109 class Timer : public TimerBase { | 111 class Timer : public TimerBase { |
| 112 friend class MockableTimer<TimerFiredClass>; | |
| 113 | |
| 110 public: | 114 public: |
| 111 typedef void (TimerFiredClass::*TimerFiredFunction)(Timer*); | 115 typedef void (TimerFiredClass::*TimerFiredFunction)(Timer*); |
| 112 | 116 |
| 113 Timer(TimerFiredClass* o, TimerFiredFunction f) | 117 Timer(TimerFiredClass* o, TimerFiredFunction f) |
| 114 : m_object(o), m_function(f) { } | 118 : m_object(o), m_function(f) { } |
| 115 | 119 |
| 116 private: | 120 private: |
| 117 virtual void fired() { (m_object->*m_function)(this); } | 121 virtual void fired() { (m_object->*m_function)(this); } |
| 118 | 122 |
| 119 TimerFiredClass* m_object; | 123 TimerFiredClass* m_object; |
| 120 TimerFiredFunction m_function; | 124 TimerFiredFunction m_function; |
| 121 }; | 125 }; |
| 122 | 126 |
| 127 // Just derive from TimerBase and skip the template-fu? | |
|
Rick Byers
2014/01/10 21:46:50
I think you need the template-fu to get the timer-
Zeeshan Qureshi
2014/01/10 22:20:41
I think we already decided on this, forgot to remo
| |
| 128 template <typename TimerFiredClass> | |
|
Rick Byers
2014/01/10 21:46:50
I'd add a comment here saying the idea is to allow
Zeeshan Qureshi
2014/01/10 22:20:41
Yes, adding.
| |
| 129 class MockableTimer : public Timer<TimerFiredClass> { | |
| 130 public: | |
| 131 typedef typename Timer<TimerFiredClass>::TimerFiredFunction TimerFiredFuncti on; | |
| 132 | |
| 133 MockableTimer(TimerFiredClass* o, TimerFiredFunction f) | |
| 134 : Timer<TimerFiredClass>(o, f) | |
| 135 , m_manualMode(false) | |
| 136 , m_firePending(false) | |
| 137 { | |
| 138 } | |
| 139 | |
| 140 bool inManualModeForTesting() const { return m_manualMode; } | |
|
Rick Byers
2014/01/10 21:46:50
blink coding guidelines say to use bare words for
| |
| 141 | |
| 142 bool firePendingForTesting() const { return m_firePending; } | |
|
Rick Byers
2014/01/10 21:46:50
Again, this is used only by the unit test, right?
Zeeshan Qureshi
2014/01/10 22:20:41
Removing the above methods seems fine, I was under
Rick Byers
2014/01/10 22:24:36
Nope, encapsulation is your friend!
| |
| 143 | |
| 144 void enterManualModeForTesting() { m_manualMode = true; } | |
|
Rick Byers
2014/01/10 21:46:50
collapse enter/leave into a single 'setManualModeF
| |
| 145 | |
| 146 void leaveManualModeForTesting() { m_manualMode = false; } | |
| 147 | |
| 148 void manualFireForTesting() | |
| 149 { | |
| 150 ASSERT(m_manualMode); | |
| 151 ASSERT(Timer<TimerFiredClass>::isActive() || m_firePending); | |
| 152 Timer<TimerFiredClass>::fired(); | |
| 153 if (!Timer<TimerFiredClass>::repeatInterval()) | |
| 154 Timer<TimerFiredClass>::stop(); | |
| 155 m_firePending = false; | |
| 156 } | |
| 157 | |
| 158 private: | |
| 159 bool m_manualMode; | |
| 160 bool m_firePending; | |
| 161 | |
| 162 virtual void fired() | |
| 163 { | |
| 164 if (m_manualMode) | |
| 165 m_firePending = true; | |
| 166 if (!m_manualMode) | |
| 167 Timer<TimerFiredClass>::fired(); | |
| 168 } | |
| 169 }; | |
| 170 | |
| 123 inline bool TimerBase::isActive() const | 171 inline bool TimerBase::isActive() const |
| 124 { | 172 { |
| 125 ASSERT(m_thread == currentThread()); | 173 ASSERT(m_thread == currentThread()); |
| 126 return m_nextFireTime; | 174 return m_nextFireTime; |
| 127 } | 175 } |
| 128 | 176 |
| 129 template <typename TimerFiredClass> | 177 template <typename TimerFiredClass> |
| 130 class DeferrableOneShotTimer : private TimerBase { | 178 class DeferrableOneShotTimer : private TimerBase { |
| 131 public: | 179 public: |
| 132 typedef void (TimerFiredClass::*TimerFiredFunction)(DeferrableOneShotTimer*) ; | 180 typedef void (TimerFiredClass::*TimerFiredFunction)(DeferrableOneShotTimer*) ; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 170 TimerFiredClass* m_object; | 218 TimerFiredClass* m_object; |
| 171 TimerFiredFunction m_function; | 219 TimerFiredFunction m_function; |
| 172 | 220 |
| 173 double m_delay; | 221 double m_delay; |
| 174 bool m_shouldRestartWhenTimerFires; | 222 bool m_shouldRestartWhenTimerFires; |
| 175 }; | 223 }; |
| 176 | 224 |
| 177 } | 225 } |
| 178 | 226 |
| 179 #endif | 227 #endif |
| OLD | NEW |