cacheset.h 61 KB

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