summaryrefslogtreecommitdiff
path: root/pjlib/src/pj++/list.hpp
blob: 76452917589177477f5c059a5efaa72d6cbeab67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* $Header: /pjproject/pjlib/src/pj++/list.hpp 2     2/24/05 11:23a Bennylp $ */
#ifndef __PJPP_LIST_H__
#define __PJPP_LIST_H__

#include <pj/list.h>

template <typename T>
struct PJ_List_Node
{
    PJ_DECL_LIST_MEMBER(T)
};


template <class Node>
class PJ_List
{
public:
    PJ_List() { pj_list_init(&root_); if (0) compiletest(); }
    ~PJ_List() {}

    class const_iterator
    {
    public:
	const_iterator() : node_(NULL) {}
	const_iterator(const Node *nd) : node_((Node*)nd) {}
	const Node * operator *() { return node_; }
	const Node * operator -> () { return node_; }
	const_iterator operator++() { return const_iterator(node_->next); }
	bool operator==(const const_iterator &rhs) { return node_ == rhs.node_; }
	bool operator!=(const const_iterator &rhs) { return node_ != rhs.node_; }

    protected:
	Node *node_;
    };

    class iterator : public const_iterator
    {
    public:
	iterator() {}
	iterator(Node *nd) : const_iterator(nd) {}
	Node * operator *() { return node_; }
	Node * operator -> () { return node_; }
	iterator operator++() { return iterator(node_->next); }
	bool operator==(const iterator &rhs) { return node_ == rhs.node_; }
	bool operator!=(const iterator &rhs) { return node_ != rhs.node_; }
    };

    bool empty() const
    {
	return pj_list_empty(&root_);
    }

    iterator begin()
    {
	return iterator(root_.next);
    }

    const_iterator begin() const
    {
	return const_iterator(root_.next);
    }

    const_iterator end() const
    {
	return const_iterator((Node*)&root_);
    }

    iterator end()
    {
	return iterator((Node*)&root_);
    }

    void insert_before (iterator &pos, Node *node)
    {
	pj_list_insert_before( *pos, node );
    }

    void insert_after(iterator &pos, Node *node)
    {
	pj_list_insert_after(*pos, node);
    }

    void merge_first(Node *list2)
    {
	pj_list_merge_first(&root_, list2);
    }

    void merge_last(PJ_List *list)
    {
	pj_list_merge_last(&root_, &list->root_);
    }

    void insert_nodes_before(iterator &pos, PJ_List *list2)
    {
	pj_list_insert_nodes_before(*pos, &list2->root_);
    }

    void insert_nodes_after(iterator &pos, PJ_List *list2)
    {
	pj_list_insert_nodes_after(*pos, &list2->root_);
    }

    void erase(iterator &it)
    {
	pj_list_erase(*it);
    }

    Node *front()
    {
	return root_.next;
    }

    const Node *front() const
    {
	return root_.next;
    }

    void pop_front()
    {
	pj_list_erase(root_.next);
    }

    Node *back()
    {
	return root_.prev;
    }

    const Node *back() const
    {
	return root_.prev;
    }

    void pop_back()
    {
	pj_list_erase(root_.prev);
    }

    iterator find(Node *node)
    {
	Node *n = pj_list_find_node(&root_, node);
	return n ? iterator(n) : end();
    }

    const_iterator find(Node *node) const
    {
	Node *n = pj_list_find_node(&root_, node);
	return n ? const_iterator(n) : end();
    }

    void push_back(Node *node)
    {
	pj_list_insert_after(root_.prev, node);
    }

    void push_front(Node *node)
    {
	pj_list_insert_before(root_.next, node);
    }

    void clear()
    {
	root_.next = &root_;
	root_.prev = &root_;
    }

private:
    struct RootNode
    {
	PJ_DECL_LIST_MEMBER(Node)
    } root_;

    void compiletest()
    {
	// If you see error in this line, 
	// it's because Node is not derived from PJ_List_Node.
	Node *n = (Node*)0;
	n = n->next; n = n->prev;
    }
};


#endif	/* __PJPP_LIST_H__ */