You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cacheset.h 56 KiB

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