QIntrusiveList Class
template <typename N, QIntrusiveListNode N::* member> class QIntrusiveListThe QIntrusiveList class is a template class that provides a list of objects using static storage. \internal
. More...
Header: | #include <QIntrusiveList> |
Detailed Description
QIntrusiveList creates a linked list of objects. Adding and removing objects from the QIntrusiveList is a constant time operation and is very quick. The list performs no memory allocations, but does require the objects being added to the list to contain a QIntrusiveListNode instance for the list's use. Even so, for small lists QIntrusiveList uses less memory than Qt's other list classes.
As QIntrusiveList uses storage inside the objects in the list, each object can only be in one list at a time. Objects are inserted by the insert() method. If the object is already in a list (including the one it is being inserted into) it is first removed, and then inserted at the head of the list. QIntrusiveList is a last-in-first-out list. That is, following an insert() the inserted object becomes the list's first() object.
struct MyObject { MyObject(int value) : value(value) {} int value; QIntrusiveListNode node; }; typedef QIntrusiveList<MyObject, &MyObject::node> MyObjectList; void foo() { MyObjectList list; MyObject m0(0); MyObject m1(1); MyObject m2(2); list.insert(&m0); list.insert(&m1); list.insert(&m2); // QIntrusiveList is LIFO, so will print: 2... 1... 0... for (MyObjectList::iterator iter = list.begin(); iter != list.end(); ++iter) { qWarning() << iter->value; } }