cacheset.h 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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 <vector>
  16. #include <string>
  17. #include <iterator>
  18. #include <algorithm>
  19. #include <stddef.h>
  20. #include <apt-pkg/error.h>
  21. #include <apt-pkg/pkgcache.h>
  22. #include <apt-pkg/cacheiterators.h>
  23. #ifndef APT_8_CLEANER_HEADERS
  24. #include <apt-pkg/cachefile.h>
  25. #endif
  26. #ifndef APT_10_CLEANER_HEADERS
  27. #include <iostream>
  28. #endif
  29. /*}}}*/
  30. class pkgCacheFile;
  31. namespace APT {
  32. class PackageContainerInterface;
  33. class VersionContainerInterface;
  34. class CacheSetHelper { /*{{{*/
  35. /** \class APT::CacheSetHelper
  36. Simple base class with a lot of virtual methods which can be overridden
  37. to alter the behavior or the output of the CacheSets.
  38. This helper is passed around by the static methods in the CacheSets and
  39. used every time they hit an error condition or something could be
  40. printed out.
  41. */
  42. public: /*{{{*/
  43. CacheSetHelper(bool const ShowError = true,
  44. GlobalError::MsgType ErrorType = GlobalError::ERROR) :
  45. ShowError(ShowError), ErrorType(ErrorType) {}
  46. virtual ~CacheSetHelper() {}
  47. enum PkgSelector { UNKNOWN, PACKAGENAME, REGEX, TASK, FNMATCH };
  48. /** \brief be notified about the package being selected via pattern
  49. *
  50. * Main use is probably to show a message to the user what happened
  51. *
  52. * \param pkg is the package which was selected
  53. * \param select is the selection method which choose the package
  54. * \param pattern is the string used by the selection method to pick the package
  55. */
  56. virtual void showPackageSelection(pkgCache::PkgIterator const &pkg, PkgSelector const select, std::string const &pattern);
  57. // use the method above instead, react only on the type you need and let the base handle the rest if need be
  58. // this allows use to add new selection methods without breaking the ABI constantly with new virtual methods
  59. APT_DEPRECATED virtual void showTaskSelection(pkgCache::PkgIterator const &pkg, std::string const &pattern);
  60. APT_DEPRECATED virtual void showRegExSelection(pkgCache::PkgIterator const &pkg, std::string const &pattern);
  61. APT_DEPRECATED virtual void showFnmatchSelection(pkgCache::PkgIterator const &pkg, std::string const &pattern);
  62. /** \brief be notified if a package can't be found via pattern
  63. *
  64. * Can be used to show a message as well as to try something else to make it match
  65. *
  66. * \param select is the method tried for selection
  67. * \param pci is the container the package should be inserted in
  68. * \param Cache is the package universe available
  69. * \param pattern is the string not matching anything
  70. */
  71. virtual void canNotFindPackage(enum PkgSelector const select, PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string const &pattern);
  72. // same as above for showPackageSelection
  73. APT_DEPRECATED virtual void canNotFindTask(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern);
  74. APT_DEPRECATED virtual void canNotFindRegEx(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern);
  75. APT_DEPRECATED virtual void canNotFindFnmatch(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern);
  76. APT_DEPRECATED virtual void canNotFindPackage(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string const &str);
  77. /** \brief specifies which version(s) we want to refer to */
  78. enum VerSelector {
  79. /** by release string */
  80. RELEASE,
  81. /** by version number string */
  82. VERSIONNUMBER,
  83. /** All versions */
  84. ALL,
  85. /** Candidate and installed version */
  86. CANDANDINST,
  87. /** Candidate version */
  88. CANDIDATE,
  89. /** Installed version */
  90. INSTALLED,
  91. /** Candidate or if non installed version */
  92. CANDINST,
  93. /** Installed or if non candidate version */
  94. INSTCAND,
  95. /** Newest version */
  96. NEWEST
  97. };
  98. /** \brief be notified about the version being selected via pattern
  99. *
  100. * Main use is probably to show a message to the user what happened
  101. * Note that at the moment this method is only called for RELEASE
  102. * and VERSION selections, not for the others.
  103. *
  104. * \param Pkg is the package which was selected for
  105. * \param Ver is the version selected
  106. * \param select is the selection method which choose the version
  107. * \param pattern is the string used by the selection method to pick the version
  108. */
  109. virtual void showVersionSelection(pkgCache::PkgIterator const &Pkg, pkgCache::VerIterator const &Ver,
  110. enum VerSelector const select, std::string const &pattern);
  111. // renamed to have a similar interface to showPackageSelection
  112. APT_DEPRECATED virtual void showSelectedVersion(pkgCache::PkgIterator const &Pkg, pkgCache::VerIterator const Ver,
  113. std::string const &ver, bool const verIsRel);
  114. /** \brief be notified if a version can't be found for a package
  115. *
  116. * Main use is probably to show a message to the user what happened
  117. *
  118. * \param select is the method tried for selection
  119. * \param vci is the container the version should be inserted in
  120. * \param Cache is the package universe available
  121. * \param Pkg is the package we wanted a version from
  122. */
  123. virtual void canNotFindVersion(enum VerSelector const select, VersionContainerInterface * const vci, pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg);
  124. // same as above for showPackageSelection
  125. APT_DEPRECATED virtual void canNotFindAllVer(VersionContainerInterface * const vci, pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg);
  126. APT_DEPRECATED virtual void canNotFindInstCandVer(VersionContainerInterface * const vci, pkgCacheFile &Cache,
  127. pkgCache::PkgIterator const &Pkg);
  128. APT_DEPRECATED virtual void canNotFindCandInstVer(VersionContainerInterface * const vci,
  129. pkgCacheFile &Cache,
  130. pkgCache::PkgIterator const &Pkg);
  131. // the difference between canNotFind and canNotGet is that the later is more low-level
  132. // and called from other places: In this case looking into the code is the only real answer…
  133. virtual pkgCache::VerIterator canNotGetVersion(enum VerSelector const select, pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg);
  134. // same as above for showPackageSelection
  135. APT_DEPRECATED virtual pkgCache::VerIterator canNotFindNewestVer(pkgCacheFile &Cache,
  136. pkgCache::PkgIterator const &Pkg);
  137. APT_DEPRECATED virtual pkgCache::VerIterator canNotFindCandidateVer(pkgCacheFile &Cache,
  138. pkgCache::PkgIterator const &Pkg);
  139. APT_DEPRECATED virtual pkgCache::VerIterator canNotFindInstalledVer(pkgCacheFile &Cache,
  140. pkgCache::PkgIterator const &Pkg);
  141. virtual pkgCache::PkgIterator canNotFindPkgName(pkgCacheFile &Cache, std::string const &str);
  142. bool showErrors() const { return ShowError; }
  143. bool showErrors(bool const newValue) { if (ShowError == newValue) return ShowError; else return ((ShowError = newValue) == false); }
  144. GlobalError::MsgType errorType() const { return ErrorType; }
  145. GlobalError::MsgType errorType(GlobalError::MsgType const &newValue)
  146. {
  147. if (ErrorType == newValue) return ErrorType;
  148. else {
  149. GlobalError::MsgType const &oldValue = ErrorType;
  150. ErrorType = newValue;
  151. return oldValue;
  152. }
  153. }
  154. /*}}}*/
  155. protected:
  156. bool ShowError;
  157. GlobalError::MsgType ErrorType;
  158. pkgCache::VerIterator canNotGetInstCandVer(pkgCacheFile &Cache,
  159. pkgCache::PkgIterator const &Pkg);
  160. pkgCache::VerIterator canNotGetCandInstVer(pkgCacheFile &Cache,
  161. pkgCache::PkgIterator const &Pkg);
  162. }; /*}}}*/
  163. class PackageContainerInterface { /*{{{*/
  164. /** \class PackageContainerInterface
  165. * Interface ensuring that all operations can be executed on the yet to
  166. * define concrete PackageContainer - access to all methods is possible,
  167. * but in general the wrappers provided by the PackageContainer template
  168. * are nicer to use.
  169. * This class mostly protects use from the need to write all implementation
  170. * of the methods working on containers in the template */
  171. public:
  172. class const_iterator { /*{{{*/
  173. public:
  174. virtual pkgCache::PkgIterator getPkg() const = 0;
  175. operator pkgCache::PkgIterator(void) const { return getPkg(); }
  176. inline const char *Name() const {return getPkg().Name(); }
  177. inline std::string FullName(bool const Pretty) const { return getPkg().FullName(Pretty); }
  178. inline std::string FullName() const { return getPkg().FullName(); }
  179. APT_DEPRECATED inline const char *Section() const {
  180. #if __GNUC__ >= 4
  181. #pragma GCC diagnostic push
  182. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  183. #endif
  184. return getPkg().Section();
  185. #if __GNUC__ >= 4
  186. #pragma GCC diagnostic pop
  187. #endif
  188. }
  189. inline bool Purge() const {return getPkg().Purge(); }
  190. inline const char *Arch() const {return getPkg().Arch(); }
  191. inline pkgCache::GrpIterator Group() const { return getPkg().Group(); }
  192. inline pkgCache::VerIterator VersionList() const { return getPkg().VersionList(); }
  193. inline pkgCache::VerIterator CurrentVer() const { return getPkg().CurrentVer(); }
  194. inline pkgCache::DepIterator RevDependsList() const { return getPkg().RevDependsList(); }
  195. inline pkgCache::PrvIterator ProvidesList() const { return getPkg().ProvidesList(); }
  196. inline pkgCache::PkgIterator::OkState State() const { return getPkg().State(); }
  197. inline const char *CandVersion() const { return getPkg().CandVersion(); }
  198. inline const char *CurVersion() const { return getPkg().CurVersion(); }
  199. inline pkgCache *Cache() const { return getPkg().Cache(); }
  200. inline unsigned long Index() const {return getPkg().Index();}
  201. // we have only valid iterators here
  202. inline bool end() const { return false; }
  203. inline pkgCache::Package const * operator->() const {return &*getPkg();}
  204. };
  205. /*}}}*/
  206. virtual bool insert(pkgCache::PkgIterator const &P) = 0;
  207. virtual bool empty() const = 0;
  208. virtual void clear() = 0;
  209. // FIXME: This is a bloody hack removed soon. Use CacheSetHelper::PkgSelector !
  210. enum APT_DEPRECATED Constructor { UNKNOWN = CacheSetHelper::UNKNOWN,
  211. REGEX = CacheSetHelper::REGEX,
  212. TASK = CacheSetHelper::TASK,
  213. FNMATCH = CacheSetHelper::FNMATCH };
  214. #if __GNUC__ >= 4
  215. #pragma GCC diagnostic push
  216. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  217. #endif
  218. void setConstructor(Constructor const by) { ConstructedBy = (CacheSetHelper::PkgSelector)by; }
  219. #if __GNUC__ >= 4
  220. #pragma GCC diagnostic pop
  221. #endif
  222. void setConstructor(CacheSetHelper::PkgSelector const by) { ConstructedBy = by; }
  223. CacheSetHelper::PkgSelector getConstructor() const { return ConstructedBy; }
  224. PackageContainerInterface() : ConstructedBy(CacheSetHelper::UNKNOWN) {}
  225. PackageContainerInterface(CacheSetHelper::PkgSelector const by) : ConstructedBy(by) {}
  226. static bool FromTask(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper);
  227. static bool FromRegEx(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper);
  228. static pkgCache::PkgIterator FromName(pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper);
  229. static bool FromFnmatch(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper);
  230. static bool FromGroup(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper);
  231. static bool FromString(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper);
  232. static bool FromCommandLine(PackageContainerInterface * const pci, pkgCacheFile &Cache, const char **cmdline, CacheSetHelper &helper);
  233. struct Modifier {
  234. enum Position { NONE, PREFIX, POSTFIX };
  235. unsigned short ID;
  236. const char * const Alias;
  237. Position Pos;
  238. Modifier (unsigned short const &id, const char * const alias, Position const &pos) : ID(id), Alias(alias), Pos(pos) {}
  239. };
  240. static bool FromModifierCommandLine(unsigned short &modID, PackageContainerInterface * const pci,
  241. pkgCacheFile &Cache, const char * cmdline,
  242. std::list<Modifier> const &mods, CacheSetHelper &helper);
  243. private:
  244. CacheSetHelper::PkgSelector ConstructedBy;
  245. };
  246. /*}}}*/
  247. template<class Container> class PackageContainer : public PackageContainerInterface {/*{{{*/
  248. /** \class APT::PackageContainer
  249. Simple wrapper around a container class like std::set to provide a similar
  250. interface to a set of packages as to the complete set of all packages in the
  251. pkgCache. */
  252. Container _cont;
  253. public: /*{{{*/
  254. /** \brief smell like a pkgCache::PkgIterator */
  255. class const_iterator : public PackageContainerInterface::const_iterator,/*{{{*/
  256. public std::iterator<std::forward_iterator_tag, typename Container::const_iterator> {
  257. typename Container::const_iterator _iter;
  258. public:
  259. const_iterator(typename Container::const_iterator i) : _iter(i) {}
  260. pkgCache::PkgIterator getPkg(void) const { return *_iter; }
  261. inline pkgCache::PkgIterator operator*(void) const { return *_iter; }
  262. operator typename Container::const_iterator(void) const { return _iter; }
  263. inline const_iterator& operator++() { ++_iter; return *this; }
  264. inline const_iterator operator++(int) { const_iterator tmp(*this); operator++(); return tmp; }
  265. inline bool operator!=(const_iterator const &i) const { return _iter != i._iter; }
  266. inline bool operator==(const_iterator const &i) const { return _iter == i._iter; }
  267. friend std::ostream& operator<<(std::ostream& out, const_iterator i) { return operator<<(out, *i); }
  268. };
  269. class iterator : public PackageContainerInterface::const_iterator,
  270. public std::iterator<std::forward_iterator_tag, typename Container::iterator> {
  271. typename Container::iterator _iter;
  272. public:
  273. iterator(typename Container::iterator i) : _iter(i) {}
  274. pkgCache::PkgIterator getPkg(void) const { return *_iter; }
  275. inline pkgCache::PkgIterator operator*(void) const { return *_iter; }
  276. operator typename Container::iterator(void) const { return _iter; }
  277. operator typename PackageContainer<Container>::const_iterator() { return typename PackageContainer<Container>::const_iterator(_iter); }
  278. inline iterator& operator++() { ++_iter; return *this; }
  279. inline iterator operator++(int) { iterator tmp(*this); operator++(); return tmp; }
  280. inline bool operator!=(iterator const &i) const { return _iter != i._iter; }
  281. inline bool operator==(iterator const &i) const { return _iter == i._iter; }
  282. inline iterator& operator=(iterator const &i) { _iter = i._iter; return *this; }
  283. inline iterator& operator=(typename Container::iterator const &i) { _iter = i; return *this; }
  284. friend std::ostream& operator<<(std::ostream& out, iterator i) { return operator<<(out, *i); }
  285. };
  286. /*}}}*/
  287. bool insert(pkgCache::PkgIterator const &P) { if (P.end() == true) return false; _cont.insert(P); return true; }
  288. template<class Cont> void insert(PackageContainer<Cont> const &pkgcont) { _cont.insert((typename Cont::const_iterator)pkgcont.begin(), (typename Cont::const_iterator)pkgcont.end()); }
  289. void insert(const_iterator begin, const_iterator end) { _cont.insert(begin, end); }
  290. bool empty() const { return _cont.empty(); }
  291. void clear() { return _cont.clear(); }
  292. //FIXME: on ABI break, replace the first with the second without bool
  293. void erase(iterator position) { _cont.erase((typename Container::iterator)position); }
  294. iterator& erase(iterator &position, bool) { return position = _cont.erase((typename Container::iterator)position); }
  295. size_t erase(const pkgCache::PkgIterator x) { return _cont.erase(x); }
  296. void erase(iterator first, iterator last) { _cont.erase(first, last); }
  297. size_t size() const { return _cont.size(); }
  298. const_iterator begin() const { return const_iterator(_cont.begin()); }
  299. const_iterator end() const { return const_iterator(_cont.end()); }
  300. iterator begin() { return iterator(_cont.begin()); }
  301. iterator end() { return iterator(_cont.end()); }
  302. const_iterator find(pkgCache::PkgIterator const &P) const { return const_iterator(_cont.find(P)); }
  303. PackageContainer() : PackageContainerInterface() {}
  304. PackageContainer(CacheSetHelper::PkgSelector const &by) : PackageContainerInterface(by) {}
  305. #if __GNUC__ >= 4
  306. #pragma GCC diagnostic push
  307. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  308. #endif
  309. APT_DEPRECATED PackageContainer(Constructor const &by) : PackageContainerInterface((CacheSetHelper::PkgSelector)by) {}
  310. #if __GNUC__ >= 4
  311. #pragma GCC diagnostic pop
  312. #endif
  313. /** \brief sort all included versions with given comparer
  314. Some containers are sorted by default, some are not and can't be,
  315. but a few like std::vector can be sorted if need be, so this can be
  316. specialized in later on. The default is that this will fail though.
  317. Specifically, already sorted containers like std::set will return
  318. false as well as there is no easy way to check that the given comparer
  319. would sort in the same way the set is currently sorted
  320. \return \b true if the set was sorted, \b false if not. */
  321. template<class Compare> bool sort(Compare /*Comp*/) { return false; }
  322. /** \brief returns all packages in the cache who belong to the given task
  323. A simple helper responsible for search for all members of a task
  324. in the cache. Optional it prints a a notice about the
  325. packages chosen cause of the given task.
  326. \param Cache the packages are in
  327. \param pattern name of the task
  328. \param helper responsible for error and message handling */
  329. static PackageContainer FromTask(pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper) {
  330. PackageContainer cont(CacheSetHelper::TASK);
  331. PackageContainerInterface::FromTask(&cont, Cache, pattern, helper);
  332. return cont;
  333. }
  334. static PackageContainer FromTask(pkgCacheFile &Cache, std::string const &pattern) {
  335. CacheSetHelper helper;
  336. return FromTask(Cache, pattern, helper);
  337. }
  338. /** \brief returns all packages in the cache whose name matchs a given pattern
  339. A simple helper responsible for executing a regular expression on all
  340. package names in the cache. Optional it prints a a notice about the
  341. packages chosen cause of the given package.
  342. \param Cache the packages are in
  343. \param pattern regular expression for package names
  344. \param helper responsible for error and message handling */
  345. static PackageContainer FromRegEx(pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper) {
  346. PackageContainer cont(CacheSetHelper::REGEX);
  347. PackageContainerInterface::FromRegEx(&cont, Cache, pattern, helper);
  348. return cont;
  349. }
  350. static PackageContainer FromRegEx(pkgCacheFile &Cache, std::string const &pattern) {
  351. CacheSetHelper helper;
  352. return FromRegEx(Cache, pattern, helper);
  353. }
  354. static PackageContainer FromFnmatch(pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper) {
  355. PackageContainer cont(CacheSetHelper::FNMATCH);
  356. PackageContainerInterface::FromFnmatch(&cont, Cache, pattern, helper);
  357. return cont;
  358. }
  359. static PackageContainer FromFnMatch(pkgCacheFile &Cache, std::string const &pattern) {
  360. CacheSetHelper helper;
  361. return FromFnmatch(Cache, pattern, helper);
  362. }
  363. /** \brief returns a package specified by a string
  364. \param Cache the package is in
  365. \param pattern String the package name should be extracted from
  366. \param helper responsible for error and message handling */
  367. static pkgCache::PkgIterator FromName(pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper) {
  368. return PackageContainerInterface::FromName(Cache, pattern, helper);
  369. }
  370. static pkgCache::PkgIterator FromName(pkgCacheFile &Cache, std::string const &pattern) {
  371. CacheSetHelper helper;
  372. return PackageContainerInterface::FromName(Cache, pattern, helper);
  373. }
  374. /** \brief returns all packages specified by a string
  375. \param Cache the packages are in
  376. \param pattern String the package name(s) should be extracted from
  377. \param helper responsible for error and message handling */
  378. static PackageContainer FromString(pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper) {
  379. PackageContainer cont;
  380. PackageContainerInterface::FromString(&cont, Cache, pattern, helper);
  381. return cont;
  382. }
  383. static PackageContainer FromString(pkgCacheFile &Cache, std::string const &pattern) {
  384. CacheSetHelper helper;
  385. return FromString(Cache, pattern, helper);
  386. }
  387. /** \brief returns all packages specified on the commandline
  388. Get all package names from the commandline and executes regex's if needed.
  389. No special package command is supported, just plain names.
  390. \param Cache the packages are in
  391. \param cmdline Command line the package names should be extracted from
  392. \param helper responsible for error and message handling */
  393. static PackageContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline, CacheSetHelper &helper) {
  394. PackageContainer cont;
  395. PackageContainerInterface::FromCommandLine(&cont, Cache, cmdline, helper);
  396. return cont;
  397. }
  398. static PackageContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline) {
  399. CacheSetHelper helper;
  400. return FromCommandLine(Cache, cmdline, helper);
  401. }
  402. /** \brief group packages by a action modifiers
  403. At some point it is needed to get from the same commandline
  404. different package sets grouped by a modifier. Take
  405. apt-get install apt awesome-
  406. as an example.
  407. \param Cache the packages are in
  408. \param cmdline Command line the package names should be extracted from
  409. \param mods list of modifiers the method should accept
  410. \param fallback the default modifier group for a package
  411. \param helper responsible for error and message handling */
  412. static std::map<unsigned short, PackageContainer> GroupedFromCommandLine(
  413. pkgCacheFile &Cache,
  414. const char **cmdline,
  415. std::list<Modifier> const &mods,
  416. unsigned short const &fallback,
  417. CacheSetHelper &helper) {
  418. std::map<unsigned short, PackageContainer> pkgsets;
  419. for (const char **I = cmdline; *I != 0; ++I) {
  420. unsigned short modID = fallback;
  421. PackageContainer pkgset;
  422. PackageContainerInterface::FromModifierCommandLine(modID, &pkgset, Cache, *I, mods, helper);
  423. pkgsets[modID].insert(pkgset);
  424. }
  425. return pkgsets;
  426. }
  427. static std::map<unsigned short, PackageContainer> GroupedFromCommandLine(
  428. pkgCacheFile &Cache,
  429. const char **cmdline,
  430. std::list<Modifier> const &mods,
  431. unsigned short const &fallback) {
  432. CacheSetHelper helper;
  433. return GroupedFromCommandLine(Cache, cmdline,
  434. mods, fallback, helper);
  435. }
  436. /*}}}*/
  437. }; /*}}}*/
  438. // specialisations for push_back containers: std::list & std::vector /*{{{*/
  439. template<> template<class Cont> void PackageContainer<std::list<pkgCache::PkgIterator> >::insert(PackageContainer<Cont> const &pkgcont) {
  440. for (typename PackageContainer<Cont>::const_iterator p = pkgcont.begin(); p != pkgcont.end(); ++p)
  441. _cont.push_back(*p);
  442. }
  443. template<> template<class Cont> void PackageContainer<std::vector<pkgCache::PkgIterator> >::insert(PackageContainer<Cont> const &pkgcont) {
  444. for (typename PackageContainer<Cont>::const_iterator p = pkgcont.begin(); p != pkgcont.end(); ++p)
  445. _cont.push_back(*p);
  446. }
  447. // these two are 'inline' as otherwise the linker has problems with seeing these untemplated
  448. // specializations again and again - but we need to see them, so that library users can use them
  449. template<> inline bool PackageContainer<std::list<pkgCache::PkgIterator> >::insert(pkgCache::PkgIterator const &P) {
  450. if (P.end() == true)
  451. return false;
  452. _cont.push_back(P);
  453. return true;
  454. }
  455. template<> inline bool PackageContainer<std::vector<pkgCache::PkgIterator> >::insert(pkgCache::PkgIterator const &P) {
  456. if (P.end() == true)
  457. return false;
  458. _cont.push_back(P);
  459. return true;
  460. }
  461. template<> inline void PackageContainer<std::list<pkgCache::PkgIterator> >::insert(const_iterator begin, const_iterator end) {
  462. for (const_iterator p = begin; p != end; ++p)
  463. _cont.push_back(*p);
  464. }
  465. template<> inline void PackageContainer<std::vector<pkgCache::PkgIterator> >::insert(const_iterator begin, const_iterator end) {
  466. for (const_iterator p = begin; p != end; ++p)
  467. _cont.push_back(*p);
  468. }
  469. /*}}}*/
  470. template<> template<class Compare> inline bool PackageContainer<std::vector<pkgCache::PkgIterator> >::sort(Compare Comp) {
  471. std::sort(_cont.begin(), _cont.end(), Comp);
  472. return true;
  473. }
  474. // class PackageUniverse - pkgCache as PackageContainerInterface /*{{{*/
  475. /** \class PackageUniverse
  476. Wraps around our usual pkgCache, so that it can be stuffed into methods
  477. expecting a PackageContainer.
  478. The wrapping is read-only in practice modeled by making erase and co
  479. private methods. */
  480. class PackageUniverse : public PackageContainerInterface {
  481. pkgCache * const _cont;
  482. public:
  483. typedef pkgCache::PkgIterator iterator;
  484. typedef pkgCache::PkgIterator const_iterator;
  485. bool empty() const { return false; }
  486. size_t size() const { return _cont->Head().PackageCount; }
  487. const_iterator begin() const { return _cont->PkgBegin(); }
  488. const_iterator end() const { return _cont->PkgEnd(); }
  489. iterator begin() { return _cont->PkgBegin(); }
  490. iterator end() { return _cont->PkgEnd(); }
  491. PackageUniverse(pkgCache * const Owner) : _cont(Owner) { }
  492. private:
  493. bool insert(pkgCache::PkgIterator const &) { return true; }
  494. template<class Cont> void insert(PackageContainer<Cont> const &) { }
  495. void insert(const_iterator, const_iterator) { }
  496. void clear() { }
  497. iterator& erase(iterator &iter) { return iter; }
  498. size_t erase(const pkgCache::PkgIterator) { return 0; }
  499. void erase(iterator, iterator) { }
  500. };
  501. /*}}}*/
  502. typedef PackageContainer<std::set<pkgCache::PkgIterator> > PackageSet;
  503. typedef PackageContainer<std::list<pkgCache::PkgIterator> > PackageList;
  504. typedef PackageContainer<std::vector<pkgCache::PkgIterator> > PackageVector;
  505. class VersionContainerInterface { /*{{{*/
  506. /** \class APT::VersionContainerInterface
  507. Same as APT::PackageContainerInterface, just for Versions */
  508. public:
  509. /** \brief smell like a pkgCache::VerIterator */
  510. class const_iterator { /*{{{*/
  511. public:
  512. virtual pkgCache::VerIterator getVer() const = 0;
  513. operator pkgCache::VerIterator(void) { return getVer(); }
  514. inline pkgCache *Cache() const { return getVer().Cache(); }
  515. inline unsigned long Index() const {return getVer().Index();}
  516. inline int CompareVer(const pkgCache::VerIterator &B) const { return getVer().CompareVer(B); }
  517. inline const char *VerStr() const { return getVer().VerStr(); }
  518. inline const char *Section() const { return getVer().Section(); }
  519. inline const char *Arch() const { return getVer().Arch(); }
  520. inline pkgCache::PkgIterator ParentPkg() const { return getVer().ParentPkg(); }
  521. inline pkgCache::DescIterator DescriptionList() const { return getVer().DescriptionList(); }
  522. inline pkgCache::DescIterator TranslatedDescription() const { return getVer().TranslatedDescription(); }
  523. inline pkgCache::DepIterator DependsList() const { return getVer().DependsList(); }
  524. inline pkgCache::PrvIterator ProvidesList() const { return getVer().ProvidesList(); }
  525. inline pkgCache::VerFileIterator FileList() const { return getVer().FileList(); }
  526. inline bool Downloadable() const { return getVer().Downloadable(); }
  527. inline const char *PriorityType() const { return getVer().PriorityType(); }
  528. inline std::string RelStr() const { return getVer().RelStr(); }
  529. inline bool Automatic() const { return getVer().Automatic(); }
  530. inline pkgCache::VerFileIterator NewestFile() const { return getVer().NewestFile(); }
  531. // we have only valid iterators here
  532. inline bool end() const { return false; }
  533. inline pkgCache::Version const * operator->() const { return &*getVer(); }
  534. };
  535. /*}}}*/
  536. virtual bool insert(pkgCache::VerIterator const &V) = 0;
  537. virtual bool empty() const = 0;
  538. virtual void clear() = 0;
  539. /** \brief specifies which version(s) will be returned if non is given */
  540. enum APT_DEPRECATED Version {
  541. ALL = CacheSetHelper::ALL,
  542. CANDANDINST = CacheSetHelper::CANDANDINST,
  543. CANDIDATE = CacheSetHelper::CANDIDATE,
  544. INSTALLED = CacheSetHelper::INSTALLED,
  545. CANDINST = CacheSetHelper::CANDINST,
  546. INSTCAND = CacheSetHelper::INSTCAND,
  547. NEWEST = CacheSetHelper::NEWEST
  548. };
  549. struct Modifier {
  550. unsigned short const ID;
  551. const char * const Alias;
  552. enum Position { NONE, PREFIX, POSTFIX } const Pos;
  553. enum CacheSetHelper::VerSelector const SelectVersion;
  554. Modifier (unsigned short const &id, const char * const alias, Position const &pos,
  555. enum CacheSetHelper::VerSelector const select) : ID(id), Alias(alias), Pos(pos),
  556. SelectVersion(select) {}
  557. #if __GNUC__ >= 4
  558. #pragma GCC diagnostic push
  559. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  560. #endif
  561. APT_DEPRECATED Modifier(unsigned short const &id, const char * const alias, Position const &pos,
  562. Version const &select) : ID(id), Alias(alias), Pos(pos),
  563. SelectVersion((CacheSetHelper::VerSelector)select) {}
  564. #if __GNUC__ >= 4
  565. #pragma GCC diagnostic pop
  566. #endif
  567. };
  568. static bool FromCommandLine(VersionContainerInterface * const vci, pkgCacheFile &Cache,
  569. const char **cmdline, CacheSetHelper::VerSelector const fallback,
  570. CacheSetHelper &helper);
  571. #if __GNUC__ >= 4
  572. #pragma GCC diagnostic push
  573. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  574. #endif
  575. APT_DEPRECATED static bool FromCommandLine(VersionContainerInterface * const vci, pkgCacheFile &Cache,
  576. const char **cmdline, Version const &fallback,
  577. CacheSetHelper &helper) {
  578. return FromCommandLine(vci, Cache, cmdline, (CacheSetHelper::VerSelector)fallback, helper);
  579. }
  580. #if __GNUC__ >= 4
  581. #pragma GCC diagnostic pop
  582. #endif
  583. static bool FromString(VersionContainerInterface * const vci, pkgCacheFile &Cache,
  584. std::string pkg, CacheSetHelper::VerSelector const fallback, CacheSetHelper &helper,
  585. bool const onlyFromName = false);
  586. #if __GNUC__ >= 4
  587. #pragma GCC diagnostic push
  588. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  589. #endif
  590. APT_DEPRECATED static bool FromString(VersionContainerInterface * const vci, pkgCacheFile &Cache,
  591. std::string pkg, Version const &fallback, CacheSetHelper &helper,
  592. bool const onlyFromName = false) {
  593. return FromString(vci, Cache, pkg, (CacheSetHelper::VerSelector)fallback, helper, onlyFromName);
  594. }
  595. #if __GNUC__ >= 4
  596. #pragma GCC diagnostic pop
  597. #endif
  598. static bool FromPackage(VersionContainerInterface * const vci, pkgCacheFile &Cache,
  599. pkgCache::PkgIterator const &P, CacheSetHelper::VerSelector const fallback,
  600. CacheSetHelper &helper);
  601. #if __GNUC__ >= 4
  602. #pragma GCC diagnostic push
  603. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  604. #endif
  605. APT_DEPRECATED static bool FromPackage(VersionContainerInterface * const vci, pkgCacheFile &Cache,
  606. pkgCache::PkgIterator const &P, Version const &fallback,
  607. CacheSetHelper &helper) {
  608. return FromPackage(vci, Cache, P, (CacheSetHelper::VerSelector)fallback, helper);
  609. }
  610. #if __GNUC__ >= 4
  611. #pragma GCC diagnostic pop
  612. #endif
  613. static bool FromModifierCommandLine(unsigned short &modID,
  614. VersionContainerInterface * const vci,
  615. pkgCacheFile &Cache, const char * cmdline,
  616. std::list<Modifier> const &mods,
  617. CacheSetHelper &helper);
  618. static bool FromDependency(VersionContainerInterface * const vci,
  619. pkgCacheFile &Cache,
  620. pkgCache::DepIterator const &D,
  621. CacheSetHelper::VerSelector const selector,
  622. CacheSetHelper &helper);
  623. #if __GNUC__ >= 4
  624. #pragma GCC diagnostic push
  625. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  626. #endif
  627. APT_DEPRECATED static bool FromDependency(VersionContainerInterface * const vci,
  628. pkgCacheFile &Cache,
  629. pkgCache::DepIterator const &D,
  630. Version const &selector,
  631. CacheSetHelper &helper) {
  632. return FromDependency(vci, Cache, D, (CacheSetHelper::VerSelector)selector, helper);
  633. }
  634. #if __GNUC__ >= 4
  635. #pragma GCC diagnostic pop
  636. #endif
  637. protected: /*{{{*/
  638. /** \brief returns the candidate version of the package
  639. \param Cache to be used to query for information
  640. \param Pkg we want the candidate version from this package
  641. \param helper used in this container instance */
  642. static pkgCache::VerIterator getCandidateVer(pkgCacheFile &Cache,
  643. pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper);
  644. /** \brief returns the installed version of the package
  645. \param Cache to be used to query for information
  646. \param Pkg we want the installed version from this package
  647. \param helper used in this container instance */
  648. static pkgCache::VerIterator getInstalledVer(pkgCacheFile &Cache,
  649. pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper);
  650. /*}}}*/
  651. };
  652. /*}}}*/
  653. template<class Container> class VersionContainer : public VersionContainerInterface {/*{{{*/
  654. /** \class APT::VersionContainer
  655. Simple wrapper around a container class like std::set to provide a similar
  656. interface to a set of versions as to the complete set of all versions in the
  657. pkgCache. */
  658. Container _cont;
  659. public: /*{{{*/
  660. /** \brief smell like a pkgCache::VerIterator */
  661. class const_iterator : public VersionContainerInterface::const_iterator,
  662. public std::iterator<std::forward_iterator_tag, typename Container::const_iterator> {/*{{{*/
  663. typename Container::const_iterator _iter;
  664. public:
  665. const_iterator(typename Container::const_iterator i) : _iter(i) {}
  666. pkgCache::VerIterator getVer(void) const { return *_iter; }
  667. inline pkgCache::VerIterator operator*(void) const { return *_iter; }
  668. operator typename Container::const_iterator(void) const { return _iter; }
  669. inline const_iterator& operator++() { ++_iter; return *this; }
  670. inline const_iterator operator++(int) { const_iterator tmp(*this); operator++(); return tmp; }
  671. inline bool operator!=(const_iterator const &i) const { return _iter != i._iter; }
  672. inline bool operator==(const_iterator const &i) const { return _iter == i._iter; }
  673. friend std::ostream& operator<<(std::ostream& out, const_iterator i) { return operator<<(out, *i); }
  674. };
  675. class iterator : public VersionContainerInterface::const_iterator,
  676. public std::iterator<std::forward_iterator_tag, typename Container::iterator> {
  677. typename Container::iterator _iter;
  678. public:
  679. iterator(typename Container::iterator i) : _iter(i) {}
  680. pkgCache::VerIterator getVer(void) const { return *_iter; }
  681. inline pkgCache::VerIterator operator*(void) const { return *_iter; }
  682. operator typename Container::iterator(void) const { return _iter; }
  683. operator typename VersionContainer<Container>::const_iterator() { return typename VersionContainer<Container>::const_iterator(_iter); }
  684. inline iterator& operator++() { ++_iter; return *this; }
  685. inline iterator operator++(int) { iterator tmp(*this); operator++(); return tmp; }
  686. inline bool operator!=(iterator const &i) const { return _iter != i._iter; }
  687. inline bool operator==(iterator const &i) const { return _iter == i._iter; }
  688. inline iterator& operator=(iterator const &i) { _iter = i._iter; return *this; }
  689. inline iterator& operator=(typename Container::iterator const &i) { _iter = i; return *this; }
  690. friend std::ostream& operator<<(std::ostream& out, iterator i) { return operator<<(out, *i); }
  691. };
  692. /*}}}*/
  693. bool insert(pkgCache::VerIterator const &V) { if (V.end() == true) return false; _cont.insert(V); return true; }
  694. template<class Cont> void insert(VersionContainer<Cont> const &vercont) { _cont.insert((typename Cont::const_iterator)vercont.begin(), (typename Cont::const_iterator)vercont.end()); }
  695. void insert(const_iterator begin, const_iterator end) { _cont.insert(begin, end); }
  696. bool empty() const { return _cont.empty(); }
  697. void clear() { return _cont.clear(); }
  698. //FIXME: on ABI break, replace the first with the second without bool
  699. void erase(iterator position) { _cont.erase((typename Container::iterator)position); }
  700. iterator& erase(iterator &position, bool) { return position = _cont.erase((typename Container::iterator)position); }
  701. size_t erase(const pkgCache::VerIterator x) { return _cont.erase(x); }
  702. void erase(iterator first, iterator last) { _cont.erase(first, last); }
  703. size_t size() const { return _cont.size(); }
  704. const_iterator begin() const { return const_iterator(_cont.begin()); }
  705. const_iterator end() const { return const_iterator(_cont.end()); }
  706. iterator begin() { return iterator(_cont.begin()); }
  707. iterator end() { return iterator(_cont.end()); }
  708. const_iterator find(pkgCache::VerIterator const &V) const { return const_iterator(_cont.find(V)); }
  709. /** \brief sort all included versions with given comparer
  710. Some containers are sorted by default, some are not and can't be,
  711. but a few like std::vector can be sorted if need be, so this can be
  712. specialized in later on. The default is that this will fail though.
  713. Specifically, already sorted containers like std::set will return
  714. false as well as there is no easy way to check that the given comparer
  715. would sort in the same way the set is currently sorted
  716. \return \b true if the set was sorted, \b false if not. */
  717. template<class Compare> bool sort(Compare /*Comp*/) { return false; }
  718. /** \brief returns all versions specified on the commandline
  719. Get all versions from the commandline, uses given default version if
  720. non specifically requested and executes regex's if needed on names.
  721. \param Cache the packages and versions are in
  722. \param cmdline Command line the versions should be extracted from
  723. \param fallback version specification
  724. \param helper responsible for error and message handling */
  725. static VersionContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
  726. CacheSetHelper::VerSelector const fallback, CacheSetHelper &helper) {
  727. VersionContainer vercon;
  728. VersionContainerInterface::FromCommandLine(&vercon, Cache, cmdline, fallback, helper);
  729. return vercon;
  730. }
  731. static VersionContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
  732. CacheSetHelper::VerSelector const fallback) {
  733. CacheSetHelper helper;
  734. return FromCommandLine(Cache, cmdline, fallback, helper);
  735. }
  736. static VersionContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline) {
  737. return FromCommandLine(Cache, cmdline, CacheSetHelper::CANDINST);
  738. }
  739. static VersionContainer FromString(pkgCacheFile &Cache, std::string const &pkg,
  740. CacheSetHelper::VerSelector const fallback, CacheSetHelper &helper,
  741. bool const /*onlyFromName = false*/) {
  742. VersionContainer vercon;
  743. VersionContainerInterface::FromString(&vercon, Cache, pkg, fallback, helper);
  744. return vercon;
  745. }
  746. static VersionContainer FromString(pkgCacheFile &Cache, std::string pkg,
  747. CacheSetHelper::VerSelector const fallback) {
  748. CacheSetHelper helper;
  749. return FromString(Cache, pkg, fallback, helper);
  750. }
  751. static VersionContainer FromString(pkgCacheFile &Cache, std::string pkg) {
  752. return FromString(Cache, pkg, CacheSetHelper::CANDINST);
  753. }
  754. #if __GNUC__ >= 4
  755. #pragma GCC diagnostic push
  756. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  757. #endif
  758. static VersionContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
  759. Version const &fallback, CacheSetHelper &helper) {
  760. VersionContainer vercon;
  761. VersionContainerInterface::FromCommandLine(&vercon, Cache, cmdline, (CacheSetHelper::VerSelector)fallback, helper);
  762. return vercon;
  763. }
  764. static VersionContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
  765. Version const &fallback) {
  766. CacheSetHelper helper;
  767. return FromCommandLine(Cache, cmdline, (CacheSetHelper::VerSelector)fallback, helper);
  768. }
  769. static VersionContainer FromString(pkgCacheFile &Cache, std::string const &pkg,
  770. Version const &fallback, CacheSetHelper &helper,
  771. bool const /*onlyFromName = false*/) {
  772. VersionContainer vercon;
  773. VersionContainerInterface::FromString(&vercon, Cache, pkg, (CacheSetHelper::VerSelector)fallback, helper);
  774. return vercon;
  775. }
  776. static VersionContainer FromString(pkgCacheFile &Cache, std::string pkg,
  777. Version const &fallback) {
  778. CacheSetHelper helper;
  779. return FromString(Cache, pkg, (CacheSetHelper::VerSelector)fallback, helper);
  780. }
  781. #if __GNUC__ >= 4
  782. #pragma GCC diagnostic pop
  783. #endif
  784. /** \brief returns all versions specified for the package
  785. \param Cache the package and versions are in
  786. \param P the package in question
  787. \param fallback the version(s) you want to get
  788. \param helper the helper used for display and error handling */
  789. static VersionContainer FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P,
  790. CacheSetHelper::VerSelector const fallback, CacheSetHelper &helper) {
  791. VersionContainer vercon;
  792. VersionContainerInterface::FromPackage(&vercon, Cache, P, fallback, helper);
  793. return vercon;
  794. }
  795. static VersionContainer FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P,
  796. CacheSetHelper::VerSelector const fallback) {
  797. CacheSetHelper helper;
  798. return FromPackage(Cache, P, fallback, helper);
  799. }
  800. #if __GNUC__ >= 4
  801. #pragma GCC diagnostic push
  802. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  803. #endif
  804. static VersionContainer FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P,
  805. Version const &fallback, CacheSetHelper &helper) {
  806. VersionContainer vercon;
  807. VersionContainerInterface::FromPackage(&vercon, Cache, P, (CacheSetHelper::VerSelector)fallback, helper);
  808. return vercon;
  809. }
  810. static VersionContainer FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P,
  811. Version const &fallback) {
  812. CacheSetHelper helper;
  813. return FromPackage(Cache, P, (CacheSetHelper::VerSelector)fallback, helper);
  814. }
  815. #if __GNUC__ >= 4
  816. #pragma GCC diagnostic pop
  817. #endif
  818. static VersionContainer FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P) {
  819. return FromPackage(Cache, P, CacheSetHelper::CANDIDATE);
  820. }
  821. static std::map<unsigned short, VersionContainer> GroupedFromCommandLine(
  822. pkgCacheFile &Cache,
  823. const char **cmdline,
  824. std::list<Modifier> const &mods,
  825. unsigned short const fallback,
  826. CacheSetHelper &helper) {
  827. std::map<unsigned short, VersionContainer> versets;
  828. for (const char **I = cmdline; *I != 0; ++I) {
  829. unsigned short modID = fallback;
  830. VersionContainer verset;
  831. VersionContainerInterface::FromModifierCommandLine(modID, &verset, Cache, *I, mods, helper);
  832. versets[modID].insert(verset);
  833. }
  834. return versets;
  835. }
  836. static std::map<unsigned short, VersionContainer> GroupedFromCommandLine(
  837. pkgCacheFile &Cache, const char **cmdline,
  838. std::list<Modifier> const &mods,
  839. unsigned short const fallback) {
  840. CacheSetHelper helper;
  841. return GroupedFromCommandLine(Cache, cmdline,
  842. mods, fallback, helper);
  843. }
  844. static VersionContainer FromDependency(pkgCacheFile &Cache, pkgCache::DepIterator const &D,
  845. CacheSetHelper::VerSelector const selector, CacheSetHelper &helper) {
  846. VersionContainer vercon;
  847. VersionContainerInterface::FromDependency(&vercon, Cache, D, selector, helper);
  848. return vercon;
  849. }
  850. static VersionContainer FromDependency(pkgCacheFile &Cache, pkgCache::DepIterator const &D,
  851. CacheSetHelper::VerSelector const selector) {
  852. CacheSetHelper helper;
  853. return FromPackage(Cache, D, selector, helper);
  854. }
  855. #if __GNUC__ >= 4
  856. #pragma GCC diagnostic push
  857. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  858. #endif
  859. static VersionContainer FromDependency(pkgCacheFile &Cache, pkgCache::DepIterator const &D,
  860. Version const &selector, CacheSetHelper &helper) {
  861. VersionContainer vercon;
  862. VersionContainerInterface::FromDependency(&vercon, Cache, D, (CacheSetHelper::VerSelector)selector, helper);
  863. return vercon;
  864. }
  865. static VersionContainer FromDependency(pkgCacheFile &Cache, pkgCache::DepIterator const &D,
  866. Version const &selector) {
  867. CacheSetHelper helper;
  868. return FromPackage(Cache, D, (CacheSetHelper::VerSelector)selector, helper);
  869. }
  870. #if __GNUC__ >= 4
  871. #pragma GCC diagnostic pop
  872. #endif
  873. static VersionContainer FromDependency(pkgCacheFile &Cache, pkgCache::DepIterator const &D) {
  874. return FromPackage(Cache, D, CacheSetHelper::CANDIDATE);
  875. }
  876. /*}}}*/
  877. }; /*}}}*/
  878. // specialisations for push_back containers: std::list & std::vector /*{{{*/
  879. template<> template<class Cont> void VersionContainer<std::list<pkgCache::VerIterator> >::insert(VersionContainer<Cont> const &vercont) {
  880. for (typename VersionContainer<Cont>::const_iterator v = vercont.begin(); v != vercont.end(); ++v)
  881. _cont.push_back(*v);
  882. }
  883. template<> template<class Cont> void VersionContainer<std::vector<pkgCache::VerIterator> >::insert(VersionContainer<Cont> const &vercont) {
  884. for (typename VersionContainer<Cont>::const_iterator v = vercont.begin(); v != vercont.end(); ++v)
  885. _cont.push_back(*v);
  886. }
  887. // these two are 'inline' as otherwise the linker has problems with seeing these untemplated
  888. // specializations again and again - but we need to see them, so that library users can use them
  889. template<> inline bool VersionContainer<std::list<pkgCache::VerIterator> >::insert(pkgCache::VerIterator const &V) {
  890. if (V.end() == true)
  891. return false;
  892. _cont.push_back(V);
  893. return true;
  894. }
  895. template<> inline bool VersionContainer<std::vector<pkgCache::VerIterator> >::insert(pkgCache::VerIterator const &V) {
  896. if (V.end() == true)
  897. return false;
  898. _cont.push_back(V);
  899. return true;
  900. }
  901. template<> inline void VersionContainer<std::list<pkgCache::VerIterator> >::insert(const_iterator begin, const_iterator end) {
  902. for (const_iterator v = begin; v != end; ++v)
  903. _cont.push_back(*v);
  904. }
  905. template<> inline void VersionContainer<std::vector<pkgCache::VerIterator> >::insert(const_iterator begin, const_iterator end) {
  906. for (const_iterator v = begin; v != end; ++v)
  907. _cont.push_back(*v);
  908. }
  909. /*}}}*/
  910. template<> template<class Compare> inline bool VersionContainer<std::vector<pkgCache::VerIterator> >::sort(Compare Comp) {
  911. std::sort(_cont.begin(), _cont.end(), Comp);
  912. return true;
  913. }
  914. typedef VersionContainer<std::set<pkgCache::VerIterator> > VersionSet;
  915. typedef VersionContainer<std::list<pkgCache::VerIterator> > VersionList;
  916. typedef VersionContainer<std::vector<pkgCache::VerIterator> > VersionVector;
  917. }
  918. #endif