Мультимедийный таймер
От: Dr.Offset  
Дата: 01.12.07 12:27
Оценка:
Написал тут такой класс...
#include <windows.h>

template <typename event_class, unsigned delay, unsigned resolution>
class multimedia_timer
{
    typedef void (event_class::* event_func)(unsigned long);

    unsigned int _timer_id;

    class timer_functor
    {
        event_func    _func;
        event_class * _t_ptr;
    public:
        inline timer_functor()
        {}
        inline timer_functor(event_func & __func, event_class * __t_ptr)
            : _func(__func), _t_ptr(__t_ptr)
        {
        }
        inline void operator() (unsigned long __arg)
        {
            (_t_ptr->*_func)(__arg);
        }
    };
    timer_functor          _functor;
    static timer_functor * _functor_ptr;

    static inline void __stdcall timer_callback(UINT, UINT, DWORD __arg, DWORD, DWORD)
    {
        _functor_ptr->operator()(__arg);
    }
public:
    inline multimedia_timer()
        : _timer_id(0)
    {}
    inline bool create_timer(unsigned long __with_arg = 0)
    {
        _timer_id = ::timeSetEvent(delay, resolution, timer_callback, __with_arg, TIME_PERIODIC);
        return _timer_id != 0;
    }
    inline void delete_timer()
    {
        ::timeKillEvent(_timer_id);
    }
    inline void set_timer_event(event_func __func)
    {
        _functor     = timer_functor(__func, static_cast<event_class*>(this));
        _functor_ptr = &_functor;
    }
};

template <typename event_class, unsigned delay, unsigned resolution>
    typename multimedia_timer<event_class, delay, resolution>::timer_functor *
        multimedia_timer<event_class, delay, resolution>::_functor_ptr = 0;


Использовать его предполагается так:

class my_timer : public multimedia_timer<my_timer, 100, 60>
{
public:
    void my_timer_callback(unsigned long __arg)
    {
        printf("%d", __arg++);
    }
    my_timer(unsigned long __arg)
    {
        set_timer_event(&my_timer::my_timer_callback);
        create_timer(__arg);
    }
    ~my_timer()
    {
        delete_timer();
    }
};

int main()
{
    my_timer t(2);
    getch();
 
    return 0;
}

Хотел бы попросить указать на проблемы, которых я мог не заметить. Может как улучшить можно работу... Спасибо.
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.