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

1230 lines
61 KiB

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