blob: 68076785ac0beede20c1edb9ee7d44c3e7d58490 [file] [log] [blame]
Benny Prijono4766ffe2005-11-01 17:56:59 +00001/* $Id$
2 *
3 */
Benny Prijono0a749f12005-10-31 21:02:30 +00004#ifndef __PJPP_LIST_H__
5#define __PJPP_LIST_H__
6
7#include <pj/list.h>
8
9template <typename T>
10struct PJ_List_Node
11{
12 PJ_DECL_LIST_MEMBER(T)
13};
14
15
16template <class Node>
17class PJ_List
18{
19public:
20 PJ_List() { pj_list_init(&root_); if (0) compiletest(); }
21 ~PJ_List() {}
22
23 class const_iterator
24 {
25 public:
26 const_iterator() : node_(NULL) {}
27 const_iterator(const Node *nd) : node_((Node*)nd) {}
28 const Node * operator *() { return node_; }
29 const Node * operator -> () { return node_; }
30 const_iterator operator++() { return const_iterator(node_->next); }
31 bool operator==(const const_iterator &rhs) { return node_ == rhs.node_; }
32 bool operator!=(const const_iterator &rhs) { return node_ != rhs.node_; }
33
34 protected:
35 Node *node_;
36 };
37
38 class iterator : public const_iterator
39 {
40 public:
41 iterator() {}
42 iterator(Node *nd) : const_iterator(nd) {}
43 Node * operator *() { return node_; }
44 Node * operator -> () { return node_; }
45 iterator operator++() { return iterator(node_->next); }
46 bool operator==(const iterator &rhs) { return node_ == rhs.node_; }
47 bool operator!=(const iterator &rhs) { return node_ != rhs.node_; }
48 };
49
50 bool empty() const
51 {
52 return pj_list_empty(&root_);
53 }
54
55 iterator begin()
56 {
57 return iterator(root_.next);
58 }
59
60 const_iterator begin() const
61 {
62 return const_iterator(root_.next);
63 }
64
65 const_iterator end() const
66 {
67 return const_iterator((Node*)&root_);
68 }
69
70 iterator end()
71 {
72 return iterator((Node*)&root_);
73 }
74
75 void insert_before (iterator &pos, Node *node)
76 {
77 pj_list_insert_before( *pos, node );
78 }
79
80 void insert_after(iterator &pos, Node *node)
81 {
82 pj_list_insert_after(*pos, node);
83 }
84
85 void merge_first(Node *list2)
86 {
87 pj_list_merge_first(&root_, list2);
88 }
89
90 void merge_last(PJ_List *list)
91 {
92 pj_list_merge_last(&root_, &list->root_);
93 }
94
95 void insert_nodes_before(iterator &pos, PJ_List *list2)
96 {
97 pj_list_insert_nodes_before(*pos, &list2->root_);
98 }
99
100 void insert_nodes_after(iterator &pos, PJ_List *list2)
101 {
102 pj_list_insert_nodes_after(*pos, &list2->root_);
103 }
104
105 void erase(iterator &it)
106 {
107 pj_list_erase(*it);
108 }
109
110 Node *front()
111 {
112 return root_.next;
113 }
114
115 const Node *front() const
116 {
117 return root_.next;
118 }
119
120 void pop_front()
121 {
122 pj_list_erase(root_.next);
123 }
124
125 Node *back()
126 {
127 return root_.prev;
128 }
129
130 const Node *back() const
131 {
132 return root_.prev;
133 }
134
135 void pop_back()
136 {
137 pj_list_erase(root_.prev);
138 }
139
140 iterator find(Node *node)
141 {
142 Node *n = pj_list_find_node(&root_, node);
143 return n ? iterator(n) : end();
144 }
145
146 const_iterator find(Node *node) const
147 {
148 Node *n = pj_list_find_node(&root_, node);
149 return n ? const_iterator(n) : end();
150 }
151
152 void push_back(Node *node)
153 {
154 pj_list_insert_after(root_.prev, node);
155 }
156
157 void push_front(Node *node)
158 {
159 pj_list_insert_before(root_.next, node);
160 }
161
162 void clear()
163 {
164 root_.next = &root_;
165 root_.prev = &root_;
166 }
167
168private:
169 struct RootNode
170 {
171 PJ_DECL_LIST_MEMBER(Node)
172 } root_;
173
174 void compiletest()
175 {
176 // If you see error in this line,
177 // it's because Node is not derived from PJ_List_Node.
178 Node *n = (Node*)0;
179 n = n->next; n = n->prev;
180 }
181};
182
183
184#endif /* __PJPP_LIST_H__ */