// // Created by EmsiaetKadosh on 25-3-4. // #pragma once template class AnywhereEditable; template class AnywhereEditableList; template class AnywhereIterator; class AnywhereIteratorEnd; /** * 此处无法进行代码编译层面的直接约束 * @tparam T 满足T extends AnywhereEditable * @tparam L 满足L extends AnywhereEditableList */ template class AnywhereEditable { public: using Lst = std::conditional_t, AnywhereEditableList, L>; private: friend class AnywhereIterator; friend class AnywhereEditableList; friend class AnywhereIteratorEnd; friend L; AnywhereEditable* prev = nullptr; AnywhereEditable* next = nullptr; AnywhereEditableList* list = nullptr; // 指示自身属于某个列表 protected: bool managedByList = false; // 指示是否在列表内管理内存,而非列表外管理内存 byte reserved[7]{}; // reserved[0]: Task::schedulePop public: AnywhereEditable() = default; AnywhereEditable(const AnywhereEditable& other) {} AnywhereEditable(AnywhereEditable&& other) noexcept : prev(other->prev), next(other->next), list(other->list) { other->prev = nullptr; other->next = nullptr; other->list = nullptr; } virtual ~AnywhereEditable() { if (list) { managedByList = false; auto lst = list; list = nullptr; lst->pop(static_cast(this)); Logger.warn(L"~AnywhereEditable() : 析构时没有清除list,导致了析构时pop"); } } int pushCopy(AnywhereEditableList& list) noexcept; int pushThis(AnywhereEditableList& list) noexcept; virtual int pop() noexcept; AnywhereEditableList* getContainer() const noexcept { return list; } }; template class AnywhereIterator { friend class AnywhereIteratorEnd; AnywhereEditable* current; public: explicit AnywhereIterator(AnywhereEditable* current) : current(current) {} AnywhereIterator(const AnywhereIterator& other) = default; T& operator*() noexcept(false) { if (current->next) return static_cast(*current); throw RuntimeException(L"AnywhereIterator::operator*() : next is null, end of list"); } T* operator->() noexcept(false) { if (current->next) return &static_cast(*current); throw RuntimeException(L"AnywhereIterator::operator*() : next is null, end of list"); } bool operator!=(const AnywhereIterator& other) const noexcept { return current != other.current; } bool operator==(const AnywhereIterator& other) const noexcept { return current == other.current; } bool operator!=(const AnywhereIteratorEnd& other) const noexcept; bool operator==(const AnywhereIteratorEnd& other) const noexcept; AnywhereIterator& operator++() noexcept(false) { if (current->next) current = current->next; else throw InvalidOperationException(L"AnywhereIterator::operator++() : end of list"); return *this; } AnywhereIterator operator++(int) noexcept(false) { if (current->next) { AnywhereIterator ret = *this; current = current->next; return ret; } throw InvalidOperationException(L"AnywhereIterator::operator++() : end of list"); } AnywhereIterator& operator--() noexcept(false) { if (current->prev) current = current->prev; else throw InvalidOperationException(L"AnywhereIterator::operator--() : begin of list"); return *this; } AnywhereIterator operator--(int) noexcept(false) { if (current->prev) { AnywhereIterator ret = *this; current = current->prev; return ret; } throw InvalidOperationException(L"AnywhereIterator::operator--() : begin of list"); } }; class AnywhereIteratorEnd { public: AnywhereIteratorEnd() {} AnywhereIteratorEnd(const AnywhereIteratorEnd& other) = delete; AnywhereIteratorEnd(AnywhereIteratorEnd&& other) = delete; template bool operator!=(const AnywhereIterator& other) const noexcept { return other.current && other.current->next; } template bool operator==(const AnywhereIterator& other) const noexcept { return !operator!=(other); } }; template class AnywhereEditableList { protected: AnywhereEditable head; AnywhereEditable tail; public: AnywhereEditableList() { head.next = &tail; tail.prev = &head; } virtual ~AnywhereEditableList() { for (AnywhereIterator it = begin(); it != end(); ++it) {} } int pushCopy(T* value) noexcept; int pushThis(T* value) noexcept; int pushNewed(T* value) noexcept; virtual int pop(T* value) noexcept; AnywhereIterator begin() noexcept { return AnywhereIterator(head.next); } AnywhereIteratorEnd end() noexcept { return AnywhereIteratorEnd(); } AnywhereIterator begin() const noexcept { return AnywhereIterator(head.next); } AnywhereIteratorEnd end() const noexcept { return AnywhereIteratorEnd(); } AnywhereEditable* front() const noexcept { return head.next == &tail ? nullptr : head.next; } AnywhereEditable* back() const noexcept { return tail.prev == &head ? nullptr : tail.prev; } }; template int AnywhereEditable::pushCopy(AnywhereEditableList& list) noexcept { return list.pushCopy(static_cast(this)); } template int AnywhereEditable::pushThis(AnywhereEditableList& list) noexcept { return list.pushThis(static_cast(this)); } template int AnywhereEditable::pop() noexcept { return list->pop(static_cast(this)); } template bool AnywhereIterator::operator!=(const AnywhereIteratorEnd& other) const noexcept { return other != *this; } template bool AnywhereIterator::operator==(const AnywhereIteratorEnd& other) const noexcept { return other == *this; } template int AnywhereEditableList::pushCopy(T* value) noexcept { T* nv = new T(*value); return pushNewed(nv); } template int AnywhereEditableList::pushThis(T* value) noexcept { if (value->list) { Logger.error(L"AnywhereEditableList::pushThis() : value is already in a list"); Failed(); } value->list = this; tail.prev->next = value; value->prev = tail.prev; value->next = &tail; tail.prev = value; Success(); } template int AnywhereEditableList::pushNewed(T* value) noexcept { const int ret = pushThis(value); if (ret) delete value; else value->managedByList = true; return ret; } template int AnywhereEditableList::pop(T* value) noexcept { if (value->list != this) { Logger.error(L"AnywhereEditableList::pop() : value is not in this list"); Failed(); } value->list = nullptr; value->next->prev = value->prev; value->prev->next = value->next; if (value->managedByList) delete value; Success(); }