1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
/*
* timer.h: Threaded timer class
*
* See the main source file 'xineliboutput.c' for copyright information and
* how to reach the author.
*
* $Id: timer.h,v 1.1 2006/06/03 10:04:28 phintuka Exp $
*
*/
#ifndef __XINELIBOUTPUT_TIMER_H
#define __XINELIBOUTPUT_TIMER_H
//
// cTimerCallback : timer callback handler interface
//
class cTimerCallback {
protected:
virtual bool TimerEvent() = 0; // return false to cancel timer
virtual void *TargetId() { return (void*)this; }
virtual int size() { return sizeof(*this); }
virtual bool is(void *data, int len)
{
return len==sizeof(*this) && TargetId()==data;
}
friend class cTimerThread;
public:
static void Set(cTimerCallback *, unsigned int TimeoutMs);
static void Cancel(cTimerCallback *);
virtual ~cTimerCallback();
};
//
// cTimerEvent : base class for timer events
//
class cTimerEvent : protected cTimerCallback {
private:
cTimerEvent(cTimerEvent&);
protected:
cTimerEvent() {};
virtual void AddEvent(unsigned int TimeoutMs);
static void CancelAll(void *Target);
template<class TCLASS> friend void CancelTimerEvents(TCLASS*);
friend class cTimerThread;
public:
static void Cancel(cTimerEvent *&);
};
//
// make gcc 3.4.5 happy
//
template<class TCLASS, class TRESULT>
cTimerEvent *CreateTimerEvent(TCLASS *c, TRESULT (TCLASS::*fp)(void),
unsigned int TimeoutMs);
template<class TCLASS, class TRESULT, class TARG1>
cTimerEvent *CreateTimerEvent(TCLASS *c, TRESULT (TCLASS::*fp)(TARG1),
TARG1 arg1,
unsigned int TimeoutMs);
template<class TCLASS>
cTimerEvent *CreateTimerEvent(TCLASS *c, void (TCLASS::*fp)(void),
unsigned int TimeoutMs, bool runOnce = true);
template<class TCLASS, class TARG1>
cTimerEvent *CreateTimerEvent(TCLASS *c, void (TCLASS::*fp)(TARG1),
TARG1 arg1,
unsigned int TimeoutMs, bool runOnce = true);
//
// Timer event templates
//
template <class TCLASS, class TRESULT>
class cTimerFunctorR0 : public cTimerEvent {
public:
protected:
typedef TRESULT (TCLASS::*TFUNC)(void);
cTimerFunctorR0(TCLASS *obj, TFUNC f, unsigned int TimeoutMs) :
m_obj(obj), m_f(f)
{
AddEvent(TimeoutMs);
}
virtual ~cTimerFunctorR0() {};
virtual bool TimerEvent(void)
{
return (*m_obj.*m_f)();
}
virtual void *TargetId() { return (void*)m_obj; }
virtual int size() { return sizeof(*this); }
virtual bool is(void *data, int len)
{
return sizeof(*this)==len && !memcmp(this,data,len);
}
private:
TCLASS *m_obj;
TFUNC m_f;
friend cTimerEvent *CreateTimerEvent<TCLASS,TRESULT>(TCLASS*,TFUNC,unsigned int);
};
template <class TCLASS, class TRESULT, class TARG1>
class cTimerFunctorR1 : public cTimerEvent {
public:
protected:
typedef TRESULT (TCLASS::*TFUNC)(TARG1);
cTimerFunctorR1(TCLASS *obj, TFUNC f, TARG1 arg1, unsigned int TimeoutMs) :
m_obj(obj), m_f(f), m_arg1(arg1)
{
AddEvent(TimeoutMs);
}
virtual ~cTimerFunctorR1() {};
virtual bool TimerEvent(void)
{
return (*m_obj.*m_f)(m_arg1);
}
virtual void *TargetId() { return (void*)m_obj; }
virtual int size() { return sizeof(*this); }
virtual bool is(void *data, int len)
{
return sizeof(*this)==len && !memcmp(this,data,len);
}
private:
TCLASS *m_obj;
TFUNC m_f;
TARG1 m_arg1;
friend cTimerEvent *CreateTimerEvent<TCLASS,TRESULT,TARG1>(TCLASS*,TFUNC,TARG1,unsigned int);
};
template <class TCLASS>
class cTimerFunctor0 : public cTimerEvent {
public:
protected:
typedef void (TCLASS::*TFUNC)(void);
cTimerFunctor0(TCLASS *obj, TFUNC f,
unsigned int TimeoutMs, bool runOnce) :
m_obj(obj), m_f(f), m_runAgain(!runOnce)
{
AddEvent(TimeoutMs);
}
virtual ~cTimerFunctor0() {};
virtual bool TimerEvent(void)
{
(*m_obj.*m_f)();
return m_runAgain;
}
virtual void *TargetId() { return (void*)m_obj; }
virtual int size() { return sizeof(*this); }
virtual bool is(void *data, int len)
{
return sizeof(*this)==len && !memcmp(this,data,len);
}
private:
TCLASS *m_obj;
TFUNC m_f;
bool m_runAgain;
friend cTimerEvent *CreateTimerEvent<TCLASS>(TCLASS*,TFUNC,unsigned int,bool);
};
template <class TCLASS, class TARG1>
class cTimerFunctor1 : public cTimerEvent {
public:
protected:
typedef void (TCLASS::*TFUNC)(TARG1);
cTimerFunctor1(TCLASS *obj, TFUNC f, TARG1 arg1,
unsigned int TimeoutMs, bool runOnce) :
m_obj(obj), m_f(f), m_arg1(arg1), m_runAgain(!runOnce)
{
AddEvent(TimeoutMs);
}
virtual ~cTimerFunctor1() {};
virtual bool TimerEvent(void)
{
(*m_obj.*m_f)(m_arg1);
return m_runAgain;
}
virtual void *TargetId() { return (void*)m_obj; }
virtual int size() { return sizeof(*this); }
virtual bool is(void *data, int len)
{
return sizeof(*this)==len && !memcmp(this,data,len);
}
private:
TCLASS *m_obj;
TFUNC m_f;
TARG1 m_arg1;
bool m_runAgain;
friend cTimerEvent *CreateTimerEvent<TCLASS,TARG1>(TCLASS*,TFUNC,TARG1,unsigned int,bool);
};
//
// Function templates for timer event creation and cancellation
//
template<class TCLASS, class TRESULT>
cTimerEvent *CreateTimerEvent(TCLASS *c, TRESULT (TCLASS::*fp)(void),
unsigned int TimeoutMs)
{
return new cTimerFunctorR0<TCLASS,TRESULT>(c,fp,TimeoutMs);
}
template<class TCLASS, class TRESULT, class TARG1>
cTimerEvent *CreateTimerEvent(TCLASS *c, TRESULT (TCLASS::*fp)(TARG1),
TARG1 arg1,
unsigned int TimeoutMs)
{
return new cTimerFunctorR1<TCLASS,TRESULT,TARG1>(c,fp,arg1,TimeoutMs);
}
template<class TCLASS>
cTimerEvent *CreateTimerEvent(TCLASS *c, void (TCLASS::*fp)(void),
unsigned int TimeoutMs, bool runOnce = true)
{
return new cTimerFunctor0<TCLASS>(c,fp,TimeoutMs,runOnce);
}
template<class TCLASS, class TARG1>
cTimerEvent *CreateTimerEvent(TCLASS *c, void (TCLASS::*fp)(TARG1),
TARG1 arg1,
unsigned int TimeoutMs, bool runOnce = true)
{
return new cTimerFunctor1<TCLASS,TARG1>(c,fp,arg1,TimeoutMs,runOnce);
}
template<class TCLASS>
void CancelTimerEvents(TCLASS *c)
{
cTimerEvent::CancelAll((void*)c);
}
// usage:
//
// 'this' derived from cTimerHandler:
// Set timer:
// cTimerCallback::Set(this, TimeoutMs);
// Cancel timer:
// - return false from handler or
// - call cTimerCallback::Cancel(this); or
// - delete 'this' object
//
// any function of any class:
// Set timer:
// - cTimerEvent *event = CreateTimerEvent(...);
// example:
// CreateTimerEvent(this, &cXinelibDevice::TimerEvent, 1, 1000);
// -> calls this->cXinelibDevice::TimerEvent(1) every second until stopped.
// Cancel timer:
// - if handler returns bool: return false from handler
// - handler is type of void: timer runs only once
// - call cTimerEvent::Cancel(event)
// Cancel all timers for object:
// - Call CancelTimerEvents(object)
// - Call CancelTimerEvents(this)
#endif // __XINELIBOUTPUT_TIMER_H
|