I have a class (let it be called "Common") that is used to create timers and call a member function from another class (let it be called "A") when the timer expires. The general idea is: - Calls a function from Common, passing it a pointer to a member function that should be executed when the timer expires - Common stores a pointer to a function on the card with the timer identifier key - When the timer enters, the card looks for the corresponding key, and then the corresponding key is called function.
Here is the code for A:
void A::setTimer()
{
std::function<void(void)> handler = std::bind(&A::onEventFunc, this);
pCommon->scheduleTimer(&handler);
}
onEventFunc is a public function of class A that does something when the timer expires.
Here is the code for Common:
static void timerHandler(int sig, siginfo_t *si, void uc)
{
map<timer_t*, std::function<void>(void)>*)::iterator iter =
globalTimerMap.find((timer_t*)si->si_value.sival_ptr);
if (iter != globalTimerMap.end)
{
std::function<void>(void)>* handler = iter->second;
(*handler)();
}
}
Common::schedulerTimer(std::function<void>(void)* handler)
{
sa.sa_sigaction = timerHandler;
globalTimerMap.insert(pair(&timerid, handler);
}
, (* handler)(); , segmantation.
, Linux , ++, . , - /, , . , , ?