]> err.no Git - sope/blob - sope-core/NGExtensions/FdExt.subproj/NSEnumerator+misc.m
fixed bugs in logger, improved performance
[sope] / sope-core / NGExtensions / FdExt.subproj / NSEnumerator+misc.m
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
5
6   OGo is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with OGo; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21 // $Id$
22
23 #import "NSEnumerator+misc.h"
24 #import <Foundation/Foundation.h>
25 #import <EOControl/EOQualifier.h>
26 #include "common.h"
27
28 @interface _NGFilterEnumerator : NSEnumerator
29 {
30   NSEnumerator *source;
31 }
32
33 + (NSEnumerator *)filterEnumeratorWithSource:(NSEnumerator *)_e;
34
35 - (NSEnumerator *)source;
36
37 @end
38
39 @interface _NGQualifierFilterEnumerator : _NGFilterEnumerator
40 {
41   EOQualifier *q;
42 }
43 - (void)setQualifier:(EOQualifier *)_q;
44 - (EOQualifier *)qualifier;
45 @end
46
47 @interface _NGSelFilterEnumerator : _NGFilterEnumerator
48 {
49   SEL  sel;
50   id   arg;
51 }
52 - (void)setSelector:(SEL)_s;
53 - (SEL)selector;
54 - (void)setArgument:(id)_arg;
55 - (id)argument;
56 @end
57
58 @implementation NSEnumerator(misc)
59
60 - (NSEnumerator *)filterWithQualifier:(EOQualifier *)_qualifier {
61   id e;
62   
63   e = [_NGQualifierFilterEnumerator filterEnumeratorWithSource:self];
64   [e setQualifier:_qualifier];
65   return e;
66 }
67 - (NSEnumerator *)filterWithQualifierString:(NSString *)_s {
68   EOQualifier *q;
69   
70   q = [EOQualifier qualifierWithQualifierFormat:_s];
71   return [self filterWithQualifier:q];
72 }
73
74 - (NSEnumerator *)filterWithSelector:(SEL)_selector withObject:(id)_argument {
75   id e;
76   
77   e = [_NGSelFilterEnumerator filterEnumeratorWithSource:self];
78   [e setSelector:_selector];
79   [e setArgument:_argument];
80   return e;
81 }
82
83 @end /* NSEnumerator(misc) */
84
85 @implementation _NGFilterEnumerator
86
87 - (id)initWithSource:(NSEnumerator *)_e {
88   self->source = [_e retain];
89   return self;
90 }
91 - (void)dealloc {
92   [self->source release];
93   [super dealloc];
94 }
95
96 + (NSEnumerator *)filterEnumeratorWithSource:(NSEnumerator *)_e {
97   return [[(_NGFilterEnumerator *)[self alloc] initWithSource:_e] autorelease];
98 }
99
100 - (NSEnumerator *)source {
101   return self->source;
102 }
103
104 - (id)nextObject {
105   return [self->source nextObject];
106 }
107
108 @end /* _NGFilterEnumerator */
109
110 @implementation _NGQualifierFilterEnumerator
111
112 - (void)dealloc {
113   [self->q release];
114   [super dealloc];
115 }
116
117 - (void)setQualifier:(EOQualifier *)_q {
118   ASSIGN(self->q, _q);
119 }
120 - (EOQualifier *)qualifier {
121   return self->q;
122 }
123
124 - (id)nextObject {
125   while (YES) {
126     id obj;
127     
128     if ((obj = [self->source nextObject]) == nil)
129       return nil;
130     if (self->q == nil)
131       return obj;
132
133     if ([(id<EOQualifierEvaluation>)self->q evaluateWithObject:obj])
134       return obj;
135   }
136 }
137
138 @end /* _NGQualifierFilterEnumerator */
139
140 @implementation _NGSelFilterEnumerator
141
142 - (void)dealloc {
143   [self->arg release];
144   [super dealloc];
145 }
146
147 - (void)setSelector:(SEL)_s {
148   self->sel = _s;
149 }
150 - (SEL)selector {
151   return self->sel;
152 }
153
154 - (void)setArgument:(id)_arg {
155   ASSIGN(self->arg, _arg);
156 }
157 - (id)argument {
158   return self->arg;
159 }
160
161 - (id)nextObject {
162   while (YES) {
163     id obj;
164     BOOL (*m)(id,SEL,id);
165     
166     if ((obj = [self->source nextObject]) == nil)
167       return nil;
168     
169     if ((m = (void *)[obj methodForSelector:self->sel]) == NULL)
170       continue;
171
172     if (m(obj, self->sel, self->arg))
173       return obj;
174   }
175 }
176
177 @end /* _NGSelFilterEnumerator */