1 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
2 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
3 | *
|
---|
4 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
5 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
6 | * the License. You may obtain a copy of the License at
|
---|
7 | * http://www.mozilla.org/MPL/
|
---|
8 | *
|
---|
9 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
10 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
11 | * for the specific language governing rights and limitations under the
|
---|
12 | * License.
|
---|
13 | *
|
---|
14 | * The Original Code is Mozilla IPC.
|
---|
15 | *
|
---|
16 | * The Initial Developer of the Original Code is
|
---|
17 | * Netscape Communications Corporation.
|
---|
18 | * Portions created by the Initial Developer are Copyright (C) 2002
|
---|
19 | * the Initial Developer. All Rights Reserved.
|
---|
20 | *
|
---|
21 | * Contributor(s):
|
---|
22 | * Darin Fisher <darin@netscape.com>
|
---|
23 | *
|
---|
24 | * Alternatively, the contents of this file may be used under the terms of
|
---|
25 | * either the GNU General Public License Version 2 or later (the "GPL"), or
|
---|
26 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
27 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
28 | * of those above. If you wish to allow use of your version of this file only
|
---|
29 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
30 | * use your version of this file under the terms of the MPL, indicate your
|
---|
31 | * decision by deleting the provisions above and replace them with the notice
|
---|
32 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
33 | * the provisions above, a recipient may use your version of this file under
|
---|
34 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | #ifndef ipcList_h__
|
---|
39 | #define ipcList_h__
|
---|
40 |
|
---|
41 | #include "prtypes.h"
|
---|
42 |
|
---|
43 | //-----------------------------------------------------------------------------
|
---|
44 | // simple list of singly-linked objects. class T must have the following
|
---|
45 | // structure:
|
---|
46 | //
|
---|
47 | // class T {
|
---|
48 | // ...
|
---|
49 | // public:
|
---|
50 | // T *mNext;
|
---|
51 | // };
|
---|
52 | //
|
---|
53 | // objects added to the list must be allocated with operator new. class T may
|
---|
54 | // optionally inherit from ipcListNode<T> if it doesn't wish to define mNext
|
---|
55 | // explicitly.
|
---|
56 | //-----------------------------------------------------------------------------
|
---|
57 |
|
---|
58 | template<class T>
|
---|
59 | class ipcList
|
---|
60 | {
|
---|
61 | public:
|
---|
62 | ipcList()
|
---|
63 | : mHead(NULL)
|
---|
64 | , mTail(NULL)
|
---|
65 | { }
|
---|
66 | ~ipcList() { DeleteAll(); }
|
---|
67 |
|
---|
68 | //
|
---|
69 | // prepends obj at the beginning of the list.
|
---|
70 | //
|
---|
71 | void Prepend(T *obj)
|
---|
72 | {
|
---|
73 | obj->mNext = mHead;
|
---|
74 | mHead = obj;
|
---|
75 | if (!mTail)
|
---|
76 | mTail = mHead;
|
---|
77 | }
|
---|
78 |
|
---|
79 | //
|
---|
80 | // appends obj to the end of the list.
|
---|
81 | //
|
---|
82 | void Append(T *obj)
|
---|
83 | {
|
---|
84 | obj->mNext = NULL;
|
---|
85 | if (mTail) {
|
---|
86 | mTail->mNext = obj;
|
---|
87 | mTail = obj;
|
---|
88 | }
|
---|
89 | else
|
---|
90 | mTail = mHead = obj;
|
---|
91 | }
|
---|
92 |
|
---|
93 | //
|
---|
94 | // inserts b into the list after a.
|
---|
95 | //
|
---|
96 | void InsertAfter(T *a, T *b)
|
---|
97 | {
|
---|
98 | b->mNext = a->mNext;
|
---|
99 | a->mNext = b;
|
---|
100 | if (mTail == a)
|
---|
101 | mTail = b;
|
---|
102 | }
|
---|
103 |
|
---|
104 | //
|
---|
105 | // removes first element w/o deleting it
|
---|
106 | //
|
---|
107 | void RemoveFirst()
|
---|
108 | {
|
---|
109 | if (mHead)
|
---|
110 | AdvanceHead();
|
---|
111 | }
|
---|
112 |
|
---|
113 | //
|
---|
114 | // removes element after the given element w/o deleting it
|
---|
115 | //
|
---|
116 | void RemoveAfter(T *obj)
|
---|
117 | {
|
---|
118 | T *rej = obj->mNext;
|
---|
119 | if (rej) {
|
---|
120 | obj->mNext = rej->mNext;
|
---|
121 | if (rej == mTail)
|
---|
122 | mTail = obj;
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | //
|
---|
127 | // deletes first element
|
---|
128 | //
|
---|
129 | void DeleteFirst()
|
---|
130 | {
|
---|
131 | T *first = mHead;
|
---|
132 | if (first) {
|
---|
133 | AdvanceHead();
|
---|
134 | delete first;
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | //
|
---|
139 | // deletes element after the given element
|
---|
140 | //
|
---|
141 | void DeleteAfter(T *obj)
|
---|
142 | {
|
---|
143 | T *rej = obj->mNext;
|
---|
144 | if (rej) {
|
---|
145 | RemoveAfter(obj);
|
---|
146 | delete rej;
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | //
|
---|
151 | // deletes all elements
|
---|
152 | //
|
---|
153 | void DeleteAll()
|
---|
154 | {
|
---|
155 | while (mHead)
|
---|
156 | DeleteFirst();
|
---|
157 | }
|
---|
158 |
|
---|
159 | const T *First() const { return mHead; }
|
---|
160 | T *First() { return mHead; }
|
---|
161 | const T *Last() const { return mTail; }
|
---|
162 | T *Last() { return mTail; }
|
---|
163 |
|
---|
164 | PRBool IsEmpty() const { return mHead == NULL; }
|
---|
165 |
|
---|
166 | //
|
---|
167 | // moves contents of list to another list
|
---|
168 | //
|
---|
169 | void MoveTo(ipcList<T> &other)
|
---|
170 | {
|
---|
171 | other.mHead = mHead;
|
---|
172 | other.mTail = mTail;
|
---|
173 | mHead = NULL;
|
---|
174 | mTail = NULL;
|
---|
175 | }
|
---|
176 |
|
---|
177 | // gets count of list elements
|
---|
178 | PRUint32 Count()
|
---|
179 | {
|
---|
180 | T *obj = mHead;
|
---|
181 | PRUint32 count = 0;
|
---|
182 | while (obj) {
|
---|
183 | count++;
|
---|
184 | obj = obj->mNext;
|
---|
185 | }
|
---|
186 |
|
---|
187 | return count;
|
---|
188 | }
|
---|
189 |
|
---|
190 | protected:
|
---|
191 | void AdvanceHead()
|
---|
192 | {
|
---|
193 | mHead = mHead->mNext;
|
---|
194 | if (!mHead)
|
---|
195 | mTail = NULL;
|
---|
196 | }
|
---|
197 |
|
---|
198 | T *mHead;
|
---|
199 | T *mTail;
|
---|
200 | };
|
---|
201 |
|
---|
202 | template<class T>
|
---|
203 | class ipcListNode
|
---|
204 | {
|
---|
205 | public:
|
---|
206 | ipcListNode() : mNext(nsnull) {}
|
---|
207 |
|
---|
208 | T *mNext;
|
---|
209 | };
|
---|
210 |
|
---|
211 | #endif // !ipcList_h__
|
---|