convert sdr::animation::EventList to o3tl::sorted_vector
instead of home-grown linked list Change-Id: I7cf24692e7b9919ac83e404e2d0167c3015b97de Reviewed-on: https://gerrit.libreoffice.org/30569 Tested-by: Jenkins <ci@libreoffice.org> Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
This commit is contained in:
@@ -23,31 +23,24 @@
|
||||
#include <sal/types.h>
|
||||
#include <vcl/timer.hxx>
|
||||
#include <svx/svxdllapi.h>
|
||||
#include <o3tl/sorted_vector.hxx>
|
||||
|
||||
|
||||
// event class
|
||||
|
||||
namespace sdr
|
||||
{
|
||||
namespace animation
|
||||
{
|
||||
|
||||
class SVX_DLLPUBLIC Event
|
||||
{
|
||||
// time of event in ms
|
||||
sal_uInt32 mnTime;
|
||||
|
||||
// pointer for linked list sorted by mnTime
|
||||
Event* mpNext;
|
||||
|
||||
public:
|
||||
// constructor/destructor
|
||||
SAL_DLLPRIVATE explicit Event();
|
||||
virtual ~Event();
|
||||
|
||||
// access to mpNext
|
||||
SAL_DLLPRIVATE Event* GetNext() const { return mpNext; }
|
||||
SAL_DLLPRIVATE void SetNext(Event* pNew);
|
||||
|
||||
// get/set time
|
||||
SAL_DLLPRIVATE sal_uInt32 GetTime() const { return mnTime; }
|
||||
void SetTime(sal_uInt32 nNew);
|
||||
@@ -55,43 +48,12 @@ namespace sdr
|
||||
// execute event
|
||||
virtual void Trigger(sal_uInt32 nTime) = 0;
|
||||
};
|
||||
} // end of namespace animation
|
||||
} // end of namespace sdr
|
||||
|
||||
|
||||
// eventlist class
|
||||
|
||||
namespace sdr
|
||||
struct CompareEvent
|
||||
{
|
||||
namespace animation
|
||||
{
|
||||
class SVX_DLLPUBLIC EventList
|
||||
{
|
||||
// pointer to first entry
|
||||
Event* mpHead;
|
||||
|
||||
public:
|
||||
// constructor/destructor
|
||||
SAL_DLLPRIVATE EventList();
|
||||
virtual ~EventList();
|
||||
|
||||
// insert/remove time dependent
|
||||
SAL_DLLPRIVATE void Insert(Event* pNew);
|
||||
SAL_DLLPRIVATE void Remove(Event* pOld);
|
||||
|
||||
// get first
|
||||
SAL_DLLPRIVATE Event* GetFirst() { return mpHead; }
|
||||
bool operator()(Event* const& lhs, Event* const& rhs) const;
|
||||
};
|
||||
} // end of namespace animation
|
||||
} // end of namespace sdr
|
||||
|
||||
|
||||
// scheduler class
|
||||
|
||||
namespace sdr
|
||||
{
|
||||
namespace animation
|
||||
{
|
||||
class SVX_DLLPUBLIC Scheduler : public Timer
|
||||
{
|
||||
// time in ms
|
||||
@@ -101,7 +63,7 @@ namespace sdr
|
||||
sal_uInt32 mnDeltaTime;
|
||||
|
||||
// list of events
|
||||
EventList maList;
|
||||
o3tl::sorted_vector<Event*, CompareEvent> maList;
|
||||
|
||||
// Flag which remembers if this timer is paused. Default
|
||||
// is false.
|
||||
@@ -135,6 +97,7 @@ namespace sdr
|
||||
SAL_DLLPRIVATE bool IsPaused() const { return mbIsPaused; }
|
||||
SAL_DLLPRIVATE void SetPaused(bool bNew);
|
||||
};
|
||||
|
||||
} // end of namespace animation
|
||||
} // end of namespace sdr
|
||||
|
||||
|
@@ -28,9 +28,7 @@ namespace sdr
|
||||
{
|
||||
namespace animation
|
||||
{
|
||||
Event::Event()
|
||||
: mnTime(0),
|
||||
mpNext(nullptr)
|
||||
Event::Event() : mnTime(0)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -39,15 +37,6 @@ namespace sdr
|
||||
}
|
||||
|
||||
|
||||
void Event::SetNext(Event* pNew)
|
||||
{
|
||||
if(pNew != mpNext)
|
||||
{
|
||||
mpNext = pNew;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Event::SetTime(sal_uInt32 nNew)
|
||||
{
|
||||
if(mnTime != nNew)
|
||||
@@ -55,93 +44,13 @@ namespace sdr
|
||||
mnTime = nNew;
|
||||
}
|
||||
}
|
||||
} // end of namespace animation
|
||||
} // end of namespace sdr
|
||||
|
||||
|
||||
// eventlist class
|
||||
|
||||
namespace sdr
|
||||
{
|
||||
namespace animation
|
||||
{
|
||||
EventList::EventList()
|
||||
: mpHead(nullptr)
|
||||
bool CompareEvent::operator()(Event* const& lhs, Event* const& rhs) const
|
||||
{
|
||||
return lhs->GetTime() < rhs->GetTime();
|
||||
}
|
||||
|
||||
EventList::~EventList()
|
||||
{
|
||||
while(mpHead)
|
||||
{
|
||||
Event* pNext = mpHead->GetNext();
|
||||
mpHead->SetNext(nullptr);
|
||||
mpHead = pNext;
|
||||
}
|
||||
}
|
||||
|
||||
void EventList::Insert(Event* pNew)
|
||||
{
|
||||
if(pNew)
|
||||
{
|
||||
Event* pCurrent = mpHead;
|
||||
Event* pPrev = nullptr;
|
||||
|
||||
while(pCurrent && pCurrent->GetTime() < pNew->GetTime())
|
||||
{
|
||||
pPrev = pCurrent;
|
||||
pCurrent = pCurrent->GetNext();
|
||||
}
|
||||
|
||||
if(pPrev)
|
||||
{
|
||||
pNew->SetNext(pPrev->GetNext());
|
||||
pPrev->SetNext(pNew);
|
||||
}
|
||||
else
|
||||
{
|
||||
pNew->SetNext(mpHead);
|
||||
mpHead = pNew;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void EventList::Remove(Event* pOld)
|
||||
{
|
||||
if(pOld && mpHead)
|
||||
{
|
||||
Event* pCurrent = mpHead;
|
||||
Event* pPrev = nullptr;
|
||||
|
||||
while(pCurrent && pCurrent != pOld)
|
||||
{
|
||||
pPrev = pCurrent;
|
||||
pCurrent = pCurrent->GetNext();
|
||||
}
|
||||
|
||||
if(pPrev)
|
||||
{
|
||||
pPrev->SetNext(pOld->GetNext());
|
||||
}
|
||||
else
|
||||
{
|
||||
mpHead = pOld->GetNext();
|
||||
}
|
||||
|
||||
pOld->SetNext(nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
} // end of namespace animation
|
||||
} // end of namespace sdr
|
||||
|
||||
|
||||
// scheduler class
|
||||
|
||||
namespace sdr
|
||||
{
|
||||
namespace animation
|
||||
{
|
||||
Scheduler::Scheduler()
|
||||
: mnTime(0L),
|
||||
mnDeltaTime(0L),
|
||||
@@ -169,38 +78,36 @@ namespace sdr
|
||||
|
||||
void Scheduler::triggerEvents()
|
||||
{
|
||||
Event* pNextEvent = maList.GetFirst();
|
||||
if (maList.empty())
|
||||
return;
|
||||
|
||||
if(pNextEvent)
|
||||
{
|
||||
// copy events which need to be executed to a vector. Remove them from
|
||||
// the scheduler
|
||||
::std::vector< Event* > EventPointerVector;
|
||||
::std::vector< Event* > aToBeExecutedList;
|
||||
|
||||
while(pNextEvent && pNextEvent->GetTime() <= mnTime)
|
||||
while(!maList.empty() && maList[0]->GetTime() <= mnTime)
|
||||
{
|
||||
maList.Remove(pNextEvent);
|
||||
EventPointerVector.push_back(pNextEvent);
|
||||
pNextEvent = maList.GetFirst();
|
||||
Event* pNextEvent = maList.front();
|
||||
maList.erase(maList.begin());
|
||||
aToBeExecutedList.push_back(pNextEvent);
|
||||
}
|
||||
|
||||
// execute events from the vector
|
||||
::std::vector< Event* >::const_iterator aEnd = EventPointerVector.end();
|
||||
for(::std::vector< Event* >::iterator aCandidate = EventPointerVector.begin();
|
||||
::std::vector< Event* >::const_iterator aEnd = aToBeExecutedList.end();
|
||||
for(::std::vector< Event* >::iterator aCandidate = aToBeExecutedList.begin();
|
||||
aCandidate != aEnd; ++aCandidate)
|
||||
{
|
||||
// trigger event. This may re-insert the event to the scheduler again
|
||||
(*aCandidate)->Trigger(mnTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Scheduler::checkTimeout()
|
||||
{
|
||||
// re-start or stop timer according to event list
|
||||
if(!IsPaused() && maList.GetFirst())
|
||||
if(!IsPaused() && !maList.empty())
|
||||
{
|
||||
mnDeltaTime = maList.GetFirst()->GetTime() - mnTime;
|
||||
mnDeltaTime = maList.front()->GetTime() - mnTime;
|
||||
|
||||
if(0L != mnDeltaTime)
|
||||
{
|
||||
@@ -222,16 +129,13 @@ namespace sdr
|
||||
Stop();
|
||||
mnTime = nTime;
|
||||
|
||||
// get event pointer
|
||||
Event* pEvent = maList.GetFirst();
|
||||
if (maList.empty())
|
||||
return;
|
||||
|
||||
if(pEvent)
|
||||
// reset event time points
|
||||
for (auto & rEvent : maList)
|
||||
{
|
||||
// retet event time points
|
||||
while(pEvent)
|
||||
{
|
||||
pEvent->SetTime(nTime);
|
||||
pEvent = pEvent->GetNext();
|
||||
rEvent->SetTime(nTime);
|
||||
}
|
||||
|
||||
if(!IsPaused())
|
||||
@@ -243,22 +147,18 @@ namespace sdr
|
||||
checkTimeout();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Scheduler::InsertEvent(Event* pNew)
|
||||
{
|
||||
if(pNew)
|
||||
{
|
||||
maList.Insert(pNew);
|
||||
maList.insert(pNew);
|
||||
checkTimeout();
|
||||
}
|
||||
}
|
||||
|
||||
void Scheduler::RemoveEvent(Event* pOld)
|
||||
{
|
||||
if(pOld && maList.GetFirst())
|
||||
if(!maList.empty())
|
||||
{
|
||||
maList.Remove(pOld);
|
||||
maList.erase(maList.find(pOld));
|
||||
checkTimeout();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user