Implement the first step toward Multi-Arch by setting up a Group
[ntk/apt.git] / apt-pkg / cacheiterators.h
CommitLineData
578bfd0a
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
578bfd0a
AL
3/* ######################################################################
4
5 Cache Iterators - Iterators for navigating the cache structure
6
7 The iterators all provides ++,==,!=,->,* and end for their type.
8 The end function can be used to tell if the list has been fully
9 traversed.
10
11 Unlike STL iterators these contain helper functions to access the data
12 that is being iterated over. This is because the data structures can't
13 be formed in a manner that is intuitive to use and also mmapable.
14
15 For each variable in the target structure that would need a translation
16 to be accessed correctly a translating function of the same name is
17 present in the iterator. If applicable the translating function will
18 return an iterator.
19
20 The DepIterator can iterate over two lists, a list of 'version depends'
21 or a list of 'package reverse depends'. The type is determined by the
22 structure passed to the constructor, which should be the structure
6c139d6e
AL
23 that has the depends pointer as a member. The provide iterator has the
24 same system.
578bfd0a 25
094a497d 26 This header is not user includable, please use apt-pkg/pkgcache.h
578bfd0a
AL
27
28 ##################################################################### */
29 /*}}}*/
578bfd0a
AL
30#ifndef PKGLIB_CACHEITERATORS_H
31#define PKGLIB_CACHEITERATORS_H
773e2c1f
DK
32// abstract Iterator template /*{{{*/
33/* This template provides the very basic iterator methods we
5bf15716 34 need to have for doing some walk-over-the-cache magic */
773e2c1f 35template<typename Str, typename Itr> class pkgCache::Iterator {
773e2c1f
DK
36 protected:
37 Str *S;
38 pkgCache *Owner;
39
40 /** \brief Returns the Pointer for this struct in the owner
41 * The implementation of this method should be pretty short
42 * as it will only return the Pointer into the mmap stored
43 * in the owner but the name of this pointer is different for
44 * each stucture and we want to abstract here at least for the
45 * basic methods from the actual structure.
46 * \return Pointer to the first structure of this type
47 */
48 virtual Str* OwnerPointer() const = 0;
49
50 public:
51 // Iteration
52 virtual void operator ++(int) = 0;
53 virtual void operator ++() = 0; // Should be {operator ++(0);};
54 inline bool end() const {return Owner == 0 || S == OwnerPointer();};
55
56 // Comparison
57 inline bool operator ==(const Itr &B) const {return S == B.S;};
58 inline bool operator !=(const Itr &B) const {return S != B.S;};
59
60 // Accessors
61 inline Str *operator ->() {return S;};
62 inline Str const *operator ->() const {return S;};
63 inline operator Str *() {return S == OwnerPointer() ? 0 : S;};
64 inline operator Str const *() const {return S == OwnerPointer() ? 0 : S;};
65 inline Str const &operator *() const {return *S;};
66 inline pkgCache *Cache() {return Owner;};
67
68 // Mixed stuff
69 inline void operator =(const Itr &B) {S = B.S; Owner = B.Owner;};
70 inline bool IsGood() const { return S && Owner && ! end();};
71 inline unsigned long Index() const {return S - OwnerPointer();};
72
73 // Constructors - look out for the variable assigning
74 inline Iterator() : S(0), Owner(0) {};
75 inline Iterator(pkgCache &Owner,Str *T = 0) : S(T), Owner(&Owner) {};
5bf15716
DK
76};
77 /*}}}*/
78// Group Iterator /*{{{*/
79/* Packages with the same name are collected in a Group so someone only
80 interest in package names can iterate easily over the names, so the
81 different architectures can be treated as of the "same" package
82 (apt internally treat them as totally different packages) */
83class pkgCache::GrpIterator: public Iterator<Group, GrpIterator> {
84 protected:
85 inline Group* OwnerPointer() const {
86 return Owner->GrpP;
87 };
88
89 public:
90 void operator ++(int) {if (S != Owner->GrpP) S = Owner->GrpP + S->Next;};
91 virtual void operator ++() {operator ++(0);};
92
93 inline const char *Name() const {return S->Name == 0?0:Owner->StrP + S->Name;};
94 inline PkgIterator PackageList() const;
95 PkgIterator FindPkg(string Arch = "any");
96 PkgIterator NextPkg(PkgIterator const &Pkg);
97
98 // Constructors
99 inline GrpIterator(pkgCache &Owner, Group *Trg) : Iterator<Group, GrpIterator>(Owner, Trg) {
100 if (S == 0)
101 S = OwnerPointer();
102 };
103 inline GrpIterator() : Iterator<Group, GrpIterator>() {};
104
773e2c1f
DK
105};
106 /*}}}*/
92fcbfc1 107// Package Iterator /*{{{*/
773e2c1f
DK
108class pkgCache::PkgIterator: public Iterator<Package, PkgIterator> {
109 long HashIndex;
110
111 protected:
112 inline Package* OwnerPointer() const {
113 return Owner->PkgP;
114 };
115
116 public:
117 // This constructor is the 'begin' constructor, never use it.
118 inline PkgIterator(pkgCache &Owner) : Iterator<Package, PkgIterator>(Owner), HashIndex(-1) {
119 S = OwnerPointer();
120 operator ++(0);
121 };
122
123 virtual void operator ++(int);
124 virtual void operator ++() {operator ++(0);};
125
126 enum OkState {NeedsNothing,NeedsUnpack,NeedsConfigure};
127
128 // Accessors
129 inline const char *Name() const {return S->Name == 0?0:Owner->StrP + S->Name;};
130 inline const char *Section() const {return S->Section == 0?0:Owner->StrP + S->Section;};
131 inline bool Purge() const {return S->CurrentState == pkgCache::State::Purge ||
132 (S->CurrentVer == 0 && S->CurrentState == pkgCache::State::NotInstalled);};
5bf15716
DK
133 inline const char *Arch() const {return S->Arch == 0?0:Owner->StrP + S->Arch;};
134 inline GrpIterator Group() const { return GrpIterator(*Owner, Owner->GrpP + S->Group);};
773e2c1f
DK
135
136 inline VerIterator VersionList() const;
137 inline VerIterator CurrentVer() const;
138 inline DepIterator RevDependsList() const;
139 inline PrvIterator ProvidesList() const;
140 OkState State() const;
141 const char *CandVersion() const;
142 const char *CurVersion() const;
143
144 //Nice printable representation
145 friend std::ostream& operator <<(std::ostream& out, PkgIterator i);
146
147 // Constructors
148 inline PkgIterator(pkgCache &Owner,Package *Trg) : Iterator<Package, PkgIterator>(Owner, Trg), HashIndex(0) {
149 if (S == 0)
150 S = OwnerPointer();
151 };
152 inline PkgIterator() : Iterator<Package, PkgIterator>(), HashIndex(0) {};
578bfd0a 153};
92fcbfc1
DK
154 /*}}}*/
155// Version Iterator /*{{{*/
773e2c1f
DK
156class pkgCache::VerIterator : public Iterator<Version, VerIterator> {
157 protected:
158 inline Version* OwnerPointer() const {
159 return Owner->VerP;
160 };
161
162 public:
163 // Iteration
164 void operator ++(int) {if (S != Owner->VerP) S = Owner->VerP + S->NextVer;};
165 inline void operator ++() {operator ++(0);};
166
167 // Comparison
168 int CompareVer(const VerIterator &B) const;
169
170 // Accessors
171 inline const char *VerStr() const {return S->VerStr == 0?0:Owner->StrP + S->VerStr;};
172 inline const char *Section() const {return S->Section == 0?0:Owner->StrP + S->Section;};
173 inline const char *Arch() const {return S->Arch == 0?0:Owner->StrP + S->Arch;};
174 inline PkgIterator ParentPkg() const {return PkgIterator(*Owner,Owner->PkgP + S->ParentPkg);};
175
176 inline DescIterator DescriptionList() const;
177 DescIterator TranslatedDescription() const;
178 inline DepIterator DependsList() const;
179 inline PrvIterator ProvidesList() const;
180 inline VerFileIterator FileList() const;
181 bool Downloadable() const;
182 inline const char *PriorityType() {return Owner->Priority(S->Priority);};
183 string RelStr();
184
185 bool Automatic() const;
186 VerFileIterator NewestFile() const;
187
188 inline VerIterator(pkgCache &Owner,Version *Trg = 0) : Iterator<Version, VerIterator>(Owner, Trg) {
189 if (S == 0)
190 S = OwnerPointer();
191 };
192 inline VerIterator() : Iterator<Version, VerIterator>() {};
578bfd0a 193};
92fcbfc1
DK
194 /*}}}*/
195// Description Iterator /*{{{*/
773e2c1f
DK
196class pkgCache::DescIterator : public Iterator<Description, DescIterator> {
197 protected:
198 inline Description* OwnerPointer() const {
199 return Owner->DescP;
200 };
201
202 public:
203 // Iteration
204 void operator ++(int) {if (S != Owner->DescP) S = Owner->DescP + S->NextDesc;};
205 inline void operator ++() {operator ++(0);};
206
207 // Comparison
208 int CompareDesc(const DescIterator &B) const;
209
210 // Accessors
211 inline const char *LanguageCode() const {return Owner->StrP + S->language_code;};
212 inline const char *md5() const {return Owner->StrP + S->md5sum;};
213 inline DescFileIterator FileList() const;
214
215 inline DescIterator() : Iterator<Description, DescIterator>() {};
216 inline DescIterator(pkgCache &Owner,Description *Trg = 0) : Iterator<Description, DescIterator>(Owner, Trg) {
217 if (S == 0)
218 S = Owner.DescP;
219 };
a52f938b 220};
92fcbfc1
DK
221 /*}}}*/
222// Dependency iterator /*{{{*/
773e2c1f
DK
223class pkgCache::DepIterator : public Iterator<Dependency, DepIterator> {
224 enum {DepVer, DepRev} Type;
225
226 protected:
227 inline Dependency* OwnerPointer() const {
228 return Owner->DepP;
229 };
230
231 public:
232 // Iteration
233 void operator ++(int) {if (S != Owner->DepP) S = Owner->DepP +
234 (Type == DepVer ? S->NextDepends : S->NextRevDepends);};
235 inline void operator ++() {operator ++(0);};
236
237 // Accessors
238 inline const char *TargetVer() const {return S->Version == 0?0:Owner->StrP + S->Version;};
239 inline PkgIterator TargetPkg() {return PkgIterator(*Owner,Owner->PkgP + S->Package);};
240 inline PkgIterator SmartTargetPkg() {PkgIterator R(*Owner,0);SmartTargetPkg(R);return R;};
241 inline VerIterator ParentVer() {return VerIterator(*Owner,Owner->VerP + S->ParentVer);};
242 inline PkgIterator ParentPkg() {return PkgIterator(*Owner,Owner->PkgP + Owner->VerP[S->ParentVer].ParentPkg);};
243 inline bool Reverse() {return Type == DepRev;};
244 bool IsCritical();
245 void GlobOr(DepIterator &Start,DepIterator &End);
246 Version **AllTargets();
247 bool SmartTargetPkg(PkgIterator &Result);
248 inline const char *CompType() {return Owner->CompType(S->CompareOp);};
249 inline const char *DepType() {return Owner->DepType(S->Type);};
250
251 inline DepIterator(pkgCache &Owner, Dependency *Trg, Version* = 0) :
252 Iterator<Dependency, DepIterator>(Owner, Trg), Type(DepVer) {
253 if (S == 0)
254 S = Owner.DepP;
255 };
256 inline DepIterator(pkgCache &Owner, Dependency *Trg, Package*) :
257 Iterator<Dependency, DepIterator>(Owner, Trg), Type(DepRev) {
258 if (S == 0)
259 S = Owner.DepP;
260 };
261 inline DepIterator() : Iterator<Dependency, DepIterator>(), Type(DepVer) {};
578bfd0a 262};
92fcbfc1
DK
263 /*}}}*/
264// Provides iterator /*{{{*/
773e2c1f
DK
265class pkgCache::PrvIterator : public Iterator<Provides, PrvIterator> {
266 enum {PrvVer, PrvPkg} Type;
267
268 protected:
269 inline Provides* OwnerPointer() const {
270 return Owner->ProvideP;
271 };
272
273 public:
274 // Iteration
275 void operator ++(int) {if (S != Owner->ProvideP) S = Owner->ProvideP +
276 (Type == PrvVer?S->NextPkgProv:S->NextProvides);};
277 inline void operator ++() {operator ++(0);};
278
279 // Accessors
280 inline const char *Name() const {return Owner->StrP + Owner->PkgP[S->ParentPkg].Name;};
281 inline const char *ProvideVersion() const {return S->ProvideVersion == 0?0:Owner->StrP + S->ProvideVersion;};
282 inline PkgIterator ParentPkg() {return PkgIterator(*Owner,Owner->PkgP + S->ParentPkg);};
283 inline VerIterator OwnerVer() {return VerIterator(*Owner,Owner->VerP + S->Version);};
284 inline PkgIterator OwnerPkg() {return PkgIterator(*Owner,Owner->PkgP + Owner->VerP[S->Version].ParentPkg);};
285
286 inline PrvIterator() : Iterator<Provides, PrvIterator>(), Type(PrvVer) {};
287
288 inline PrvIterator(pkgCache &Owner, Provides *Trg, Version*) :
289 Iterator<Provides, PrvIterator>(Owner, Trg), Type(PrvVer) {
290 if (S == 0)
291 S = Owner.ProvideP;
292 };
293 inline PrvIterator(pkgCache &Owner, Provides *Trg, Package*) :
294 Iterator<Provides, PrvIterator>(Owner, Trg), Type(PrvPkg) {
295 if (S == 0)
296 S = Owner.ProvideP;
297 };
578bfd0a 298};
92fcbfc1
DK
299 /*}}}*/
300// Package file /*{{{*/
773e2c1f
DK
301class pkgCache::PkgFileIterator : public Iterator<PackageFile, PkgFileIterator> {
302 protected:
303 inline PackageFile* OwnerPointer() const {
304 return Owner->PkgFileP;
305 };
306
307 public:
308 // Iteration
309 void operator ++(int) {if (S != Owner->PkgFileP) S = Owner->PkgFileP + S->NextFile;};
310 inline void operator ++() {operator ++(0);};
311
312 // Accessors
313 inline const char *FileName() const {return S->FileName == 0?0:Owner->StrP + S->FileName;};
314 inline const char *Archive() const {return S->Archive == 0?0:Owner->StrP + S->Archive;};
315 inline const char *Component() const {return S->Component == 0?0:Owner->StrP + S->Component;};
316 inline const char *Version() const {return S->Version == 0?0:Owner->StrP + S->Version;};
317 inline const char *Origin() const {return S->Origin == 0?0:Owner->StrP + S->Origin;};
318 inline const char *Codename() const {return S->Codename ==0?0:Owner->StrP + S->Codename;};
319 inline const char *Label() const {return S->Label == 0?0:Owner->StrP + S->Label;};
320 inline const char *Site() const {return S->Site == 0?0:Owner->StrP + S->Site;};
321 inline const char *Architecture() const {return S->Architecture == 0?0:Owner->StrP + S->Architecture;};
322 inline const char *IndexType() const {return S->IndexType == 0?0:Owner->StrP + S->IndexType;};
323
324 bool IsOk();
325 string RelStr();
326
327 // Constructors
328 inline PkgFileIterator() : Iterator<PackageFile, PkgFileIterator>() {};
329 inline PkgFileIterator(pkgCache &Owner,PackageFile *Trg = 0) : Iterator<PackageFile, PkgFileIterator>(Owner, Trg) {};
578bfd0a 330};
92fcbfc1
DK
331 /*}}}*/
332// Version File /*{{{*/
773e2c1f
DK
333class pkgCache::VerFileIterator : public pkgCache::Iterator<VerFile, VerFileIterator> {
334 protected:
335 inline VerFile* OwnerPointer() const {
336 return Owner->VerFileP;
337 };
338
339 public:
340 // Iteration
341 void operator ++(int) {if (S != Owner->VerFileP) S = Owner->VerFileP + S->NextFile;};
342 inline void operator ++() {operator ++(0);};
343
344 // Accessors
345 inline PkgFileIterator File() const {return PkgFileIterator(*Owner,S->File + Owner->PkgFileP);};
346
347 inline VerFileIterator() : Iterator<VerFile, VerFileIterator>() {};
348 inline VerFileIterator(pkgCache &Owner,VerFile *Trg) : Iterator<VerFile, VerFileIterator>(Owner, Trg) {};
dcb79bae 349};
92fcbfc1
DK
350 /*}}}*/
351// Description File /*{{{*/
773e2c1f
DK
352class pkgCache::DescFileIterator : public Iterator<DescFile, DescFileIterator> {
353 protected:
354 inline DescFile* OwnerPointer() const {
355 return Owner->DescFileP;
356 };
357
358 public:
359 // Iteration
360 void operator ++(int) {if (S != Owner->DescFileP) S = Owner->DescFileP + S->NextFile;};
361 inline void operator ++() {operator ++(0);};
362
363 // Accessors
364 inline PkgFileIterator File() const {return PkgFileIterator(*Owner,S->File + Owner->PkgFileP);};
365
366 inline DescFileIterator() : Iterator<DescFile, DescFileIterator>() {};
367 inline DescFileIterator(pkgCache &Owner,DescFile *Trg) : Iterator<DescFile, DescFileIterator>(Owner, Trg) {};
a52f938b 368};
92fcbfc1
DK
369 /*}}}*/
370// Inlined Begin functions cant be in the class because of order problems /*{{{*/
5bf15716
DK
371inline pkgCache::PkgIterator pkgCache::GrpIterator::PackageList() const
372 {return PkgIterator(*Owner,Owner->PkgP + S->FirstPackage);};
578bfd0a 373inline pkgCache::VerIterator pkgCache::PkgIterator::VersionList() const
773e2c1f 374 {return VerIterator(*Owner,Owner->VerP + S->VersionList);};
578bfd0a 375inline pkgCache::VerIterator pkgCache::PkgIterator::CurrentVer() const
773e2c1f 376 {return VerIterator(*Owner,Owner->VerP + S->CurrentVer);};
578bfd0a 377inline pkgCache::DepIterator pkgCache::PkgIterator::RevDependsList() const
773e2c1f 378 {return DepIterator(*Owner,Owner->DepP + S->RevDepends,S);};
578bfd0a 379inline pkgCache::PrvIterator pkgCache::PkgIterator::ProvidesList() const
773e2c1f 380 {return PrvIterator(*Owner,Owner->ProvideP + S->ProvidesList,S);};
a52f938b 381inline pkgCache::DescIterator pkgCache::VerIterator::DescriptionList() const
773e2c1f 382 {return DescIterator(*Owner,Owner->DescP + S->DescriptionList);};
578bfd0a 383inline pkgCache::PrvIterator pkgCache::VerIterator::ProvidesList() const
773e2c1f 384 {return PrvIterator(*Owner,Owner->ProvideP + S->ProvidesList,S);};
578bfd0a 385inline pkgCache::DepIterator pkgCache::VerIterator::DependsList() const
773e2c1f 386 {return DepIterator(*Owner,Owner->DepP + S->DependsList,S);};
dcb79bae 387inline pkgCache::VerFileIterator pkgCache::VerIterator::FileList() const
773e2c1f 388 {return VerFileIterator(*Owner,Owner->VerFileP + S->FileList);};
a52f938b 389inline pkgCache::DescFileIterator pkgCache::DescIterator::FileList() const
773e2c1f 390 {return DescFileIterator(*Owner,Owner->DescFileP + S->FileList);};
92fcbfc1 391 /*}}}*/
578bfd0a 392#endif