cacheset.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. /** \file cacheset.h
  4. Wrappers around std::set to have set::iterators which behave
  5. similar to the Iterators of the cache structures.
  6. Provides also a few helper methods which work with these sets */
  7. /*}}}*/
  8. #ifndef APT_CACHESET_H
  9. #define APT_CACHESET_H
  10. // Include Files /*{{{*/
  11. #include <fstream>
  12. #include <map>
  13. #include <set>
  14. #include <list>
  15. #include <string>
  16. #include <iterator>
  17. #include <stddef.h>
  18. #include <apt-pkg/error.h>
  19. #include <apt-pkg/pkgcache.h>
  20. #include <apt-pkg/cacheiterators.h>
  21. #ifndef APT_8_CLEANER_HEADERS
  22. #include <apt-pkg/cachefile.h>
  23. #endif
  24. #ifndef APT_10_CLEANER_HEADERS
  25. #include <iostream>
  26. #endif
  27. /*}}}*/
  28. class pkgCacheFile;
  29. namespace APT {
  30. class PackageContainerInterface;
  31. class VersionContainerInterface;
  32. class CacheSetHelper { /*{{{*/
  33. /** \class APT::CacheSetHelper
  34. Simple base class with a lot of virtual methods which can be overridden
  35. to alter the behavior or the output of the CacheSets.
  36. This helper is passed around by the static methods in the CacheSets and
  37. used every time they hit an error condition or something could be
  38. printed out.
  39. */
  40. public: /*{{{*/
  41. CacheSetHelper(bool const ShowError = true,
  42. GlobalError::MsgType ErrorType = GlobalError::ERROR) :
  43. ShowError(ShowError), ErrorType(ErrorType) {}
  44. virtual ~CacheSetHelper() {}
  45. virtual void showTaskSelection(pkgCache::PkgIterator const &pkg, std::string const &pattern);
  46. virtual void showRegExSelection(pkgCache::PkgIterator const &pkg, std::string const &pattern);
  47. #if (APT_PKG_MAJOR >= 4 && APT_PKG_MINOR >= 13)
  48. virtual void showFnmatchSelection(pkgCache::PkgIterator const &pkg, std::string const &pattern);
  49. #endif
  50. virtual void showSelectedVersion(pkgCache::PkgIterator const &Pkg, pkgCache::VerIterator const Ver,
  51. std::string const &ver, bool const verIsRel);
  52. virtual void canNotFindTask(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern);
  53. virtual void canNotFindRegEx(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern);
  54. #if (APT_PKG_MAJOR >= 4 && APT_PKG_MINOR >= 13)
  55. virtual void canNotFindFnmatch(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern);
  56. #endif
  57. virtual void canNotFindPackage(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string const &str);
  58. virtual void canNotFindAllVer(VersionContainerInterface * const vci, pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg);
  59. virtual void canNotFindInstCandVer(VersionContainerInterface * const vci, pkgCacheFile &Cache,
  60. pkgCache::PkgIterator const &Pkg);
  61. virtual void canNotFindCandInstVer(VersionContainerInterface * const vci,
  62. pkgCacheFile &Cache,
  63. pkgCache::PkgIterator const &Pkg);
  64. virtual pkgCache::PkgIterator canNotFindPkgName(pkgCacheFile &Cache, std::string const &str);
  65. virtual pkgCache::VerIterator canNotFindNewestVer(pkgCacheFile &Cache,
  66. pkgCache::PkgIterator const &Pkg);
  67. virtual pkgCache::VerIterator canNotFindCandidateVer(pkgCacheFile &Cache,
  68. pkgCache::PkgIterator const &Pkg);
  69. virtual pkgCache::VerIterator canNotFindInstalledVer(pkgCacheFile &Cache,
  70. pkgCache::PkgIterator const &Pkg);
  71. bool showErrors() const { return ShowError; }
  72. bool showErrors(bool const newValue) { if (ShowError == newValue) return ShowError; else return ((ShowError = newValue) == false); }
  73. GlobalError::MsgType errorType() const { return ErrorType; }
  74. GlobalError::MsgType errorType(GlobalError::MsgType const &newValue)
  75. {
  76. if (ErrorType == newValue) return ErrorType;
  77. else {
  78. GlobalError::MsgType const &oldValue = ErrorType;
  79. ErrorType = newValue;
  80. return oldValue;
  81. }
  82. }
  83. /*}}}*/
  84. protected:
  85. bool ShowError;
  86. GlobalError::MsgType ErrorType;
  87. }; /*}}}*/
  88. class PackageContainerInterface { /*{{{*/
  89. /** \class PackageContainerInterface
  90. * Interface ensuring that all operations can be executed on the yet to
  91. * define concrete PackageContainer - access to all methods is possible,
  92. * but in general the wrappers provided by the PackageContainer template
  93. * are nicer to use.
  94. * This class mostly protects use from the need to write all implementation
  95. * of the methods working on containers in the template */
  96. public:
  97. class const_iterator { /*{{{*/
  98. public:
  99. virtual pkgCache::PkgIterator getPkg() const = 0;
  100. operator pkgCache::PkgIterator(void) const { return getPkg(); }
  101. inline const char *Name() const {return getPkg().Name(); }
  102. inline std::string FullName(bool const Pretty) const { return getPkg().FullName(Pretty); }
  103. inline std::string FullName() const { return getPkg().FullName(); }
  104. APT_DEPRECATED inline const char *Section() const {
  105. #if __GNUC__ >= 4
  106. #pragma GCC diagnostic push
  107. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  108. #endif
  109. return getPkg().Section();
  110. #if __GNUC__ >= 4
  111. #pragma GCC diagnostic pop
  112. #endif
  113. }
  114. inline bool Purge() const {return getPkg().Purge(); }
  115. inline const char *Arch() const {return getPkg().Arch(); }
  116. inline pkgCache::GrpIterator Group() const { return getPkg().Group(); }
  117. inline pkgCache::VerIterator VersionList() const { return getPkg().VersionList(); }
  118. inline pkgCache::VerIterator CurrentVer() const { return getPkg().CurrentVer(); }
  119. inline pkgCache::DepIterator RevDependsList() const { return getPkg().RevDependsList(); }
  120. inline pkgCache::PrvIterator ProvidesList() const { return getPkg().ProvidesList(); }
  121. inline pkgCache::PkgIterator::OkState State() const { return getPkg().State(); }
  122. inline const char *CandVersion() const { return getPkg().CandVersion(); }
  123. inline const char *CurVersion() const { return getPkg().CurVersion(); }
  124. inline pkgCache *Cache() const { return getPkg().Cache(); }
  125. inline unsigned long Index() const {return getPkg().Index();}
  126. // we have only valid iterators here
  127. inline bool end() const { return false; }
  128. inline pkgCache::Package const * operator->() const {return &*getPkg();}
  129. };
  130. /*}}}*/
  131. virtual bool insert(pkgCache::PkgIterator const &P) = 0;
  132. virtual bool empty() const = 0;
  133. virtual void clear() = 0;
  134. enum Constructor { UNKNOWN, REGEX, TASK, FNMATCH };
  135. virtual void setConstructor(Constructor const &con) = 0;
  136. virtual Constructor getConstructor() const = 0;
  137. static bool FromTask(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper);
  138. static bool FromRegEx(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper);
  139. static pkgCache::PkgIterator FromName(pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper);
  140. static bool FromFnmatch(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper);
  141. static bool FromGroup(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper);
  142. static bool FromString(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper);
  143. static bool FromCommandLine(PackageContainerInterface * const pci, pkgCacheFile &Cache, const char **cmdline, CacheSetHelper &helper);
  144. struct Modifier {
  145. enum Position { NONE, PREFIX, POSTFIX };
  146. unsigned short ID;
  147. const char * const Alias;
  148. Position Pos;
  149. Modifier (unsigned short const &id, const char * const alias, Position const &pos) : ID(id), Alias(alias), Pos(pos) {}
  150. };
  151. static bool FromModifierCommandLine(unsigned short &modID, PackageContainerInterface * const pci,
  152. pkgCacheFile &Cache, const char * cmdline,
  153. std::list<Modifier> const &mods, CacheSetHelper &helper);
  154. };
  155. /*}}}*/
  156. template<class Container> class PackageContainer : public PackageContainerInterface {/*{{{*/
  157. /** \class APT::PackageContainer
  158. Simple wrapper around a container class like std::set to provide a similar
  159. interface to a set of packages as to the complete set of all packages in the
  160. pkgCache. */
  161. Container _cont;
  162. public: /*{{{*/
  163. /** \brief smell like a pkgCache::PkgIterator */
  164. class const_iterator : public PackageContainerInterface::const_iterator,/*{{{*/
  165. public std::iterator<std::forward_iterator_tag, typename Container::const_iterator> {
  166. typename Container::const_iterator _iter;
  167. public:
  168. const_iterator(typename Container::const_iterator i) : _iter(i) {}
  169. pkgCache::PkgIterator getPkg(void) const { return *_iter; }
  170. inline pkgCache::PkgIterator operator*(void) const { return *_iter; }
  171. operator typename Container::const_iterator(void) const { return _iter; }
  172. inline const_iterator& operator++() { ++_iter; return *this; }
  173. inline const_iterator operator++(int) { const_iterator tmp(*this); operator++(); return tmp; }
  174. inline bool operator!=(const_iterator const &i) const { return _iter != i._iter; }
  175. inline bool operator==(const_iterator const &i) const { return _iter == i._iter; }
  176. friend std::ostream& operator<<(std::ostream& out, const_iterator i) { return operator<<(out, *i); }
  177. };
  178. class iterator : public PackageContainerInterface::const_iterator,
  179. public std::iterator<std::forward_iterator_tag, typename Container::iterator> {
  180. typename Container::iterator _iter;
  181. public:
  182. iterator(typename Container::iterator i) : _iter(i) {}
  183. pkgCache::PkgIterator getPkg(void) const { return *_iter; }
  184. inline pkgCache::PkgIterator operator*(void) const { return *_iter; }
  185. operator typename Container::iterator(void) const { return _iter; }
  186. operator typename PackageContainer<Container>::const_iterator() { return typename PackageContainer<Container>::const_iterator(_iter); }
  187. inline iterator& operator++() { ++_iter; return *this; }
  188. inline iterator operator++(int) { iterator tmp(*this); operator++(); return tmp; }
  189. inline bool operator!=(iterator const &i) const { return _iter != i._iter; }
  190. inline bool operator==(iterator const &i) const { return _iter == i._iter; }
  191. inline iterator& operator=(iterator const &i) { _iter = i._iter; return *this; }
  192. inline iterator& operator=(typename Container::iterator const &i) { _iter = i; return *this; }
  193. friend std::ostream& operator<<(std::ostream& out, iterator i) { return operator<<(out, *i); }
  194. };
  195. /*}}}*/
  196. bool insert(pkgCache::PkgIterator const &P) { if (P.end() == true) return false; _cont.insert(P); return true; }
  197. template<class Cont> void insert(PackageContainer<Cont> const &pkgcont) { _cont.insert((typename Cont::const_iterator)pkgcont.begin(), (typename Cont::const_iterator)pkgcont.end()); }
  198. void insert(const_iterator begin, const_iterator end) { _cont.insert(begin, end); }
  199. bool empty() const { return _cont.empty(); }
  200. void clear() { return _cont.clear(); }
  201. //FIXME: on ABI break, replace the first with the second without bool
  202. void erase(iterator position) { _cont.erase((typename Container::iterator)position); }
  203. iterator& erase(iterator &position, bool) { return position = _cont.erase((typename Container::iterator)position); }
  204. size_t erase(const pkgCache::PkgIterator x) { return _cont.erase(x); }
  205. void erase(iterator first, iterator last) { _cont.erase(first, last); }
  206. size_t size() const { return _cont.size(); }
  207. const_iterator begin() const { return const_iterator(_cont.begin()); }
  208. const_iterator end() const { return const_iterator(_cont.end()); }
  209. iterator begin() { return iterator(_cont.begin()); }
  210. iterator end() { return iterator(_cont.end()); }
  211. const_iterator find(pkgCache::PkgIterator const &P) const { return const_iterator(_cont.find(P)); }
  212. void setConstructor(Constructor const &by) { ConstructedBy = by; }
  213. Constructor getConstructor() const { return ConstructedBy; }
  214. PackageContainer() : ConstructedBy(UNKNOWN) {}
  215. PackageContainer(Constructor const &by) : ConstructedBy(by) {}
  216. /** \brief returns all packages in the cache who belong to the given task
  217. A simple helper responsible for search for all members of a task
  218. in the cache. Optional it prints a a notice about the
  219. packages chosen cause of the given task.
  220. \param Cache the packages are in
  221. \param pattern name of the task
  222. \param helper responsible for error and message handling */
  223. static PackageContainer FromTask(pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper) {
  224. PackageContainer cont(TASK);
  225. PackageContainerInterface::FromTask(&cont, Cache, pattern, helper);
  226. return cont;
  227. }
  228. static PackageContainer FromTask(pkgCacheFile &Cache, std::string const &pattern) {
  229. CacheSetHelper helper;
  230. return FromTask(Cache, pattern, helper);
  231. }
  232. /** \brief returns all packages in the cache whose name matchs a given pattern
  233. A simple helper responsible for executing a regular expression on all
  234. package names in the cache. Optional it prints a a notice about the
  235. packages chosen cause of the given package.
  236. \param Cache the packages are in
  237. \param pattern regular expression for package names
  238. \param helper responsible for error and message handling */
  239. static PackageContainer FromRegEx(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) {
  240. PackageContainer cont(REGEX);
  241. PackageContainerInterface::FromRegEx(&cont, Cache, pattern, helper);
  242. return cont;
  243. }
  244. static PackageContainer FromRegEx(pkgCacheFile &Cache, std::string const &pattern) {
  245. CacheSetHelper helper;
  246. return FromRegEx(Cache, pattern, helper);
  247. }
  248. static PackageContainer FromFnmatch(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) {
  249. PackageContainer cont(FNMATCH);
  250. PackageContainerInterface::FromFnmatch(&cont, Cache, pattern, helper);
  251. return cont;
  252. }
  253. static PackageContainer FromFnMatch(pkgCacheFile &Cache, std::string const &pattern) {
  254. CacheSetHelper helper;
  255. return FromFnmatch(Cache, pattern, helper);
  256. }
  257. /** \brief returns a package specified by a string
  258. \param Cache the package is in
  259. \param pattern String the package name should be extracted from
  260. \param helper responsible for error and message handling */
  261. static pkgCache::PkgIterator FromName(pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper) {
  262. return PackageContainerInterface::FromName(Cache, pattern, helper);
  263. }
  264. static pkgCache::PkgIterator FromName(pkgCacheFile &Cache, std::string const &pattern) {
  265. CacheSetHelper helper;
  266. return PackageContainerInterface::FromName(Cache, pattern, helper);
  267. }
  268. /** \brief returns all packages specified by a string
  269. \param Cache the packages are in
  270. \param pattern String the package name(s) should be extracted from
  271. \param helper responsible for error and message handling */
  272. static PackageContainer FromString(pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper) {
  273. PackageContainer cont;
  274. PackageContainerInterface::FromString(&cont, Cache, pattern, helper);
  275. return cont;
  276. }
  277. static PackageContainer FromString(pkgCacheFile &Cache, std::string const &pattern) {
  278. CacheSetHelper helper;
  279. return FromString(Cache, pattern, helper);
  280. }
  281. /** \brief returns all packages specified on the commandline
  282. Get all package names from the commandline and executes regex's if needed.
  283. No special package command is supported, just plain names.
  284. \param Cache the packages are in
  285. \param cmdline Command line the package names should be extracted from
  286. \param helper responsible for error and message handling */
  287. static PackageContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline, CacheSetHelper &helper) {
  288. PackageContainer cont;
  289. PackageContainerInterface::FromCommandLine(&cont, Cache, cmdline, helper);
  290. return cont;
  291. }
  292. static PackageContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline) {
  293. CacheSetHelper helper;
  294. return FromCommandLine(Cache, cmdline, helper);
  295. }
  296. /** \brief group packages by a action modifiers
  297. At some point it is needed to get from the same commandline
  298. different package sets grouped by a modifier. Take
  299. apt-get install apt awesome-
  300. as an example.
  301. \param Cache the packages are in
  302. \param cmdline Command line the package names should be extracted from
  303. \param mods list of modifiers the method should accept
  304. \param fallback the default modifier group for a package
  305. \param helper responsible for error and message handling */
  306. static std::map<unsigned short, PackageContainer> GroupedFromCommandLine(
  307. pkgCacheFile &Cache,
  308. const char **cmdline,
  309. std::list<Modifier> const &mods,
  310. unsigned short const &fallback,
  311. CacheSetHelper &helper) {
  312. std::map<unsigned short, PackageContainer> pkgsets;
  313. for (const char **I = cmdline; *I != 0; ++I) {
  314. unsigned short modID = fallback;
  315. PackageContainer pkgset;
  316. PackageContainerInterface::FromModifierCommandLine(modID, &pkgset, Cache, *I, mods, helper);
  317. pkgsets[modID].insert(pkgset);
  318. }
  319. return pkgsets;
  320. }
  321. static std::map<unsigned short, PackageContainer> GroupedFromCommandLine(
  322. pkgCacheFile &Cache,
  323. const char **cmdline,
  324. std::list<Modifier> const &mods,
  325. unsigned short const &fallback) {
  326. CacheSetHelper helper;
  327. return GroupedFromCommandLine(Cache, cmdline,
  328. mods, fallback, helper);
  329. }
  330. /*}}}*/
  331. private: /*{{{*/
  332. Constructor ConstructedBy;
  333. /*}}}*/
  334. }; /*}}}*/
  335. template<> template<class Cont> void PackageContainer<std::list<pkgCache::PkgIterator> >::insert(PackageContainer<Cont> const &pkgcont) {
  336. for (typename PackageContainer<Cont>::const_iterator p = pkgcont.begin(); p != pkgcont.end(); ++p)
  337. _cont.push_back(*p);
  338. }
  339. // these two are 'inline' as otherwise the linker has problems with seeing these untemplated
  340. // specializations again and again - but we need to see them, so that library users can use them
  341. template<> inline bool PackageContainer<std::list<pkgCache::PkgIterator> >::insert(pkgCache::PkgIterator const &P) {
  342. if (P.end() == true)
  343. return false;
  344. _cont.push_back(P);
  345. return true;
  346. }
  347. template<> inline void PackageContainer<std::list<pkgCache::PkgIterator> >::insert(const_iterator begin, const_iterator end) {
  348. for (const_iterator p = begin; p != end; ++p)
  349. _cont.push_back(*p);
  350. }
  351. typedef PackageContainer<std::set<pkgCache::PkgIterator> > PackageSet;
  352. typedef PackageContainer<std::list<pkgCache::PkgIterator> > PackageList;
  353. class VersionContainerInterface { /*{{{*/
  354. /** \class APT::VersionContainerInterface
  355. Same as APT::PackageContainerInterface, just for Versions */
  356. public:
  357. /** \brief smell like a pkgCache::VerIterator */
  358. class const_iterator { /*{{{*/
  359. public:
  360. virtual pkgCache::VerIterator getVer() const = 0;
  361. operator pkgCache::VerIterator(void) { return getVer(); }
  362. inline pkgCache *Cache() const { return getVer().Cache(); }
  363. inline unsigned long Index() const {return getVer().Index();}
  364. inline int CompareVer(const pkgCache::VerIterator &B) const { return getVer().CompareVer(B); }
  365. inline const char *VerStr() const { return getVer().VerStr(); }
  366. inline const char *Section() const { return getVer().Section(); }
  367. inline const char *Arch() const { return getVer().Arch(); }
  368. inline pkgCache::PkgIterator ParentPkg() const { return getVer().ParentPkg(); }
  369. inline pkgCache::DescIterator DescriptionList() const { return getVer().DescriptionList(); }
  370. inline pkgCache::DescIterator TranslatedDescription() const { return getVer().TranslatedDescription(); }
  371. inline pkgCache::DepIterator DependsList() const { return getVer().DependsList(); }
  372. inline pkgCache::PrvIterator ProvidesList() const { return getVer().ProvidesList(); }
  373. inline pkgCache::VerFileIterator FileList() const { return getVer().FileList(); }
  374. inline bool Downloadable() const { return getVer().Downloadable(); }
  375. inline const char *PriorityType() const { return getVer().PriorityType(); }
  376. inline std::string RelStr() const { return getVer().RelStr(); }
  377. inline bool Automatic() const { return getVer().Automatic(); }
  378. inline pkgCache::VerFileIterator NewestFile() const { return getVer().NewestFile(); }
  379. // we have only valid iterators here
  380. inline bool end() const { return false; }
  381. inline pkgCache::Version const * operator->() const { return &*getVer(); }
  382. };
  383. /*}}}*/
  384. virtual bool insert(pkgCache::VerIterator const &V) = 0;
  385. virtual bool empty() const = 0;
  386. virtual void clear() = 0;
  387. /** \brief specifies which version(s) will be returned if non is given */
  388. enum Version {
  389. /** All versions */
  390. ALL,
  391. /** Candidate and installed version */
  392. CANDANDINST,
  393. /** Candidate version */
  394. CANDIDATE,
  395. /** Installed version */
  396. INSTALLED,
  397. /** Candidate or if non installed version */
  398. CANDINST,
  399. /** Installed or if non candidate version */
  400. INSTCAND,
  401. /** Newest version */
  402. NEWEST
  403. };
  404. struct Modifier {
  405. enum Position { NONE, PREFIX, POSTFIX };
  406. unsigned short ID;
  407. const char * const Alias;
  408. Position Pos;
  409. Version SelectVersion;
  410. Modifier (unsigned short const &id, const char * const alias, Position const &pos,
  411. Version const &select) : ID(id), Alias(alias), Pos(pos),
  412. SelectVersion(select) {}
  413. };
  414. static bool FromCommandLine(VersionContainerInterface * const vci, pkgCacheFile &Cache,
  415. const char **cmdline, Version const &fallback,
  416. CacheSetHelper &helper);
  417. static bool FromString(VersionContainerInterface * const vci, pkgCacheFile &Cache,
  418. std::string pkg, Version const &fallback, CacheSetHelper &helper,
  419. bool const onlyFromName = false);
  420. static bool FromPackage(VersionContainerInterface * const vci, pkgCacheFile &Cache,
  421. pkgCache::PkgIterator const &P, Version const &fallback,
  422. CacheSetHelper &helper);
  423. static bool FromModifierCommandLine(unsigned short &modID,
  424. VersionContainerInterface * const vci,
  425. pkgCacheFile &Cache, const char * cmdline,
  426. std::list<Modifier> const &mods,
  427. CacheSetHelper &helper);
  428. static bool FromDependency(VersionContainerInterface * const vci,
  429. pkgCacheFile &Cache,
  430. pkgCache::DepIterator const &D,
  431. Version const &selector,
  432. CacheSetHelper &helper);
  433. protected: /*{{{*/
  434. /** \brief returns the candidate version of the package
  435. \param Cache to be used to query for information
  436. \param Pkg we want the candidate version from this package
  437. \param helper used in this container instance */
  438. static pkgCache::VerIterator getCandidateVer(pkgCacheFile &Cache,
  439. pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper);
  440. /** \brief returns the installed version of the package
  441. \param Cache to be used to query for information
  442. \param Pkg we want the installed version from this package
  443. \param helper used in this container instance */
  444. static pkgCache::VerIterator getInstalledVer(pkgCacheFile &Cache,
  445. pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper);
  446. /*}}}*/
  447. };
  448. /*}}}*/
  449. template<class Container> class VersionContainer : public VersionContainerInterface {/*{{{*/
  450. /** \class APT::VersionContainer
  451. Simple wrapper around a container class like std::set to provide a similar
  452. interface to a set of versions as to the complete set of all versions in the
  453. pkgCache. */
  454. Container _cont;
  455. public: /*{{{*/
  456. /** \brief smell like a pkgCache::VerIterator */
  457. class const_iterator : public VersionContainerInterface::const_iterator,
  458. public std::iterator<std::forward_iterator_tag, typename Container::const_iterator> {/*{{{*/
  459. typename Container::const_iterator _iter;
  460. public:
  461. const_iterator(typename Container::const_iterator i) : _iter(i) {}
  462. pkgCache::VerIterator getVer(void) const { return *_iter; }
  463. inline pkgCache::VerIterator operator*(void) const { return *_iter; }
  464. operator typename Container::const_iterator(void) const { return _iter; }
  465. inline const_iterator& operator++() { ++_iter; return *this; }
  466. inline const_iterator operator++(int) { const_iterator tmp(*this); operator++(); return tmp; }
  467. inline bool operator!=(const_iterator const &i) const { return _iter != i._iter; }
  468. inline bool operator==(const_iterator const &i) const { return _iter == i._iter; }
  469. friend std::ostream& operator<<(std::ostream& out, const_iterator i) { return operator<<(out, *i); }
  470. };
  471. class iterator : public VersionContainerInterface::const_iterator,
  472. public std::iterator<std::forward_iterator_tag, typename Container::iterator> {
  473. typename Container::iterator _iter;
  474. public:
  475. iterator(typename Container::iterator i) : _iter(i) {}
  476. pkgCache::VerIterator getVer(void) const { return *_iter; }
  477. inline pkgCache::VerIterator operator*(void) const { return *_iter; }
  478. operator typename Container::iterator(void) const { return _iter; }
  479. operator typename VersionContainer<Container>::const_iterator() { return typename VersionContainer<Container>::const_iterator(_iter); }
  480. inline iterator& operator++() { ++_iter; return *this; }
  481. inline iterator operator++(int) { iterator tmp(*this); operator++(); return tmp; }
  482. inline bool operator!=(iterator const &i) const { return _iter != i._iter; }
  483. inline bool operator==(iterator const &i) const { return _iter == i._iter; }
  484. inline iterator& operator=(iterator const &i) { _iter = i._iter; return *this; }
  485. inline iterator& operator=(typename Container::iterator const &i) { _iter = i; return *this; }
  486. friend std::ostream& operator<<(std::ostream& out, iterator i) { return operator<<(out, *i); }
  487. };
  488. /*}}}*/
  489. bool insert(pkgCache::VerIterator const &V) { if (V.end() == true) return false; _cont.insert(V); return true; }
  490. template<class Cont> void insert(VersionContainer<Cont> const &vercont) { _cont.insert((typename Cont::const_iterator)vercont.begin(), (typename Cont::const_iterator)vercont.end()); }
  491. void insert(const_iterator begin, const_iterator end) { _cont.insert(begin, end); }
  492. bool empty() const { return _cont.empty(); }
  493. void clear() { return _cont.clear(); }
  494. //FIXME: on ABI break, replace the first with the second without bool
  495. void erase(iterator position) { _cont.erase((typename Container::iterator)position); }
  496. iterator& erase(iterator &position, bool) { return position = _cont.erase((typename Container::iterator)position); }
  497. size_t erase(const pkgCache::VerIterator x) { return _cont.erase(x); }
  498. void erase(iterator first, iterator last) { _cont.erase(first, last); }
  499. size_t size() const { return _cont.size(); }
  500. const_iterator begin() const { return const_iterator(_cont.begin()); }
  501. const_iterator end() const { return const_iterator(_cont.end()); }
  502. iterator begin() { return iterator(_cont.begin()); }
  503. iterator end() { return iterator(_cont.end()); }
  504. const_iterator find(pkgCache::VerIterator const &V) const { return const_iterator(_cont.find(V)); }
  505. /** \brief returns all versions specified on the commandline
  506. Get all versions from the commandline, uses given default version if
  507. non specifically requested and executes regex's if needed on names.
  508. \param Cache the packages and versions are in
  509. \param cmdline Command line the versions should be extracted from
  510. \param fallback version specification
  511. \param helper responsible for error and message handling */
  512. static VersionContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
  513. Version const &fallback, CacheSetHelper &helper) {
  514. VersionContainer vercon;
  515. VersionContainerInterface::FromCommandLine(&vercon, Cache, cmdline, fallback, helper);
  516. return vercon;
  517. }
  518. static VersionContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
  519. Version const &fallback) {
  520. CacheSetHelper helper;
  521. return FromCommandLine(Cache, cmdline, fallback, helper);
  522. }
  523. static VersionContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline) {
  524. return FromCommandLine(Cache, cmdline, CANDINST);
  525. }
  526. static VersionContainer FromString(pkgCacheFile &Cache, std::string const &pkg,
  527. Version const &fallback, CacheSetHelper &helper,
  528. bool const onlyFromName = false) {
  529. VersionContainer vercon;
  530. VersionContainerInterface::FromString(&vercon, Cache, pkg, fallback, helper);
  531. return vercon;
  532. }
  533. static VersionContainer FromString(pkgCacheFile &Cache, std::string pkg,
  534. Version const &fallback) {
  535. CacheSetHelper helper;
  536. return FromString(Cache, pkg, fallback, helper);
  537. }
  538. static VersionContainer FromString(pkgCacheFile &Cache, std::string pkg) {
  539. return FromString(Cache, pkg, CANDINST);
  540. }
  541. /** \brief returns all versions specified for the package
  542. \param Cache the package and versions are in
  543. \param P the package in question
  544. \param fallback the version(s) you want to get
  545. \param helper the helper used for display and error handling */
  546. static VersionContainer FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P,
  547. Version const &fallback, CacheSetHelper &helper) {
  548. VersionContainer vercon;
  549. VersionContainerInterface::FromPackage(&vercon, Cache, P, fallback, helper);
  550. return vercon;
  551. }
  552. static VersionContainer FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P,
  553. Version const &fallback) {
  554. CacheSetHelper helper;
  555. return FromPackage(Cache, P, fallback, helper);
  556. }
  557. static VersionContainer FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P) {
  558. return FromPackage(Cache, P, CANDIDATE);
  559. }
  560. static std::map<unsigned short, VersionContainer> GroupedFromCommandLine(
  561. pkgCacheFile &Cache,
  562. const char **cmdline,
  563. std::list<Modifier> const &mods,
  564. unsigned short const fallback,
  565. CacheSetHelper &helper) {
  566. std::map<unsigned short, VersionContainer> versets;
  567. for (const char **I = cmdline; *I != 0; ++I) {
  568. unsigned short modID = fallback;
  569. VersionContainer verset;
  570. VersionContainerInterface::FromModifierCommandLine(modID, &verset, Cache, *I, mods, helper);
  571. versets[modID].insert(verset);
  572. }
  573. return versets;
  574. }
  575. static std::map<unsigned short, VersionContainer> GroupedFromCommandLine(
  576. pkgCacheFile &Cache, const char **cmdline,
  577. std::list<Modifier> const &mods,
  578. unsigned short const fallback) {
  579. CacheSetHelper helper;
  580. return GroupedFromCommandLine(Cache, cmdline,
  581. mods, fallback, helper);
  582. }
  583. static VersionContainer FromDependency(pkgCacheFile &Cache, pkgCache::DepIterator const &D,
  584. Version const &selector, CacheSetHelper &helper) {
  585. VersionContainer vercon;
  586. VersionContainerInterface::FromDependency(&vercon, Cache, D, selector, helper);
  587. return vercon;
  588. }
  589. static VersionContainer FromDependency(pkgCacheFile &Cache, pkgCache::DepIterator const &D,
  590. Version const &selector) {
  591. CacheSetHelper helper;
  592. return FromPackage(Cache, D, selector, helper);
  593. }
  594. static VersionContainer FromDependency(pkgCacheFile &Cache, pkgCache::DepIterator const &D) {
  595. return FromPackage(Cache, D, CANDIDATE);
  596. }
  597. /*}}}*/
  598. }; /*}}}*/
  599. template<> template<class Cont> void VersionContainer<std::list<pkgCache::VerIterator> >::insert(VersionContainer<Cont> const &vercont) {
  600. for (typename VersionContainer<Cont>::const_iterator v = vercont.begin(); v != vercont.end(); ++v)
  601. _cont.push_back(*v);
  602. }
  603. // these two are 'inline' as otherwise the linker has problems with seeing these untemplated
  604. // specializations again and again - but we need to see them, so that library users can use them
  605. template<> inline bool VersionContainer<std::list<pkgCache::VerIterator> >::insert(pkgCache::VerIterator const &V) {
  606. if (V.end() == true)
  607. return false;
  608. _cont.push_back(V);
  609. return true;
  610. }
  611. template<> inline void VersionContainer<std::list<pkgCache::VerIterator> >::insert(const_iterator begin, const_iterator end) {
  612. for (const_iterator v = begin; v != end; ++v)
  613. _cont.push_back(*v);
  614. }
  615. typedef VersionContainer<std::set<pkgCache::VerIterator> > VersionSet;
  616. typedef VersionContainer<std::list<pkgCache::VerIterator> > VersionList;
  617. }
  618. #endif