Skip to content

Commit

Permalink
Begin rewriting page lists again
Browse files Browse the repository at this point in the history
This commit sorts the NormalPages lists by page number and alters the way pointers are removed from the list to avoid resetting the iterator each time.
  • Loading branch information
ZXGuesser committed Sep 15, 2019
1 parent c9fe318 commit da7d6d5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 27 deletions.
6 changes: 4 additions & 2 deletions filemonitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ void FileMonitor::run()
// Page is 'normal', add to NormalPages list
_pageList->GetMagazines()[mag]->GetNormalPages()->addPage(q);
std::cerr << "[FileMonitor::run] page was special, is now normal " << std::hex << q->GetPageNumber() << std::endl;
_pageList->GetMagazines()[mag]->GetNormalPages()->sortPages(); // TODO: add pages in correct place in list
}
}
else
Expand Down Expand Up @@ -170,6 +171,7 @@ void FileMonitor::run()
// Page is 'normal', add to NormalPages list
_pageList->GetMagazines()[mag]->GetNormalPages()->addPage(q);
std::cerr << "[FileMonitor::run] page was carousel, is now normal " << std::hex << q->GetPageNumber() << std::endl;
_pageList->GetMagazines()[mag]->GetNormalPages()->sortPages(); // TODO: add pages in correct place in list
}
}
else
Expand All @@ -186,8 +188,7 @@ void FileMonitor::run()
if (q->Special() || q->IsCarousel())
{
// Page is no longer 'normal'
// TODO: remove from NormalPages list
_pageList->GetMagazines()[mag]->GetNormalPages()->deletePage(q);
// page will be removed from NormalPages list by the service thread

if (q->Special())
{
Expand Down Expand Up @@ -252,6 +253,7 @@ void FileMonitor::run()
// Page is 'normal'
_pageList->GetMagazines()[mag]->GetNormalPages()->addPage(q);
//std::cerr << "[FileMonitor::run] new page is normal " << std::hex << q->GetPageNumber() << std::endl;
_pageList->GetMagazines()[mag]->GetNormalPages()->sortPages();
}

if (_pageList->CheckForPacket29(q))
Expand Down
41 changes: 31 additions & 10 deletions normalpages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ void NormalPages::addPage(TTXPageStream* p)
_NormalPagesList.push_front(p);
}

void NormalPages::deletePage(TTXPageStream* p)
{
_NormalPagesList.remove(p);
ResetIter();
}

TTXPageStream* NormalPages::NextPage()
{
if (_page == nullptr)
Expand All @@ -36,21 +30,32 @@ TTXPageStream* NormalPages::NextPage()
++_iter;
_page = *_iter;
}


loop:
if (_iter == _NormalPagesList.end())
{
_page = nullptr;
}

if (_page)
{
/* remove pointers from this list if the pages are marked for deletion, or have become 'Special' pages, then loop back to get the next page instead */

if (_page->GetStatusFlag()==TTXPageStream::MARKED)
{
std::cerr << "[NormalPages::NextPage] Deleted " << _page->GetSourcePage() << std::endl;
_page->SetState(TTXPageStream::GONE);
_NormalPagesList.remove(_page);
ResetIter();
return nullptr;
_iter = _NormalPagesList.erase(_iter);
_page = *_iter;
goto loop; // jump back to try for the next page
}

if (_page->GetSpecialFlag() || _page->GetSpecialFlag())
{
std::cerr << "[NormalPages::NextPage] " << _page->GetSourcePage() << " became Special" << std::endl;
_iter = _NormalPagesList.erase(_iter);
_page = *_iter;
goto loop; // jump back to try for the next page
}
}

Expand All @@ -62,3 +67,19 @@ void NormalPages::ResetIter()
_iter=_NormalPagesList.begin();
_page=nullptr;
}

template <typename TTXPageStream>
struct pageLessThan
{
bool operator()(const TTXPageStream *a, const TTXPageStream *b) const{
return a->GetPageNumber() < b->GetPageNumber();
}
};

void NormalPages::sortPages()
{
// sort the page list by page number
_NormalPagesList.sort(pageLessThan<TTXPageStream>());
}


4 changes: 2 additions & 2 deletions normalpages.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class NormalPages
void ResetIter();

void addPage(TTXPageStream* p);

void deletePage(TTXPageStream* p);
void sortPages();


protected:
Expand Down
15 changes: 2 additions & 13 deletions pagelist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,18 +425,7 @@ void PageList::PopulatePageTypeLists()
_mag[mag]->GetNormalPages()->addPage(ptr);
}
}

_mag[mag]->GetNormalPages()->sortPages(); // sort the NormalPages lists by page number
}
}

/* Want this to happen in the Service thread.
// Not the best idea, to check for deletes here
if (_fileToDelete==ptr->GetSourcePage()) // This works but isn't great. Probably not too efficient
{
std::cerr << "[PageList::Locate]ready to delete " << ptr->GetSourcePage() << std::endl;
_fileToDelete="null"; // Signal that delete has been done.
//@todo Delete the page object, remove it from pagelist, fixup mag, skip to the next page
_pageList[mag].remove(*(p++)); // Also post-increment the iterator OR WE CRASH!
// We can do this safely because this thread is in the Service thread and won't clash WRONG!
}
*/

0 comments on commit da7d6d5

Please sign in to comment.