cacheset.h 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  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::template iterator_base<iterator_type>
  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 static_cast<iterator_type const*>(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 typename Container::value_type getType(void) const { 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 typename Container::value_type getType(void) const { 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 typename Container::value_type getType(void) const { 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 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 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. 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. // FIXME: This is a bloody hack removed soon. Use CacheSetHelper::PkgSelector !
  308. enum APT_DEPRECATED Constructor { UNKNOWN = CacheSetHelper::UNKNOWN,
  309. REGEX = CacheSetHelper::REGEX,
  310. TASK = CacheSetHelper::TASK,
  311. FNMATCH = CacheSetHelper::FNMATCH };
  312. APT_IGNORE_DEPRECATED_PUSH
  313. void setConstructor(Constructor const by) { ConstructedBy = (CacheSetHelper::PkgSelector)by; }
  314. APT_IGNORE_DEPRECATED_POP
  315. void setConstructor(CacheSetHelper::PkgSelector const by) { ConstructedBy = by; }
  316. CacheSetHelper::PkgSelector getConstructor() const { return ConstructedBy; }
  317. PackageContainerInterface();
  318. explicit PackageContainerInterface(CacheSetHelper::PkgSelector const by);
  319. PackageContainerInterface& operator=(PackageContainerInterface const &other);
  320. virtual ~PackageContainerInterface();
  321. APT_DEPRECATED static bool FromTask(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) {
  322. return helper.PackageFrom(CacheSetHelper::TASK, pci, Cache, pattern); }
  323. APT_DEPRECATED static bool FromRegEx(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) {
  324. return helper.PackageFrom(CacheSetHelper::REGEX, pci, Cache, pattern); }
  325. APT_DEPRECATED static bool FromFnmatch(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) {
  326. return helper.PackageFrom(CacheSetHelper::FNMATCH, pci, Cache, pattern); }
  327. APT_DEPRECATED static bool FromGroup(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) {
  328. return helper.PackageFrom(CacheSetHelper::PACKAGENAME, pci, Cache, pattern); }
  329. APT_DEPRECATED static bool FromString(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper) {
  330. return helper.PackageFrom(CacheSetHelper::STRING, pci, Cache, pattern); }
  331. APT_DEPRECATED static bool FromCommandLine(PackageContainerInterface * const pci, pkgCacheFile &Cache, const char **cmdline, CacheSetHelper &helper) {
  332. return helper.PackageFromCommandLine(pci, Cache, cmdline); }
  333. APT_DEPRECATED typedef CacheSetHelper::PkgModifier Modifier;
  334. APT_IGNORE_DEPRECATED_PUSH
  335. APT_DEPRECATED static pkgCache::PkgIterator FromName(pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper) {
  336. return helper.PackageFromName(Cache, pattern); }
  337. APT_DEPRECATED static bool FromModifierCommandLine(unsigned short &modID, PackageContainerInterface * const pci,
  338. pkgCacheFile &Cache, const char * cmdline,
  339. std::list<Modifier> const &mods, CacheSetHelper &helper) {
  340. return helper.PackageFromModifierCommandLine(modID, pci, Cache, cmdline, mods); }
  341. APT_IGNORE_DEPRECATED_POP
  342. private:
  343. CacheSetHelper::PkgSelector ConstructedBy;
  344. void * const d;
  345. };
  346. /*}}}*/
  347. template<class Container> class PackageContainer : public PackageContainerInterface {/*{{{*/
  348. /** \class APT::PackageContainer
  349. Simple wrapper around a container class like std::set to provide a similar
  350. interface to a set of packages as to the complete set of all packages in the
  351. pkgCache. */
  352. Container _cont;
  353. public: /*{{{*/
  354. /** \brief smell like a pkgCache::PkgIterator */
  355. typedef Container_const_iterator<PackageContainerInterface, Container, PackageContainer> const_iterator;
  356. typedef Container_iterator<PackageContainerInterface, Container, PackageContainer> iterator;
  357. typedef Container_const_reverse_iterator<PackageContainerInterface, Container, PackageContainer> const_reverse_iterator;
  358. typedef Container_reverse_iterator<PackageContainerInterface, Container, PackageContainer> reverse_iterator;
  359. bool insert(pkgCache::PkgIterator const &P) APT_OVERRIDE { if (P.end() == true) return false; _cont.insert(P); return true; }
  360. template<class Cont> void insert(PackageContainer<Cont> const &pkgcont) { _cont.insert((typename Cont::const_iterator)pkgcont.begin(), (typename Cont::const_iterator)pkgcont.end()); }
  361. void insert(const_iterator begin, const_iterator end) { _cont.insert(begin, end); }
  362. bool empty() const APT_OVERRIDE { return _cont.empty(); }
  363. void clear() APT_OVERRIDE { return _cont.clear(); }
  364. size_t size() const APT_OVERRIDE { return _cont.size(); }
  365. #if __cplusplus >= 201103L
  366. iterator erase( const_iterator pos ) { return iterator(_cont.erase(pos._iter)); }
  367. iterator erase( const_iterator first, const_iterator last ) { return iterator(_cont.erase(first._iter, last._iter)); }
  368. #else
  369. iterator erase( iterator pos ) { return iterator(_cont.erase(pos._iter)); }
  370. iterator erase( iterator first, iterator last ) { return iterator(_cont.erase(first._iter, last._iter)); }
  371. #endif
  372. const_iterator begin() const { return const_iterator(_cont.begin()); }
  373. const_iterator end() const { return const_iterator(_cont.end()); }
  374. const_reverse_iterator rbegin() const { return const_reverse_iterator(_cont.rbegin()); }
  375. const_reverse_iterator rend() const { return const_reverse_iterator(_cont.rend()); }
  376. #if __cplusplus >= 201103L
  377. const_iterator cbegin() const { return const_iterator(_cont.cbegin()); }
  378. const_iterator cend() const { return const_iterator(_cont.cend()); }
  379. const_reverse_iterator crbegin() const { return const_reverse_iterator(_cont.crbegin()); }
  380. const_reverse_iterator crend() const { return const_reverse_iterator(_cont.crend()); }
  381. #endif
  382. iterator begin() { return iterator(_cont.begin()); }
  383. iterator end() { return iterator(_cont.end()); }
  384. reverse_iterator rbegin() { return reverse_iterator(_cont.rbegin()); }
  385. reverse_iterator rend() { return reverse_iterator(_cont.rend()); }
  386. const_iterator find(pkgCache::PkgIterator const &P) const { return const_iterator(_cont.find(P)); }
  387. PackageContainer() : PackageContainerInterface(CacheSetHelper::UNKNOWN) {}
  388. explicit PackageContainer(CacheSetHelper::PkgSelector const &by) : PackageContainerInterface(by) {}
  389. APT_IGNORE_DEPRECATED_PUSH
  390. APT_DEPRECATED PackageContainer(Constructor const &by) : PackageContainerInterface((CacheSetHelper::PkgSelector)by) {}
  391. APT_IGNORE_DEPRECATED_POP
  392. /** \brief sort all included versions with given comparer
  393. Some containers are sorted by default, some are not and can't be,
  394. but a few like std::vector can be sorted if need be, so this can be
  395. specialized in later on. The default is that this will fail though.
  396. Specifically, already sorted containers like std::set will return
  397. false as well as there is no easy way to check that the given comparer
  398. would sort in the same way the set is currently sorted
  399. \return \b true if the set was sorted, \b false if not. */
  400. template<class Compare> bool sort(Compare /*Comp*/) { return false; }
  401. /** \brief returns all packages in the cache who belong to the given task
  402. A simple helper responsible for search for all members of a task
  403. in the cache. Optional it prints a a notice about the
  404. packages chosen cause of the given task.
  405. \param Cache the packages are in
  406. \param pattern name of the task
  407. \param helper responsible for error and message handling */
  408. static PackageContainer FromTask(pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper) {
  409. PackageContainer cont(CacheSetHelper::TASK);
  410. helper.PackageFrom(CacheSetHelper::TASK, &cont, Cache, pattern);
  411. return cont;
  412. }
  413. static PackageContainer FromTask(pkgCacheFile &Cache, std::string const &pattern) {
  414. CacheSetHelper helper;
  415. return FromTask(Cache, pattern, helper);
  416. }
  417. /** \brief returns all packages in the cache whose name matchs a given pattern
  418. A simple helper responsible for executing a regular expression on all
  419. package names in the cache. Optional it prints a a notice about the
  420. packages chosen cause of the given package.
  421. \param Cache the packages are in
  422. \param pattern regular expression for package names
  423. \param helper responsible for error and message handling */
  424. static PackageContainer FromRegEx(pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper) {
  425. PackageContainer cont(CacheSetHelper::REGEX);
  426. helper.PackageFrom(CacheSetHelper::REGEX, &cont, Cache, pattern);
  427. return cont;
  428. }
  429. static PackageContainer FromRegEx(pkgCacheFile &Cache, std::string const &pattern) {
  430. CacheSetHelper helper;
  431. return FromRegEx(Cache, pattern, helper);
  432. }
  433. static PackageContainer FromFnmatch(pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper) {
  434. PackageContainer cont(CacheSetHelper::FNMATCH);
  435. helper.PackageFrom(CacheSetHelper::FNMATCH, &cont, Cache, pattern);
  436. return cont;
  437. }
  438. static PackageContainer FromFnMatch(pkgCacheFile &Cache, std::string const &pattern) {
  439. CacheSetHelper helper;
  440. return FromFnmatch(Cache, pattern, helper);
  441. }
  442. APT_IGNORE_DEPRECATED_PUSH
  443. /** \brief returns a package specified by a string
  444. \param Cache the package is in
  445. \param pattern String the package name should be extracted from
  446. \param helper responsible for error and message handling */
  447. APT_DEPRECATED static pkgCache::PkgIterator FromName(pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper) {
  448. return helper.PackageFromName(Cache, pattern);
  449. }
  450. APT_DEPRECATED static pkgCache::PkgIterator FromName(pkgCacheFile &Cache, std::string const &pattern) {
  451. CacheSetHelper helper;
  452. return FromName(Cache, pattern, helper);
  453. }
  454. APT_IGNORE_DEPRECATED_POP
  455. /** \brief returns all packages specified by a string
  456. \param Cache the packages are in
  457. \param pattern String the package name(s) should be extracted from
  458. \param helper responsible for error and message handling */
  459. static PackageContainer FromString(pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper) {
  460. PackageContainer cont;
  461. helper.PackageFrom(CacheSetHelper::PACKAGENAME, &cont, Cache, pattern);
  462. return cont;
  463. }
  464. static PackageContainer FromString(pkgCacheFile &Cache, std::string const &pattern) {
  465. CacheSetHelper helper;
  466. return FromString(Cache, pattern, helper);
  467. }
  468. /** \brief returns all packages specified on the commandline
  469. Get all package names from the commandline and executes regex's if needed.
  470. No special package command is supported, just plain names.
  471. \param Cache the packages are in
  472. \param cmdline Command line the package names should be extracted from
  473. \param helper responsible for error and message handling */
  474. static PackageContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline, CacheSetHelper &helper) {
  475. PackageContainer cont;
  476. helper.PackageFromCommandLine(&cont, Cache, cmdline);
  477. return cont;
  478. }
  479. static PackageContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline) {
  480. CacheSetHelper helper;
  481. return FromCommandLine(Cache, cmdline, helper);
  482. }
  483. /** \brief group packages by a action modifiers
  484. At some point it is needed to get from the same commandline
  485. different package sets grouped by a modifier. Take
  486. apt-get install apt awesome-
  487. as an example.
  488. \param Cache the packages are in
  489. \param cmdline Command line the package names should be extracted from
  490. \param mods list of modifiers the method should accept
  491. \param fallback the default modifier group for a package
  492. \param helper responsible for error and message handling */
  493. static std::map<unsigned short, PackageContainer> GroupedFromCommandLine(
  494. pkgCacheFile &Cache,
  495. const char **cmdline,
  496. std::list<CacheSetHelper::PkgModifier> const &mods,
  497. unsigned short const &fallback,
  498. CacheSetHelper &helper) {
  499. std::map<unsigned short, PackageContainer> pkgsets;
  500. for (const char **I = cmdline; *I != 0; ++I) {
  501. unsigned short modID = fallback;
  502. PackageContainer pkgset;
  503. helper.PackageFromModifierCommandLine(modID, &pkgset, Cache, *I, mods);
  504. pkgsets[modID].insert(pkgset);
  505. }
  506. return pkgsets;
  507. }
  508. static std::map<unsigned short, PackageContainer> GroupedFromCommandLine(
  509. pkgCacheFile &Cache,
  510. const char **cmdline,
  511. std::list<CacheSetHelper::PkgModifier> const &mods,
  512. unsigned short const &fallback) {
  513. CacheSetHelper helper;
  514. return GroupedFromCommandLine(Cache, cmdline,
  515. mods, fallback, helper);
  516. }
  517. /*}}}*/
  518. }; /*}}}*/
  519. // various specialisations for PackageContainer /*{{{*/
  520. template<> template<class Cont> void PackageContainer<std::list<pkgCache::PkgIterator> >::insert(PackageContainer<Cont> const &pkgcont) {
  521. for (typename PackageContainer<Cont>::const_iterator p = pkgcont.begin(); p != pkgcont.end(); ++p)
  522. _cont.push_back(*p);
  523. }
  524. #if __cplusplus >= 201103L
  525. template<> template<class Cont> void PackageContainer<std::forward_list<pkgCache::PkgIterator> >::insert(PackageContainer<Cont> const &pkgcont) {
  526. for (typename PackageContainer<Cont>::const_iterator p = pkgcont.begin(); p != pkgcont.end(); ++p)
  527. _cont.push_front(*p);
  528. }
  529. #endif
  530. template<> template<class Cont> void PackageContainer<std::deque<pkgCache::PkgIterator> >::insert(PackageContainer<Cont> const &pkgcont) {
  531. for (typename PackageContainer<Cont>::const_iterator p = pkgcont.begin(); p != pkgcont.end(); ++p)
  532. _cont.push_back(*p);
  533. }
  534. template<> template<class Cont> void PackageContainer<std::vector<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. // these are 'inline' as otherwise the linker has problems with seeing these untemplated
  539. // specializations again and again - but we need to see them, so that library users can use them
  540. template<> inline bool PackageContainer<std::list<pkgCache::PkgIterator> >::insert(pkgCache::PkgIterator const &P) {
  541. if (P.end() == true)
  542. return false;
  543. _cont.push_back(P);
  544. return true;
  545. }
  546. #if __cplusplus >= 201103L
  547. template<> inline bool PackageContainer<std::forward_list<pkgCache::PkgIterator> >::insert(pkgCache::PkgIterator const &P) {
  548. if (P.end() == true)
  549. return false;
  550. _cont.push_front(P);
  551. return true;
  552. }
  553. #endif
  554. template<> inline bool PackageContainer<std::deque<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. template<> inline bool PackageContainer<std::vector<pkgCache::PkgIterator> >::insert(pkgCache::PkgIterator const &P) {
  561. if (P.end() == true)
  562. return false;
  563. _cont.push_back(P);
  564. return true;
  565. }
  566. template<> inline void PackageContainer<std::list<pkgCache::PkgIterator> >::insert(const_iterator begin, const_iterator end) {
  567. for (const_iterator p = begin; p != end; ++p)
  568. _cont.push_back(*p);
  569. }
  570. #if __cplusplus >= 201103L
  571. template<> inline void PackageContainer<std::forward_list<pkgCache::PkgIterator> >::insert(const_iterator begin, const_iterator end) {
  572. for (const_iterator p = begin; p != end; ++p)
  573. _cont.push_front(*p);
  574. }
  575. #endif
  576. template<> inline void PackageContainer<std::deque<pkgCache::PkgIterator> >::insert(const_iterator begin, const_iterator end) {
  577. for (const_iterator p = begin; p != end; ++p)
  578. _cont.push_back(*p);
  579. }
  580. template<> inline void PackageContainer<std::vector<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 PackageContainer<std::set<pkgCache::PkgIterator> >::iterator PackageContainer<std::set<pkgCache::PkgIterator> >::erase(iterator i) {
  586. _cont.erase(i._iter);
  587. return end();
  588. }
  589. template<> inline PackageContainer<std::set<pkgCache::PkgIterator> >::iterator PackageContainer<std::set<pkgCache::PkgIterator> >::erase(iterator first, iterator last) {
  590. _cont.erase(first, last);
  591. return end();
  592. }
  593. #endif
  594. template<> template<class Compare> inline bool PackageContainer<std::vector<pkgCache::PkgIterator> >::sort(Compare Comp) {
  595. std::sort(_cont.begin(), _cont.end(), Comp);
  596. return true;
  597. }
  598. template<> template<class Compare> inline bool PackageContainer<std::list<pkgCache::PkgIterator> >::sort(Compare Comp) {
  599. _cont.sort(Comp);
  600. return true;
  601. }
  602. #if __cplusplus >= 201103L
  603. template<> template<class Compare> inline bool PackageContainer<std::forward_list<pkgCache::PkgIterator> >::sort(Compare Comp) {
  604. _cont.sort(Comp);
  605. return true;
  606. }
  607. #endif
  608. template<> template<class Compare> inline bool PackageContainer<std::deque<pkgCache::PkgIterator> >::sort(Compare Comp) {
  609. std::sort(_cont.begin(), _cont.end(), Comp);
  610. return true;
  611. }
  612. /*}}}*/
  613. // class PackageUniverse - pkgCache as PackageContainerInterface /*{{{*/
  614. /** \class PackageUniverse
  615. Wraps around our usual pkgCache, so that it can be stuffed into methods
  616. expecting a PackageContainer.
  617. The wrapping is read-only in practice modeled by making erase and co
  618. private methods. */
  619. class APT_PUBLIC PackageUniverse : public PackageContainerInterface {
  620. pkgCache * const _cont;
  621. void * const d;
  622. public:
  623. class const_iterator : public APT::Container_iterator_base<APT::PackageContainerInterface, PackageUniverse, PackageUniverse::const_iterator, pkgCache::PkgIterator, pkgCache::PkgIterator>
  624. {
  625. public:
  626. explicit const_iterator(pkgCache::PkgIterator i):
  627. Container_iterator_base<APT::PackageContainerInterface, PackageUniverse, PackageUniverse::const_iterator, pkgCache::PkgIterator, pkgCache::PkgIterator>(i) {}
  628. inline pkgCache::PkgIterator getType(void) const { return _iter; }
  629. };
  630. typedef const_iterator iterator;
  631. bool empty() const APT_OVERRIDE { return false; }
  632. size_t size() const APT_OVERRIDE { return _cont->Head().PackageCount; }
  633. const_iterator begin() const { return const_iterator(_cont->PkgBegin()); }
  634. const_iterator end() const { return const_iterator(_cont->PkgEnd()); }
  635. const_iterator cbegin() const { return const_iterator(_cont->PkgBegin()); }
  636. const_iterator cend() const { return const_iterator(_cont->PkgEnd()); }
  637. iterator begin() { return iterator(_cont->PkgBegin()); }
  638. iterator end() { return iterator(_cont->PkgEnd()); }
  639. pkgCache * data() const { return _cont; }
  640. explicit PackageUniverse(pkgCache * const Owner);
  641. explicit PackageUniverse(pkgCacheFile * const Owner);
  642. virtual ~PackageUniverse();
  643. private:
  644. APT_HIDDEN bool insert(pkgCache::PkgIterator const &) APT_OVERRIDE { return true; }
  645. template<class Cont> APT_HIDDEN void insert(PackageContainer<Cont> const &) { }
  646. APT_HIDDEN void insert(const_iterator, const_iterator) { }
  647. APT_HIDDEN void clear() APT_OVERRIDE { }
  648. APT_HIDDEN iterator erase( const_iterator pos );
  649. APT_HIDDEN iterator erase( const_iterator first, const_iterator last );
  650. };
  651. /*}}}*/
  652. typedef PackageContainer<std::set<pkgCache::PkgIterator> > PackageSet;
  653. #if __cplusplus >= 201103L
  654. typedef PackageContainer<std::unordered_set<pkgCache::PkgIterator> > PackageUnorderedSet;
  655. typedef PackageContainer<std::forward_list<pkgCache::PkgIterator> > PackageForwardList;
  656. #endif
  657. typedef PackageContainer<std::list<pkgCache::PkgIterator> > PackageList;
  658. typedef PackageContainer<std::deque<pkgCache::PkgIterator> > PackageDeque;
  659. typedef PackageContainer<std::vector<pkgCache::PkgIterator> > PackageVector;
  660. class VersionContainerInterface { /*{{{*/
  661. /** \class APT::VersionContainerInterface
  662. Same as APT::PackageContainerInterface, just for Versions */
  663. public:
  664. /** \brief smell like a pkgCache::VerIterator */
  665. template<class Itr> class iterator_base { /*{{{*/
  666. pkgCache::VerIterator getType() const { return static_cast<Itr const*>(this)->getType(); };
  667. public:
  668. operator pkgCache::VerIterator(void) { return getType(); }
  669. inline pkgCache *Cache() const { return getType().Cache(); }
  670. inline unsigned long Index() const {return getType().Index();}
  671. inline int CompareVer(const pkgCache::VerIterator &B) const { return getType().CompareVer(B); }
  672. inline const char *VerStr() const { return getType().VerStr(); }
  673. inline const char *Section() const { return getType().Section(); }
  674. inline const char *Arch() const { return getType().Arch(); }
  675. inline pkgCache::PkgIterator ParentPkg() const { return getType().ParentPkg(); }
  676. inline pkgCache::DescIterator DescriptionList() const { return getType().DescriptionList(); }
  677. inline pkgCache::DescIterator TranslatedDescription() const { return getType().TranslatedDescription(); }
  678. inline pkgCache::DepIterator DependsList() const { return getType().DependsList(); }
  679. inline pkgCache::PrvIterator ProvidesList() const { return getType().ProvidesList(); }
  680. inline pkgCache::VerFileIterator FileList() const { return getType().FileList(); }
  681. inline bool Downloadable() const { return getType().Downloadable(); }
  682. inline const char *PriorityType() const { return getType().PriorityType(); }
  683. inline std::string RelStr() const { return getType().RelStr(); }
  684. inline bool Automatic() const { return getType().Automatic(); }
  685. inline pkgCache::VerFileIterator NewestFile() const { return getType().NewestFile(); }
  686. // we have only valid iterators here
  687. inline bool end() const { return false; }
  688. inline pkgCache::Version const * operator->() const { return &*getType(); }
  689. };
  690. /*}}}*/
  691. virtual bool insert(pkgCache::VerIterator const &V) = 0;
  692. virtual bool empty() const = 0;
  693. virtual void clear() = 0;
  694. virtual size_t size() const = 0;
  695. /** \brief specifies which version(s) will be returned if non is given */
  696. enum APT_DEPRECATED Version {
  697. ALL = CacheSetHelper::ALL,
  698. CANDANDINST = CacheSetHelper::CANDANDINST,
  699. CANDIDATE = CacheSetHelper::CANDIDATE,
  700. INSTALLED = CacheSetHelper::INSTALLED,
  701. CANDINST = CacheSetHelper::CANDINST,
  702. INSTCAND = CacheSetHelper::INSTCAND,
  703. NEWEST = CacheSetHelper::NEWEST
  704. };
  705. struct Modifier {
  706. unsigned short const ID;
  707. const char * const Alias;
  708. enum Position { NONE, PREFIX, POSTFIX } const Pos;
  709. enum CacheSetHelper::VerSelector const SelectVersion;
  710. Modifier (unsigned short const &id, const char * const alias, Position const &pos,
  711. enum CacheSetHelper::VerSelector const select) : ID(id), Alias(alias), Pos(pos),
  712. SelectVersion(select) {}
  713. APT_IGNORE_DEPRECATED_PUSH
  714. APT_DEPRECATED Modifier(unsigned short const &id, const char * const alias, Position const &pos,
  715. Version const &select) : ID(id), Alias(alias), Pos(pos),
  716. SelectVersion((CacheSetHelper::VerSelector)select) {}
  717. APT_IGNORE_DEPRECATED_POP
  718. };
  719. static bool FromCommandLine(VersionContainerInterface * const vci, pkgCacheFile &Cache,
  720. const char **cmdline, CacheSetHelper::VerSelector const fallback,
  721. CacheSetHelper &helper);
  722. APT_IGNORE_DEPRECATED_PUSH
  723. APT_DEPRECATED static bool FromCommandLine(VersionContainerInterface * const vci, pkgCacheFile &Cache,
  724. const char **cmdline, Version const &fallback,
  725. CacheSetHelper &helper) {
  726. return FromCommandLine(vci, Cache, cmdline, (CacheSetHelper::VerSelector)fallback, helper);
  727. }
  728. APT_IGNORE_DEPRECATED_POP
  729. static bool FromString(VersionContainerInterface * const vci, pkgCacheFile &Cache,
  730. std::string pkg, CacheSetHelper::VerSelector const fallback, CacheSetHelper &helper,
  731. bool const onlyFromName = false);
  732. APT_IGNORE_DEPRECATED_PUSH
  733. APT_DEPRECATED static bool FromString(VersionContainerInterface * const vci, pkgCacheFile &Cache,
  734. std::string pkg, Version const &fallback, CacheSetHelper &helper,
  735. bool const onlyFromName = false) {
  736. return FromString(vci, Cache, pkg, (CacheSetHelper::VerSelector)fallback, helper, onlyFromName);
  737. }
  738. APT_IGNORE_DEPRECATED_POP
  739. static bool FromPackage(VersionContainerInterface * const vci, pkgCacheFile &Cache,
  740. pkgCache::PkgIterator const &P, CacheSetHelper::VerSelector const fallback,
  741. CacheSetHelper &helper);
  742. APT_IGNORE_DEPRECATED_PUSH
  743. APT_DEPRECATED static bool FromPackage(VersionContainerInterface * const vci, pkgCacheFile &Cache,
  744. pkgCache::PkgIterator const &P, Version const &fallback,
  745. CacheSetHelper &helper) {
  746. return FromPackage(vci, Cache, P, (CacheSetHelper::VerSelector)fallback, helper);
  747. }
  748. APT_IGNORE_DEPRECATED_POP
  749. static bool FromModifierCommandLine(unsigned short &modID,
  750. VersionContainerInterface * const vci,
  751. pkgCacheFile &Cache, const char * cmdline,
  752. std::list<Modifier> const &mods,
  753. CacheSetHelper &helper);
  754. static bool FromDependency(VersionContainerInterface * const vci,
  755. pkgCacheFile &Cache,
  756. pkgCache::DepIterator const &D,
  757. CacheSetHelper::VerSelector const selector,
  758. CacheSetHelper &helper);
  759. APT_IGNORE_DEPRECATED_PUSH
  760. APT_DEPRECATED static bool FromDependency(VersionContainerInterface * const vci,
  761. pkgCacheFile &Cache,
  762. pkgCache::DepIterator const &D,
  763. Version const &selector,
  764. CacheSetHelper &helper) {
  765. return FromDependency(vci, Cache, D, (CacheSetHelper::VerSelector)selector, helper);
  766. }
  767. APT_IGNORE_DEPRECATED_POP
  768. VersionContainerInterface();
  769. VersionContainerInterface& operator=(VersionContainerInterface const &other);
  770. virtual ~VersionContainerInterface();
  771. private:
  772. void * const d;
  773. protected: /*{{{*/
  774. /** \brief returns the candidate version of the package
  775. \param Cache to be used to query for information
  776. \param Pkg we want the candidate version from this package
  777. \param helper used in this container instance */
  778. static pkgCache::VerIterator getCandidateVer(pkgCacheFile &Cache,
  779. pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper);
  780. /** \brief returns the installed version of the package
  781. \param Cache to be used to query for information
  782. \param Pkg we want the installed version from this package
  783. \param helper used in this container instance */
  784. static pkgCache::VerIterator getInstalledVer(pkgCacheFile &Cache,
  785. pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper);
  786. /*}}}*/
  787. };
  788. /*}}}*/
  789. template<class Container> class VersionContainer : public VersionContainerInterface {/*{{{*/
  790. /** \class APT::VersionContainer
  791. Simple wrapper around a container class like std::set to provide a similar
  792. interface to a set of versions as to the complete set of all versions in the
  793. pkgCache. */
  794. Container _cont;
  795. public: /*{{{*/
  796. typedef Container_const_iterator<VersionContainerInterface, Container, VersionContainer> const_iterator;
  797. typedef Container_iterator<VersionContainerInterface, Container, VersionContainer> iterator;
  798. typedef Container_const_reverse_iterator<VersionContainerInterface, Container, VersionContainer> const_reverse_iterator;
  799. typedef Container_reverse_iterator<VersionContainerInterface, Container, VersionContainer> reverse_iterator;
  800. bool insert(pkgCache::VerIterator const &V) APT_OVERRIDE { if (V.end() == true) return false; _cont.insert(V); return true; }
  801. template<class Cont> void insert(VersionContainer<Cont> const &vercont) { _cont.insert((typename Cont::const_iterator)vercont.begin(), (typename Cont::const_iterator)vercont.end()); }
  802. void insert(const_iterator begin, const_iterator end) { _cont.insert(begin, end); }
  803. bool empty() const APT_OVERRIDE { return _cont.empty(); }
  804. void clear() APT_OVERRIDE { return _cont.clear(); }
  805. size_t size() const APT_OVERRIDE { return _cont.size(); }
  806. #if __cplusplus >= 201103L
  807. iterator erase( const_iterator pos ) { return iterator(_cont.erase(pos._iter)); }
  808. iterator erase( const_iterator first, const_iterator last ) { return iterator(_cont.erase(first._iter, last._iter)); }
  809. #else
  810. iterator erase( iterator pos ) { return iterator(_cont.erase(pos._iter)); }
  811. iterator erase( iterator first, iterator last ) { return iterator(_cont.erase(first._iter, last._iter)); }
  812. #endif
  813. const_iterator begin() const { return const_iterator(_cont.begin()); }
  814. const_iterator end() const { return const_iterator(_cont.end()); }
  815. const_reverse_iterator rbegin() const { return const_reverse_iterator(_cont.rbegin()); }
  816. const_reverse_iterator rend() const { return const_reverse_iterator(_cont.rend()); }
  817. #if __cplusplus >= 201103L
  818. const_iterator cbegin() const { return const_iterator(_cont.cbegin()); }
  819. const_iterator cend() const { return const_iterator(_cont.cend()); }
  820. const_reverse_iterator crbegin() const { return const_reverse_iterator(_cont.crbegin()); }
  821. const_reverse_iterator crend() const { return const_reverse_iterator(_cont.crend()); }
  822. #endif
  823. iterator begin() { return iterator(_cont.begin()); }
  824. iterator end() { return iterator(_cont.end()); }
  825. reverse_iterator rbegin() { return reverse_iterator(_cont.rbegin()); }
  826. reverse_iterator rend() { return reverse_iterator(_cont.rend()); }
  827. const_iterator find(pkgCache::VerIterator const &V) const { return const_iterator(_cont.find(V)); }
  828. /** \brief sort all included versions with given comparer
  829. Some containers are sorted by default, some are not and can't be,
  830. but a few like std::vector can be sorted if need be, so this can be
  831. specialized in later on. The default is that this will fail though.
  832. Specifically, already sorted containers like std::set will return
  833. false as well as there is no easy way to check that the given comparer
  834. would sort in the same way the set is currently sorted
  835. \return \b true if the set was sorted, \b false if not. */
  836. template<class Compare> bool sort(Compare /*Comp*/) { return false; }
  837. /** \brief returns all versions specified on the commandline
  838. Get all versions from the commandline, uses given default version if
  839. non specifically requested and executes regex's if needed on names.
  840. \param Cache the packages and versions are in
  841. \param cmdline Command line the versions should be extracted from
  842. \param fallback version specification
  843. \param helper responsible for error and message handling */
  844. static VersionContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
  845. CacheSetHelper::VerSelector const fallback, CacheSetHelper &helper) {
  846. VersionContainer vercon;
  847. VersionContainerInterface::FromCommandLine(&vercon, Cache, cmdline, fallback, helper);
  848. return vercon;
  849. }
  850. static VersionContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
  851. CacheSetHelper::VerSelector const fallback) {
  852. CacheSetHelper helper;
  853. return FromCommandLine(Cache, cmdline, fallback, helper);
  854. }
  855. static VersionContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline) {
  856. return FromCommandLine(Cache, cmdline, CacheSetHelper::CANDINST);
  857. }
  858. static VersionContainer FromString(pkgCacheFile &Cache, std::string const &pkg,
  859. CacheSetHelper::VerSelector const fallback, CacheSetHelper &helper,
  860. bool const /*onlyFromName = false*/) {
  861. VersionContainer vercon;
  862. VersionContainerInterface::FromString(&vercon, Cache, pkg, fallback, helper);
  863. return vercon;
  864. }
  865. static VersionContainer FromString(pkgCacheFile &Cache, std::string pkg,
  866. CacheSetHelper::VerSelector const fallback) {
  867. CacheSetHelper helper;
  868. return FromString(Cache, pkg, fallback, helper);
  869. }
  870. static VersionContainer FromString(pkgCacheFile &Cache, std::string pkg) {
  871. return FromString(Cache, pkg, CacheSetHelper::CANDINST);
  872. }
  873. APT_IGNORE_DEPRECATED_PUSH
  874. static VersionContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
  875. Version const &fallback, CacheSetHelper &helper) {
  876. VersionContainer vercon;
  877. VersionContainerInterface::FromCommandLine(&vercon, Cache, cmdline, (CacheSetHelper::VerSelector)fallback, helper);
  878. return vercon;
  879. }
  880. static VersionContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
  881. Version const &fallback) {
  882. CacheSetHelper helper;
  883. return FromCommandLine(Cache, cmdline, (CacheSetHelper::VerSelector)fallback, helper);
  884. }
  885. static VersionContainer FromString(pkgCacheFile &Cache, std::string const &pkg,
  886. Version const &fallback, CacheSetHelper &helper,
  887. bool const /*onlyFromName = false*/) {
  888. VersionContainer vercon;
  889. VersionContainerInterface::FromString(&vercon, Cache, pkg, (CacheSetHelper::VerSelector)fallback, helper);
  890. return vercon;
  891. }
  892. static VersionContainer FromString(pkgCacheFile &Cache, std::string pkg,
  893. Version const &fallback) {
  894. CacheSetHelper helper;
  895. return FromString(Cache, pkg, (CacheSetHelper::VerSelector)fallback, helper);
  896. }
  897. APT_IGNORE_DEPRECATED_POP
  898. /** \brief returns all versions specified for the package
  899. \param Cache the package and versions are in
  900. \param P the package in question
  901. \param fallback the version(s) you want to get
  902. \param helper the helper used for display and error handling */
  903. static VersionContainer FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P,
  904. CacheSetHelper::VerSelector const fallback, CacheSetHelper &helper) {
  905. VersionContainer vercon;
  906. VersionContainerInterface::FromPackage(&vercon, Cache, P, fallback, helper);
  907. return vercon;
  908. }
  909. static VersionContainer FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P,
  910. CacheSetHelper::VerSelector const fallback) {
  911. CacheSetHelper helper;
  912. return FromPackage(Cache, P, fallback, helper);
  913. }
  914. APT_IGNORE_DEPRECATED_PUSH
  915. static VersionContainer FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P,
  916. Version const &fallback, CacheSetHelper &helper) {
  917. VersionContainer vercon;
  918. VersionContainerInterface::FromPackage(&vercon, Cache, P, (CacheSetHelper::VerSelector)fallback, helper);
  919. return vercon;
  920. }
  921. static VersionContainer FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P,
  922. Version const &fallback) {
  923. CacheSetHelper helper;
  924. return FromPackage(Cache, P, (CacheSetHelper::VerSelector)fallback, helper);
  925. }
  926. APT_IGNORE_DEPRECATED_POP
  927. static VersionContainer FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P) {
  928. return FromPackage(Cache, P, CacheSetHelper::CANDIDATE);
  929. }
  930. static std::map<unsigned short, VersionContainer> GroupedFromCommandLine(
  931. pkgCacheFile &Cache,
  932. const char **cmdline,
  933. std::list<Modifier> const &mods,
  934. unsigned short const fallback,
  935. CacheSetHelper &helper) {
  936. std::map<unsigned short, VersionContainer> versets;
  937. for (const char **I = cmdline; *I != 0; ++I) {
  938. unsigned short modID = fallback;
  939. VersionContainer verset;
  940. VersionContainerInterface::FromModifierCommandLine(modID, &verset, Cache, *I, mods, helper);
  941. versets[modID].insert(verset);
  942. }
  943. return versets;
  944. }
  945. static std::map<unsigned short, VersionContainer> GroupedFromCommandLine(
  946. pkgCacheFile &Cache, const char **cmdline,
  947. std::list<Modifier> const &mods,
  948. unsigned short const fallback) {
  949. CacheSetHelper helper;
  950. return GroupedFromCommandLine(Cache, cmdline,
  951. mods, fallback, helper);
  952. }
  953. static VersionContainer FromDependency(pkgCacheFile &Cache, pkgCache::DepIterator const &D,
  954. CacheSetHelper::VerSelector const selector, CacheSetHelper &helper) {
  955. VersionContainer vercon;
  956. VersionContainerInterface::FromDependency(&vercon, Cache, D, selector, helper);
  957. return vercon;
  958. }
  959. static VersionContainer FromDependency(pkgCacheFile &Cache, pkgCache::DepIterator const &D,
  960. CacheSetHelper::VerSelector const selector) {
  961. CacheSetHelper helper;
  962. return FromDependency(Cache, D, selector, helper);
  963. }
  964. APT_IGNORE_DEPRECATED_PUSH
  965. static VersionContainer FromDependency(pkgCacheFile &Cache, pkgCache::DepIterator const &D,
  966. Version const &selector, CacheSetHelper &helper) {
  967. VersionContainer vercon;
  968. VersionContainerInterface::FromDependency(&vercon, Cache, D, (CacheSetHelper::VerSelector)selector, helper);
  969. return vercon;
  970. }
  971. static VersionContainer FromDependency(pkgCacheFile &Cache, pkgCache::DepIterator const &D,
  972. Version const &selector) {
  973. CacheSetHelper helper;
  974. return FromDependency(Cache, D, (CacheSetHelper::VerSelector)selector, helper);
  975. }
  976. APT_IGNORE_DEPRECATED_POP
  977. static VersionContainer FromDependency(pkgCacheFile &Cache, pkgCache::DepIterator const &D) {
  978. return FromDependency(Cache, D, CacheSetHelper::CANDIDATE);
  979. }
  980. /*}}}*/
  981. }; /*}}}*/
  982. // various specialisations for VersionContainer /*{{{*/
  983. template<> template<class Cont> void VersionContainer<std::list<pkgCache::VerIterator> >::insert(VersionContainer<Cont> const &vercont) {
  984. for (typename VersionContainer<Cont>::const_iterator v = vercont.begin(); v != vercont.end(); ++v)
  985. _cont.push_back(*v);
  986. }
  987. #if __cplusplus >= 201103L
  988. template<> template<class Cont> void VersionContainer<std::forward_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_front(*v);
  991. }
  992. #endif
  993. template<> template<class Cont> void VersionContainer<std::deque<pkgCache::VerIterator> >::insert(VersionContainer<Cont> const &vercont) {
  994. for (typename VersionContainer<Cont>::const_iterator v = vercont.begin(); v != vercont.end(); ++v)
  995. _cont.push_back(*v);
  996. }
  997. template<> template<class Cont> void VersionContainer<std::vector<pkgCache::VerIterator> >::insert(VersionContainer<Cont> const &vercont) {
  998. for (typename VersionContainer<Cont>::const_iterator v = vercont.begin(); v != vercont.end(); ++v)
  999. _cont.push_back(*v);
  1000. }
  1001. // these are 'inline' as otherwise the linker has problems with seeing these untemplated
  1002. // specializations again and again - but we need to see them, so that library users can use them
  1003. template<> inline bool VersionContainer<std::list<pkgCache::VerIterator> >::insert(pkgCache::VerIterator const &V) {
  1004. if (V.end() == true)
  1005. return false;
  1006. _cont.push_back(V);
  1007. return true;
  1008. }
  1009. #if __cplusplus >= 201103L
  1010. template<> inline bool VersionContainer<std::forward_list<pkgCache::VerIterator> >::insert(pkgCache::VerIterator const &V) {
  1011. if (V.end() == true)
  1012. return false;
  1013. _cont.push_front(V);
  1014. return true;
  1015. }
  1016. #endif
  1017. template<> inline bool VersionContainer<std::deque<pkgCache::VerIterator> >::insert(pkgCache::VerIterator const &V) {
  1018. if (V.end() == true)
  1019. return false;
  1020. _cont.push_back(V);
  1021. return true;
  1022. }
  1023. template<> inline bool VersionContainer<std::vector<pkgCache::VerIterator> >::insert(pkgCache::VerIterator const &V) {
  1024. if (V.end() == true)
  1025. return false;
  1026. _cont.push_back(V);
  1027. return true;
  1028. }
  1029. template<> inline void VersionContainer<std::list<pkgCache::VerIterator> >::insert(const_iterator begin, const_iterator end) {
  1030. for (const_iterator v = begin; v != end; ++v)
  1031. _cont.push_back(*v);
  1032. }
  1033. #if __cplusplus >= 201103L
  1034. template<> inline void VersionContainer<std::forward_list<pkgCache::VerIterator> >::insert(const_iterator begin, const_iterator end) {
  1035. for (const_iterator v = begin; v != end; ++v)
  1036. _cont.push_front(*v);
  1037. }
  1038. #endif
  1039. template<> inline void VersionContainer<std::deque<pkgCache::VerIterator> >::insert(const_iterator begin, const_iterator end) {
  1040. for (const_iterator v = begin; v != end; ++v)
  1041. _cont.push_back(*v);
  1042. }
  1043. template<> inline void VersionContainer<std::vector<pkgCache::VerIterator> >::insert(const_iterator begin, const_iterator end) {
  1044. for (const_iterator v = begin; v != end; ++v)
  1045. _cont.push_back(*v);
  1046. }
  1047. #if __cplusplus < 201103L
  1048. template<> inline VersionContainer<std::set<pkgCache::VerIterator> >::iterator VersionContainer<std::set<pkgCache::VerIterator> >::erase(iterator i) {
  1049. _cont.erase(i._iter);
  1050. return end();
  1051. }
  1052. template<> inline VersionContainer<std::set<pkgCache::VerIterator> >::iterator VersionContainer<std::set<pkgCache::VerIterator> >::erase(iterator first, iterator last) {
  1053. _cont.erase(first, last);
  1054. return end();
  1055. }
  1056. #endif
  1057. template<> template<class Compare> inline bool VersionContainer<std::vector<pkgCache::VerIterator> >::sort(Compare Comp) {
  1058. std::sort(_cont.begin(), _cont.end(), Comp);
  1059. return true;
  1060. }
  1061. template<> template<class Compare> inline bool VersionContainer<std::list<pkgCache::VerIterator> >::sort(Compare Comp) {
  1062. _cont.sort(Comp);
  1063. return true;
  1064. }
  1065. #if __cplusplus >= 201103L
  1066. template<> template<class Compare> inline bool VersionContainer<std::forward_list<pkgCache::VerIterator> >::sort(Compare Comp) {
  1067. _cont.sort(Comp);
  1068. return true;
  1069. }
  1070. #endif
  1071. template<> template<class Compare> inline bool VersionContainer<std::deque<pkgCache::VerIterator> >::sort(Compare Comp) {
  1072. std::sort(_cont.begin(), _cont.end(), Comp);
  1073. return true;
  1074. }
  1075. /*}}}*/
  1076. typedef VersionContainer<std::set<pkgCache::VerIterator> > VersionSet;
  1077. #if __cplusplus >= 201103L
  1078. typedef VersionContainer<std::unordered_set<pkgCache::VerIterator> > VersionUnorderedSet;
  1079. typedef VersionContainer<std::forward_list<pkgCache::VerIterator> > VersionForwardList;
  1080. #endif
  1081. typedef VersionContainer<std::list<pkgCache::VerIterator> > VersionList;
  1082. typedef VersionContainer<std::deque<pkgCache::VerIterator> > VersionDeque;
  1083. typedef VersionContainer<std::vector<pkgCache::VerIterator> > VersionVector;
  1084. }
  1085. #endif