cacheiterators.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. // Package Iterator /*{{{*/
  27. class pkgCache::PkgIterator
  28. {
  29. friend class pkgCache;
  30. Package *Pkg;
  31. pkgCache *Owner;
  32. long HashIndex;
  33. protected:
  34. // This constructor is the 'begin' constructor, never use it.
  35. inline PkgIterator(pkgCache &Owner) : Owner(&Owner), HashIndex(-1)
  36. {
  37. Pkg = Owner.PkgP;
  38. operator ++(0);
  39. };
  40. public:
  41. enum OkState {NeedsNothing,NeedsUnpack,NeedsConfigure};
  42. // Iteration
  43. void operator ++(int);
  44. inline void operator ++() {operator ++(0);};
  45. inline bool end() const {return Owner == 0 || Pkg == Owner->PkgP?true:false;};
  46. // Comparison
  47. inline bool operator ==(const PkgIterator &B) const {return Pkg == B.Pkg;};
  48. inline bool operator !=(const PkgIterator &B) const {return Pkg != B.Pkg;};
  49. // Accessors
  50. inline Package *operator ->() {return Pkg;};
  51. inline Package const *operator ->() const {return Pkg;};
  52. inline Package const &operator *() const {return *Pkg;};
  53. inline operator Package *() {return Pkg == Owner->PkgP?0:Pkg;};
  54. inline operator Package const *() const {return Pkg == Owner->PkgP?0:Pkg;};
  55. inline pkgCache *Cache() {return Owner;};
  56. inline const char *Name() const {return Pkg->Name == 0?0:Owner->StrP + Pkg->Name;};
  57. inline const char *Section() const {return Pkg->Section == 0?0:Owner->StrP + Pkg->Section;};
  58. inline bool Purge() const {return Pkg->CurrentState == pkgCache::State::Purge ||
  59. (Pkg->CurrentVer == 0 && Pkg->CurrentState == pkgCache::State::NotInstalled);};
  60. inline VerIterator VersionList() const;
  61. inline VerIterator CurrentVer() const;
  62. inline DepIterator RevDependsList() const;
  63. inline PrvIterator ProvidesList() const;
  64. inline unsigned long Index() const {return Pkg - Owner->PkgP;};
  65. OkState State() const;
  66. //Nice printable representation
  67. friend std::ostream& operator<<(std::ostream& out, pkgCache::PkgIterator Pkg);
  68. const char *CandVersion() const;
  69. const char *CurVersion() const;
  70. // Constructors
  71. inline PkgIterator(pkgCache &Owner,Package *Trg) : Pkg(Trg), Owner(&Owner),
  72. HashIndex(0)
  73. {
  74. if (Pkg == 0)
  75. Pkg = Owner.PkgP;
  76. };
  77. inline PkgIterator() : Pkg(0), Owner(0), HashIndex(0) {};
  78. };
  79. /*}}}*/
  80. // Version Iterator /*{{{*/
  81. class pkgCache::VerIterator
  82. {
  83. Version *Ver;
  84. pkgCache *Owner;
  85. void _dummy();
  86. public:
  87. // Iteration
  88. void operator ++(int) {if (Ver != Owner->VerP) Ver = Owner->VerP + Ver->NextVer;};
  89. inline void operator ++() {operator ++(0);};
  90. inline bool end() const {return Owner == 0 || (Ver == Owner->VerP?true:false);};
  91. inline void operator =(const VerIterator &B) {Ver = B.Ver; Owner = B.Owner;};
  92. // Comparison
  93. inline bool operator ==(const VerIterator &B) const {return Ver == B.Ver;};
  94. inline bool operator !=(const VerIterator &B) const {return Ver != B.Ver;};
  95. int CompareVer(const VerIterator &B) const;
  96. // Testing
  97. inline bool IsGood() const { return Ver && Owner && ! end();};
  98. // Accessors
  99. inline Version *operator ->() {return Ver;};
  100. inline Version const *operator ->() const {return Ver;};
  101. inline Version &operator *() {return *Ver;};
  102. inline Version const &operator *() const {return *Ver;};
  103. inline operator Version *() {return Ver == Owner->VerP?0:Ver;};
  104. inline operator Version const *() const {return Ver == Owner->VerP?0:Ver;};
  105. inline pkgCache *Cache() {return Owner;};
  106. inline const char *VerStr() const {return Ver->VerStr == 0?0:Owner->StrP + Ver->VerStr;};
  107. inline const char *Section() const {return Ver->Section == 0?0:Owner->StrP + Ver->Section;};
  108. inline const char *Arch() const {return Ver->Arch == 0?0:Owner->StrP + Ver->Arch;};
  109. inline PkgIterator ParentPkg() const {return PkgIterator(*Owner,Owner->PkgP + Ver->ParentPkg);};
  110. inline DescIterator DescriptionList() const;
  111. DescIterator TranslatedDescription() const;
  112. inline DepIterator DependsList() const;
  113. inline PrvIterator ProvidesList() const;
  114. inline VerFileIterator FileList() const;
  115. inline unsigned long Index() const {return Ver - Owner->VerP;};
  116. bool Downloadable() const;
  117. inline const char *PriorityType() {return Owner->Priority(Ver->Priority);};
  118. string RelStr();
  119. bool Automatic() const;
  120. VerFileIterator NewestFile() const;
  121. inline VerIterator() : Ver(0), Owner(0) {};
  122. inline VerIterator(pkgCache &Owner,Version *Trg = 0) : Ver(Trg),
  123. Owner(&Owner)
  124. {
  125. if (Ver == 0)
  126. Ver = Owner.VerP;
  127. };
  128. };
  129. /*}}}*/
  130. // Description Iterator /*{{{*/
  131. class pkgCache::DescIterator
  132. {
  133. Description *Desc;
  134. pkgCache *Owner;
  135. void _dummy();
  136. public:
  137. // Iteration
  138. void operator ++(int) {if (Desc != Owner->DescP) Desc = Owner->DescP + Desc->NextDesc;};
  139. inline void operator ++() {operator ++(0);};
  140. inline bool end() const {return Owner == 0 || Desc == Owner->DescP?true:false;};
  141. inline void operator =(const DescIterator &B) {Desc = B.Desc; Owner = B.Owner;};
  142. // Comparison
  143. inline bool operator ==(const DescIterator &B) const {return Desc == B.Desc;};
  144. inline bool operator !=(const DescIterator &B) const {return Desc != B.Desc;};
  145. int CompareDesc(const DescIterator &B) const;
  146. // Accessors
  147. inline Description *operator ->() {return Desc;};
  148. inline Description const *operator ->() const {return Desc;};
  149. inline Description &operator *() {return *Desc;};
  150. inline Description const &operator *() const {return *Desc;};
  151. inline operator Description *() {return Desc == Owner->DescP?0:Desc;};
  152. inline operator Description const *() const {return Desc == Owner->DescP?0:Desc;};
  153. inline pkgCache *Cache() {return Owner;};
  154. inline const char *LanguageCode() const {return Owner->StrP + Desc->language_code;};
  155. inline const char *md5() const {return Owner->StrP + Desc->md5sum;};
  156. inline DescFileIterator FileList() const;
  157. inline unsigned long Index() const {return Desc - Owner->DescP;};
  158. inline DescIterator() : Desc(0), Owner(0) {};
  159. inline DescIterator(pkgCache &Owner,Description *Trg = 0) : Desc(Trg),
  160. Owner(&Owner)
  161. {
  162. if (Desc == 0)
  163. Desc = Owner.DescP;
  164. };
  165. };
  166. /*}}}*/
  167. // Dependency iterator /*{{{*/
  168. class pkgCache::DepIterator
  169. {
  170. Dependency *Dep;
  171. enum {DepVer, DepRev} Type;
  172. pkgCache *Owner;
  173. void _dummy();
  174. public:
  175. // Iteration
  176. void operator ++(int) {if (Dep != Owner->DepP) Dep = Owner->DepP +
  177. (Type == DepVer?Dep->NextDepends:Dep->NextRevDepends);};
  178. inline void operator ++() {operator ++(0);};
  179. inline bool end() const {return Owner == 0 || Dep == Owner->DepP?true:false;};
  180. // Comparison
  181. inline bool operator ==(const DepIterator &B) const {return Dep == B.Dep;};
  182. inline bool operator !=(const DepIterator &B) const {return Dep != B.Dep;};
  183. // Accessors
  184. inline Dependency *operator ->() {return Dep;};
  185. inline Dependency const *operator ->() const {return Dep;};
  186. inline Dependency &operator *() {return *Dep;};
  187. inline Dependency const &operator *() const {return *Dep;};
  188. inline operator Dependency *() {return Dep == Owner->DepP?0:Dep;};
  189. inline operator Dependency const *() const {return Dep == Owner->DepP?0:Dep;};
  190. inline pkgCache *Cache() {return Owner;};
  191. inline const char *TargetVer() const {return Dep->Version == 0?0:Owner->StrP + Dep->Version;};
  192. inline PkgIterator TargetPkg() {return PkgIterator(*Owner,Owner->PkgP + Dep->Package);};
  193. inline PkgIterator SmartTargetPkg() {PkgIterator R(*Owner,0);SmartTargetPkg(R);return R;};
  194. inline VerIterator ParentVer() {return VerIterator(*Owner,Owner->VerP + Dep->ParentVer);};
  195. inline PkgIterator ParentPkg() {return PkgIterator(*Owner,Owner->PkgP + Owner->VerP[Dep->ParentVer].ParentPkg);};
  196. inline bool Reverse() {return Type == DepRev;};
  197. inline unsigned long Index() const {return Dep - Owner->DepP;};
  198. bool IsCritical();
  199. void GlobOr(DepIterator &Start,DepIterator &End);
  200. Version **AllTargets();
  201. bool SmartTargetPkg(PkgIterator &Result);
  202. inline const char *CompType() {return Owner->CompType(Dep->CompareOp);};
  203. inline const char *DepType() {return Owner->DepType(Dep->Type);};
  204. inline DepIterator(pkgCache &Owner,Dependency *Trg,Version * = 0) :
  205. Dep(Trg), Type(DepVer), Owner(&Owner)
  206. {
  207. if (Dep == 0)
  208. Dep = Owner.DepP;
  209. };
  210. inline DepIterator(pkgCache &Owner,Dependency *Trg,Package *) :
  211. Dep(Trg), Type(DepRev), Owner(&Owner)
  212. {
  213. if (Dep == 0)
  214. Dep = Owner.DepP;
  215. };
  216. inline DepIterator() : Dep(0), Type(DepVer), Owner(0) {};
  217. };
  218. /*}}}*/
  219. // Provides iterator /*{{{*/
  220. class pkgCache::PrvIterator
  221. {
  222. Provides *Prv;
  223. enum {PrvVer, PrvPkg} Type;
  224. pkgCache *Owner;
  225. void _dummy();
  226. public:
  227. // Iteration
  228. void operator ++(int) {if (Prv != Owner->ProvideP) Prv = Owner->ProvideP +
  229. (Type == PrvVer?Prv->NextPkgProv:Prv->NextProvides);};
  230. inline void operator ++() {operator ++(0);};
  231. inline bool end() const {return Owner == 0 || Prv == Owner->ProvideP?true:false;};
  232. // Comparison
  233. inline bool operator ==(const PrvIterator &B) const {return Prv == B.Prv;};
  234. inline bool operator !=(const PrvIterator &B) const {return Prv != B.Prv;};
  235. // Accessors
  236. inline Provides *operator ->() {return Prv;};
  237. inline Provides const *operator ->() const {return Prv;};
  238. inline Provides &operator *() {return *Prv;};
  239. inline Provides const &operator *() const {return *Prv;};
  240. inline operator Provides *() {return Prv == Owner->ProvideP?0:Prv;};
  241. inline operator Provides const *() const {return Prv == Owner->ProvideP?0:Prv;};
  242. inline pkgCache *Cache() {return Owner;};
  243. inline const char *Name() const {return Owner->StrP + Owner->PkgP[Prv->ParentPkg].Name;};
  244. inline const char *ProvideVersion() const {return Prv->ProvideVersion == 0?0:Owner->StrP + Prv->ProvideVersion;};
  245. inline PkgIterator ParentPkg() {return PkgIterator(*Owner,Owner->PkgP + Prv->ParentPkg);};
  246. inline VerIterator OwnerVer() {return VerIterator(*Owner,Owner->VerP + Prv->Version);};
  247. inline PkgIterator OwnerPkg() {return PkgIterator(*Owner,Owner->PkgP + Owner->VerP[Prv->Version].ParentPkg);};
  248. inline unsigned long Index() const {return Prv - Owner->ProvideP;};
  249. inline PrvIterator() : Prv(0), Type(PrvVer), Owner(0) {};
  250. inline PrvIterator(pkgCache &Owner,Provides *Trg,Version *) :
  251. Prv(Trg), Type(PrvVer), Owner(&Owner)
  252. {
  253. if (Prv == 0)
  254. Prv = Owner.ProvideP;
  255. };
  256. inline PrvIterator(pkgCache &Owner,Provides *Trg,Package *) :
  257. Prv(Trg), Type(PrvPkg), Owner(&Owner)
  258. {
  259. if (Prv == 0)
  260. Prv = Owner.ProvideP;
  261. };
  262. };
  263. /*}}}*/
  264. // Package file /*{{{*/
  265. class pkgCache::PkgFileIterator
  266. {
  267. pkgCache *Owner;
  268. PackageFile *File;
  269. public:
  270. // Iteration
  271. void operator ++(int) {if (File!= Owner->PkgFileP) File = Owner->PkgFileP + File->NextFile;};
  272. inline void operator ++() {operator ++(0);};
  273. inline bool end() const {return Owner == 0 || File == Owner->PkgFileP?true:false;};
  274. // Comparison
  275. inline bool operator ==(const PkgFileIterator &B) const {return File == B.File;};
  276. inline bool operator !=(const PkgFileIterator &B) const {return File != B.File;};
  277. // Accessors
  278. inline PackageFile *operator ->() {return File;};
  279. inline PackageFile const *operator ->() const {return File;};
  280. inline PackageFile const &operator *() const {return *File;};
  281. inline operator PackageFile *() {return File == Owner->PkgFileP?0:File;};
  282. inline operator PackageFile const *() const {return File == Owner->PkgFileP?0:File;};
  283. inline pkgCache *Cache() {return Owner;};
  284. inline const char *FileName() const {return File->FileName == 0?0:Owner->StrP + File->FileName;};
  285. inline const char *Archive() const {return File->Archive == 0?0:Owner->StrP + File->Archive;};
  286. inline const char *Component() const {return File->Component == 0?0:Owner->StrP + File->Component;};
  287. inline const char *Version() const {return File->Version == 0?0:Owner->StrP + File->Version;};
  288. inline const char *Origin() const {return File->Origin == 0?0:Owner->StrP + File->Origin;};
  289. inline const char *Codename() const {return File->Codename ==0?0:Owner->StrP + File->Codename;};
  290. inline const char *Label() const {return File->Label == 0?0:Owner->StrP + File->Label;};
  291. inline const char *Site() const {return File->Site == 0?0:Owner->StrP + File->Site;};
  292. inline const char *Architecture() const {return File->Architecture == 0?0:Owner->StrP + File->Architecture;};
  293. inline const char *IndexType() const {return File->IndexType == 0?0:Owner->StrP + File->IndexType;};
  294. inline unsigned long Index() const {return File - Owner->PkgFileP;};
  295. bool IsOk();
  296. string RelStr();
  297. // Constructors
  298. inline PkgFileIterator() : Owner(0), File(0) {};
  299. inline PkgFileIterator(pkgCache &Owner) : Owner(&Owner), File(Owner.PkgFileP) {};
  300. inline PkgFileIterator(pkgCache &Owner,PackageFile *Trg) : Owner(&Owner), File(Trg) {};
  301. };
  302. /*}}}*/
  303. // Version File /*{{{*/
  304. class pkgCache::VerFileIterator
  305. {
  306. pkgCache *Owner;
  307. VerFile *FileP;
  308. public:
  309. // Iteration
  310. void operator ++(int) {if (FileP != Owner->VerFileP) FileP = Owner->VerFileP + FileP->NextFile;};
  311. inline void operator ++() {operator ++(0);};
  312. inline bool end() const {return Owner == 0 || FileP == Owner->VerFileP?true:false;};
  313. // Comparison
  314. inline bool operator ==(const VerFileIterator &B) const {return FileP == B.FileP;};
  315. inline bool operator !=(const VerFileIterator &B) const {return FileP != B.FileP;};
  316. // Accessors
  317. inline VerFile *operator ->() {return FileP;};
  318. inline VerFile const *operator ->() const {return FileP;};
  319. inline VerFile const &operator *() const {return *FileP;};
  320. inline operator VerFile *() {return FileP == Owner->VerFileP?0:FileP;};
  321. inline operator VerFile const *() const {return FileP == Owner->VerFileP?0:FileP;};
  322. inline pkgCache *Cache() {return Owner;};
  323. inline PkgFileIterator File() const {return PkgFileIterator(*Owner,FileP->File + Owner->PkgFileP);};
  324. inline unsigned long Index() const {return FileP - Owner->VerFileP;};
  325. inline VerFileIterator() : Owner(0), FileP(0) {};
  326. inline VerFileIterator(pkgCache &Owner,VerFile *Trg) : Owner(&Owner), FileP(Trg) {};
  327. };
  328. /*}}}*/
  329. // Description File /*{{{*/
  330. class pkgCache::DescFileIterator
  331. {
  332. pkgCache *Owner;
  333. DescFile *FileP;
  334. public:
  335. // Iteration
  336. void operator ++(int) {if (FileP != Owner->DescFileP) FileP = Owner->DescFileP + FileP->NextFile;};
  337. inline void operator ++() {operator ++(0);};
  338. inline bool end() const {return Owner == 0 || FileP == Owner->DescFileP?true:false;};
  339. // Comparison
  340. inline bool operator ==(const DescFileIterator &B) const {return FileP == B.FileP;};
  341. inline bool operator !=(const DescFileIterator &B) const {return FileP != B.FileP;};
  342. // Accessors
  343. inline DescFile *operator ->() {return FileP;};
  344. inline DescFile const *operator ->() const {return FileP;};
  345. inline DescFile const &operator *() const {return *FileP;};
  346. inline operator DescFile *() {return FileP == Owner->DescFileP?0:FileP;};
  347. inline operator DescFile const *() const {return FileP == Owner->DescFileP?0:FileP;};
  348. inline pkgCache *Cache() {return Owner;};
  349. inline PkgFileIterator File() const {return PkgFileIterator(*Owner,FileP->File + Owner->PkgFileP);};
  350. inline unsigned long Index() const {return FileP - Owner->DescFileP;};
  351. inline DescFileIterator() : Owner(0), FileP(0) {};
  352. inline DescFileIterator(pkgCache &Owner,DescFile *Trg) : Owner(&Owner), FileP(Trg) {};
  353. };
  354. /*}}}*/
  355. // Inlined Begin functions cant be in the class because of order problems /*{{{*/
  356. inline pkgCache::VerIterator pkgCache::PkgIterator::VersionList() const
  357. {return VerIterator(*Owner,Owner->VerP + Pkg->VersionList);};
  358. inline pkgCache::VerIterator pkgCache::PkgIterator::CurrentVer() const
  359. {return VerIterator(*Owner,Owner->VerP + Pkg->CurrentVer);};
  360. inline pkgCache::DepIterator pkgCache::PkgIterator::RevDependsList() const
  361. {return DepIterator(*Owner,Owner->DepP + Pkg->RevDepends,Pkg);};
  362. inline pkgCache::PrvIterator pkgCache::PkgIterator::ProvidesList() const
  363. {return PrvIterator(*Owner,Owner->ProvideP + Pkg->ProvidesList,Pkg);};
  364. inline pkgCache::DescIterator pkgCache::VerIterator::DescriptionList() const
  365. {return DescIterator(*Owner,Owner->DescP + Ver->DescriptionList);};
  366. inline pkgCache::PrvIterator pkgCache::VerIterator::ProvidesList() const
  367. {return PrvIterator(*Owner,Owner->ProvideP + Ver->ProvidesList,Ver);};
  368. inline pkgCache::DepIterator pkgCache::VerIterator::DependsList() const
  369. {return DepIterator(*Owner,Owner->DepP + Ver->DependsList,Ver);};
  370. inline pkgCache::VerFileIterator pkgCache::VerIterator::FileList() const
  371. {return VerFileIterator(*Owner,Owner->VerFileP + Ver->FileList);};
  372. inline pkgCache::DescFileIterator pkgCache::DescIterator::FileList() const
  373. {return DescFileIterator(*Owner,Owner->DescFileP + Desc->FileList);};
  374. /*}}}*/
  375. #endif