cacheiterators.h 14 KB

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