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