#ifndef STORM_LIST_TS_LINK_HPP #define STORM_LIST_TS_LINK_HPP #include #include template class TSLink { public: // Member variables TSLink* m_prevlink = nullptr; T* m_next = nullptr; // Member functions ~TSLink(); bool IsLinked(void); T* Next(void); TSLink* NextLink(ptrdiff_t linkoffset); T* Prev(void); T* RawNext(void); void Unlink(void); }; template TSLink::~TSLink() { this->Unlink(); } template bool TSLink::IsLinked() { return this->m_next != nullptr; } template T* TSLink::Next() { // Check for sentinel node (indicates list end) return reinterpret_cast(this->m_next) <= 0 ? nullptr : this->m_next; } template TSLink* TSLink::NextLink(ptrdiff_t linkoffset) { T* next = this->m_next; if (reinterpret_cast(next) <= 0) { // End of list return reinterpret_cast*>(~reinterpret_cast(next)); } else { ptrdiff_t offset; if (linkoffset < 0) { offset = reinterpret_cast(this) - reinterpret_cast(this->m_prevlink->m_next); } else { offset = linkoffset; } return reinterpret_cast*>(reinterpret_cast(this->m_next) + offset); } } template T* TSLink::Prev() { return this->m_prevlink->m_prevlink->Next(); } template T* TSLink::RawNext() { return this->m_next; } template void TSLink::Unlink() { if (this->m_prevlink) { this->NextLink(-1)->m_prevlink = this->m_prevlink; this->m_prevlink->m_next = this->m_next; this->m_prevlink = nullptr; this->m_next = nullptr; } } #endif