Here is an example of how lists are used.
struct Animal {
const char* species_;
list_links link_;
Animal(const char* species)
: species_(species) {
}
};
list<Animal, &Animal::link_> animal_list;
// that is, a “list of Animals, linked by the Animal::link_ member”
// list is initially empty
assert(animal_list.empty());
// add some Animals to it. Note that the `link_` members must be cleared
// before we add them; that is done automatically by the constructor, but
// in your buddy allocator you may need to call `...link_.reset()` explicitly
Animal dog("dog"), cat("cat"), capybara("capybara");
animal_list.push_back(&dog);
animal_list.push_front(&cat);
animal_list.push_back(&capybara);
for (Animal* a = animal_list.front(); a; a = animal_list.next(a)) {
log_printf("%s\n", a->species_);
}
// prints `cat`, `dog`, `capybara`
// remove an Animal from the list
animal_list.erase(&dog);
for (Animal* a = animal_list.front(); a; a = animal_list.next(a)) {
log_printf("%s\n", a->species_);
}
// prints `cat`, `capybara`