4.6.0
Freundlich's C++ toolkit
Loading...
Searching...
No Matches
fcppt.intrusive

Description

Intrusive list implementation.

Intrusive lists

An intrusive list is a list that does not actually own elements. Instead, when an element is constructed it gets the list it is going to be part of as a parameter. As long as the element is alive, it is part of the list. Such a list can be used to track elements whose lifetime is managed elsewhere. For example, fcppt.signal uses this technique to know which functions it has to call.

A type T can be inserted into an fcppt::intrusive::list<T> if it derives from fcppt::intrusive::base<T>. The implementation of fcppt::intrusive::base owns a pair of pointers that represent a list node. When such an object is destroyed or moved from, it automatically unlinks itself from the list.

Here is an example: First, we declare the list type we are going to use:

class element;
using element_list = fcppt::intrusive::list<element>;

Then, we define the element type, deriving from the base class and adding a constructor that takes a list as an argument:

class element : public fcppt::intrusive::base<element>
{
public:
element(fcppt::reference<element_list> const _list, int const _value)
: fcppt::intrusive::base<element>{_list.get()}, value_{_value}
{
}
element(element &&) noexcept = default;
element &operator=(element &&) noexcept = default;
~element() = default;
[[nodiscard]] int value() const { return value_; }
private:
int value_;
};

Here is a small example of how a list can be used:

element_list list{};
element e1{fcppt::make_ref(list), 5};
{
element const e2{fcppt::make_ref(list), 10};
// Prints 5, 10
for (element const &e : list)
{
std::cout << e.value() << ' ';
}
}
std::cout << '\n';
// Prints 5
for (element const &e : list)
{
std::cout << e.value() << ' ';
}

Classes

class  fcppt::intrusive::base< Type >
 The base class of an element. More...
 
class  fcppt::intrusive::iterator< Type >
 The iterator type of an intrusive list. More...
 
class  fcppt::intrusive::list< Type >
 An intrusive list. More...