Move time event processing from FApplication to FObject

This commit is contained in:
Markus Gans 2018-09-27 03:02:07 +02:00
parent affdf6fe11
commit 11ba43fa0f
7 changed files with 114 additions and 98 deletions

View File

@ -1,3 +1,6 @@
2018-09-27 Markus Gans <guru.mail@muenster.de>
* Move time event processing from FApplication to FObject
2018-09-26 Markus Gans <guru.mail@muenster.de> 2018-09-26 Markus Gans <guru.mail@muenster.de>
* The FListViewItem class now has a getData() and a setData() method * The FListViewItem class now has a getData() and a setData() method
similar to the FListBoxItem class. similar to the FListBoxItem class.

View File

@ -1149,51 +1149,6 @@ void FApplication::processResizeEvent()
} }
} }
//----------------------------------------------------------------------
uInt FApplication::processTimerEvent()
{
FObject::TimerList::iterator iter, last;
timeval currentTime;
uInt activated = 0;
getCurrentTime (&currentTime);
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() void FApplication::processCloseWidget()
{ {
@ -1233,4 +1188,11 @@ bool FApplication::processNextEvent()
return ( num_events > 0 ); return ( num_events > 0 );
} }
//----------------------------------------------------------------------
void FApplication::performTimerAction ( const FObject* receiver
, const FEvent* event )
{
sendEvent(receiver, event);
}
} // namespace finalcut } // namespace finalcut

View File

@ -240,22 +240,8 @@ int FObject::addTimer (int interval)
timeval time_interval; timeval time_interval;
timeval currentTime; timeval currentTime;
int id = 1; int id = 1;
timer_modify_lock = true; 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 // find an unused timer id
if ( ! timer_list->empty() ) if ( ! timer_list->empty() )
{ {
@ -389,4 +375,52 @@ bool FObject::event (FEvent* ev)
void FObject::onTimer (FTimerEvent*) void FObject::onTimer (FTimerEvent*)
{ } { }
//----------------------------------------------------------------------
uInt FObject::processTimerEvent()
{
FObject::TimerList::iterator iter, last;
timeval currentTime;
uInt activated = 0;
getCurrentTime (&currentTime);
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 } // namespace finalcut

View File

@ -175,9 +175,10 @@ class FApplication : public FWidget
void sendWheelEvent (const FPoint&, const FPoint&); void sendWheelEvent (const FPoint&, const FPoint&);
void processMouseEvent(); void processMouseEvent();
void processResizeEvent(); void processResizeEvent();
uInt processTimerEvent();
void processCloseWidget(); void processCloseWidget();
bool processNextEvent(); bool processNextEvent();
virtual void performTimerAction ( const FObject*
, const FEvent* );
// Data Members // Data Members
int app_argc; int app_argc;

View File

@ -115,12 +115,17 @@ class FObject
// Typedef // Typedef
typedef std::vector<timer_data> TimerList; typedef std::vector<timer_data> TimerList;
// Accessor
TimerList* getTimerList() const;
// Method
uInt processTimerEvent();
// Event handler // Event handler
virtual bool event (FEvent*); virtual bool event (FEvent*);
virtual void onTimer (FTimerEvent*); virtual void onTimer (FTimerEvent*);
// Data Members // Data Member
static TimerList* timer_list;
bool widget_object; bool widget_object;
private: private:
@ -130,11 +135,15 @@ class FObject
// Disable assignment operator (=) // Disable assignment operator (=)
FObject& operator = (const FObject&); FObject& operator = (const FObject&);
// Method
virtual void performTimerAction (const FObject*, const FEvent*);
// Data Members // Data Members
FObject* parent_obj; FObject* parent_obj;
FObjectList children_list; FObjectList children_list;
bool has_parent; bool has_parent;
static bool timer_modify_lock; static bool timer_modify_lock;
static TimerList* timer_list;
}; };
#pragma pack(pop) #pragma pack(pop)
@ -195,6 +204,10 @@ inline bool FObject::isInstanceOf (const char classname[]) const
inline bool FObject::isTimerInUpdating() const inline bool FObject::isTimerInUpdating() const
{ return timer_modify_lock; } { return timer_modify_lock; }
//----------------------------------------------------------------------
inline FObject::TimerList* FObject::getTimerList() const
{ return timer_list; }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
// Operator functions for timeval // Operator functions for timeval

View File

@ -65,7 +65,7 @@ class FTermcap
{ } { }
// Destructor // Destructor
virtual ~FTermcap() ~FTermcap()
{ } { }
// Accessors // Accessors

View File

@ -46,9 +46,9 @@ class FObject_protected : public finalcut::FObject
return finalcut::FObject::event(ev); return finalcut::FObject::event(ev);
} }
finalcut::FObject::TimerList* getTimerList() const TimerList* getTimerList() const
{ {
return timer_list; return finalcut::FObject::getTimerList();
} }
}; };
#pragma pack(pop) #pragma pack(pop)
@ -334,6 +334,9 @@ void FObjectTest::timeTest()
CPPUNIT_ASSERT ( ! finalcut::FObject::isTimeout (&time1, timeout) ); CPPUNIT_ASSERT ( ! finalcut::FObject::isTimeout (&time1, timeout) );
sleep(1); sleep(1);
CPPUNIT_ASSERT ( finalcut::FObject::isTimeout (&time1, timeout) ); 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) );
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------