cfbf97bf52d6235ecdc3536595e8bf8566beb03f
[senf.git] / boost / intrusive / detail / list_node.hpp
1 /////////////////////////////////////////////////////////////////////////////\r
2 //\r
3 // (C) Copyright Olaf Krzikalla 2004-2006.\r
4 // (C) Copyright Ion GaztaƱaga  2006-2007\r
5 //\r
6 // Distributed under the Boost Software License, Version 1.0.\r
7 //    (See accompanying file LICENSE_1_0.txt or copy at\r
8 //          http://www.boost.org/LICENSE_1_0.txt)\r
9 //\r
10 // See http://www.boost.org/libs/intrusive for documentation.\r
11 //\r
12 /////////////////////////////////////////////////////////////////////////////\r
13 \r
14 #ifndef BOOST_INTRUSIVE_LIST_NODE_HPP\r
15 #define BOOST_INTRUSIVE_LIST_NODE_HPP\r
16 \r
17 #include <boost/intrusive/detail/config_begin.hpp>\r
18 #include <iterator>\r
19 #include <boost/assert.hpp>\r
20 #include <boost/intrusive/detail/pointer_type.hpp>\r
21 #include <boost/intrusive/detail/pointer_to_other.hpp>\r
22 #include <boost/intrusive/list_algorithms.hpp>\r
23 #include <boost/get_pointer.hpp>\r
24 #include <cstddef>\r
25 \r
26 namespace boost {\r
27 namespace intrusive {\r
28 namespace detail {\r
29 \r
30 // list_node_traits can be used with list_algorithms and supplies\r
31 // a list_node holding the pointers needed for a double-linked list\r
32 // it is used by ilist_derived_node and ilist_member_node\r
33 template<class VoidPointer>\r
34 struct list_node_traits\r
35 {\r
36    struct node;\r
37    typedef typename boost::pointer_to_other\r
38       <VoidPointer, node>::type          node_ptr;\r
39    typedef typename boost::pointer_to_other\r
40       <VoidPointer, const node>::type    const_node_ptr;\r
41 \r
42    struct node\r
43    {\r
44       node_ptr prev_, next_;\r
45    };\r
46 \r
47    static node_ptr get_previous(const_node_ptr n)\r
48    {  return n->prev_;  }\r
49 \r
50    static void set_previous(node_ptr n, node_ptr prev)\r
51    {  n->prev_ = prev;  }\r
52 \r
53    static node_ptr get_next(const_node_ptr n)\r
54    {  return n->next_;  }\r
55 \r
56    static void set_next(node_ptr n, node_ptr next)\r
57    {  n->next_ = next;  }\r
58 };\r
59 \r
60 \r
61 // list_iterator provides some basic functions for a \r
62 // node oriented bidirectional iterator:\r
63 template<class T, class Self, class NodeTraits>\r
64 class list_iterator\r
65    :  public std::iterator<std::bidirectional_iterator_tag, T>\r
66 {\r
67    protected:\r
68    typedef typename NodeTraits::node            node;\r
69    typedef typename NodeTraits::node_ptr        node_ptr;\r
70  \r
71    list_iterator()\r
72       : node_ (0)\r
73    {}\r
74 \r
75    explicit list_iterator(node_ptr node)\r
76       : node_ (node)\r
77    {}\r
78 \r
79    node_ptr list_node() const\r
80    { return node_; }\r
81 \r
82    Self &operator=(const node_ptr &node)\r
83    {  node_ = node;  return static_cast<Self&>(*this);  }\r
84 \r
85    public:\r
86    Self& operator++() \r
87    { \r
88       node_ = NodeTraits::get_next(node_); \r
89       return static_cast<Self&> (*this); \r
90    }\r
91    \r
92    Self operator++(int)\r
93    {\r
94       Self result (node_);\r
95       node_ = NodeTraits::get_next(node_);\r
96       return result;\r
97    }\r
98 \r
99    Self& operator--() \r
100    { \r
101       node_ = NodeTraits::get_previous(node_); \r
102       return static_cast<Self&> (*this); \r
103    }\r
104    \r
105    Self operator--(int)\r
106    {\r
107       Self result (node_);\r
108       node_ = NodeTraits::get_previous(node_);\r
109       return result;\r
110    }\r
111 \r
112    bool operator== (const Self& i) const\r
113    { return node_ == i.list_node(); }\r
114 \r
115    bool operator!= (const Self& i) const\r
116    { return !operator== (i); }\r
117 \r
118    private:\r
119    node_ptr node_;\r
120 };\r
121 \r
122 } //namespace detail \r
123 } //namespace intrusive \r
124 } //namespace boost \r
125 \r
126 #include <boost/intrusive/detail/config_end.hpp>\r
127 \r
128 #endif //BOOST_INTRUSIVE_LIST_NODE_HPP\r