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? | |
|
Zeeshan Qureshi
2013/12/13 02:05:58
Deriving from TimerBase might just be cleaner.
Rick Byers
2013/12/13 22:52:25
I like that you don't have to duplicate anything h
| |
| 128 template <typename TimerFiredClass> | |
| 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_suspended(false) | |
| 136 { | |
| 137 } | |
| 138 | |
| 139 bool isSuspended() { return m_suspended; } | |
| 140 | |
| 141 void suspend() { m_suspended = true; } | |
|
Rick Byers
2013/12/13 22:52:25
I wonder if we should make these a little scarrier
Zeeshan Qureshi
2014/01/07 19:35:09
Yes!
| |
| 142 | |
| 143 void resume() { m_suspended = false; } | |
| 144 | |
| 145 // Should this only work when timer is suspended? | |
|
Zeeshan Qureshi
2013/12/13 02:05:58
Not sure, maybe only allow this when m_suspended?
Rick Byers
2013/12/13 22:52:25
Yeah, probably a good idea to ASSERT(m_suspended)
Zeeshan Qureshi
2014/01/07 19:35:09
Done, it's a little tricky to reason about pending
| |
| 146 void fire() { Timer<TimerFiredClass>::fired(); } | |
| 147 | |
| 148 private: | |
| 149 bool m_suspended; | |
| 150 | |
| 151 virtual void fired() | |
| 152 { | |
| 153 if (!m_suspended) | |
| 154 Timer<TimerFiredClass>::fired(); | |
| 155 } | |
| 156 }; | |
| 157 | |
| 123 inline bool TimerBase::isActive() const | 158 inline bool TimerBase::isActive() const |
| 124 { | 159 { |
| 125 ASSERT(m_thread == currentThread()); | 160 ASSERT(m_thread == currentThread()); |
| 126 return m_nextFireTime; | 161 return m_nextFireTime; |
| 127 } | 162 } |
| 128 | 163 |
| 129 template <typename TimerFiredClass> | 164 template <typename TimerFiredClass> |
| 130 class DeferrableOneShotTimer : private TimerBase { | 165 class DeferrableOneShotTimer : private TimerBase { |
| 131 public: | 166 public: |
| 132 typedef void (TimerFiredClass::*TimerFiredFunction)(DeferrableOneShotTimer*) ; | 167 typedef void (TimerFiredClass::*TimerFiredFunction)(DeferrableOneShotTimer*) ; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 170 TimerFiredClass* m_object; | 205 TimerFiredClass* m_object; |
| 171 TimerFiredFunction m_function; | 206 TimerFiredFunction m_function; |
| 172 | 207 |
| 173 double m_delay; | 208 double m_delay; |
| 174 bool m_shouldRestartWhenTimerFires; | 209 bool m_shouldRestartWhenTimerFires; |
| 175 }; | 210 }; |
| 176 | 211 |
| 177 } | 212 } |
| 178 | 213 |
| 179 #endif | 214 #endif |
| OLD | NEW |