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