diff --git a/ChangeLog b/ChangeLog index 0ab75438..6021715f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,6 @@ +2018-09-27 Markus Gans + * Move time event processing from FApplication to FObject + 2018-09-26 Markus Gans * The FListViewItem class now has a getData() and a setData() method similar to the FListBoxItem class. diff --git a/src/fapplication.cpp b/src/fapplication.cpp index 0e012029..3e3a5d3d 100644 --- a/src/fapplication.cpp +++ b/src/fapplication.cpp @@ -1149,51 +1149,6 @@ void FApplication::processResizeEvent() } } -//---------------------------------------------------------------------- -uInt FApplication::processTimerEvent() -{ - FObject::TimerList::iterator iter, last; - timeval currentTime; - uInt activated = 0; - - getCurrentTime (¤tTime); - - if ( isTimerInUpdating() ) - return 0; - - if ( ! timer_list ) - return 0; - - if ( timer_list->empty() ) - return 0; - - iter = timer_list->begin(); - last = timer_list->end(); - - while ( iter != last ) - { - if ( ! iter->id - || ! iter->object - || currentTime < iter->timeout ) // no timer expired - break; - - iter->timeout += iter->interval; - - if ( iter->timeout < currentTime ) - iter->timeout = currentTime + iter->interval; - - if ( iter->interval.tv_usec > 0 || iter->interval.tv_sec > 0 ) - activated++; - - FTimerEvent t_ev(fc::Timer_Event, iter->id); - sendEvent(iter->object, &t_ev); - - ++iter; - } - - return activated; -} - //---------------------------------------------------------------------- void FApplication::processCloseWidget() { @@ -1233,4 +1188,11 @@ bool FApplication::processNextEvent() return ( num_events > 0 ); } +//---------------------------------------------------------------------- +void FApplication::performTimerAction ( const FObject* receiver + , const FEvent* event ) +{ + sendEvent(receiver, event); +} + } // namespace finalcut diff --git a/src/fobject.cpp b/src/fobject.cpp index 897cb893..80436273 100644 --- a/src/fobject.cpp +++ b/src/fobject.cpp @@ -240,22 +240,8 @@ int FObject::addTimer (int interval) timeval time_interval; timeval currentTime; int id = 1; - timer_modify_lock = true; - if ( ! timer_list ) - { - try - { - timer_list = new TimerList(); - } - catch (const std::bad_alloc& ex) - { - std::cerr << "not enough memory to alloc " << ex.what() << std::endl; - return -1; - } - } - // find an unused timer id if ( ! timer_list->empty() ) { @@ -389,4 +375,52 @@ bool FObject::event (FEvent* ev) void FObject::onTimer (FTimerEvent*) { } +//---------------------------------------------------------------------- +uInt FObject::processTimerEvent() +{ + FObject::TimerList::iterator iter, last; + timeval currentTime; + uInt activated = 0; + + getCurrentTime (¤tTime); + + if ( isTimerInUpdating() ) + return 0; + + if ( ! timer_list ) + return 0; + + if ( timer_list->empty() ) + return 0; + + iter = timer_list->begin(); + last = timer_list->end(); + + while ( iter != last ) + { + if ( ! iter->id + || ! iter->object + || currentTime < iter->timeout ) // no timer expired + break; + + iter->timeout += iter->interval; + + if ( iter->timeout < currentTime ) + iter->timeout = currentTime + iter->interval; + + if ( iter->interval.tv_usec > 0 || iter->interval.tv_sec > 0 ) + activated++; + + FTimerEvent t_ev(fc::Timer_Event, iter->id); + performTimerAction (iter->object, &t_ev); + ++iter; + } + + return activated; +} + +//---------------------------------------------------------------------- +void FObject::performTimerAction (const FObject*, const FEvent*) +{ } + } // namespace finalcut diff --git a/src/include/final/fapplication.h b/src/include/final/fapplication.h index 73f838e4..3b04c661 100644 --- a/src/include/final/fapplication.h +++ b/src/include/final/fapplication.h @@ -175,9 +175,10 @@ class FApplication : public FWidget void sendWheelEvent (const FPoint&, const FPoint&); void processMouseEvent(); void processResizeEvent(); - uInt processTimerEvent(); void processCloseWidget(); bool processNextEvent(); + virtual void performTimerAction ( const FObject* + , const FEvent* ); // Data Members int app_argc; diff --git a/src/include/final/fobject.h b/src/include/final/fobject.h index 4f42c39f..64793e3c 100644 --- a/src/include/final/fobject.h +++ b/src/include/final/fobject.h @@ -71,37 +71,37 @@ class FObject virtual ~FObject(); // Accessors - virtual const char* getClassName() const; - FObject* getParent() const; - FObject* getChild (int) const; - const FObjectList& getChildren() const; - int numOfChildren() const; - FObjectIterator begin(); - FObjectIterator end(); - constFObjectIterator begin() const; - constFObjectIterator end() const; + virtual const char* getClassName() const; + FObject* getParent() const; + FObject* getChild (int) const; + const FObjectList& getChildren() const; + int numOfChildren() const; + FObjectIterator begin(); + FObjectIterator end(); + constFObjectIterator begin() const; + constFObjectIterator end() const; // Inquiries - bool hasParent() const; - bool hasChildren() const; - bool isChild (FObject*) const; - bool isDirectChild (FObject*) const; - bool isWidget() const; - bool isInstanceOf (const char[]) const; - bool isTimerInUpdating() const; + bool hasParent() const; + bool hasChildren() const; + bool isChild (FObject*) const; + bool isDirectChild (FObject*) const; + bool isWidget() const; + bool isInstanceOf (const char[]) const; + bool isTimerInUpdating() const; // Methods - void removeParent(); - void addChild (FObject*); - void delChild (FObject*); + void removeParent(); + void addChild (FObject*); + void delChild (FObject*); // Timer methods - static void getCurrentTime (timeval*); - static bool isTimeout (timeval*, long); - int addTimer (int); - bool delTimer (int); - bool delOwnTimer(); - bool delAllTimer(); + static void getCurrentTime (timeval*); + static bool isTimeout (timeval*, long); + int addTimer (int); + bool delTimer (int); + bool delOwnTimer(); + bool delAllTimer(); protected: struct timer_data @@ -115,13 +115,18 @@ class FObject // Typedef typedef std::vector TimerList; - // Event handler - virtual bool event (FEvent*); - virtual void onTimer (FTimerEvent*); + // Accessor + TimerList* getTimerList() const; - // Data Members - static TimerList* timer_list; - bool widget_object; + // Method + uInt processTimerEvent(); + + // Event handler + virtual bool event (FEvent*); + virtual void onTimer (FTimerEvent*); + + // Data Member + bool widget_object; private: // Disable copy constructor @@ -130,11 +135,15 @@ class FObject // Disable assignment operator (=) FObject& operator = (const FObject&); + // Method + virtual void performTimerAction (const FObject*, const FEvent*); + // Data Members - FObject* parent_obj; - FObjectList children_list; - bool has_parent; - static bool timer_modify_lock; + FObject* parent_obj; + FObjectList children_list; + bool has_parent; + static bool timer_modify_lock; + static TimerList* timer_list; }; #pragma pack(pop) @@ -195,6 +204,10 @@ inline bool FObject::isInstanceOf (const char classname[]) const inline bool FObject::isTimerInUpdating() const { return timer_modify_lock; } +//---------------------------------------------------------------------- +inline FObject::TimerList* FObject::getTimerList() const +{ return timer_list; } + //---------------------------------------------------------------------- // Operator functions for timeval diff --git a/src/include/final/ftermcap.h b/src/include/final/ftermcap.h index d4d76f7a..eb619c0d 100644 --- a/src/include/final/ftermcap.h +++ b/src/include/final/ftermcap.h @@ -65,7 +65,7 @@ class FTermcap { } // Destructor - virtual ~FTermcap() + ~FTermcap() { } // Accessors diff --git a/test/fobject-test.cpp b/test/fobject-test.cpp index 24f20484..43406965 100644 --- a/test/fobject-test.cpp +++ b/test/fobject-test.cpp @@ -46,9 +46,9 @@ class FObject_protected : public finalcut::FObject return finalcut::FObject::event(ev); } - finalcut::FObject::TimerList* getTimerList() const + TimerList* getTimerList() const { - return timer_list; + return finalcut::FObject::getTimerList(); } }; #pragma pack(pop) @@ -334,6 +334,9 @@ void FObjectTest::timeTest() CPPUNIT_ASSERT ( ! finalcut::FObject::isTimeout (&time1, timeout) ); sleep(1); CPPUNIT_ASSERT ( finalcut::FObject::isTimeout (&time1, timeout) ); + time1.tv_sec = 300; + time1.tv_usec = 2000000; // > 1000000 µs to test diff underflow + CPPUNIT_ASSERT ( finalcut::FObject::isTimeout (&time1, timeout) ); } //----------------------------------------------------------------------