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