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_slist_algorithms_code
13 #include <boost/intrusive/circular_slist_algorithms.hpp>
14 #include <cassert>
15 
16 struct my_node
17 {
18    my_node *next_;
19    //other members...
20 };
21 
22 //Define our own slist_node_traits
23 struct my_slist_node_traits
24 {
25    typedef my_node                                 node;
26    typedef my_node *                               node_ptr;
27    typedef const my_node *                         const_node_ptr;
get_nextmy_slist_node_traits28    static node_ptr get_next(const_node_ptr n)      {  return n->next_;  }
set_nextmy_slist_node_traits29    static void set_next(node_ptr n, node_ptr next) {  n->next_ = next;  }
30 };
31 
main()32 int main()
33 {
34    typedef boost::intrusive::circular_slist_algorithms<my_slist_node_traits> algo;
35    my_node one, two, three;
36 
37    //Create an empty singly linked list container:
38    //"one" will be the first node of the container
39    algo::init_header(&one);
40    assert(algo::count(&one) == 1);
41 
42    //Now add a new node
43    algo::link_after(&one, &two);
44    assert(algo::count(&one) == 2);
45 
46    //Now add a new node after "one"
47    algo::link_after(&one, &three);
48    assert(algo::count(&one) == 3);
49 
50    //Now unlink the node after one
51    algo::unlink_after(&one);
52    assert(algo::count(&one) == 2);
53 
54    //Now unlink two
55    algo::unlink(&two);
56    assert(algo::count(&one) == 1);
57 
58    return 0;
59 }
60 //]
61