2017-12-03 06:05:16 +00:00
|
|
|
#ifndef BOO_INTERNAL_COMMON_HPP
|
|
|
|
#define BOO_INTERNAL_COMMON_HPP
|
|
|
|
|
|
|
|
#include "boo/BooObject.hpp"
|
|
|
|
#include <iterator>
|
|
|
|
|
|
|
|
namespace boo
|
|
|
|
{
|
|
|
|
|
2017-12-04 02:50:33 +00:00
|
|
|
/** Linked-list iterator shareable by ListNode types. */
|
|
|
|
template <class T>
|
2017-12-07 04:08:44 +00:00
|
|
|
class ListIterator
|
2017-12-03 06:05:16 +00:00
|
|
|
{
|
|
|
|
T* m_node;
|
|
|
|
public:
|
2017-12-07 04:08:44 +00:00
|
|
|
using iterator_category = std::bidirectional_iterator_tag;
|
|
|
|
using value_type = T;
|
|
|
|
using difference_type = std::ptrdiff_t;
|
|
|
|
using pointer = T*;
|
|
|
|
using reference = T&;
|
|
|
|
|
2017-12-04 02:50:33 +00:00
|
|
|
explicit ListIterator(T* node) : m_node(node) {}
|
2017-12-03 06:05:16 +00:00
|
|
|
T& operator*() const { return *m_node; }
|
2017-12-04 02:50:33 +00:00
|
|
|
bool operator!=(const ListIterator& other) const { return m_node != other.m_node; }
|
|
|
|
ListIterator& operator++() { m_node = m_node->m_next; return *this; }
|
|
|
|
ListIterator& operator--() { m_node = m_node->m_prev; return *this; }
|
2017-12-03 06:05:16 +00:00
|
|
|
};
|
|
|
|
|
2017-12-04 02:50:33 +00:00
|
|
|
/** Linked-list IObj node made part of objects participating in list.
|
2017-12-03 06:05:16 +00:00
|
|
|
* Subclasses must implement static methods _getHeadPtr() and _getHeadLock()
|
|
|
|
* to support the common list-management functionality.
|
|
|
|
*/
|
|
|
|
template <class N, class H, class P = IObj>
|
|
|
|
struct ListNode : P
|
|
|
|
{
|
2017-12-04 02:50:33 +00:00
|
|
|
using iterator = ListIterator<N>;
|
2017-12-03 06:05:16 +00:00
|
|
|
iterator begin() { return iterator(static_cast<N*>(this)); }
|
|
|
|
iterator end() { return iterator(nullptr); }
|
|
|
|
|
|
|
|
H m_head;
|
|
|
|
N* m_next;
|
|
|
|
N* m_prev = nullptr;
|
|
|
|
ListNode(H head) : m_head(head)
|
|
|
|
{
|
|
|
|
auto lk = N::_getHeadLock(head);
|
|
|
|
m_next = N::_getHeadPtr(head);
|
|
|
|
if (m_next)
|
|
|
|
m_next->m_prev = static_cast<N*>(this);
|
|
|
|
N::_getHeadPtr(head) = static_cast<N*>(this);
|
|
|
|
}
|
|
|
|
protected:
|
|
|
|
~ListNode()
|
|
|
|
{
|
|
|
|
if (m_prev)
|
|
|
|
{
|
|
|
|
if (m_next)
|
|
|
|
m_next->m_prev = m_prev;
|
|
|
|
m_prev->m_next = m_next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (m_next)
|
|
|
|
m_next->m_prev = nullptr;
|
|
|
|
N::_getHeadPtr(m_head) = m_next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // BOO_INTERNAL_COMMON_HPP
|