首页 > 代码库 > 写的一个简单定时器(非独立线程)

写的一个简单定时器(非独立线程)

//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;
}