diff options
author | Pierre Ossman <ossman@cendio.se> | 2020-05-18 18:53:43 +0200 |
---|---|---|
committer | Pierre Ossman <ossman@cendio.se> | 2020-05-19 19:55:57 +0200 |
commit | bd5ad5e79f35ec284d250aed66db4baa290c6089 (patch) | |
tree | 0d7853ed1ab7be9098c8d0957803e66c1dd78d7c | |
parent | 3aab38257ec93a17a1380efb5b71e50d94b6a4cd (diff) | |
download | tigervnc-bd5ad5e79f35ec284d250aed66db4baa290c6089.tar.gz tigervnc-bd5ad5e79f35ec284d250aed66db4baa290c6089.zip |
Support calling methods from timers
We can't safely use the normal timers in base classes as we cannot
guarantee that subclasses will call the base class' handleTimeout()
properly if the subclass overrides it.
-rw-r--r-- | common/rfb/Timer.h | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/common/rfb/Timer.h b/common/rfb/Timer.h index ef509725..ddfce1b2 100644 --- a/common/rfb/Timer.h +++ b/common/rfb/Timer.h @@ -35,6 +35,8 @@ namespace rfb { dispatch elapsed Timer callbacks and to determine how long to wait in select() for the next timeout to occur. + For classes that can be derived it's best to use MethodTimer which can call a specific + method on the class, thus avoiding conflicts when subclassing. */ struct Timer { @@ -101,6 +103,19 @@ namespace rfb { static std::list<Timer*> pending; }; + template<class T> class MethodTimer + : public Timer, public Timer::Callback { + public: + MethodTimer(T* obj_, bool (T::*cb_)(Timer*)) + : Timer(this), obj(obj_), cb(cb_) {} + + virtual bool handleTimeout(Timer* t) { return (obj->*cb)(t); } + + private: + T* obj; + bool (T::*cb)(Timer*); + }; + }; #endif |