Browse Source

putil: make LinkedListNode moveable

rdb 7 years ago
parent
commit
f5a78d599d
2 changed files with 39 additions and 0 deletions
  1. 36 0
      panda/src/putil/linkedListNode.I
  2. 3 0
      panda/src/putil/linkedListNode.h

+ 36 - 0
panda/src/putil/linkedListNode.I

@@ -33,6 +33,25 @@ LinkedListNode(bool) {
   _prev = this;
 }
 
+/**
+ * This move constructor replaces the other link with this one.
+ */
+INLINE LinkedListNode::
+LinkedListNode(LinkedListNode &&from) noexcept {
+  if (from._prev != nullptr) {
+    nassertv(from._prev->_next == &from);
+    from._prev->_next = this;
+  }
+  _prev = from._prev;
+  if (from._next != nullptr) {
+    nassertv(from._next->_prev == &from);
+    from._next->_prev = this;
+  }
+  _next = from._next;
+  from._next = nullptr;
+  from._prev = nullptr;
+}
+
 /**
  *
  */
@@ -41,6 +60,23 @@ INLINE LinkedListNode::
   nassertv((_next == nullptr && _prev == nullptr) || (_next == this && _prev == this));
 }
 
+/**
+ * Replaces the given other node with this node.
+ */
+INLINE LinkedListNode &LinkedListNode::
+operator = (LinkedListNode &&from) {
+  nassertr((_next == nullptr && _prev == nullptr) || (_next == this && _prev == this), *this);
+  nassertr(from._prev != nullptr && from._next != nullptr, *this);
+  nassertr(from._prev->_next == &from && from._next->_prev == &from, *this);
+  from._prev->_next = this;
+  from._next->_prev = this;
+  _prev = from._prev;
+  _next = from._next;
+  from._next = nullptr;
+  from._prev = nullptr;
+  return *this;
+}
+
 /**
  * Returns true if the node is member of any list, false if it has been
  * removed or never added.  The head of a list generally appears to to always

+ 3 - 0
panda/src/putil/linkedListNode.h

@@ -32,8 +32,11 @@ class EXPCL_PANDA_PUTIL LinkedListNode {
 protected:
   INLINE LinkedListNode();
   INLINE LinkedListNode(bool);
+  INLINE LinkedListNode(LinkedListNode &&from) noexcept;
   INLINE ~LinkedListNode();
 
+  INLINE LinkedListNode &operator = (LinkedListNode &&from);
+
   INLINE bool is_on_list() const;
   INLINE void remove_from_list();
   INLINE void insert_before(LinkedListNode *node);