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>
* The FListViewItem class now has a getData() and a setData() method
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()
{
@ -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

View File

@ -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 (&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

View File

@ -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;

View File

@ -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<timer_data> 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

View File

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

View File

@ -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) );
}
//----------------------------------------------------------------------