zoukankan      html  css  js  c++  java
  • 写的一个简单定时器(非独立线程)

    //Callback.h
    #ifndef __CALLBACK_H__
    #define __CALLBACK_H__
    
    typedef void (*T_CallBack)(void *);
    typedef struct  
    {
        T_CallBack cb;
        void *obj;
    }ST_CallBack;
    
    int __NewTimer(void* obj, int interval, bool isloop, T_CallBack cb);
    void __DeleteTimer(int handle);
    
    
    #define NewTimer(handle,obj,delay,isloop,CLASS,MEMBER) do{ 
    union {       
        T_CallBack cb; 
        void (CLASS::*___M)(void); 
    }___T; 
        ___T.___M = &CLASS::MEMBER;    
        handle = __NewTimer(obj, delay, isloop, ___T.cb); 
    }while(0)
    
    #define DeleteTimer(handle) do{ 
        if ( handle) { 
            __DeleteTimer(handle); 
            handle = 0; 
        } 
    }while(0)
    
    //#define NewTimer
    
    int TimerGetDelay(int handle);
    void TimerSetDelay(int handle, int delay);
    void TimerStart(int handle);
    void TimerStop(int handle);
    
    
    #endif

    //Callback.cpp
    
    
    typedef struct  
    {    
        Uint32 lastticks;    
        bool isloop;
        int delay;
        ST_CallBack cb;    
        bool isdel;
        bool ispause;
    }ST_TimerTask;
    
    static std::list<ST_TimerTask*> timerTasks; 
    
    int __NewTimer(void* obj, int delay, bool isloop, T_CallBack cb)
    {  
        ST_TimerTask *task;
    
        NEW_OBJ(task, ST_TimerTask);
        task->isloop = isloop;
        task->delay = delay;
        task->cb.obj = obj;
        task->cb.cb = (T_CallBack)cb;
        task->lastticks = SDL_GetTicks();
        task->isdel = false;    
        task->ispause = false;
        timerTasks.push_back(task);
        return (int)task;
    }
    
    void __DeleteTimer(int handle)
    {
        std::list<ST_TimerTask*>::iterator iter;
        ST_TimerTask *task;
        for (iter = timerTasks.begin(); iter != timerTasks.end(); iter++)    {
            task = (*iter);
            if ( (int)task == handle){
                task->isdel = true;
                return;
            }
        }
    }
    
    void DeleteAllTimer(void)
    {
        std::list<ST_TimerTask*>::iterator iter;
        ST_TimerTask *task;
        for (iter = timerTasks.begin(); iter != timerTasks.end();)    {
            task = (*iter);
            if ( task){
                iter = timerTasks.erase(iter);        
                DELETE_OBJ(task);
            }else
                iter++;
        }
        timerTasks.clear();
    }
    
    void TimerRunning(Uint32 curticks)
    {
        std::list<ST_TimerTask*>::iterator iter;
        ST_TimerTask *task;
        for (iter = timerTasks.begin(); iter != timerTasks.end(); )    {
            task = (*iter);
            if ( task) { 
                if(curticks - task->lastticks >= task->delay){
                    if ( task->isdel == false && task->ispause == false)
                        task->cb.cb(task->cb.obj);                
                    if (task->isdel || task->isloop == false){
                        iter = timerTasks.erase(iter);
                        DELETE_OBJ(task);
                        continue;
                    }
                    task->lastticks = curticks;            
                }            
            }
            iter++;
        }
    }
    
    int TimerGetDelay(int handle)
    {
        ST_TimerTask *task=(ST_TimerTask*)handle;
        return task->delay;
    }
    
    void TimerSetDelay(int handle, int delay)
    {
        ST_TimerTask *task=(ST_TimerTask*)handle;
        task->delay = delay;
    }
    void TimerStart(int handle)
    {
        ST_TimerTask *task=(ST_TimerTask*)handle;
        task->ispause = false;
    }
    void TimerStop(int handle)
    {
        ST_TimerTask *task=(ST_TimerTask*)handle;
        task->ispause = true;
    }
    
    
    
     
  • 相关阅读:
    C# 实现保留两位小数的方法
    ueditor 上传大容量视频报http请求错误的解决方法
    Python3 message提示 AttributeError: module 'tkinter' has no attribute 'messagebox'
    Laravel5 went wrong FatalErrorException in HtmlServiceProvider.php line 36
    安装eclipse中文汉化包后无法打开eclipse
    phpstudy 下开启openssl
    mysql 数据库还原出错ERROR:Unknown command '' mysql中断
    MySql启动提示:The server quit without updating PID file(…)失败
    windows 安装 setuptools
    Nginx 域名转发
  • 原文地址:https://www.cnblogs.com/colife/p/3779210.html
Copyright © 2011-2022 走看看