diff --git a/src/simulator/timer.cc b/src/simulator/timer.cc index 3a48bebbf..24f6a9207 100644 --- a/src/simulator/timer.cc +++ b/src/simulator/timer.cc @@ -84,6 +84,23 @@ Timer::~Timer () delete m_impl; } +void +Timer::SetFunction (void (*fn) (void)) +{ + struct FnTimerImplZero : public TimerImpl + { + typedef void (*FN) (void); + FnTimerImplZero (FN fn) + : m_fn (fn) {} + virtual EventId Schedule (const Time &delay) { + return Simulator::Schedule (delay, m_fn); + } + FN m_fn; + } *function = new FnTimerImplZero (fn); + delete m_impl; + m_impl = function; +} + void Timer::SetDelay (const Time &time) { @@ -171,6 +188,11 @@ class TimerTests : public Test public: TimerTests (); virtual bool RunTests (void); + void bazi (int) {} + void bazcir (const int&) {} + void bazir (int&) {} + void bazip (int *) {} + void bazcip (const int *) {} }; TimerTests::TimerTests () @@ -185,7 +207,7 @@ TimerTests::RunTests (void) int a = 0; int &b = a; const int &c = a; - Timer timer; + Timer timer = Timer (0); timer.SetFunction (&bari, a); timer.SetArguments (2); @@ -208,6 +230,10 @@ TimerTests::RunTests (void) timer.SetDelay (Seconds (1.0)); timer.Schedule (); + timer.SetFunction (&TimerTests::bazi, this, 1); + timer.SetFunction (&TimerTests::bazir, this, 1); + timer.SetFunction (&TimerTests::bazcir, this, 1); + Simulator::Run (); Simulator::Destroy (); return ok; diff --git a/src/simulator/timer.h b/src/simulator/timer.h index 8bd7e996c..dbd790051 100644 --- a/src/simulator/timer.h +++ b/src/simulator/timer.h @@ -134,6 +134,52 @@ public: typename T1, typename T2, typename T3> void SetFunction (void (*fn) (U1, U2, U3), T1 a1, T2 a2, T3 a3); + /** + * \param memPtr the member function pointer + * \param objPtr the pointer to object + * + * Store this function and object in this Timer for later use by Timer::Schedule. + */ + template + void SetFunction (MEM_PTR memPtr, OBJ_PTR objPtr); + /** + * \param memPtr the member function pointer + * \param objPtr the pointer to object + * \param a1 the first argument + * + * Store this function and this argument in this Timer for later use by + * Timer::Schedule. + */ + template + void SetFunction (MEM_PTR memPtr, OBJ_PTR objPtr, T1 a1); + /** + * \param memPtr the member function pointer + * \param objPtr the pointer to object + * \param a1 the first argument + * \param a2 the second argument + * + * Store this function and these arguments in this Timer for later use by + * Timer::Schedule. + */ + template + void SetFunction (MEM_PTR memPtr, OBJ_PTR objPtr, T1 a1, T2 a2); + /** + * \param memPtr the member function pointer + * \param objPtr the pointer to object + * \param a1 the first argument + * \param a2 the second argument + * \param a3 the third argument + * + * Store this function and these arguments in this Timer for later use by + * Timer::Schedule. + */ + template + void SetFunction (MEM_PTR memPtr, OBJ_PTR objPtr, T1 a1, T2 a2, T3 a3); + + /** * \param a1 the first argument * @@ -276,6 +322,50 @@ Timer::SetArguments (T1 a1) impl->SetArguments (a1); } +template +void +Timer::SetFunction (MEM_PTR memPtr, OBJ_PTR objPtr) +{ + struct MemFnTimerImplZero : public TimerImpl + { + MemFnTimerImplZero (MEM_PTR memPtr, OBJ_PTR objPtr) + : m_memPtr (memPtr), m_objPtr (objPtr) {} + virtual EventId Schedule (const Time &delay) { + return Simulator::Schedule (delay, m_memPtr, m_objPtr); + } + MEM_PTR m_memPtr; + OBJ_PTR m_objPtr; + } *function = new MemFnTimerImplZero (memPtr, objPtr); + delete m_impl; + m_impl = function; +} + + + +template +void +Timer::SetFunction (MEM_PTR memPtr, OBJ_PTR objPtr, T1 a1) +{ + struct MemFnTimerImplZero : public TimerImpl + { + MemFnTimerImplZero (MEM_PTR memPtr, OBJ_PTR objPtr) + : m_memPtr (memPtr), m_objPtr (objPtr) {} + virtual void SetArguments (typename TimerTraits::ParameterType a1) { + m_a1 = a1; + } + virtual EventId Schedule (const Time &delay) { + return Simulator::Schedule (delay, m_memPtr, m_objPtr, m_a1); + } + MEM_PTR m_memPtr; + OBJ_PTR m_objPtr; + typename TimerTraits::StoredType m_a1; + } *function = new MemFnTimerImplZero (memPtr, objPtr); + function->SetArguments (a1); + delete m_impl; + m_impl = function; +} +