cacheset.h 46 KB

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