Purge support
[ntk/apt.git] / apt-pkg / cacheiterators.h
... / ...
CommitLineData
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3// $Id: cacheiterators.h,v 1.14 1999/07/10 04:58:42 jgg 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// Header section: pkglib
32#ifndef PKGLIB_CACHEITERATORS_H
33#define PKGLIB_CACHEITERATORS_H
34
35#ifdef __GNUG__
36#pragma interface "apt-pkg/cacheiterators.h"
37#endif
38
39// Package Iterator
40class pkgCache::PkgIterator
41{
42 Package *Pkg;
43 pkgCache *Owner;
44 long HashIndex;
45
46 public:
47
48 enum OkState {NeedsNothing,NeedsUnpack,NeedsConfigure};
49
50 // Iteration
51 void operator ++(int);
52 inline void operator ++() {operator ++(0);};
53 inline bool end() const {return Owner == 0 || Pkg == Owner->PkgP?true:false;};
54
55 // Comparison
56 inline bool operator ==(const PkgIterator &B) const {return Pkg == B.Pkg;};
57 inline bool operator !=(const PkgIterator &B) const {return Pkg != B.Pkg;};
58
59 // Accessors
60 inline Package *operator ->() {return Pkg;};
61 inline Package const *operator ->() const {return Pkg;};
62 inline Package const &operator *() const {return *Pkg;};
63 inline operator Package *() {return Pkg == Owner->PkgP?0:Pkg;};
64 inline operator Package const *() const {return Pkg == Owner->PkgP?0:Pkg;};
65
66 inline const char *Name() const {return Pkg->Name == 0?0:Owner->StrP + Pkg->Name;};
67 inline const char *Section() const {return Pkg->Section == 0?0:Owner->StrP + Pkg->Section;};
68 inline const char *TargetDist() const {return Pkg->TargetDist == 0?0:Owner->StrP + Pkg->TargetDist;};
69 inline bool Purge() const {return Pkg->CurrentState == pkgCache::State::Purge;};
70 inline VerIterator VersionList() const;
71 inline VerIterator TargetVer() const;
72 inline VerIterator CurrentVer() const;
73 inline DepIterator RevDependsList() const;
74 inline PrvIterator ProvidesList() const;
75 inline unsigned long Index() const {return Pkg - Owner->PkgP;};
76 OkState State() const;
77
78 // Constructors
79 inline PkgIterator(pkgCache &Owner) : Owner(&Owner), HashIndex(-1)
80 {
81 Pkg = Owner.PkgP;
82 operator ++(0);
83 };
84 inline PkgIterator(pkgCache &Owner,Package *Trg) : Pkg(Trg), Owner(&Owner),
85 HashIndex(0)
86 {
87 if (Pkg == 0)
88 Pkg = Owner.PkgP;
89 };
90 inline PkgIterator() : Pkg(0), Owner(0), HashIndex(0) {};
91};
92
93// Version Iterator
94class pkgCache::VerIterator
95{
96 Version *Ver;
97 pkgCache *Owner;
98
99 void _dummy();
100
101 public:
102
103 // Iteration
104 void operator ++(int) {if (Ver != Owner->VerP) Ver = Owner->VerP + Ver->NextVer;};
105 inline void operator ++() {operator ++(0);};
106 inline bool end() const {return Ver == Owner->VerP?true:false;};
107 inline void operator =(const VerIterator &B) {Ver = B.Ver; Owner = B.Owner;};
108
109 // Comparison
110 inline bool operator ==(const VerIterator &B) const {return Ver == B.Ver;};
111 inline bool operator !=(const VerIterator &B) const {return Ver != B.Ver;};
112 int CompareVer(const VerIterator &B) const;
113
114 // Accessors
115 inline Version *operator ->() {return Ver;};
116 inline Version const *operator ->() const {return Ver;};
117 inline Version &operator *() {return *Ver;};
118 inline Version const &operator *() const {return *Ver;};
119 inline operator Version *() {return Ver == Owner->VerP?0:Ver;};
120 inline operator Version const *() const {return Ver == Owner->VerP?0:Ver;};
121
122 inline const char *VerStr() const {return Ver->VerStr == 0?0:Owner->StrP + Ver->VerStr;};
123 inline const char *Section() const {return Ver->Section == 0?0:Owner->StrP + Ver->Section;};
124 inline const char *Arch() const {return Ver->Arch == 0?0:Owner->StrP + Ver->Arch;};
125 inline PkgIterator ParentPkg() const {return PkgIterator(*Owner,Owner->PkgP + Ver->ParentPkg);};
126 inline DepIterator DependsList() const;
127 inline PrvIterator ProvidesList() const;
128 inline VerFileIterator FileList() const;
129 inline unsigned long Index() const {return Ver - Owner->VerP;};
130 bool Downloadable() const;
131 const char *PriorityType();
132
133 bool Automatic() const;
134 VerFileIterator NewestFile() const;
135
136 inline VerIterator() : Ver(0), Owner(0) {};
137 inline VerIterator(pkgCache &Owner,Version *Trg = 0) : Ver(Trg),
138 Owner(&Owner)
139 {
140 if (Ver == 0)
141 Ver = Owner.VerP;
142 };
143};
144
145// Dependency iterator
146class pkgCache::DepIterator
147{
148 Dependency *Dep;
149 enum {DepVer, DepRev} Type;
150 pkgCache *Owner;
151
152 void _dummy();
153
154 public:
155
156 // Iteration
157 void operator ++(int) {if (Dep != Owner->DepP) Dep = Owner->DepP +
158 (Type == DepVer?Dep->NextDepends:Dep->NextRevDepends);};
159 inline void operator ++() {operator ++(0);};
160 inline bool end() const {return Owner == 0 || Dep == Owner->DepP?true:false;};
161
162 // Comparison
163 inline bool operator ==(const DepIterator &B) const {return Dep == B.Dep;};
164 inline bool operator !=(const DepIterator &B) const {return Dep != B.Dep;};
165
166 // Accessors
167 inline Dependency *operator ->() {return Dep;};
168 inline Dependency const *operator ->() const {return Dep;};
169 inline Dependency &operator *() {return *Dep;};
170 inline Dependency const &operator *() const {return *Dep;};
171 inline operator Dependency *() {return Dep == Owner->DepP?0:Dep;};
172 inline operator Dependency const *() const {return Dep == Owner->DepP?0:Dep;};
173
174 inline const char *TargetVer() const {return Dep->Version == 0?0:Owner->StrP + Dep->Version;};
175 inline PkgIterator TargetPkg() {return PkgIterator(*Owner,Owner->PkgP + Dep->Package);};
176 inline PkgIterator SmartTargetPkg() {PkgIterator R(*Owner);SmartTargetPkg(R);return R;};
177 inline VerIterator ParentVer() {return VerIterator(*Owner,Owner->VerP + Dep->ParentVer);};
178 inline PkgIterator ParentPkg() {return PkgIterator(*Owner,Owner->PkgP + Owner->VerP[Dep->ParentVer].ParentPkg);};
179 inline bool Reverse() {return Type == DepRev;};
180 inline unsigned long Index() const {return Dep - Owner->DepP;};
181 bool IsCritical();
182 void GlobOr(DepIterator &Start,DepIterator &End);
183 Version **AllTargets();
184 bool SmartTargetPkg(PkgIterator &Result);
185 const char *CompType();
186 const char *DepType();
187
188 inline DepIterator(pkgCache &Owner,Dependency *Trg,Version * = 0) :
189 Dep(Trg), Type(DepVer), Owner(&Owner)
190 {
191 if (Dep == 0)
192 Dep = Owner.DepP;
193 };
194 inline DepIterator(pkgCache &Owner,Dependency *Trg,Package *) :
195 Dep(Trg), Type(DepRev), Owner(&Owner)
196 {
197 if (Dep == 0)
198 Dep = Owner.DepP;
199 };
200 inline DepIterator() : Dep(0), Type(DepVer), Owner(0) {};
201};
202
203// Provides iterator
204class pkgCache::PrvIterator
205{
206 Provides *Prv;
207 enum {PrvVer, PrvPkg} Type;
208 pkgCache *Owner;
209
210 void _dummy();
211
212 public:
213
214 // Iteration
215 void operator ++(int) {if (Prv != Owner->ProvideP) Prv = Owner->ProvideP +
216 (Type == PrvVer?Prv->NextPkgProv:Prv->NextProvides);};
217 inline void operator ++() {operator ++(0);};
218 inline bool end() const {return Prv == Owner->ProvideP?true:false;};
219
220 // Comparison
221 inline bool operator ==(const PrvIterator &B) const {return Prv == B.Prv;};
222 inline bool operator !=(const PrvIterator &B) const {return Prv != B.Prv;};
223
224 // Accessors
225 inline Provides *operator ->() {return Prv;};
226 inline Provides const *operator ->() const {return Prv;};
227 inline Provides &operator *() {return *Prv;};
228 inline Provides const &operator *() const {return *Prv;};
229 inline operator Provides *() {return Prv == Owner->ProvideP?0:Prv;};
230 inline operator Provides const *() const {return Prv == Owner->ProvideP?0:Prv;};
231
232 inline const char *Name() const {return Owner->StrP + Owner->PkgP[Prv->ParentPkg].Name;};
233 inline const char *ProvideVersion() const {return Prv->ProvideVersion == 0?0:Owner->StrP + Prv->ProvideVersion;};
234 inline PkgIterator ParentPkg() {return PkgIterator(*Owner,Owner->PkgP + Prv->ParentPkg);};
235 inline VerIterator OwnerVer() {return VerIterator(*Owner,Owner->VerP + Prv->Version);};
236 inline PkgIterator OwnerPkg() {return PkgIterator(*Owner,Owner->PkgP + Owner->VerP[Prv->Version].ParentPkg);};
237 inline unsigned long Index() const {return Prv - Owner->ProvideP;};
238
239 inline PrvIterator(pkgCache &Owner,Provides *Trg,Version *) :
240 Prv(Trg), Type(PrvVer), Owner(&Owner)
241 {
242 if (Prv == 0)
243 Prv = Owner.ProvideP;
244 };
245 inline PrvIterator(pkgCache &Owner,Provides *Trg,Package *) :
246 Prv(Trg), Type(PrvPkg), Owner(&Owner)
247 {
248 if (Prv == 0)
249 Prv = Owner.ProvideP;
250 };
251};
252
253// Package file
254class pkgCache::PkgFileIterator
255{
256 pkgCache *Owner;
257 PackageFile *File;
258
259 public:
260
261 // Iteration
262 void operator ++(int) {if (File!= Owner->PkgFileP) File = Owner->PkgFileP + File->NextFile;};
263 inline void operator ++() {operator ++(0);};
264 inline bool end() const {return File == Owner->PkgFileP?true:false;};
265
266 // Comparison
267 inline bool operator ==(const PkgFileIterator &B) const {return File == B.File;};
268 inline bool operator !=(const PkgFileIterator &B) const {return File != B.File;};
269
270 // Accessors
271 inline PackageFile *operator ->() {return File;};
272 inline PackageFile const *operator ->() const {return File;};
273 inline PackageFile const &operator *() const {return *File;};
274 inline operator PackageFile *() {return File == Owner->PkgFileP?0:File;};
275 inline operator PackageFile const *() const {return File == Owner->PkgFileP?0:File;};
276
277 inline const char *FileName() const {return File->FileName == 0?0:Owner->StrP + File->FileName;};
278 inline const char *Archive() const {return File->Archive == 0?0:Owner->StrP + File->Archive;};
279 inline const char *Component() const {return File->Component == 0?0:Owner->StrP + File->Component;};
280 inline const char *Version() const {return File->Version == 0?0:Owner->StrP + File->Version;};
281 inline const char *Origin() const {return File->Origin == 0?0:Owner->StrP + File->Origin;};
282 inline const char *Label() const {return File->Origin == 0?0:Owner->StrP + File->Label;};
283 inline const char *Architecture() const {return File->Origin == 0?0:Owner->StrP + File->Architecture;};
284
285 inline unsigned long Index() const {return File - Owner->PkgFileP;};
286
287 bool IsOk();
288
289 // Constructors
290 inline PkgFileIterator(pkgCache &Owner) : Owner(&Owner), File(Owner.PkgFileP + Owner.Head().FileList) {};
291 inline PkgFileIterator(pkgCache &Owner,PackageFile *Trg) : Owner(&Owner), File(Trg) {};
292};
293
294// Version File
295class pkgCache::VerFileIterator
296{
297 pkgCache *Owner;
298 VerFile *FileP;
299
300 public:
301
302 // Iteration
303 void operator ++(int) {if (FileP != Owner->VerFileP) FileP = Owner->VerFileP + FileP->NextFile;};
304 inline void operator ++() {operator ++(0);};
305 inline bool end() const {return FileP == Owner->VerFileP?true:false;};
306
307 // Comparison
308 inline bool operator ==(const VerFileIterator &B) const {return FileP == B.FileP;};
309 inline bool operator !=(const VerFileIterator &B) const {return FileP != B.FileP;};
310
311 // Accessors
312 inline VerFile *operator ->() {return FileP;};
313 inline VerFile const *operator ->() const {return FileP;};
314 inline VerFile const &operator *() const {return *FileP;};
315 inline operator VerFile *() {return FileP == Owner->VerFileP?0:FileP;};
316 inline operator VerFile const *() const {return FileP == Owner->VerFileP?0:FileP;};
317
318 inline PkgFileIterator File() const {return PkgFileIterator(*Owner,FileP->File + Owner->PkgFileP);};
319 inline unsigned long Index() const {return FileP - Owner->VerFileP;};
320
321 inline VerFileIterator(pkgCache &Owner,VerFile *Trg) : Owner(&Owner), FileP(Trg) {};
322};
323
324// Inlined Begin functions cant be in the class because of order problems
325inline pkgCache::VerIterator pkgCache::PkgIterator::VersionList() const
326 {return VerIterator(*Owner,Owner->VerP + Pkg->VersionList);};
327inline pkgCache::VerIterator pkgCache::PkgIterator::CurrentVer() const
328 {return VerIterator(*Owner,Owner->VerP + Pkg->CurrentVer);};
329inline pkgCache::VerIterator pkgCache::PkgIterator::TargetVer() const
330 {return VerIterator(*Owner,Owner->VerP + Pkg->TargetVer);};
331inline pkgCache::DepIterator pkgCache::PkgIterator::RevDependsList() const
332 {return DepIterator(*Owner,Owner->DepP + Pkg->RevDepends,Pkg);};
333inline pkgCache::PrvIterator pkgCache::PkgIterator::ProvidesList() const
334 {return PrvIterator(*Owner,Owner->ProvideP + Pkg->ProvidesList,Pkg);};
335inline pkgCache::PrvIterator pkgCache::VerIterator::ProvidesList() const
336 {return PrvIterator(*Owner,Owner->ProvideP + Ver->ProvidesList,Ver);};
337inline pkgCache::DepIterator pkgCache::VerIterator::DependsList() const
338 {return DepIterator(*Owner,Owner->DepP + Ver->DependsList,Ver);};
339inline pkgCache::VerFileIterator pkgCache::VerIterator::FileList() const
340 {return VerFileIterator(*Owner,Owner->VerFileP + Ver->FileList);};
341
342#endif