cacheset.h 59 KB

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