1 /////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga  2006-2013
4 //
5 // Distributed under the Boost Software License, Version 1.0.
6 //    (See accompanying file LICENSE_1_0.txt or copy at
7 //          http://www.boost.org/LICENSE_1_0.txt)
8 //
9 // See http://www.boost.org/libs/intrusive for documentation.
10 //
11 /////////////////////////////////////////////////////////////////////////////
12 //[doc_list_algorithms_code
13 #include <boost/intrusive/circular_list_algorithms.hpp>
14 #include <cassert>
15 
16 struct my_node
17 {
18    my_node *next_, *prev_;
19    //other members...
20 };
21 
22 //Define our own list_node_traits
23 struct my_list_node_traits
24 {
25    typedef my_node                                    node;
26    typedef my_node *                                  node_ptr;
27    typedef const my_node *                            const_node_ptr;
get_nextmy_list_node_traits28    static node_ptr get_next(const_node_ptr n)         {  return n->next_;  }
set_nextmy_list_node_traits29    static void set_next(node_ptr n, node_ptr next)    {  n->next_ = next;  }
get_previousmy_list_node_traits30    static node *get_previous(const_node_ptr n)        {  return n->prev_;  }
set_previousmy_list_node_traits31    static void set_previous(node_ptr n, node_ptr prev){  n->prev_ = prev;  }
32 };
33 
main()34 int main()
35 {
36    typedef boost::intrusive::circular_list_algorithms<my_list_node_traits> algo;
37    my_node one, two, three;
38 
39    //Create an empty doubly linked list container:
40    //"one" will be the first node of the container
41    algo::init_header(&one);
42    assert(algo::count(&one) == 1);
43 
44    //Now add a new node before "one"
45    algo::link_before(&one, &two);
46    assert(algo::count(&one) == 2);
47 
48    //Now add a new node after "two"
49    algo::link_after(&two, &three);
50    assert(algo::count(&one) == 3);
51 
52    //Now unlink the node after one
53    algo::unlink(&three);
54    assert(algo::count(&one) == 2);
55 
56    //Now unlink two
57    algo::unlink(&two);
58    assert(algo::count(&one) == 1);
59 
60    //Now unlink one
61    algo::unlink(&one);
62    assert(algo::count(&one) == 1);
63 
64    return 0;
65 }
66 //]
67