tasn pushed a commit to branch master. http://git.enlightenment.org/bindings/cxx/eflxx.git/commit/?id=053bedcd17ea54a4fbdd670436f41c6302f5fc19
commit 053bedcd17ea54a4fbdd670436f41c6302f5fc19 Author: Andreas Volz <[email protected]> Date: Fri Jul 22 22:39:05 2011 +0000 Ecorexx::Timer design changed to have a factory and provide destroy function to wrap C interface better than before also used sigc++ slots as callback provider SVN revision: 61598 --- ecorexx/include/ecorexx/Timer.h | 33 +++++++---------- ecorexx/src/Timer.cpp | 79 ++++++++++++++++++++++++----------------- 2 files changed, 58 insertions(+), 54 deletions(-) diff --git a/ecorexx/include/ecorexx/Timer.h b/ecorexx/include/ecorexx/Timer.h index 50b7efd..947a971 100644 --- a/ecorexx/include/ecorexx/Timer.h +++ b/ecorexx/include/ecorexx/Timer.h @@ -9,23 +9,12 @@ namespace Ecorexx { -// TODO: maybe change signal architecture back to callback architecture. Think about it! - class Timer { - typedef sigc::signal <void> Signal; - typedef sigc::signal <void> Loop; - //typedef sigc::slot1 <void, void> Slot; - public: - Timer( double seconds, bool singleshot = false ); - virtual ~Timer(); + static Timer *factory (double seconds, sigc::slot<bool, Timer&> task, bool loop = false); - //virtual void tick(); - -// static Timer* singleShot( double seconds, const Timer::Slot& ); // TODO: CountedPtr - - void del (); + void destroy (); void setInterval (double interval); @@ -43,16 +32,18 @@ public: static void setPrecision (double precision); - -public: /* signals */ - Timer::Signal timeout; - Timer::Loop loop; - + static void dump (); + private: - Ecore_Timer* _et; - bool _ss; + Ecore_Timer *mETimer; + sigc::slot<bool, Timer&> mTask; + + static Eina_Bool dispatcherFunc (void *data); - static Eina_Bool __dispatcher( void* data ); + Timer (); // allow no construction + Timer (const Timer&); // forbid copy constructor + Timer (double seconds, sigc::slot<bool, Timer&> task, bool loop); // private construction -> use factory () + virtual ~Timer (); // forbid direct delete -> use destroy() }; } // end namespace Ecorexx diff --git a/ecorexx/src/Timer.cpp b/ecorexx/src/Timer.cpp index 5ebf9a4..871e008 100644 --- a/ecorexx/src/Timer.cpp +++ b/ecorexx/src/Timer.cpp @@ -9,60 +9,67 @@ namespace Ecorexx { -Timer::Timer( double seconds, bool singleshot ) - :_ss( singleshot ) +Timer::Timer (double seconds, sigc::slot<bool, Timer&> task, bool loop) : + mETimer (NULL), + mTask (task) { - Dout( dc::notice, "Timer::Timer() - current frequency is " << seconds ); - _et = ecore_timer_add( seconds, &Timer::__dispatcher, this ); - - // TODO: find out why to use this function and the difference between ecore_time_get() and ecore_loop_time_get() - //ecore_timer_loop_add (double in, int (*func) (void *data), const void *data); + if (!loop) + { + mETimer = ecore_timer_add (seconds, Timer::dispatcherFunc, this); + } + else + { + mETimer = ecore_timer_loop_add (seconds, Timer::dispatcherFunc, this); + } } -Timer::~Timer() +Timer *Timer::factory (double seconds, sigc::slot<bool, Timer&> task, bool loop) { - ecore_timer_del( _et ); + return new Timer (seconds, task, loop); } -/*Timer* Timer::singleShot( double seconds, const Timer::Slot& slot ) +Timer::~Timer() { - Timer* ecoretimer = new Timer( seconds, true ); - ecoretimer->timeout.connect( slot ); -}*/ +} -void Timer::del () +void Timer::destroy () { - assert (ecore_timer_del (_et)); + assert (ecore_timer_del (mETimer)); + + // !!!ATTENTION!!! + // suicide for a C++ object is dangerous, but allowed + // the simple rule is that no member functions or member variables are allowed to access after this point! + delete (this); } void Timer::setInterval (double seconds) { - ecore_timer_interval_set (_et, seconds); + ecore_timer_interval_set (mETimer, seconds); } double Timer::getInterval () { - return ecore_timer_interval_get (_et); + return ecore_timer_interval_get (mETimer); } void Timer::freeze () { - ecore_timer_freeze (_et); + ecore_timer_freeze (mETimer); } void Timer::thaw () { - ecore_timer_thaw (_et); + ecore_timer_thaw (mETimer); } void Timer::delay (double add) { - ecore_timer_delay (_et, add); + ecore_timer_delay (mETimer, add); } double Timer::getPending () { - return ecore_timer_pending_get (_et); + return ecore_timer_pending_get (mETimer); } double Timer::getPrecision () @@ -75,21 +82,27 @@ void Timer::setPrecision (double precision) ecore_timer_precision_set (precision); } -/*void Timer::tick() +void Timer::dump () { - Dout( dc::notice, "Timer[ " << this << " ]::tick()" ); -}*/ + ecore_timer_dump (); +} -Eina_Bool Timer::__dispatcher( void* data ) +Eina_Bool Timer::dispatcherFunc (void *data) { - Timer* object = reinterpret_cast<Timer*>( data ); - assert( object ); - object->timeout.emit( ); - //object->tick(); - /*bool singleshot = object->_ss; - if ( singleshot ) delete object; - return singleshot? 0:1;*/ - return ECORE_CALLBACK_RENEW; + Timer* eTimer = static_cast <Timer*>( data ); + assert (eTimer); + + bool ret = eTimer->mTask (*eTimer); + + if (!ret) + { + // do a suicide as the delete operator isn't public available + // the reason is that the C design below is a suicide design :-( + delete eTimer; + return false; + } + + return ret; } } // end namespace Ecorexx --
