From 2a9fdacc60f667c8797446dc867bedfaded46e37 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 21 Aug 2016 06:04:00 -0400 Subject: [PATCH] intrusive_list: move iterator implementation above list Will make keeping non-member list functions easier to keep together with the class. --- src/common/intrusive_list.h | 142 ++++++++++++++++++------------------ 1 file changed, 71 insertions(+), 71 deletions(-) diff --git a/src/common/intrusive_list.h b/src/common/intrusive_list.h index 1b24bb05..4160d5d8 100644 --- a/src/common/intrusive_list.h +++ b/src/common/intrusive_list.h @@ -39,6 +39,77 @@ private: IntrusiveListNode* prev = this; }; +template +class IntrusiveListIterator { +public: + using iterator_category = std::bidirectional_iterator_tag; + using difference_type = std::ptrdiff_t; + using value_type = T; + using pointer = value_type*; + using const_pointer = const value_type*; + using reference = value_type&; + using const_reference = const value_type&; + + // If value_type is const, we want "const IntrusiveListNode", not "const IntrusiveListNode" + using node_type = std::conditional_t::value, + const IntrusiveListNode>, + IntrusiveListNode>; + using node_pointer = node_type*; + using node_reference = node_type&; + + IntrusiveListIterator() = default; + IntrusiveListIterator(const IntrusiveListIterator& other) = default; + IntrusiveListIterator& operator=(const IntrusiveListIterator& other) = default; + + IntrusiveListIterator(node_pointer list_root, node_pointer node) : root(list_root), node(node) { + } + + IntrusiveListIterator& operator++() { + node = node == root ? node : node->next; + return *this; + } + IntrusiveListIterator operator++(int) { + IntrusiveListIterator it(*this); + node = node == root ? node : node->next; + return it; + } + IntrusiveListIterator& operator--() { + node = node->prev == root ? node : node->prev; + return *this; + } + IntrusiveListIterator operator--(int) { + IntrusiveListIterator it(*this); + node = node->prev == root ? node : node->prev; + return it; + } + + bool operator==(const IntrusiveListIterator& other) const { + DEBUG_ASSERT(root == other.root); + return node == other.node; + } + bool operator!=(const IntrusiveListIterator& other) const { + return !(*this == other); + } + + reference operator*() const { + DEBUG_ASSERT(node != root); + return static_cast(*node); + } + pointer operator->() const { + DEBUG_ASSERT(node != root); + return std::addressof(operator*()); + } + + node_pointer AsNodePointer() const { + return node; + } + +private: + friend class IntrusiveList; + node_pointer root = nullptr; + node_pointer node = nullptr; +}; + template class IntrusiveList { public: @@ -189,76 +260,5 @@ private: std::shared_ptr> root = std::make_shared>(); }; -template -class IntrusiveListIterator { -public: - using iterator_category = std::bidirectional_iterator_tag; - using difference_type = std::ptrdiff_t; - using value_type = T; - using pointer = value_type*; - using const_pointer = const value_type*; - using reference = value_type&; - using const_reference = const value_type&; - - // If value_type is const, we want "const IntrusiveListNode", not "const IntrusiveListNode" - using node_type = std::conditional_t::value, - const IntrusiveListNode>, - IntrusiveListNode>; - using node_pointer = node_type*; - using node_reference = node_type&; - - IntrusiveListIterator() = default; - IntrusiveListIterator(const IntrusiveListIterator& other) = default; - IntrusiveListIterator& operator=(const IntrusiveListIterator& other) = default; - - IntrusiveListIterator(node_pointer list_root, node_pointer node) : root(list_root), node(node) { - } - - IntrusiveListIterator& operator++() { - node = node == root ? node : node->next; - return *this; - } - IntrusiveListIterator operator++(int) { - IntrusiveListIterator it(*this); - node = node == root ? node : node->next; - return it; - } - IntrusiveListIterator& operator--() { - node = node->prev == root ? node : node->prev; - return *this; - } - IntrusiveListIterator operator--(int) { - IntrusiveListIterator it(*this); - node = node->prev == root ? node : node->prev; - return it; - } - - bool operator==(const IntrusiveListIterator& other) const { - DEBUG_ASSERT(root == other.root); - return node == other.node; - } - bool operator!=(const IntrusiveListIterator& other) const { - return !(*this == other); - } - - reference operator*() const { - DEBUG_ASSERT(node != root); - return static_cast(*node); - } - pointer operator->() const { - DEBUG_ASSERT(node != root); - return std::addressof(operator*()); - } - - node_pointer AsNodePointer() const { - return node; - } - -private: - friend class IntrusiveList; - node_pointer root = nullptr; - node_pointer node = nullptr; -}; - } // namespace Common } // namespace Dynarmic