cacheiterators.h 13 KB

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