putil: make LinkedListNode moveable

This commit is contained in:
rdb 2018-06-30 17:36:08 +02:00
parent 9441c28f61
commit f5a78d599d
2 changed files with 39 additions and 0 deletions

View File

@ -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

View File

@ -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);