cacheiterators.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: cacheiterators.h,v 1.8 1998/11/14 07:20:08 jgg Exp $
  4. /* ######################################################################
  5. Cache Iterators - Iterators for navigating the cache structure
  6. The iterators all provides ++,==,!=,->,* and end for their type.
  7. The end function can be used to tell if the list has been fully
  8. traversed.
  9. Unlike STL iterators these contain helper functions to access the data
  10. that is being iterated over. This is because the data structures can't
  11. be formed in a manner that is intuitive to use and also mmapable.
  12. For each variable in the target structure that would need a translation
  13. to be accessed correctly a translating function of the same name is
  14. present in the iterator. If applicable the translating function will
  15. return an iterator.
  16. The DepIterator can iterate over two lists, a list of 'version depends'
  17. or a list of 'package reverse depends'. The type is determined by the
  18. structure passed to the constructor, which should be the structure
  19. that has the depends pointer as a member. The provide iterator has the
  20. same system.
  21. This header is not user includable, please use apt-pkg/pkgcache.h
  22. ##################################################################### */
  23. /*}}}*/
  24. // Header section: pkglib
  25. #ifndef PKGLIB_CACHEITERATORS_H
  26. #define PKGLIB_CACHEITERATORS_H
  27. #ifdef __GNUG__
  28. #pragma interface "apt-pkg/cacheiterators.h"
  29. #endif
  30. // Package Iterator
  31. class pkgCache::PkgIterator
  32. {
  33. Package *Pkg;
  34. pkgCache *Owner;
  35. long HashIndex;
  36. public:
  37. enum OkState {NeedsNothing,NeedsUnpack,NeedsConfigure};
  38. // Iteration
  39. void operator ++(int);
  40. inline void operator ++() {operator ++(0);};
  41. inline bool end() const {return Owner == 0 || Pkg == Owner->PkgP?true:false;};
  42. // Comparison
  43. inline bool operator ==(const PkgIterator &B) const {return Pkg == B.Pkg;};
  44. inline bool operator !=(const PkgIterator &B) const {return Pkg != B.Pkg;};
  45. // Accessors
  46. inline Package *operator ->() {return Pkg;};
  47. inline Package const *operator ->() const {return Pkg;};
  48. inline Package const &operator *() const {return *Pkg;};
  49. inline operator Package *() {return Pkg == Owner->PkgP?0:Pkg;};
  50. inline operator Package const *() const {return Pkg == Owner->PkgP?0:Pkg;};
  51. inline const char *Name() const {return Pkg->Name == 0?0:Owner->StrP + Pkg->Name;};
  52. inline const char *Section() const {return Pkg->Section == 0?0:Owner->StrP + Pkg->Section;};
  53. inline const char *TargetDist() const {return Pkg->TargetDist == 0?0:Owner->StrP + Pkg->TargetDist;};
  54. inline VerIterator VersionList() const;
  55. inline VerIterator TargetVer() const;
  56. inline VerIterator CurrentVer() const;
  57. inline DepIterator RevDependsList() const;
  58. inline PrvIterator ProvidesList() const;
  59. inline unsigned long Index() const {return Pkg - Owner->PkgP;};
  60. OkState State() const;
  61. // Constructors
  62. inline PkgIterator(pkgCache &Owner) : Owner(&Owner), HashIndex(-1)
  63. {
  64. Pkg = Owner.PkgP;
  65. operator ++(0);
  66. };
  67. inline PkgIterator(pkgCache &Owner,Package *Trg) : Pkg(Trg), Owner(&Owner),
  68. HashIndex(0)
  69. {
  70. if (Pkg == 0)
  71. Pkg = Owner.PkgP;
  72. };
  73. inline PkgIterator() : Pkg(0), Owner(0), HashIndex(0) {};
  74. };
  75. // Version Iterator
  76. class pkgCache::VerIterator
  77. {
  78. Version *Ver;
  79. pkgCache &Owner;
  80. void _dummy();
  81. public:
  82. // Iteration
  83. void operator ++(int) {if (Ver != Owner.VerP) Ver = Owner.VerP + Ver->NextVer;};
  84. inline void operator ++() {operator ++(0);};
  85. inline bool end() const {return Ver == Owner.VerP?true:false;};
  86. inline void operator =(const VerIterator &B) {Ver = B.Ver;};
  87. // Comparison
  88. inline bool operator ==(const VerIterator &B) const {return Ver == B.Ver;};
  89. inline bool operator !=(const VerIterator &B) const {return Ver != B.Ver;};
  90. int CompareVer(const VerIterator &B) const;
  91. // Accessors
  92. inline Version *operator ->() {return Ver;};
  93. inline Version const *operator ->() const {return Ver;};
  94. inline Version &operator *() {return *Ver;};
  95. inline Version const &operator *() const {return *Ver;};
  96. inline operator Version *() {return Ver == Owner.VerP?0:Ver;};
  97. inline operator Version const *() const {return Ver == Owner.VerP?0:Ver;};
  98. inline const char *VerStr() const {return Ver->VerStr == 0?0:Owner.StrP + Ver->VerStr;};
  99. inline const char *Section() const {return Ver->Section == 0?0:Owner.StrP + Ver->Section;};
  100. inline PkgIterator ParentPkg() const {return PkgIterator(Owner,Owner.PkgP + Ver->ParentPkg);};
  101. inline DepIterator DependsList() const;
  102. inline PrvIterator ProvidesList() const;
  103. inline VerFileIterator FileList() const;
  104. inline unsigned long Index() const {return Ver - Owner.VerP;};
  105. bool Downloadable() const;
  106. inline VerIterator(pkgCache &Owner,Version *Trg = 0) : Ver(Trg), Owner(Owner)
  107. {
  108. if (Ver == 0)
  109. Ver = Owner.VerP;
  110. };
  111. };
  112. // Dependency iterator
  113. class pkgCache::DepIterator
  114. {
  115. Dependency *Dep;
  116. enum {DepVer, DepRev} Type;
  117. pkgCache *Owner;
  118. void _dummy();
  119. public:
  120. // Iteration
  121. void operator ++(int) {if (Dep != Owner->DepP) Dep = Owner->DepP +
  122. (Type == DepVer?Dep->NextDepends:Dep->NextRevDepends);};
  123. inline void operator ++() {operator ++(0);};
  124. inline bool end() const {return Owner == 0 || Dep == Owner->DepP?true:false;};
  125. // Comparison
  126. inline bool operator ==(const DepIterator &B) const {return Dep == B.Dep;};
  127. inline bool operator !=(const DepIterator &B) const {return Dep != B.Dep;};
  128. // Accessors
  129. inline Dependency *operator ->() {return Dep;};
  130. inline Dependency const *operator ->() const {return Dep;};
  131. inline Dependency &operator *() {return *Dep;};
  132. inline Dependency const &operator *() const {return *Dep;};
  133. inline operator Dependency *() {return Dep == Owner->DepP?0:Dep;};
  134. inline operator Dependency const *() const {return Dep == Owner->DepP?0:Dep;};
  135. inline const char *TargetVer() const {return Dep->Version == 0?0:Owner->StrP + Dep->Version;};
  136. inline PkgIterator TargetPkg() {return PkgIterator(*Owner,Owner->PkgP + Dep->Package);};
  137. inline PkgIterator SmartTargetPkg() {PkgIterator R(*Owner);SmartTargetPkg(R);return R;};
  138. inline VerIterator ParentVer() {return VerIterator(*Owner,Owner->VerP + Dep->ParentVer);};
  139. inline PkgIterator ParentPkg() {return PkgIterator(*Owner,Owner->PkgP + Owner->VerP[Dep->ParentVer].ParentPkg);};
  140. inline bool Reverse() {return Type == DepRev;};
  141. inline unsigned long Index() const {return Dep - Owner->DepP;};
  142. bool IsCritical();
  143. void GlobOr(DepIterator &Start,DepIterator &End);
  144. Version **AllTargets();
  145. bool SmartTargetPkg(PkgIterator &Result);
  146. const char *CompType();
  147. const char *DepType();
  148. inline DepIterator(pkgCache &Owner,Dependency *Trg,Version * = 0) :
  149. Dep(Trg), Type(DepVer), Owner(&Owner)
  150. {
  151. if (Dep == 0)
  152. Dep = Owner.DepP;
  153. };
  154. inline DepIterator(pkgCache &Owner,Dependency *Trg,Package *) :
  155. Dep(Trg), Type(DepRev), Owner(&Owner)
  156. {
  157. if (Dep == 0)
  158. Dep = Owner.DepP;
  159. };
  160. inline DepIterator() : Dep(0), Type(DepVer), Owner(0) {};
  161. };
  162. // Provides iterator
  163. class pkgCache::PrvIterator
  164. {
  165. Provides *Prv;
  166. enum {PrvVer, PrvPkg} Type;
  167. pkgCache *Owner;
  168. void _dummy();
  169. public:
  170. // Iteration
  171. void operator ++(int) {if (Prv != Owner->ProvideP) Prv = Owner->ProvideP +
  172. (Type == PrvVer?Prv->NextPkgProv:Prv->NextProvides);};
  173. inline void operator ++() {operator ++(0);};
  174. inline bool end() const {return Prv == Owner->ProvideP?true:false;};
  175. // Comparison
  176. inline bool operator ==(const PrvIterator &B) const {return Prv == B.Prv;};
  177. inline bool operator !=(const PrvIterator &B) const {return Prv != B.Prv;};
  178. // Accessors
  179. inline Provides *operator ->() {return Prv;};
  180. inline Provides const *operator ->() const {return Prv;};
  181. inline Provides &operator *() {return *Prv;};
  182. inline Provides const &operator *() const {return *Prv;};
  183. inline operator Provides *() {return Prv == Owner->ProvideP?0:Prv;};
  184. inline operator Provides const *() const {return Prv == Owner->ProvideP?0:Prv;};
  185. inline const char *Name() const {return Owner->StrP + Owner->PkgP[Prv->ParentPkg].Name;};
  186. inline const char *ProvideVersion() const {return Prv->ProvideVersion == 0?0:Owner->StrP + Prv->ProvideVersion;};
  187. inline PkgIterator ParentPkg() {return PkgIterator(*Owner,Owner->PkgP + Prv->ParentPkg);};
  188. inline VerIterator OwnerVer() {return VerIterator(*Owner,Owner->VerP + Prv->Version);};
  189. inline PkgIterator OwnerPkg() {return PkgIterator(*Owner,Owner->PkgP + Owner->VerP[Prv->Version].ParentPkg);};
  190. inline unsigned long Index() const {return Prv - Owner->ProvideP;};
  191. inline PrvIterator(pkgCache &Owner,Provides *Trg,Version *) :
  192. Prv(Trg), Type(PrvVer), Owner(&Owner)
  193. {
  194. if (Prv == 0)
  195. Prv = Owner.ProvideP;
  196. };
  197. inline PrvIterator(pkgCache &Owner,Provides *Trg,Package *) :
  198. Prv(Trg), Type(PrvPkg), Owner(&Owner)
  199. {
  200. if (Prv == 0)
  201. Prv = Owner.ProvideP;
  202. };
  203. };
  204. // Package file
  205. class pkgCache::PkgFileIterator
  206. {
  207. pkgCache *Owner;
  208. PackageFile *File;
  209. public:
  210. // Iteration
  211. void operator ++(int) {if (File!= Owner->PkgFileP) File = Owner->PkgFileP + File->NextFile;};
  212. inline void operator ++() {operator ++(0);};
  213. inline bool end() const {return File == Owner->PkgFileP?true:false;};
  214. // Comparison
  215. inline bool operator ==(const PkgFileIterator &B) const {return File == B.File;};
  216. inline bool operator !=(const PkgFileIterator &B) const {return File != B.File;};
  217. // Accessors
  218. inline PackageFile *operator ->() {return File;};
  219. inline PackageFile const *operator ->() const {return File;};
  220. inline PackageFile const &operator *() const {return *File;};
  221. inline operator PackageFile *() {return File == Owner->PkgFileP?0:File;};
  222. inline operator PackageFile const *() const {return File == Owner->PkgFileP?0:File;};
  223. inline const char *FileName() const {return File->FileName == 0?0:Owner->StrP + File->FileName;};
  224. inline const char *Version() const {return File->Version == 0?0:Owner->StrP + File->Version;};
  225. inline const char *Distribution() const {return File->Distribution == 0?0:Owner->StrP + File->Distribution;};
  226. inline unsigned long Index() const {return File - Owner->PkgFileP;};
  227. bool IsOk();
  228. // Constructors
  229. inline PkgFileIterator(pkgCache &Owner) : Owner(&Owner), File(Owner.PkgFileP + Owner.Head().FileList) {};
  230. inline PkgFileIterator(pkgCache &Owner,PackageFile *Trg) : Owner(&Owner), File(Trg) {};
  231. };
  232. // Version File
  233. class pkgCache::VerFileIterator
  234. {
  235. pkgCache *Owner;
  236. VerFile *FileP;
  237. public:
  238. // Iteration
  239. void operator ++(int) {if (FileP != Owner->VerFileP) FileP = Owner->VerFileP + FileP->NextFile;};
  240. inline void operator ++() {operator ++(0);};
  241. inline bool end() const {return FileP == Owner->VerFileP?true:false;};
  242. // Comparison
  243. inline bool operator ==(const VerFileIterator &B) const {return FileP == B.FileP;};
  244. inline bool operator !=(const VerFileIterator &B) const {return FileP != B.FileP;};
  245. // Accessors
  246. inline VerFile *operator ->() {return FileP;};
  247. inline VerFile const *operator ->() const {return FileP;};
  248. inline VerFile const &operator *() const {return *FileP;};
  249. inline operator VerFile *() {return FileP == Owner->VerFileP?0:FileP;};
  250. inline operator VerFile const *() const {return FileP == Owner->VerFileP?0:FileP;};
  251. inline PkgFileIterator File() const {return PkgFileIterator(*Owner,FileP->File + Owner->PkgFileP);};
  252. inline unsigned long Index() const {return FileP - Owner->VerFileP;};
  253. inline VerFileIterator(pkgCache &Owner,VerFile *Trg) : Owner(&Owner), FileP(Trg) {};
  254. };
  255. // Inlined Begin functions cant be in the class because of order problems
  256. inline pkgCache::VerIterator pkgCache::PkgIterator::VersionList() const
  257. {return VerIterator(*Owner,Owner->VerP + Pkg->VersionList);};
  258. inline pkgCache::VerIterator pkgCache::PkgIterator::CurrentVer() const
  259. {return VerIterator(*Owner,Owner->VerP + Pkg->CurrentVer);};
  260. inline pkgCache::VerIterator pkgCache::PkgIterator::TargetVer() const
  261. {return VerIterator(*Owner,Owner->VerP + Pkg->TargetVer);};
  262. inline pkgCache::DepIterator pkgCache::PkgIterator::RevDependsList() const
  263. {return DepIterator(*Owner,Owner->DepP + Pkg->RevDepends,Pkg);};
  264. inline pkgCache::PrvIterator pkgCache::PkgIterator::ProvidesList() const
  265. {return PrvIterator(*Owner,Owner->ProvideP + Pkg->ProvidesList,Pkg);};
  266. inline pkgCache::PrvIterator pkgCache::VerIterator::ProvidesList() const
  267. {return PrvIterator(Owner,Owner.ProvideP + Ver->ProvidesList,Ver);};
  268. inline pkgCache::DepIterator pkgCache::VerIterator::DependsList() const
  269. {return DepIterator(Owner,Owner.DepP + Ver->DependsList,Ver);};
  270. inline pkgCache::VerFileIterator pkgCache::VerIterator::FileList() const
  271. {return VerFileIterator(Owner,Owner.VerFileP + Ver->FileList);};
  272. #endif