cacheiterators.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. /* ######################################################################
  4. Cache Iterators - Iterators for navigating the cache structure
  5. The iterators all provides ++,==,!=,->,* and end for their type.
  6. The end function can be used to tell if the list has been fully
  7. traversed.
  8. Unlike STL iterators these contain helper functions to access the data
  9. that is being iterated over. This is because the data structures can't
  10. be formed in a manner that is intuitive to use and also mmapable.
  11. For each variable in the target structure that would need a translation
  12. to be accessed correctly a translating function of the same name is
  13. present in the iterator. If applicable the translating function will
  14. return an iterator.
  15. The DepIterator can iterate over two lists, a list of 'version depends'
  16. or a list of 'package reverse depends'. The type is determined by the
  17. structure passed to the constructor, which should be the structure
  18. that has the depends pointer as a member. The provide iterator has the
  19. same system.
  20. This header is not user includable, please use apt-pkg/pkgcache.h
  21. ##################################################################### */
  22. /*}}}*/
  23. #ifndef PKGLIB_CACHEITERATORS_H
  24. #define PKGLIB_CACHEITERATORS_H
  25. // abstract Iterator template /*{{{*/
  26. /* This template provides the very basic iterator methods we
  27. need to have for doing some walk-over-the-cache magic, */
  28. template<typename Str, typename Itr> class pkgCache::Iterator {
  29. __attribute__ ((deprecated)) void _dummy(); // FIXME: Who on earth uses this method ???
  30. protected:
  31. Str *S;
  32. pkgCache *Owner;
  33. /** \brief Returns the Pointer for this struct in the owner
  34. * The implementation of this method should be pretty short
  35. * as it will only return the Pointer into the mmap stored
  36. * in the owner but the name of this pointer is different for
  37. * each stucture and we want to abstract here at least for the
  38. * basic methods from the actual structure.
  39. * \return Pointer to the first structure of this type
  40. */
  41. virtual Str* OwnerPointer() const = 0;
  42. public:
  43. // Iteration
  44. virtual void operator ++(int) = 0;
  45. virtual void operator ++() = 0; // Should be {operator ++(0);};
  46. inline bool end() const {return Owner == 0 || S == OwnerPointer();};
  47. // Comparison
  48. inline bool operator ==(const Itr &B) const {return S == B.S;};
  49. inline bool operator !=(const Itr &B) const {return S != B.S;};
  50. // Accessors
  51. inline Str *operator ->() {return S;};
  52. inline Str const *operator ->() const {return S;};
  53. inline operator Str *() {return S == OwnerPointer() ? 0 : S;};
  54. inline operator Str const *() const {return S == OwnerPointer() ? 0 : S;};
  55. inline Str const &operator *() const {return *S;};
  56. inline pkgCache *Cache() {return Owner;};
  57. // Mixed stuff
  58. inline void operator =(const Itr &B) {S = B.S; Owner = B.Owner;};
  59. inline bool IsGood() const { return S && Owner && ! end();};
  60. inline unsigned long Index() const {return S - OwnerPointer();};
  61. // Constructors - look out for the variable assigning
  62. inline Iterator() : S(0), Owner(0) {};
  63. inline Iterator(pkgCache &Owner,Str *T = 0) : S(T), Owner(&Owner) {};
  64. };
  65. /*}}}*/
  66. // Package Iterator /*{{{*/
  67. class pkgCache::PkgIterator: public Iterator<Package, PkgIterator> {
  68. long HashIndex;
  69. protected:
  70. inline Package* OwnerPointer() const {
  71. return Owner->PkgP;
  72. };
  73. public:
  74. // This constructor is the 'begin' constructor, never use it.
  75. inline PkgIterator(pkgCache &Owner) : Iterator<Package, PkgIterator>(Owner), HashIndex(-1) {
  76. S = OwnerPointer();
  77. operator ++(0);
  78. };
  79. virtual void operator ++(int);
  80. virtual void operator ++() {operator ++(0);};
  81. enum OkState {NeedsNothing,NeedsUnpack,NeedsConfigure};
  82. // Accessors
  83. inline const char *Name() const {return S->Name == 0?0:Owner->StrP + S->Name;};
  84. inline const char *Section() const {return S->Section == 0?0:Owner->StrP + S->Section;};
  85. inline bool Purge() const {return S->CurrentState == pkgCache::State::Purge ||
  86. (S->CurrentVer == 0 && S->CurrentState == pkgCache::State::NotInstalled);};
  87. inline VerIterator VersionList() const;
  88. inline VerIterator CurrentVer() const;
  89. inline DepIterator RevDependsList() const;
  90. inline PrvIterator ProvidesList() const;
  91. OkState State() const;
  92. const char *CandVersion() const;
  93. const char *CurVersion() const;
  94. //Nice printable representation
  95. friend std::ostream& operator <<(std::ostream& out, PkgIterator i);
  96. // Constructors
  97. inline PkgIterator(pkgCache &Owner,Package *Trg) : Iterator<Package, PkgIterator>(Owner, Trg), HashIndex(0) {
  98. if (S == 0)
  99. S = OwnerPointer();
  100. };
  101. inline PkgIterator() : Iterator<Package, PkgIterator>(), HashIndex(0) {};
  102. };
  103. /*}}}*/
  104. // Version Iterator /*{{{*/
  105. class pkgCache::VerIterator : public Iterator<Version, VerIterator> {
  106. protected:
  107. inline Version* OwnerPointer() const {
  108. return Owner->VerP;
  109. };
  110. public:
  111. // Iteration
  112. void operator ++(int) {if (S != Owner->VerP) S = Owner->VerP + S->NextVer;};
  113. inline void operator ++() {operator ++(0);};
  114. // Comparison
  115. int CompareVer(const VerIterator &B) const;
  116. // Accessors
  117. inline const char *VerStr() const {return S->VerStr == 0?0:Owner->StrP + S->VerStr;};
  118. inline const char *Section() const {return S->Section == 0?0:Owner->StrP + S->Section;};
  119. inline const char *Arch() const {return S->Arch == 0?0:Owner->StrP + S->Arch;};
  120. inline PkgIterator ParentPkg() const {return PkgIterator(*Owner,Owner->PkgP + S->ParentPkg);};
  121. inline DescIterator DescriptionList() const;
  122. DescIterator TranslatedDescription() const;
  123. inline DepIterator DependsList() const;
  124. inline PrvIterator ProvidesList() const;
  125. inline VerFileIterator FileList() const;
  126. bool Downloadable() const;
  127. inline const char *PriorityType() {return Owner->Priority(S->Priority);};
  128. string RelStr();
  129. bool Automatic() const;
  130. VerFileIterator NewestFile() const;
  131. inline VerIterator(pkgCache &Owner,Version *Trg = 0) : Iterator<Version, VerIterator>(Owner, Trg) {
  132. if (S == 0)
  133. S = OwnerPointer();
  134. };
  135. inline VerIterator() : Iterator<Version, VerIterator>() {};
  136. };
  137. /*}}}*/
  138. // Description Iterator /*{{{*/
  139. class pkgCache::DescIterator : public Iterator<Description, DescIterator> {
  140. protected:
  141. inline Description* OwnerPointer() const {
  142. return Owner->DescP;
  143. };
  144. public:
  145. // Iteration
  146. void operator ++(int) {if (S != Owner->DescP) S = Owner->DescP + S->NextDesc;};
  147. inline void operator ++() {operator ++(0);};
  148. // Comparison
  149. int CompareDesc(const DescIterator &B) const;
  150. // Accessors
  151. inline const char *LanguageCode() const {return Owner->StrP + S->language_code;};
  152. inline const char *md5() const {return Owner->StrP + S->md5sum;};
  153. inline DescFileIterator FileList() const;
  154. inline DescIterator() : Iterator<Description, DescIterator>() {};
  155. inline DescIterator(pkgCache &Owner,Description *Trg = 0) : Iterator<Description, DescIterator>(Owner, Trg) {
  156. if (S == 0)
  157. S = Owner.DescP;
  158. };
  159. };
  160. /*}}}*/
  161. // Dependency iterator /*{{{*/
  162. class pkgCache::DepIterator : public Iterator<Dependency, DepIterator> {
  163. enum {DepVer, DepRev} Type;
  164. protected:
  165. inline Dependency* OwnerPointer() const {
  166. return Owner->DepP;
  167. };
  168. public:
  169. // Iteration
  170. void operator ++(int) {if (S != Owner->DepP) S = Owner->DepP +
  171. (Type == DepVer ? S->NextDepends : S->NextRevDepends);};
  172. inline void operator ++() {operator ++(0);};
  173. // Accessors
  174. inline const char *TargetVer() const {return S->Version == 0?0:Owner->StrP + S->Version;};
  175. inline PkgIterator TargetPkg() {return PkgIterator(*Owner,Owner->PkgP + S->Package);};
  176. inline PkgIterator SmartTargetPkg() {PkgIterator R(*Owner,0);SmartTargetPkg(R);return R;};
  177. inline VerIterator ParentVer() {return VerIterator(*Owner,Owner->VerP + S->ParentVer);};
  178. inline PkgIterator ParentPkg() {return PkgIterator(*Owner,Owner->PkgP + Owner->VerP[S->ParentVer].ParentPkg);};
  179. inline bool Reverse() {return Type == DepRev;};
  180. bool IsCritical();
  181. void GlobOr(DepIterator &Start,DepIterator &End);
  182. Version **AllTargets();
  183. bool SmartTargetPkg(PkgIterator &Result);
  184. inline const char *CompType() {return Owner->CompType(S->CompareOp);};
  185. inline const char *DepType() {return Owner->DepType(S->Type);};
  186. inline DepIterator(pkgCache &Owner, Dependency *Trg, Version* = 0) :
  187. Iterator<Dependency, DepIterator>(Owner, Trg), Type(DepVer) {
  188. if (S == 0)
  189. S = Owner.DepP;
  190. };
  191. inline DepIterator(pkgCache &Owner, Dependency *Trg, Package*) :
  192. Iterator<Dependency, DepIterator>(Owner, Trg), Type(DepRev) {
  193. if (S == 0)
  194. S = Owner.DepP;
  195. };
  196. inline DepIterator() : Iterator<Dependency, DepIterator>(), Type(DepVer) {};
  197. };
  198. /*}}}*/
  199. // Provides iterator /*{{{*/
  200. class pkgCache::PrvIterator : public Iterator<Provides, PrvIterator> {
  201. enum {PrvVer, PrvPkg} Type;
  202. protected:
  203. inline Provides* OwnerPointer() const {
  204. return Owner->ProvideP;
  205. };
  206. public:
  207. // Iteration
  208. void operator ++(int) {if (S != Owner->ProvideP) S = Owner->ProvideP +
  209. (Type == PrvVer?S->NextPkgProv:S->NextProvides);};
  210. inline void operator ++() {operator ++(0);};
  211. // Accessors
  212. inline const char *Name() const {return Owner->StrP + Owner->PkgP[S->ParentPkg].Name;};
  213. inline const char *ProvideVersion() const {return S->ProvideVersion == 0?0:Owner->StrP + S->ProvideVersion;};
  214. inline PkgIterator ParentPkg() {return PkgIterator(*Owner,Owner->PkgP + S->ParentPkg);};
  215. inline VerIterator OwnerVer() {return VerIterator(*Owner,Owner->VerP + S->Version);};
  216. inline PkgIterator OwnerPkg() {return PkgIterator(*Owner,Owner->PkgP + Owner->VerP[S->Version].ParentPkg);};
  217. inline PrvIterator() : Iterator<Provides, PrvIterator>(), Type(PrvVer) {};
  218. inline PrvIterator(pkgCache &Owner, Provides *Trg, Version*) :
  219. Iterator<Provides, PrvIterator>(Owner, Trg), Type(PrvVer) {
  220. if (S == 0)
  221. S = Owner.ProvideP;
  222. };
  223. inline PrvIterator(pkgCache &Owner, Provides *Trg, Package*) :
  224. Iterator<Provides, PrvIterator>(Owner, Trg), Type(PrvPkg) {
  225. if (S == 0)
  226. S = Owner.ProvideP;
  227. };
  228. };
  229. /*}}}*/
  230. // Package file /*{{{*/
  231. class pkgCache::PkgFileIterator : public Iterator<PackageFile, PkgFileIterator> {
  232. protected:
  233. inline PackageFile* OwnerPointer() const {
  234. return Owner->PkgFileP;
  235. };
  236. public:
  237. // Iteration
  238. void operator ++(int) {if (S != Owner->PkgFileP) S = Owner->PkgFileP + S->NextFile;};
  239. inline void operator ++() {operator ++(0);};
  240. // Accessors
  241. inline const char *FileName() const {return S->FileName == 0?0:Owner->StrP + S->FileName;};
  242. inline const char *Archive() const {return S->Archive == 0?0:Owner->StrP + S->Archive;};
  243. inline const char *Component() const {return S->Component == 0?0:Owner->StrP + S->Component;};
  244. inline const char *Version() const {return S->Version == 0?0:Owner->StrP + S->Version;};
  245. inline const char *Origin() const {return S->Origin == 0?0:Owner->StrP + S->Origin;};
  246. inline const char *Codename() const {return S->Codename ==0?0:Owner->StrP + S->Codename;};
  247. inline const char *Label() const {return S->Label == 0?0:Owner->StrP + S->Label;};
  248. inline const char *Site() const {return S->Site == 0?0:Owner->StrP + S->Site;};
  249. inline const char *Architecture() const {return S->Architecture == 0?0:Owner->StrP + S->Architecture;};
  250. inline const char *IndexType() const {return S->IndexType == 0?0:Owner->StrP + S->IndexType;};
  251. bool IsOk();
  252. string RelStr();
  253. // Constructors
  254. inline PkgFileIterator() : Iterator<PackageFile, PkgFileIterator>() {};
  255. inline PkgFileIterator(pkgCache &Owner,PackageFile *Trg = 0) : Iterator<PackageFile, PkgFileIterator>(Owner, Trg) {};
  256. };
  257. /*}}}*/
  258. // Version File /*{{{*/
  259. class pkgCache::VerFileIterator : public pkgCache::Iterator<VerFile, VerFileIterator> {
  260. protected:
  261. inline VerFile* OwnerPointer() const {
  262. return Owner->VerFileP;
  263. };
  264. public:
  265. // Iteration
  266. void operator ++(int) {if (S != Owner->VerFileP) S = Owner->VerFileP + S->NextFile;};
  267. inline void operator ++() {operator ++(0);};
  268. // Accessors
  269. inline PkgFileIterator File() const {return PkgFileIterator(*Owner,S->File + Owner->PkgFileP);};
  270. inline VerFileIterator() : Iterator<VerFile, VerFileIterator>() {};
  271. inline VerFileIterator(pkgCache &Owner,VerFile *Trg) : Iterator<VerFile, VerFileIterator>(Owner, Trg) {};
  272. };
  273. /*}}}*/
  274. // Description File /*{{{*/
  275. class pkgCache::DescFileIterator : public Iterator<DescFile, DescFileIterator> {
  276. protected:
  277. inline DescFile* OwnerPointer() const {
  278. return Owner->DescFileP;
  279. };
  280. public:
  281. // Iteration
  282. void operator ++(int) {if (S != Owner->DescFileP) S = Owner->DescFileP + S->NextFile;};
  283. inline void operator ++() {operator ++(0);};
  284. // Accessors
  285. inline PkgFileIterator File() const {return PkgFileIterator(*Owner,S->File + Owner->PkgFileP);};
  286. inline DescFileIterator() : Iterator<DescFile, DescFileIterator>() {};
  287. inline DescFileIterator(pkgCache &Owner,DescFile *Trg) : Iterator<DescFile, DescFileIterator>(Owner, Trg) {};
  288. };
  289. /*}}}*/
  290. // Inlined Begin functions cant be in the class because of order problems /*{{{*/
  291. inline pkgCache::VerIterator pkgCache::PkgIterator::VersionList() const
  292. {return VerIterator(*Owner,Owner->VerP + S->VersionList);};
  293. inline pkgCache::VerIterator pkgCache::PkgIterator::CurrentVer() const
  294. {return VerIterator(*Owner,Owner->VerP + S->CurrentVer);};
  295. inline pkgCache::DepIterator pkgCache::PkgIterator::RevDependsList() const
  296. {return DepIterator(*Owner,Owner->DepP + S->RevDepends,S);};
  297. inline pkgCache::PrvIterator pkgCache::PkgIterator::ProvidesList() const
  298. {return PrvIterator(*Owner,Owner->ProvideP + S->ProvidesList,S);};
  299. inline pkgCache::DescIterator pkgCache::VerIterator::DescriptionList() const
  300. {return DescIterator(*Owner,Owner->DescP + S->DescriptionList);};
  301. inline pkgCache::PrvIterator pkgCache::VerIterator::ProvidesList() const
  302. {return PrvIterator(*Owner,Owner->ProvideP + S->ProvidesList,S);};
  303. inline pkgCache::DepIterator pkgCache::VerIterator::DependsList() const
  304. {return DepIterator(*Owner,Owner->DepP + S->DependsList,S);};
  305. inline pkgCache::VerFileIterator pkgCache::VerIterator::FileList() const
  306. {return VerFileIterator(*Owner,Owner->VerFileP + S->FileList);};
  307. inline pkgCache::DescFileIterator pkgCache::DescIterator::FileList() const
  308. {return DescFileIterator(*Owner,Owner->DescFileP + S->FileList);};
  309. /*}}}*/
  310. #endif