cacheset.h 46 KB

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