cacheiterators.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. #include<iterator>
  26. // abstract Iterator template /*{{{*/
  27. /* This template provides the very basic iterator methods we
  28. need to have for doing some walk-over-the-cache magic */
  29. template<typename Str, typename Itr> class pkgCache::Iterator :
  30. public std::iterator<std::forward_iterator_tag, Str> {
  31. protected:
  32. Str *S;
  33. pkgCache *Owner;
  34. /** \brief Returns the Pointer for this struct in the owner
  35. * The implementation of this method should be pretty short
  36. * as it will only return the Pointer into the mmap stored
  37. * in the owner but the name of this pointer is different for
  38. * each stucture and we want to abstract here at least for the
  39. * basic methods from the actual structure.
  40. * \return Pointer to the first structure of this type
  41. */
  42. virtual Str* OwnerPointer() const = 0;
  43. public:
  44. // Iteration
  45. virtual void operator ++(int) = 0;
  46. virtual void operator ++() = 0; // Should be {operator ++(0);};
  47. inline bool end() const {return Owner == 0 || S == OwnerPointer();};
  48. // Comparison
  49. inline bool operator ==(const Itr &B) const {return S == B.S;};
  50. inline bool operator !=(const Itr &B) const {return S != B.S;};
  51. // Accessors
  52. inline Str *operator ->() {return S;};
  53. inline Str const *operator ->() const {return S;};
  54. inline operator Str *() {return S == OwnerPointer() ? 0 : S;};
  55. inline operator Str const *() const {return S == OwnerPointer() ? 0 : S;};
  56. inline Str &operator *() {return *S;};
  57. inline Str const &operator *() const {return *S;};
  58. inline pkgCache *Cache() const {return Owner;};
  59. // Mixed stuff
  60. inline void operator =(const Itr &B) {S = B.S; Owner = B.Owner;};
  61. inline bool IsGood() const { return S && Owner && ! end();};
  62. inline unsigned long Index() const {return S - OwnerPointer();};
  63. // Constructors - look out for the variable assigning
  64. inline Iterator() : S(0), Owner(0) {};
  65. inline Iterator(pkgCache &Owner,Str *T = 0) : S(T), Owner(&Owner) {};
  66. };
  67. /*}}}*/
  68. // Group Iterator /*{{{*/
  69. /* Packages with the same name are collected in a Group so someone only
  70. interest in package names can iterate easily over the names, so the
  71. different architectures can be treated as of the "same" package
  72. (apt internally treat them as totally different packages) */
  73. class pkgCache::GrpIterator: public Iterator<Group, GrpIterator> {
  74. long HashIndex;
  75. protected:
  76. inline Group* OwnerPointer() const {
  77. return Owner->GrpP;
  78. };
  79. public:
  80. // This constructor is the 'begin' constructor, never use it.
  81. inline GrpIterator(pkgCache &Owner) : Iterator<Group, GrpIterator>(Owner), HashIndex(-1) {
  82. S = OwnerPointer();
  83. operator ++(0);
  84. };
  85. virtual void operator ++(int);
  86. virtual void operator ++() {operator ++(0);};
  87. inline const char *Name() const {return S->Name == 0?0:Owner->StrP + S->Name;};
  88. inline PkgIterator PackageList() const;
  89. PkgIterator FindPkg(string Arch = "any");
  90. /** \brief find the package with the "best" architecture
  91. The best architecture is either the "native" or the first
  92. in the list of Architectures which is not an end-Pointer */
  93. PkgIterator FindPreferredPkg();
  94. PkgIterator NextPkg(PkgIterator const &Pkg);
  95. // Constructors
  96. inline GrpIterator(pkgCache &Owner, Group *Trg) : Iterator<Group, GrpIterator>(Owner, Trg), HashIndex(0) {
  97. if (S == 0)
  98. S = OwnerPointer();
  99. };
  100. inline GrpIterator() : Iterator<Group, GrpIterator>(), HashIndex(0) {};
  101. };
  102. /*}}}*/
  103. // Package Iterator /*{{{*/
  104. class pkgCache::PkgIterator: public Iterator<Package, PkgIterator> {
  105. long HashIndex;
  106. protected:
  107. inline Package* OwnerPointer() const {
  108. return Owner->PkgP;
  109. };
  110. public:
  111. // This constructor is the 'begin' constructor, never use it.
  112. inline PkgIterator(pkgCache &Owner) : Iterator<Package, PkgIterator>(Owner), HashIndex(-1) {
  113. S = OwnerPointer();
  114. operator ++(0);
  115. };
  116. virtual void operator ++(int);
  117. virtual void operator ++() {operator ++(0);};
  118. enum OkState {NeedsNothing,NeedsUnpack,NeedsConfigure};
  119. // Accessors
  120. inline const char *Name() const {return S->Name == 0?0:Owner->StrP + S->Name;};
  121. inline const char *Section() const {return S->Section == 0?0:Owner->StrP + S->Section;};
  122. inline bool Purge() const {return S->CurrentState == pkgCache::State::Purge ||
  123. (S->CurrentVer == 0 && S->CurrentState == pkgCache::State::NotInstalled);};
  124. inline const char *Arch() const {return S->Arch == 0?0:Owner->StrP + S->Arch;};
  125. inline GrpIterator Group() const { return GrpIterator(*Owner, Owner->GrpP + S->Group);};
  126. inline VerIterator VersionList() const;
  127. inline VerIterator CurrentVer() const;
  128. inline DepIterator RevDependsList() const;
  129. inline PrvIterator ProvidesList() const;
  130. OkState State() const;
  131. const char *CandVersion() const;
  132. const char *CurVersion() const;
  133. //Nice printable representation
  134. friend std::ostream& operator <<(std::ostream& out, PkgIterator i);
  135. std::string FullName(bool const &Pretty = false) const;
  136. // Constructors
  137. inline PkgIterator(pkgCache &Owner,Package *Trg) : Iterator<Package, PkgIterator>(Owner, Trg), HashIndex(0) {
  138. if (S == 0)
  139. S = OwnerPointer();
  140. };
  141. inline PkgIterator() : Iterator<Package, PkgIterator>(), HashIndex(0) {};
  142. };
  143. /*}}}*/
  144. // Version Iterator /*{{{*/
  145. class pkgCache::VerIterator : public Iterator<Version, VerIterator> {
  146. protected:
  147. inline Version* OwnerPointer() const {
  148. return Owner->VerP;
  149. };
  150. public:
  151. // Iteration
  152. void operator ++(int) {if (S != Owner->VerP) S = Owner->VerP + S->NextVer;};
  153. inline void operator ++() {operator ++(0);};
  154. // Comparison
  155. int CompareVer(const VerIterator &B) const;
  156. // Accessors
  157. inline const char *VerStr() const {return S->VerStr == 0?0:Owner->StrP + S->VerStr;};
  158. inline const char *Section() const {return S->Section == 0?0:Owner->StrP + S->Section;};
  159. inline const char *Arch() const {
  160. if(S->MultiArch == pkgCache::Version::All)
  161. return "all";
  162. return S->ParentPkg == 0?0:Owner->StrP + ParentPkg()->Arch;
  163. };
  164. inline const char *Arch(bool const pseudo) const {
  165. if(pseudo == false)
  166. return Arch();
  167. return S->ParentPkg == 0?0:Owner->StrP + ParentPkg()->Arch;
  168. };
  169. inline PkgIterator ParentPkg() const {return PkgIterator(*Owner,Owner->PkgP + S->ParentPkg);};
  170. inline DescIterator DescriptionList() const;
  171. DescIterator TranslatedDescription() const;
  172. inline DepIterator DependsList() const;
  173. inline PrvIterator ProvidesList() const;
  174. inline VerFileIterator FileList() const;
  175. bool Downloadable() const;
  176. inline const char *PriorityType() const {return Owner->Priority(S->Priority);};
  177. string RelStr() const;
  178. bool Automatic() const;
  179. bool Pseudo() const;
  180. VerFileIterator NewestFile() const;
  181. inline VerIterator(pkgCache &Owner,Version *Trg = 0) : Iterator<Version, VerIterator>(Owner, Trg) {
  182. if (S == 0)
  183. S = OwnerPointer();
  184. };
  185. inline VerIterator() : Iterator<Version, VerIterator>() {};
  186. };
  187. /*}}}*/
  188. // Description Iterator /*{{{*/
  189. class pkgCache::DescIterator : public Iterator<Description, DescIterator> {
  190. protected:
  191. inline Description* OwnerPointer() const {
  192. return Owner->DescP;
  193. };
  194. public:
  195. // Iteration
  196. void operator ++(int) {if (S != Owner->DescP) S = Owner->DescP + S->NextDesc;};
  197. inline void operator ++() {operator ++(0);};
  198. // Comparison
  199. int CompareDesc(const DescIterator &B) const;
  200. // Accessors
  201. inline const char *LanguageCode() const {return Owner->StrP + S->language_code;};
  202. inline const char *md5() const {return Owner->StrP + S->md5sum;};
  203. inline DescFileIterator FileList() const;
  204. inline DescIterator() : Iterator<Description, DescIterator>() {};
  205. inline DescIterator(pkgCache &Owner,Description *Trg = 0) : Iterator<Description, DescIterator>(Owner, Trg) {
  206. if (S == 0)
  207. S = Owner.DescP;
  208. };
  209. };
  210. /*}}}*/
  211. // Dependency iterator /*{{{*/
  212. class pkgCache::DepIterator : public Iterator<Dependency, DepIterator> {
  213. enum {DepVer, DepRev} Type;
  214. protected:
  215. inline Dependency* OwnerPointer() const {
  216. return Owner->DepP;
  217. };
  218. public:
  219. // Iteration
  220. void operator ++(int) {if (S != Owner->DepP) S = Owner->DepP +
  221. (Type == DepVer ? S->NextDepends : S->NextRevDepends);};
  222. inline void operator ++() {operator ++(0);};
  223. // Accessors
  224. inline const char *TargetVer() const {return S->Version == 0?0:Owner->StrP + S->Version;};
  225. inline PkgIterator TargetPkg() {return PkgIterator(*Owner,Owner->PkgP + S->Package);};
  226. inline PkgIterator SmartTargetPkg() {PkgIterator R(*Owner,0);SmartTargetPkg(R);return R;};
  227. inline VerIterator ParentVer() {return VerIterator(*Owner,Owner->VerP + S->ParentVer);};
  228. inline PkgIterator ParentPkg() {return PkgIterator(*Owner,Owner->PkgP + Owner->VerP[S->ParentVer].ParentPkg);};
  229. inline bool Reverse() {return Type == DepRev;};
  230. bool IsCritical();
  231. void GlobOr(DepIterator &Start,DepIterator &End);
  232. Version **AllTargets();
  233. bool SmartTargetPkg(PkgIterator &Result);
  234. inline const char *CompType() {return Owner->CompType(S->CompareOp);};
  235. inline const char *DepType() {return Owner->DepType(S->Type);};
  236. inline DepIterator(pkgCache &Owner, Dependency *Trg, Version* = 0) :
  237. Iterator<Dependency, DepIterator>(Owner, Trg), Type(DepVer) {
  238. if (S == 0)
  239. S = Owner.DepP;
  240. };
  241. inline DepIterator(pkgCache &Owner, Dependency *Trg, Package*) :
  242. Iterator<Dependency, DepIterator>(Owner, Trg), Type(DepRev) {
  243. if (S == 0)
  244. S = Owner.DepP;
  245. };
  246. inline DepIterator() : Iterator<Dependency, DepIterator>(), Type(DepVer) {};
  247. };
  248. /*}}}*/
  249. // Provides iterator /*{{{*/
  250. class pkgCache::PrvIterator : public Iterator<Provides, PrvIterator> {
  251. enum {PrvVer, PrvPkg} Type;
  252. protected:
  253. inline Provides* OwnerPointer() const {
  254. return Owner->ProvideP;
  255. };
  256. public:
  257. // Iteration
  258. void operator ++(int) {if (S != Owner->ProvideP) S = Owner->ProvideP +
  259. (Type == PrvVer?S->NextPkgProv:S->NextProvides);};
  260. inline void operator ++() {operator ++(0);};
  261. // Accessors
  262. inline const char *Name() const {return Owner->StrP + Owner->PkgP[S->ParentPkg].Name;};
  263. inline const char *ProvideVersion() const {return S->ProvideVersion == 0?0:Owner->StrP + S->ProvideVersion;};
  264. inline PkgIterator ParentPkg() {return PkgIterator(*Owner,Owner->PkgP + S->ParentPkg);};
  265. inline VerIterator OwnerVer() {return VerIterator(*Owner,Owner->VerP + S->Version);};
  266. inline PkgIterator OwnerPkg() {return PkgIterator(*Owner,Owner->PkgP + Owner->VerP[S->Version].ParentPkg);};
  267. inline PrvIterator() : Iterator<Provides, PrvIterator>(), Type(PrvVer) {};
  268. inline PrvIterator(pkgCache &Owner, Provides *Trg, Version*) :
  269. Iterator<Provides, PrvIterator>(Owner, Trg), Type(PrvVer) {
  270. if (S == 0)
  271. S = Owner.ProvideP;
  272. };
  273. inline PrvIterator(pkgCache &Owner, Provides *Trg, Package*) :
  274. Iterator<Provides, PrvIterator>(Owner, Trg), Type(PrvPkg) {
  275. if (S == 0)
  276. S = Owner.ProvideP;
  277. };
  278. };
  279. /*}}}*/
  280. // Package file /*{{{*/
  281. class pkgCache::PkgFileIterator : public Iterator<PackageFile, PkgFileIterator> {
  282. protected:
  283. inline PackageFile* OwnerPointer() const {
  284. return Owner->PkgFileP;
  285. };
  286. public:
  287. // Iteration
  288. void operator ++(int) {if (S != Owner->PkgFileP) S = Owner->PkgFileP + S->NextFile;};
  289. inline void operator ++() {operator ++(0);};
  290. // Accessors
  291. inline const char *FileName() const {return S->FileName == 0?0:Owner->StrP + S->FileName;};
  292. inline const char *Archive() const {return S->Archive == 0?0:Owner->StrP + S->Archive;};
  293. inline const char *Component() const {return S->Component == 0?0:Owner->StrP + S->Component;};
  294. inline const char *Version() const {return S->Version == 0?0:Owner->StrP + S->Version;};
  295. inline const char *Origin() const {return S->Origin == 0?0:Owner->StrP + S->Origin;};
  296. inline const char *Codename() const {return S->Codename ==0?0:Owner->StrP + S->Codename;};
  297. inline const char *Label() const {return S->Label == 0?0:Owner->StrP + S->Label;};
  298. inline const char *Site() const {return S->Site == 0?0:Owner->StrP + S->Site;};
  299. inline const char *Architecture() const {return S->Architecture == 0?0:Owner->StrP + S->Architecture;};
  300. inline const char *IndexType() const {return S->IndexType == 0?0:Owner->StrP + S->IndexType;};
  301. bool IsOk();
  302. string RelStr();
  303. // Constructors
  304. inline PkgFileIterator() : Iterator<PackageFile, PkgFileIterator>() {};
  305. inline PkgFileIterator(pkgCache &Owner) : Iterator<PackageFile, PkgFileIterator>(Owner, Owner.PkgFileP) {};
  306. inline PkgFileIterator(pkgCache &Owner,PackageFile *Trg) : Iterator<PackageFile, PkgFileIterator>(Owner, Trg) {};
  307. };
  308. /*}}}*/
  309. // Version File /*{{{*/
  310. class pkgCache::VerFileIterator : public pkgCache::Iterator<VerFile, VerFileIterator> {
  311. protected:
  312. inline VerFile* OwnerPointer() const {
  313. return Owner->VerFileP;
  314. };
  315. public:
  316. // Iteration
  317. void operator ++(int) {if (S != Owner->VerFileP) S = Owner->VerFileP + S->NextFile;};
  318. inline void operator ++() {operator ++(0);};
  319. // Accessors
  320. inline PkgFileIterator File() const {return PkgFileIterator(*Owner,S->File + Owner->PkgFileP);};
  321. inline VerFileIterator() : Iterator<VerFile, VerFileIterator>() {};
  322. inline VerFileIterator(pkgCache &Owner,VerFile *Trg) : Iterator<VerFile, VerFileIterator>(Owner, Trg) {};
  323. };
  324. /*}}}*/
  325. // Description File /*{{{*/
  326. class pkgCache::DescFileIterator : public Iterator<DescFile, DescFileIterator> {
  327. protected:
  328. inline DescFile* OwnerPointer() const {
  329. return Owner->DescFileP;
  330. };
  331. public:
  332. // Iteration
  333. void operator ++(int) {if (S != Owner->DescFileP) S = Owner->DescFileP + S->NextFile;};
  334. inline void operator ++() {operator ++(0);};
  335. // Accessors
  336. inline PkgFileIterator File() const {return PkgFileIterator(*Owner,S->File + Owner->PkgFileP);};
  337. inline DescFileIterator() : Iterator<DescFile, DescFileIterator>() {};
  338. inline DescFileIterator(pkgCache &Owner,DescFile *Trg) : Iterator<DescFile, DescFileIterator>(Owner, Trg) {};
  339. };
  340. /*}}}*/
  341. // Inlined Begin functions cant be in the class because of order problems /*{{{*/
  342. inline pkgCache::PkgIterator pkgCache::GrpIterator::PackageList() const
  343. {return PkgIterator(*Owner,Owner->PkgP + S->FirstPackage);};
  344. inline pkgCache::VerIterator pkgCache::PkgIterator::VersionList() const
  345. {return VerIterator(*Owner,Owner->VerP + S->VersionList);};
  346. inline pkgCache::VerIterator pkgCache::PkgIterator::CurrentVer() const
  347. {return VerIterator(*Owner,Owner->VerP + S->CurrentVer);};
  348. inline pkgCache::DepIterator pkgCache::PkgIterator::RevDependsList() const
  349. {return DepIterator(*Owner,Owner->DepP + S->RevDepends,S);};
  350. inline pkgCache::PrvIterator pkgCache::PkgIterator::ProvidesList() const
  351. {return PrvIterator(*Owner,Owner->ProvideP + S->ProvidesList,S);};
  352. inline pkgCache::DescIterator pkgCache::VerIterator::DescriptionList() const
  353. {return DescIterator(*Owner,Owner->DescP + S->DescriptionList);};
  354. inline pkgCache::PrvIterator pkgCache::VerIterator::ProvidesList() const
  355. {return PrvIterator(*Owner,Owner->ProvideP + S->ProvidesList,S);};
  356. inline pkgCache::DepIterator pkgCache::VerIterator::DependsList() const
  357. {return DepIterator(*Owner,Owner->DepP + S->DependsList,S);};
  358. inline pkgCache::VerFileIterator pkgCache::VerIterator::FileList() const
  359. {return VerFileIterator(*Owner,Owner->VerFileP + S->FileList);};
  360. inline pkgCache::DescFileIterator pkgCache::DescIterator::FileList() const
  361. {return DescFileIterator(*Owner,Owner->DescFileP + S->FileList);};
  362. /*}}}*/
  363. #endif