Timer class with queued callbacks
来源:互联网 发布:java swing html布局 编辑:程序博客网 时间:2024/05/21 20:13
ses boost::function, but feel free to edit TimedCallback, and use function pointers or whatever... Example usage:// the callbackvoid foo(void) { cout << "bar" << endl; }// delay::Timer::getInstance()->delay(&foo, 1000);// somewhere, in your main-loop if using renderOneFrame(), or a framelistener's frameStarted()......::Timer::getInstance()->processQueue(); Timer.h#ifndef TIMER_H#define TIMER_H#include #include #include #include #include typedef std::pair< unsigned long, boost::function > TimedCallback;class PrioritizeCallbacks{public:bool operator() ( const TimedCallback& lhs, const TimedCallback& rhs ) const{return rhs.first < lhs.first;}};typedef std::priority_queue< TimedCallback, std::vector, PrioritizeCallbacks > TimedCallbackQueue;class Timer{public:static Timer* getInstance();static void Release();/** * /return Milliseconds since timer started */unsigned long getTime() { return mTimer->getMilliseconds(); }/** * /return Microseconds since timer started */unsigned long getMicroTime() { return mTimer->getMicroseconds(); }/** * /brief Delayed execution of a callback * /param cb */inline void delay(TimedCallback cb) { mQueue.push(cb); }/** * /brief Delayed execution of a callback * /param func the callback to execute * /param time to delay execution of callback */void delay(boost::function func, unsigned long time);/** * /brief Process the callback queue, executes pending functions * /note Have to call this manually, at shortest possible intervals, * If using Ogre::Root::startRendering() for example, this would be on every * onFrameStarted(). */void processQueue();/** * /brief Clears the queue */void clearQueue();private:Timer();virtual ~Timer();protected:Ogre::Timer* mTimer;TimedCallbackQueue mQueue;static Timer* ms_instance;};#endif // TIMER_H Timer.cpp#include "Timer.h"Timer* Timer::ms_instance = 0;Timer::Timer(){mTimer = new Ogre::Timer();}Timer::~Timer(){delete mTimer;}Timer* Timer::getInstance(){if(ms_instance == 0){ms_instance = new Timer();}return ms_instance;}void Timer::Release(){if(ms_instance){delete ms_instance;}ms_instance = 0;}void Timer::delay(boost::function func, unsigned long time){TimedCallback cb(getTime() + time, func);delay(cb);}void Timer::processQueue(){while(!mQueue.empty()){const TimedCallback &cb = mQueue.top();long delta = cb.first - getTime();if(delta>0) return;cb.second();mQueue.pop();}}void Timer::clearQueue(){mQueue = TimedCallbackQueue();}转自 http://www.ogre3d.org/tikiwiki/Timer+class+with+queued+callbacks&structure=Cookbook
- Timer class with queued callbacks
- Callbacks with Delegates [转]
- State Machines with C Callbacks
- Callbacks
- callbacks
- Switching out callbacks with promises in Mongoose
- Queued Services
- Requests Queued
- touch-screen with Timer
- Effective C#之21:Express Callbacks with Delegates
- [知其然不知其所以然-32] Queued Spinlock
- 133 - The Dole Queued
- sizeof with base class
- C WIth Class Designer
- class with pointer
- C# Framework Class Library 中的各种 Timer
- echo timer > /sys/class/leds/*/trigger
- Generic Callbacks
- c# mvc webGrid 无刷新分页
- 1204 Word Puzzles
- GridView 导出为Excel
- 关于《南都遭封杀……漩涡中的中国首富李彦宏……》的感想
- Creator(级联查询)
- Timer class with queued callbacks
- [ZZ]如何用栈实现递归与非递归的转换
- asp.net中导出到Excel的方法汇总
- 关于setTimeout和setInterval
- CRC校验字节型算法总结
- DYNAMIC_DOWNCAST
- dos实现文件夹树目录生成
- qt字库的移植(使其很好的显示中文)
- mysql 语句