| 1 | // compile with:
|
|---|
| 2 | // g++ -std=c++11 -Wall -Wextra -pedantic -Wno-ignored-qualifiers -g3 -O0 -I${BOOST_INCLUDE} unlink-header.cpp -o unlink-header
|
|---|
| 3 |
|
|---|
| 4 | #include <iostream>
|
|---|
| 5 | #include <cassert>
|
|---|
| 6 | #include <vector>
|
|---|
| 7 | #include <boost/intrusive/list.hpp>
|
|---|
| 8 | #include <boost/intrusive/trivial_value_traits.hpp>
|
|---|
| 9 |
|
|---|
| 10 | namespace bi = boost::intrusive;
|
|---|
| 11 |
|
|---|
| 12 | struct A
|
|---|
| 13 | {
|
|---|
| 14 | A(int val = 42)
|
|---|
| 15 | : val_(val), prev_(nullptr), next_(nullptr)
|
|---|
| 16 | {
|
|---|
| 17 | std::cout << "constructing element with val_=" << val << "\n";
|
|---|
| 18 | }
|
|---|
| 19 | ~A()
|
|---|
| 20 | {
|
|---|
| 21 | std::cout << "destroying element with val_=" << val_ << "\n";
|
|---|
| 22 | assert(is_unlinked());
|
|---|
| 23 | }
|
|---|
| 24 | bool is_unlinked() const { return not(prev_ or next_); }
|
|---|
| 25 | int val_;
|
|---|
| 26 | A* prev_;
|
|---|
| 27 | A* next_;
|
|---|
| 28 | };
|
|---|
| 29 |
|
|---|
| 30 | struct Node_Traits
|
|---|
| 31 | {
|
|---|
| 32 | typedef A node;
|
|---|
| 33 | typedef A* node_ptr;
|
|---|
| 34 | typedef const A* const_node_ptr;
|
|---|
| 35 | static node *get_next(const node *n) { return n->next_; }
|
|---|
| 36 | static void set_next(node *n, node *next) { n->next_ = next; }
|
|---|
| 37 | static node *get_previous(const node *n) { return n->prev_; }
|
|---|
| 38 | static void set_previous(node *n, node *prev) { n->prev_ = prev; }
|
|---|
| 39 | };
|
|---|
| 40 |
|
|---|
| 41 | typedef bi::trivial_value_traits< Node_Traits, bi::safe_link > Value_Traits;
|
|---|
| 42 | typedef bi::list< A, bi::value_traits< Value_Traits > > list_t;
|
|---|
| 43 |
|
|---|
| 44 | int main()
|
|---|
| 45 | {
|
|---|
| 46 | std::cout << "constructing vector\n";
|
|---|
| 47 | std::vector< A > a_cont;
|
|---|
| 48 | a_cont.reserve(3);
|
|---|
| 49 | a_cont.emplace_back(0);
|
|---|
| 50 | a_cont.emplace_back(1);
|
|---|
| 51 | a_cont.emplace_back(2);
|
|---|
| 52 | {
|
|---|
| 53 | std::cout << "constructing list\n";
|
|---|
| 54 | list_t l(a_cont.begin(), a_cont.end());
|
|---|
| 55 | std::cout << "clearing list\n";
|
|---|
| 56 | l.clear();
|
|---|
| 57 | std::cout << "clearing vector\n";
|
|---|
| 58 | a_cont.clear();
|
|---|
| 59 | std::cout << "destroying list\n";
|
|---|
| 60 | }
|
|---|
| 61 | std::cout << "done\n"; // never reached
|
|---|
| 62 | }
|
|---|