diff --git a/filemonitor.cpp b/filemonitor.cpp index ffe5613..cd4dca4 100644 --- a/filemonitor.cpp +++ b/filemonitor.cpp @@ -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 @@ -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 @@ -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()) { @@ -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)) diff --git a/normalpages.cpp b/normalpages.cpp index 161d57a..7981474 100644 --- a/normalpages.cpp +++ b/normalpages.cpp @@ -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) @@ -36,7 +30,8 @@ TTXPageStream* NormalPages::NextPage() ++_iter; _page = *_iter; } - + +loop: if (_iter == _NormalPagesList.end()) { _page = nullptr; @@ -44,13 +39,23 @@ TTXPageStream* NormalPages::NextPage() 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 } } @@ -62,3 +67,19 @@ void NormalPages::ResetIter() _iter=_NormalPagesList.begin(); _page=nullptr; } + +template +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()); +} + + diff --git a/normalpages.h b/normalpages.h index 4015d1d..21f158b 100644 --- a/normalpages.h +++ b/normalpages.h @@ -23,8 +23,8 @@ class NormalPages void ResetIter(); void addPage(TTXPageStream* p); - - void deletePage(TTXPageStream* p); + + void sortPages(); protected: diff --git a/pagelist.cpp b/pagelist.cpp index 6213737..aac4d34 100644 --- a/pagelist.cpp +++ b/pagelist.cpp @@ -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! - } - -*/