Add Template Variable Parameter For class Timer

This commit is contained in:
Kirigaya Kazuto 2017-04-19 14:11:08 +08:00
parent 43ff36f8c6
commit 06f2d8e448
2 changed files with 48 additions and 0 deletions

View File

@ -1257,14 +1257,28 @@ namespace MiniEngine
SDL_StopTextInput();
}
/// Global Executor For class Timer
Uint32 _global_timer_executor(Uint32 interval,void* param)
{
printf("DEBUG: Global Timer Executor.\n");
auto p=reinterpret_cast<std::function<Uint32(Uint32 interval)>*>(param);
return (*p)(interval);
}
Timer::Timer()
{
_enabled=false;
_detached=false;
_delete_on_disable=false;
id=-1;
}
Timer::Timer(SDL_TimerCallback callback,Uint32 interval,void* param) : Timer()
{
_real_timer_call(callback,interval,param);
}
void Timer::_real_timer_call(SDL_TimerCallback callback,Uint32 interval,void* param)
{
_callback=callback;
_interval=interval;
@ -1293,6 +1307,15 @@ namespace MiniEngine
SDL_RemoveTimer(id);
_enabled=false;
id=-1;
_callback=nullptr;
if(_delete_on_disable)
{
_delete_delegator(reinterpret_cast<std::function<Uint32(Uint32 interval)>*>(_param));
_delete_on_disable=false;
}
_param=nullptr;
return 0;
}
else
@ -1314,6 +1337,12 @@ namespace MiniEngine
}
}
//static
void Timer::_delete_delegator(std::function<Uint32(Uint32)>* param)
{
delete param;
}
AudioPlayer::AudioPlayer()
{
if (!_sysAudioCounter)

View File

@ -426,24 +426,43 @@ namespace MiniEngine
};
};
Uint32 _global_timer_executor(Uint32 interval,void* param);
class Timer
{
public:
Timer();
/// Uint32 func(Uint32,void*) ...
template<typename Callable,typename... Args>
Timer(Callable&& callable,Uint32 interval,Args&&... args) : Timer()
{
auto realCall=[&](Uint32 interval)->Uint32{return callable(interval,args...);};
auto pfunc=new std::function<Uint32(Uint32 interval)>(realCall);
_real_timer_call(_global_timer_executor,interval,pfunc);
}
/// Restore For Capability
Timer(SDL_TimerCallback callback,Uint32 interval,void* param);
int enable();
int disable();
bool isenable();
void detach();
~Timer();
static void _delete_delegator(std::function<Uint32(Uint32)>* Delegator);
private:
void _real_timer_call(SDL_TimerCallback callback,Uint32 interval,void* param);
SDL_TimerCallback _callback;
Uint32 _interval;
void* _param;
SDL_TimerID id;
bool _enabled;
bool _detached;
/// Reserved Variable For Template variable Parameter
bool _delete_on_disable;
};
class AudioPlayer