]> git.saurik.com Git - apt-legacy.git/blob - apt-pkg/cacheiterators.h
I feel... vindicated?
[apt-legacy.git] / apt-pkg / cacheiterators.h
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: cacheiterators.h,v 1.18.2.1 2004/05/08 22:44:27 mdz Exp $
4 /* ######################################################################
5
6 Cache Iterators - Iterators for navigating the cache structure
7
8 The iterators all provides ++,==,!=,->,* and end for their type.
9 The end function can be used to tell if the list has been fully
10 traversed.
11
12 Unlike STL iterators these contain helper functions to access the data
13 that is being iterated over. This is because the data structures can't
14 be formed in a manner that is intuitive to use and also mmapable.
15
16 For each variable in the target structure that would need a translation
17 to be accessed correctly a translating function of the same name is
18 present in the iterator. If applicable the translating function will
19 return an iterator.
20
21 The DepIterator can iterate over two lists, a list of 'version depends'
22 or a list of 'package reverse depends'. The type is determined by the
23 structure passed to the constructor, which should be the structure
24 that has the depends pointer as a member. The provide iterator has the
25 same system.
26
27 This header is not user includable, please use apt-pkg/pkgcache.h
28
29 ##################################################################### */
30 /*}}}*/
31 #ifndef PKGLIB_CACHEITERATORS_H
32 #define PKGLIB_CACHEITERATORS_H
33
34
35 // Package Iterator
36 class pkgCache::PkgIterator
37 {
38 friend class pkgCache;
39 Package *Pkg;
40 pkgCache *Owner;
41 long HashIndex;
42
43 protected:
44
45 // This constructor is the 'begin' constructor, never use it.
46 inline PkgIterator(pkgCache &Owner) : Owner(&Owner), HashIndex(-1)
47 {
48 Pkg = Owner.PkgP;
49 operator ++(0);
50 };
51
52 public:
53
54 enum OkState {NeedsNothing,NeedsUnpack,NeedsConfigure};
55
56 // Iteration
57 void operator ++(int);
58 inline void operator ++() {operator ++(0);};
59 inline bool end() const {return Owner == 0 || Pkg == Owner->PkgP?true:false;};
60
61 // Comparison
62 inline bool operator ==(const PkgIterator &B) const {return Pkg == B.Pkg;};
63 inline bool operator !=(const PkgIterator &B) const {return Pkg != B.Pkg;};
64
65 // Accessors
66 inline Package *operator ->() {return Pkg;};
67 inline Package const *operator ->() const {return Pkg;};
68 inline Package const &operator *() const {return *Pkg;};
69 inline operator Package *() {return Pkg == Owner->PkgP?0:Pkg;};
70 inline operator Package const *() const {return Pkg == Owner->PkgP?0:Pkg;};
71 inline pkgCache *Cache() {return Owner;};
72
73 inline const char *Name() const {return Pkg->Name == 0?0:Owner->StrP + Pkg->Name;};
74 inline const char *Display() const {return Pkg->Display == 0?0:Owner->StrP + Pkg->Display;};
75 inline const char *Section() const {return Pkg->Section == 0?0:Owner->StrP + Pkg->Section;};
76 inline bool Purge() const {return Pkg->CurrentState == pkgCache::State::Purge ||
77 (Pkg->CurrentVer == 0 && Pkg->CurrentState == pkgCache::State::NotInstalled);};
78 inline VerIterator VersionList() const;
79 inline VerIterator CurrentVer() const;
80 inline DepIterator RevDependsList() const;
81 inline PrvIterator ProvidesList() const;
82 inline TagIterator TagList() const;
83 inline unsigned long Index() const {return Pkg - Owner->PkgP;};
84 OkState State() const;
85
86 // Constructors
87 inline PkgIterator(pkgCache &Owner,Package *Trg) : Pkg(Trg), Owner(&Owner),
88 HashIndex(0)
89 {
90 if (Pkg == 0)
91 Pkg = Owner.PkgP;
92 };
93 inline PkgIterator() : Pkg(0), Owner(0), HashIndex(0) {};
94 };
95
96 // Version Iterator
97 class pkgCache::VerIterator
98 {
99 Version *Ver;
100 pkgCache *Owner;
101
102 void _dummy();
103
104 public:
105
106 // Iteration
107 void operator ++(int) {if (Ver != Owner->VerP) Ver = Owner->VerP + Ver->NextVer;};
108 inline void operator ++() {operator ++(0);};
109 inline bool end() const {return Owner == NULL || (Ver == Owner->VerP?true:false);};
110 inline void operator =(const VerIterator &B) {Ver = B.Ver; Owner = B.Owner;};
111
112 // Comparison
113 inline bool operator ==(const VerIterator &B) const {return Ver == B.Ver;};
114 inline bool operator !=(const VerIterator &B) const {return Ver != B.Ver;};
115 int CompareVer(const VerIterator &B) const;
116
117 // Accessors
118 inline Version *operator ->() {return Ver;};
119 inline Version const *operator ->() const {return Ver;};
120 inline Version &operator *() {return *Ver;};
121 inline Version const &operator *() const {return *Ver;};
122 inline operator Version *() {return Ver == Owner->VerP?0:Ver;};
123 inline operator Version const *() const {return Ver == Owner->VerP?0:Ver;};
124 inline pkgCache *Cache() {return Owner;};
125
126 inline const char *VerStr() const {return Ver->VerStr == 0?0:Owner->StrP + Ver->VerStr;};
127 inline const char *Display() const {return Ver->Display == 0?0:Owner->StrP + Ver->Display;};
128 inline const char *Section() const {return Ver->Section == 0?0:Owner->StrP + Ver->Section;};
129 inline const char *Arch() const {return Ver->Arch == 0?0:Owner->StrP + Ver->Arch;};
130 inline PkgIterator ParentPkg() const {return PkgIterator(*Owner,Owner->PkgP + Ver->ParentPkg);};
131 inline DescIterator DescriptionList() const;
132 DescIterator TranslatedDescription() const;
133 inline DepIterator DependsList() const;
134 inline PrvIterator ProvidesList() const;
135 inline VerFileIterator FileList() const;
136 inline unsigned long Index() const {return Ver - Owner->VerP;};
137 bool Downloadable() const;
138 inline const char *PriorityType() {return Owner->Priority(Ver->Priority);};
139 string RelStr();
140
141 bool Automatic() const;
142 VerFileIterator NewestFile() const;
143
144 inline VerIterator() : Ver(0), Owner(0) {};
145 inline VerIterator(pkgCache &Owner,Version *Trg = 0) : Ver(Trg),
146 Owner(&Owner)
147 {
148 if (Ver == 0)
149 Ver = Owner.VerP;
150 };
151 };
152
153 // Tag Iterator
154 class pkgCache::TagIterator
155 {
156 Tag *Tg;
157 pkgCache *Owner;
158
159 void _dummy();
160
161 public:
162
163 // Iteration
164 void operator ++(int) {if (Tg != Owner->TagP) Tg = Owner->TagP + Tg->NextTag;};
165 inline void operator ++() {operator ++(0);};
166 inline bool end() const {return Tg == Owner->TagP?true:false;};
167 inline void operator =(const TagIterator &B) {Tg = B.Tg; Owner = B.Owner;};
168
169 // Comparison
170 inline bool operator ==(const TagIterator &B) const {return Tg == B.Tg;};
171 inline bool operator !=(const TagIterator &B) const {return Tg != B.Tg;};
172 int CompareTag(const TagIterator &B) const;
173
174 // Accessors
175 inline Tag *operator ->() {return Tg;};
176 inline Tag const *operator ->() const {return Tg;};
177 inline Tag &operator *() {return *Tg;};
178 inline Tag const &operator *() const {return *Tg;};
179 inline operator Tag *() {return Tg == Owner->TagP?0:Tg;};
180 inline operator Tag const *() const {return Tg == Owner->TagP?0:Tg;};
181 inline pkgCache *Cache() {return Owner;};
182
183 inline const char *Name() const {return Owner->StrP + Tg->Name;};
184 inline unsigned long Index() const {return Tg - Owner->TagP;};
185
186 inline TagIterator() : Tg(0), Owner(0) {};
187 inline TagIterator(pkgCache &Owner,Tag *Trg = 0) : Tg(Trg),
188 Owner(&Owner)
189 {
190 if (Tg == 0)
191 Tg = Owner.TagP;
192 };
193 };
194
195 // Description Iterator
196 class pkgCache::DescIterator
197 {
198 Description *Desc;
199 pkgCache *Owner;
200
201 void _dummy();
202
203 public:
204
205 // Iteration
206 void operator ++(int) {if (Desc != Owner->DescP) Desc = Owner->DescP + Desc->NextDesc;};
207 inline void operator ++() {operator ++(0);};
208 inline bool end() const {return Desc == Owner->DescP?true:false;};
209 inline void operator =(const DescIterator &B) {Desc = B.Desc; Owner = B.Owner;};
210
211 // Comparison
212 inline bool operator ==(const DescIterator &B) const {return Desc == B.Desc;};
213 inline bool operator !=(const DescIterator &B) const {return Desc != B.Desc;};
214 int CompareDesc(const DescIterator &B) const;
215
216 // Accessors
217 inline Description *operator ->() {return Desc;};
218 inline Description const *operator ->() const {return Desc;};
219 inline Description &operator *() {return *Desc;};
220 inline Description const &operator *() const {return *Desc;};
221 inline operator Description *() {return Desc == Owner->DescP?0:Desc;};
222 inline operator Description const *() const {return Desc == Owner->DescP?0:Desc;};
223 inline pkgCache *Cache() {return Owner;};
224
225 inline const char *LanguageCode() const {return Owner->StrP + Desc->language_code;};
226 inline const char *md5() const {return Owner->StrP + Desc->md5sum;};
227 inline DescFileIterator FileList() const;
228 inline unsigned long Index() const {return Desc - Owner->DescP;};
229
230 inline DescIterator() : Desc(0), Owner(0) {};
231 inline DescIterator(pkgCache &Owner,Description *Trg = 0) : Desc(Trg),
232 Owner(&Owner)
233 {
234 if (Desc == 0)
235 Desc = Owner.DescP;
236 };
237 };
238
239 // Dependency iterator
240 class pkgCache::DepIterator
241 {
242 Dependency *Dep;
243 enum {DepVer, DepRev} Type;
244 pkgCache *Owner;
245
246 void _dummy();
247
248 public:
249
250 // Iteration
251 void operator ++(int) {if (Dep != Owner->DepP) Dep = Owner->DepP +
252 (Type == DepVer?Dep->NextDepends:Dep->NextRevDepends);};
253 inline void operator ++() {operator ++(0);};
254 inline bool end() const {return Owner == 0 || Dep == Owner->DepP?true:false;};
255
256 // Comparison
257 inline bool operator ==(const DepIterator &B) const {return Dep == B.Dep;};
258 inline bool operator !=(const DepIterator &B) const {return Dep != B.Dep;};
259
260 // Accessors
261 inline Dependency *operator ->() {return Dep;};
262 inline Dependency const *operator ->() const {return Dep;};
263 inline Dependency &operator *() {return *Dep;};
264 inline Dependency const &operator *() const {return *Dep;};
265 inline operator Dependency *() {return Dep == Owner->DepP?0:Dep;};
266 inline operator Dependency const *() const {return Dep == Owner->DepP?0:Dep;};
267 inline pkgCache *Cache() {return Owner;};
268
269 inline const char *TargetVer() const {return Dep->Version == 0?0:Owner->StrP + Dep->Version;};
270 inline PkgIterator TargetPkg() {return PkgIterator(*Owner,Owner->PkgP + Dep->Package);};
271 inline PkgIterator SmartTargetPkg() {PkgIterator R(*Owner,0);SmartTargetPkg(R);return R;};
272 inline VerIterator ParentVer() {return VerIterator(*Owner,Owner->VerP + Dep->ParentVer);};
273 inline PkgIterator ParentPkg() {return PkgIterator(*Owner,Owner->PkgP + Owner->VerP[Dep->ParentVer].ParentPkg);};
274 inline bool Reverse() {return Type == DepRev;};
275 inline unsigned long Index() const {return Dep - Owner->DepP;};
276 bool IsCritical();
277 void GlobOr(DepIterator &Start,DepIterator &End);
278 Version **AllTargets();
279 bool SmartTargetPkg(PkgIterator &Result);
280 inline const char *CompType() {return Owner->CompType(Dep->CompareOp);};
281 inline const char *DepType() {return Owner->DepType(Dep->Type);};
282
283 inline DepIterator(pkgCache &Owner,Dependency *Trg,Version * = 0) :
284 Dep(Trg), Type(DepVer), Owner(&Owner)
285 {
286 if (Dep == 0)
287 Dep = Owner.DepP;
288 };
289 inline DepIterator(pkgCache &Owner,Dependency *Trg,Package *) :
290 Dep(Trg), Type(DepRev), Owner(&Owner)
291 {
292 if (Dep == 0)
293 Dep = Owner.DepP;
294 };
295 inline DepIterator() : Dep(0), Type(DepVer), Owner(0) {};
296 };
297
298 // Provides iterator
299 class pkgCache::PrvIterator
300 {
301 Provides *Prv;
302 enum {PrvVer, PrvPkg} Type;
303 pkgCache *Owner;
304
305 void _dummy();
306
307 public:
308
309 // Iteration
310 void operator ++(int) {if (Prv != Owner->ProvideP) Prv = Owner->ProvideP +
311 (Type == PrvVer?Prv->NextPkgProv:Prv->NextProvides);};
312 inline void operator ++() {operator ++(0);};
313 inline bool end() const {return Owner == 0 || Prv == Owner->ProvideP?true:false;};
314
315 // Comparison
316 inline bool operator ==(const PrvIterator &B) const {return Prv == B.Prv;};
317 inline bool operator !=(const PrvIterator &B) const {return Prv != B.Prv;};
318
319 // Accessors
320 inline Provides *operator ->() {return Prv;};
321 inline Provides const *operator ->() const {return Prv;};
322 inline Provides &operator *() {return *Prv;};
323 inline Provides const &operator *() const {return *Prv;};
324 inline operator Provides *() {return Prv == Owner->ProvideP?0:Prv;};
325 inline operator Provides const *() const {return Prv == Owner->ProvideP?0:Prv;};
326 inline pkgCache *Cache() {return Owner;};
327
328 inline const char *Name() const {return Owner->StrP + Owner->PkgP[Prv->ParentPkg].Name;};
329 inline const char *ProvideVersion() const {return Prv->ProvideVersion == 0?0:Owner->StrP + Prv->ProvideVersion;};
330 inline PkgIterator ParentPkg() {return PkgIterator(*Owner,Owner->PkgP + Prv->ParentPkg);};
331 inline VerIterator OwnerVer() {return VerIterator(*Owner,Owner->VerP + Prv->Version);};
332 inline PkgIterator OwnerPkg() {return PkgIterator(*Owner,Owner->PkgP + Owner->VerP[Prv->Version].ParentPkg);};
333 inline unsigned long Index() const {return Prv - Owner->ProvideP;};
334
335 inline PrvIterator() : Prv(0), Type(PrvVer), Owner(0) {};
336
337 inline PrvIterator(pkgCache &Owner,Provides *Trg,Version *) :
338 Prv(Trg), Type(PrvVer), Owner(&Owner)
339 {
340 if (Prv == 0)
341 Prv = Owner.ProvideP;
342 };
343 inline PrvIterator(pkgCache &Owner,Provides *Trg,Package *) :
344 Prv(Trg), Type(PrvPkg), Owner(&Owner)
345 {
346 if (Prv == 0)
347 Prv = Owner.ProvideP;
348 };
349 };
350
351 // Package file
352 class pkgCache::PkgFileIterator
353 {
354 pkgCache *Owner;
355 PackageFile *File;
356
357 public:
358
359 // Iteration
360 void operator ++(int) {if (File!= Owner->PkgFileP) File = Owner->PkgFileP + File->NextFile;};
361 inline void operator ++() {operator ++(0);};
362 inline bool end() const {return File == Owner->PkgFileP?true:false;};
363
364 // Comparison
365 inline bool operator ==(const PkgFileIterator &B) const {return File == B.File;};
366 inline bool operator !=(const PkgFileIterator &B) const {return File != B.File;};
367
368 // Accessors
369 inline PackageFile *operator ->() {return File;};
370 inline PackageFile const *operator ->() const {return File;};
371 inline PackageFile const &operator *() const {return *File;};
372 inline operator PackageFile *() {return File == Owner->PkgFileP?0:File;};
373 inline operator PackageFile const *() const {return File == Owner->PkgFileP?0:File;};
374 inline pkgCache *Cache() {return Owner;};
375
376 inline const char *FileName() const {return File->FileName == 0?0:Owner->StrP + File->FileName;};
377 inline const char *Archive() const {return File->Archive == 0?0:Owner->StrP + File->Archive;};
378 inline const char *Component() const {return File->Component == 0?0:Owner->StrP + File->Component;};
379 inline const char *Version() const {return File->Version == 0?0:Owner->StrP + File->Version;};
380 inline const char *Origin() const {return File->Origin == 0?0:Owner->StrP + File->Origin;};
381 inline const char *Label() const {return File->Label == 0?0:Owner->StrP + File->Label;};
382 inline const char *Site() const {return File->Site == 0?0:Owner->StrP + File->Site;};
383 inline const char *Architecture() const {return File->Architecture == 0?0:Owner->StrP + File->Architecture;};
384 inline const char *IndexType() const {return File->IndexType == 0?0:Owner->StrP + File->IndexType;};
385
386 inline unsigned long Index() const {return File - Owner->PkgFileP;};
387
388 bool IsOk();
389 string RelStr();
390
391 // Constructors
392 inline PkgFileIterator() : Owner(0), File(0) {};
393 inline PkgFileIterator(pkgCache &Owner) : Owner(&Owner), File(Owner.PkgFileP) {};
394 inline PkgFileIterator(pkgCache &Owner,PackageFile *Trg) : Owner(&Owner), File(Trg) {};
395 };
396
397 // Version File
398 class pkgCache::VerFileIterator
399 {
400 pkgCache *Owner;
401 VerFile *FileP;
402
403 public:
404
405 // Iteration
406 void operator ++(int) {if (FileP != Owner->VerFileP) FileP = Owner->VerFileP + FileP->NextFile;};
407 inline void operator ++() {operator ++(0);};
408 inline bool end() const {return FileP == Owner->VerFileP?true:false;};
409
410 // Comparison
411 inline bool operator ==(const VerFileIterator &B) const {return FileP == B.FileP;};
412 inline bool operator !=(const VerFileIterator &B) const {return FileP != B.FileP;};
413
414 // Accessors
415 inline VerFile *operator ->() {return FileP;};
416 inline VerFile const *operator ->() const {return FileP;};
417 inline VerFile const &operator *() const {return *FileP;};
418 inline operator VerFile *() {return FileP == Owner->VerFileP?0:FileP;};
419 inline operator VerFile const *() const {return FileP == Owner->VerFileP?0:FileP;};
420 inline pkgCache *Cache() {return Owner;};
421
422 inline PkgFileIterator File() const {return PkgFileIterator(*Owner,FileP->File + Owner->PkgFileP);};
423 inline unsigned long Index() const {return FileP - Owner->VerFileP;};
424
425 inline VerFileIterator() : Owner(0), FileP(0) {};
426 inline VerFileIterator(pkgCache &Owner,VerFile *Trg) : Owner(&Owner), FileP(Trg) {};
427 };
428
429 // Description File
430 class pkgCache::DescFileIterator
431 {
432 pkgCache *Owner;
433 DescFile *FileP;
434
435 public:
436
437 // Iteration
438 void operator ++(int) {if (FileP != Owner->DescFileP) FileP = Owner->DescFileP + FileP->NextFile;};
439 inline void operator ++() {operator ++(0);};
440 inline bool end() const {return FileP == Owner->DescFileP?true:false;};
441
442 // Comparison
443 inline bool operator ==(const DescFileIterator &B) const {return FileP == B.FileP;};
444 inline bool operator !=(const DescFileIterator &B) const {return FileP != B.FileP;};
445
446 // Accessors
447 inline DescFile *operator ->() {return FileP;};
448 inline DescFile const *operator ->() const {return FileP;};
449 inline DescFile const &operator *() const {return *FileP;};
450 inline operator DescFile *() {return FileP == Owner->DescFileP?0:FileP;};
451 inline operator DescFile const *() const {return FileP == Owner->DescFileP?0:FileP;};
452 inline pkgCache *Cache() {return Owner;};
453
454 inline PkgFileIterator File() const {return PkgFileIterator(*Owner,FileP->File + Owner->PkgFileP);};
455 inline unsigned long Index() const {return FileP - Owner->DescFileP;};
456
457 inline DescFileIterator() : Owner(0), FileP(0) {};
458 inline DescFileIterator(pkgCache &Owner,DescFile *Trg) : Owner(&Owner), FileP(Trg) {};
459 };
460
461 // Inlined Begin functions cant be in the class because of order problems
462 inline pkgCache::VerIterator pkgCache::PkgIterator::VersionList() const
463 {return VerIterator(*Owner,Owner->VerP + Pkg->VersionList);};
464 inline pkgCache::VerIterator pkgCache::PkgIterator::CurrentVer() const
465 {return VerIterator(*Owner,Owner->VerP + Pkg->CurrentVer);};
466 inline pkgCache::DepIterator pkgCache::PkgIterator::RevDependsList() const
467 {return DepIterator(*Owner,Owner->DepP + Pkg->RevDepends,Pkg);};
468 inline pkgCache::PrvIterator pkgCache::PkgIterator::ProvidesList() const
469 {return PrvIterator(*Owner,Owner->ProvideP + Pkg->ProvidesList,Pkg);};
470 inline pkgCache::TagIterator pkgCache::PkgIterator::TagList() const
471 {return TagIterator(*Owner,Owner->TagP + Pkg->TagList);};
472 inline pkgCache::DescIterator pkgCache::VerIterator::DescriptionList() const
473 {return DescIterator(*Owner,Owner->DescP + Ver->DescriptionList);};
474 inline pkgCache::PrvIterator pkgCache::VerIterator::ProvidesList() const
475 {return PrvIterator(*Owner,Owner->ProvideP + Ver->ProvidesList,Ver);};
476 inline pkgCache::DepIterator pkgCache::VerIterator::DependsList() const
477 {return DepIterator(*Owner,Owner->DepP + Ver->DependsList,Ver);};
478 inline pkgCache::VerFileIterator pkgCache::VerIterator::FileList() const
479 {return VerFileIterator(*Owner,Owner->VerFileP + Ver->FileList);};
480 inline pkgCache::DescFileIterator pkgCache::DescIterator::FileList() const
481 {return DescFileIterator(*Owner,Owner->DescFileP + Desc->FileList);};
482
483 #endif