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