cacheset.h 54 KB

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