Debian Codegraph @

Framework/Pantomime/CWContainer.m

raw
1 /*
2 ** CWContainer.m
3 **
4 ** Copyright (c) 2002-2004 Ludovic Marcotte
5 **
6 ** Author: Ludovic Marcotte <[email protected]>
7 **
8 ** This library is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU Lesser General Public
10 ** License as published by the Free Software Foundation; either
11 ** version 2.1 of the License, or (at your option) any later version.
12 **
13 ** This library is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ** Lesser General Public License for more details.
17 **
18 ** You should have received a copy of the GNU General Public License
19 ** along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #import <Pantomime/CWContainer.h>
23
24 #import <Pantomime/CWConstants.h>
25 #import <Pantomime/CWInternetAddress.h>
26 #import <Pantomime/CWMessage.h>
27
28
29 //
30 //
31 //
32 @implementation CWContainer
33
34 - (id) init
35 {
36 self = [super init];
37 if (self)
38 {
39 message = nil;
40 parent = nil;
41 child = nil;
42 next = nil;
43 }
44 return self;
45 }
46
47
48 //
49 //
50 //
51 - (void) dealloc
52 {
53 TEST_RELEASE(parent);
54 TEST_RELEASE(child);
55 TEST_RELEASE(next);
56
57 [super dealloc];
58 }
59
60
61 //
62 // access / mutation methods
63 //
64 - (void) setParent: (CWContainer *) theParent
65 {
66 if (theParent && theParent != self)
67 {
68 ASSIGN(parent, theParent);
69 }
70 else
71 {
72 DESTROY(parent);
73 }
74 }
75
76
77 //
78 //
79 //
80 #warning Fix mem leaks
81 - (void) setChild: (CWContainer *) theChild
82 {
83 if (!theChild || theChild == self || theChild->next == self || theChild == child)
84 {
85 return;
86 }
87
88 if (theChild)
89 {
90 CWContainer *aChild;
91
92 // We search down in the children of theChild to be sure that
93 // self IS NOT reachable
94 // FIXME - we should use childrenEnumerator since we are NOT looping
95 // in all children with this code
96 aChild = theChild->child;
97
98 while (aChild)
99 {
100 if (aChild == self)
101 {
102 return;
103 }
104 aChild = aChild->next;
105 }
106
107
108 RETAIN(theChild);
109 //RELEASE(child);
110 //child = theChild;
111
112 // We finally add it!
113 if (!child)
114 {
115 child = theChild;
116 }
117 else
118 {
119 aChild = child;
120
121 // We go at the end of our list of children
122 //while ( aChild->next != nil && aChild->next != aChild )
123 while (aChild->next != nil)
124 {
125 if (aChild->next == aChild)
126 {
127 aChild->next = theChild;
128 return;
129 }
130
131 // We don't add the child if it's already there
132 if (aChild == theChild)
133 {
134 return;
135 }
136
137 aChild = aChild->next;
138 }
139
140 aChild->next = theChild;
141 }
142
143 }
144 else
145 {
146 DESTROY(child);
147 }
148 }
149
150
151 //
152 //
153 //
154 - (CWContainer *) childAtIndex: (unsigned int) theIndex
155 {
156 CWContainer *aChild;
157 unsigned int i;
158
159 aChild = child;
160
161 for (i = 0; i < theIndex && aChild; i++)
162 {
163 aChild = aChild->next;
164 }
165
166 return aChild;
167 }
168
169
170 //
171 //
172 //
173 - (unsigned int) count
174 {
175 if (child)
176 {
177 CWContainer *aChild;
178 unsigned int count;
179
180 aChild = child;
181 count = 0;
182
183 while (aChild)
184 {
185 //if ( aChild == self || aChild->next == aChild )
186 if (aChild == self)
187 {
188 count = 1;
189 break;
190 }
191
192 aChild = aChild->next;
193 count++;
194 }
195
196 return count;
197 }
198
199 return 0;
200 }
201
202
203 //
204 //
205 //
206 - (void) setNext: (CWContainer *) theNext
207 {
208 if (theNext)
209 {
210 ASSIGN(next, theNext);
211 }
212 else
213 {
214 DESTROY(next);
215 }
216 }
217
218
219 //
220 //
221 //
222 - (NSEnumerator *) childrenEnumerator
223 {
224 NSMutableArray *aMutableArray;
225 CWContainer *aContainer;
226
227 aMutableArray = [[NSMutableArray alloc] init];
228 AUTORELEASE(aMutableArray);
229
230 aContainer = child;
231
232 while (aContainer)
233 {
234 [aMutableArray addObject: aContainer];
235
236 // We add, recursively, all its children
237 [aMutableArray addObjectsFromArray: [[aContainer childrenEnumerator] allObjects]];
238
239 // We get our next container
240 aContainer = aContainer->next;
241 }
242
243 return [aMutableArray objectEnumerator];
244 }
245
246 @end