Base revisions
[ntk/apt.git] / apt-pkg / cacheiterators.h
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: cacheiterators.h,v 1.1 1998/07/02 02:58:12 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.
25
26 This header is not user includable, please use pkglib/pkgcache.h
27
28 ##################################################################### */
29 /*}}}*/
30 // Header section: pkglib
31 #ifndef PKGLIB_CACHEITERATORS_H
32 #define PKGLIB_CACHEITERATORS_H
33
34 // Package Iterator
35 class pkgCache::PkgIterator
36 {
37 Package *Pkg;
38 pkgCache *Owner;
39 long HashIndex;
40
41 public:
42
43 enum OkState {NeedsNothing,NeedsUnpack,NeedsConfigure};
44
45 // Iteration
46 void operator ++(int);
47 inline void operator ++() {operator ++(0);};
48 inline bool end() const {return Owner == 0 || Pkg == Owner->PkgP?true:false;};
49
50 // Comparison
51 inline bool operator ==(const PkgIterator &B) const {return Pkg == B.Pkg;};
52 inline bool operator !=(const PkgIterator &B) const {return Pkg != B.Pkg;};
53
54 // Accessors
55 inline Package *operator ->() {return Pkg;};
56 inline Package const *operator ->() const {return Pkg;};
57 inline Package const &operator *() const {return *Pkg;};
58 inline operator Package *() {return Pkg == Owner->PkgP?0:Pkg;};
59 inline operator Package const *() const {return Pkg == Owner->PkgP?0:Pkg;};
60 inline const char *Name() const {return Pkg->Name == 0?0:Owner->StrP + Pkg->Name;};
61 inline const char *Section() const {return Pkg->Section == 0?0:Owner->StrP + Pkg->Section;};
62 inline const char *TargetDist() const {return Pkg->TargetDist == 0?0:Owner->StrP + Pkg->TargetDist;};
63 inline VerIterator VersionList() const;
64 inline VerIterator TargetVer() const;
65 inline VerIterator CurrentVer() const;
66 inline DepIterator RevDependsList() const;
67 inline PrvIterator ProvidesList() const;
68 OkState State() const;
69
70 // Constructors
71 inline PkgIterator(pkgCache &Owner) : Owner(&Owner), HashIndex(-1)
72 {
73 Pkg = Owner.PkgP;
74 operator ++(0);
75 };
76 inline PkgIterator(pkgCache &Owner,Package *Trg) : Pkg(Trg), Owner(&Owner),
77 HashIndex(0)
78 {
79 if (Pkg == 0)
80 Pkg = Owner.PkgP;
81 };
82 inline PkgIterator() : Pkg(0), Owner(0), HashIndex(0) {};
83 };
84
85 // Version Iterator
86 class pkgCache::VerIterator
87 {
88 Version *Ver;
89 pkgCache &Owner;
90
91 void _dummy();
92
93 public:
94
95 // Iteration
96 void operator ++(int) {if (Ver != Owner.VerP) Ver = Owner.VerP + Ver->NextVer;};
97 inline void operator ++() {operator ++(0);};
98 inline bool end() const {return Ver == Owner.VerP?true:false;};
99 inline void operator =(const VerIterator &B) {Ver = B.Ver;};
100
101 // Comparison
102 inline bool operator ==(const VerIterator &B) const {return Ver == B.Ver;};
103 inline bool operator !=(const VerIterator &B) const {return Ver != B.Ver;};
104 int CompareVer(const VerIterator &B) const;
105
106 // Accessors
107 inline Version *operator ->() {return Ver;};
108 inline Version const *operator ->() const {return Ver;};
109 inline Version &operator *() {return *Ver;};
110 inline Version const &operator *() const {return *Ver;};
111 inline operator Version *() {return Ver == Owner.VerP?0:Ver;};
112 inline operator Version const *() const {return Ver == Owner.VerP?0:Ver;};
113 inline const char *VerStr() const {return Ver->VerStr == 0?0:Owner.StrP + Ver->VerStr;};
114 inline const char *Section() const {return Ver->Section == 0?0:Owner.StrP + Ver->Section;};
115 inline PkgFileIterator File() const;
116 inline PkgIterator ParentPkg() const {return PkgIterator(Owner,Owner.PkgP + Ver->ParentPkg);};
117 inline DepIterator DependsList() const;
118 inline PrvIterator ProvidesList() const;
119
120 inline VerIterator(pkgCache &Owner,Version *Trg) : Ver(Trg), Owner(Owner)
121 {
122 if (Ver == 0)
123 Ver = Owner.VerP;
124 };
125 };
126
127 // Dependency iterator
128 class pkgCache::DepIterator
129 {
130 Dependency *Dep;
131 enum {DepVer, DepRev} Type;
132 pkgCache *Owner;
133
134 void _dummy();
135
136 public:
137
138 // Iteration
139 void operator ++(int) {if (Dep != Owner->DepP) Dep = Owner->DepP +
140 (Type == DepVer?Dep->NextDepends:Dep->NextRevDepends);};
141 inline void operator ++() {operator ++(0);};
142 inline bool end() const {return Owner == 0 || Dep == Owner->DepP?true:false;};
143
144 // Comparison
145 inline bool operator ==(const DepIterator &B) const {return Dep == B.Dep;};
146 inline bool operator !=(const DepIterator &B) const {return Dep != B.Dep;};
147
148 // Accessors
149 inline Dependency *operator ->() {return Dep;};
150 inline Dependency const *operator ->() const {return Dep;};
151 inline Dependency &operator *() {return *Dep;};
152 inline Dependency const &operator *() const {return *Dep;};
153 inline operator Dependency *() {return Dep == Owner->DepP?0:Dep;};
154 inline operator Dependency const *() const {return Dep == Owner->DepP?0:Dep;};
155 inline const char *TargetVer() const {return Dep->Version == 0?0:Owner->StrP + Dep->Version;};
156 inline PkgIterator TargetPkg() {return PkgIterator(*Owner,Owner->PkgP + Dep->Package);};
157 Version **AllTargets();
158 bool SmartTargetPkg(PkgIterator &Result);
159 inline PkgIterator SmartTargetPkg() {PkgIterator R(*Owner);SmartTargetPkg(R);return R;};
160 inline VerIterator ParentVer() {return VerIterator(*Owner,Owner->VerP + Dep->ParentVer);};
161 inline PkgIterator ParentPkg() {return PkgIterator(*Owner,Owner->PkgP + Owner->VerP[Dep->ParentVer].ParentPkg);};
162 bool IsCritical();
163 inline bool Reverse() {return Type == DepRev;};
164
165 inline DepIterator(pkgCache &Owner,Dependency *Trg,Version * = 0) :
166 Dep(Trg), Type(DepVer), Owner(&Owner)
167 {
168 if (Dep == 0)
169 Dep = Owner.DepP;
170 };
171 inline DepIterator(pkgCache &Owner,Dependency *Trg,Package *) :
172 Dep(Trg), Type(DepRev), Owner(&Owner)
173 {
174 if (Dep == 0)
175 Dep = Owner.DepP;
176 };
177 inline DepIterator() : Dep(0), Type(DepVer), Owner(0) {};
178 };
179
180 // Provides iterator
181 class pkgCache::PrvIterator
182 {
183 Provides *Prv;
184 enum {PrvVer, PrvPkg} Type;
185 pkgCache *Owner;
186
187 void _dummy();
188
189 public:
190
191 // Iteration
192 void operator ++(int) {if (Prv != Owner->ProvideP) Prv = Owner->ProvideP +
193 (Type == PrvVer?Prv->NextPkgProv:Prv->NextProvides);};
194 inline void operator ++() {operator ++(0);};
195 inline bool end() const {return Prv == Owner->ProvideP?true:false;};
196
197 // Comparison
198 inline bool operator ==(const PrvIterator &B) const {return Prv == B.Prv;};
199 inline bool operator !=(const PrvIterator &B) const {return Prv != B.Prv;};
200
201 // Accessors
202 inline Provides *operator ->() {return Prv;};
203 inline Provides const *operator ->() const {return Prv;};
204 inline Provides &operator *() {return *Prv;};
205 inline Provides const &operator *() const {return *Prv;};
206 inline operator Provides *() {return Prv == Owner->ProvideP?0:Prv;};
207 inline operator Provides const *() const {return Prv == Owner->ProvideP?0:Prv;};
208 inline const char *Name() const {return Owner->StrP + Owner->PkgP[Prv->ParentPkg].Name;};
209 inline const char *ProvideVersion() const {return Prv->ProvideVersion == 0?0:Owner->StrP + Prv->ProvideVersion;};
210 inline PkgIterator ParentPkg() {return PkgIterator(*Owner,Owner->PkgP + Prv->ParentPkg);};
211 inline VerIterator OwnerVer() {return VerIterator(*Owner,Owner->VerP + Prv->Version);};
212 inline PkgIterator OwnerPkg() {return PkgIterator(*Owner,Owner->PkgP + Owner->VerP[Prv->Version].ParentPkg);};
213
214 inline PrvIterator(pkgCache &Owner,Provides *Trg,Version *) :
215 Prv(Trg), Type(PrvVer), Owner(&Owner)
216 {
217 if (Prv == 0)
218 Prv = Owner.ProvideP;
219 };
220 inline PrvIterator(pkgCache &Owner,Provides *Trg,Package *) :
221 Prv(Trg), Type(PrvPkg), Owner(&Owner)
222 {
223 if (Prv == 0)
224 Prv = Owner.ProvideP;
225 };
226 };
227
228 // Package file
229 class pkgCache::PkgFileIterator
230 {
231 pkgCache *Owner;
232 PackageFile *File;
233
234 public:
235
236 // Iteration
237 void operator ++(int) {if (File!= Owner->PkgFileP) File = Owner->PkgFileP + File->NextFile;};
238 inline void operator ++() {operator ++(0);};
239 inline bool end() const {return File == Owner->PkgFileP?true:false;};
240
241 // Comparison
242 inline bool operator ==(const PkgFileIterator &B) const {return File == B.File;};
243 inline bool operator !=(const PkgFileIterator &B) const {return File != B.File;};
244
245 // Accessors
246 inline PackageFile *operator ->() {return File;};
247 inline PackageFile const *operator ->() const {return File;};
248 inline PackageFile const &operator *() const {return *File;};
249 inline operator PackageFile *() {return File == Owner->PkgFileP?0:File;};
250 inline operator PackageFile const *() const {return File == Owner->PkgFileP?0:File;};
251
252 inline const char *FileName() const {return File->FileName == 0?0:Owner->StrP + File->FileName;};
253 inline const char *Version() const {return File->Version == 0?0:Owner->StrP + File->Version;};
254 inline const char *Distribution() const {return File->Distribution == 0?0:Owner->StrP + File->Distribution;};
255
256 bool IsOk();
257
258 // Constructors
259 inline PkgFileIterator(pkgCache &Owner) : Owner(&Owner), File(Owner.PkgFileP + Owner.Head().FileList) {};
260 inline PkgFileIterator(pkgCache &Owner,PackageFile *Trg) : Owner(&Owner), File(Trg) {};
261 };
262
263 // Inlined Begin functions cant be in the class because of order problems
264 inline pkgCache::VerIterator pkgCache::PkgIterator::VersionList() const
265 {return VerIterator(*Owner,Owner->VerP + Pkg->VersionList);};
266 inline pkgCache::VerIterator pkgCache::PkgIterator::CurrentVer() const
267 {return VerIterator(*Owner,Owner->VerP + Pkg->CurrentVer);};
268 inline pkgCache::VerIterator pkgCache::PkgIterator::TargetVer() const
269 {return VerIterator(*Owner,Owner->VerP + Pkg->TargetVer);};
270 inline pkgCache::DepIterator pkgCache::PkgIterator::RevDependsList() const
271 {return DepIterator(*Owner,Owner->DepP + Pkg->RevDepends,Pkg);};
272 inline pkgCache::PrvIterator pkgCache::PkgIterator::ProvidesList() const
273 {return PrvIterator(*Owner,Owner->ProvideP + Pkg->ProvidesList,Pkg);};
274 inline pkgCache::PrvIterator pkgCache::VerIterator::ProvidesList() const
275 {return PrvIterator(Owner,Owner.ProvideP + Ver->ProvidesList,Ver);};
276 inline pkgCache::DepIterator pkgCache::VerIterator::DependsList() const
277 {return DepIterator(Owner,Owner.DepP + Ver->DependsList,Ver);};
278 inline pkgCache::PkgFileIterator pkgCache::VerIterator::File() const
279 {return PkgFileIterator(Owner,Owner.PkgFileP + Ver->File);};
280
281 #endif