warning: extra ‘;’ [-Wpedantic]
[ntk/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 <iostream>
13 #include <fstream>
14 #include <map>
15 #include <set>
16 #include <list>
17 #include <string>
18 #include <iterator>
19
20 #include <apt-pkg/error.h>
21 #include <apt-pkg/pkgcache.h>
22
23 #ifndef APT_8_CLEANER_HEADERS
24 #include <apt-pkg/cachefile.h>
25 #endif
26 /*}}}*/
27
28 class pkgCacheFile;
29
30 namespace APT {
31 class PackageContainerInterface;
32 class VersionContainerInterface;
33
34 class CacheSetHelper { /*{{{*/
35 /** \class APT::CacheSetHelper
36 Simple base class with a lot of virtual methods which can be overridden
37 to alter the behavior or the output of the CacheSets.
38
39 This helper is passed around by the static methods in the CacheSets and
40 used every time they hit an error condition or something could be
41 printed out.
42 */
43 public: /*{{{*/
44 CacheSetHelper(bool const ShowError = true,
45 GlobalError::MsgType ErrorType = GlobalError::ERROR) :
46 ShowError(ShowError), ErrorType(ErrorType) {}
47 virtual ~CacheSetHelper() {}
48
49 virtual void showTaskSelection(pkgCache::PkgIterator const &pkg, std::string const &pattern);
50 virtual void showRegExSelection(pkgCache::PkgIterator const &pkg, std::string const &pattern);
51 #if (APT_PKG_MAJOR >= 4 && APT_PKG_MINOR >= 13)
52 virtual void showFnmatchSelection(pkgCache::PkgIterator const &pkg, std::string const &pattern);
53 #endif
54 virtual void showSelectedVersion(pkgCache::PkgIterator const &Pkg, pkgCache::VerIterator const Ver,
55 std::string const &ver, bool const verIsRel);
56
57 virtual void canNotFindTask(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern);
58 virtual void canNotFindRegEx(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern);
59 #if (APT_PKG_MAJOR >= 4 && APT_PKG_MINOR >= 13)
60 virtual void canNotFindFnmatch(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern);
61 #endif
62 virtual void canNotFindPackage(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string const &str);
63
64 virtual void canNotFindAllVer(VersionContainerInterface * const vci, pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg);
65 virtual void canNotFindInstCandVer(VersionContainerInterface * const vci, pkgCacheFile &Cache,
66 pkgCache::PkgIterator const &Pkg);
67 virtual void canNotFindCandInstVer(VersionContainerInterface * const vci,
68 pkgCacheFile &Cache,
69 pkgCache::PkgIterator const &Pkg);
70
71 virtual pkgCache::PkgIterator canNotFindPkgName(pkgCacheFile &Cache, std::string const &str);
72 virtual pkgCache::VerIterator canNotFindNewestVer(pkgCacheFile &Cache,
73 pkgCache::PkgIterator const &Pkg);
74 virtual pkgCache::VerIterator canNotFindCandidateVer(pkgCacheFile &Cache,
75 pkgCache::PkgIterator const &Pkg);
76 virtual pkgCache::VerIterator canNotFindInstalledVer(pkgCacheFile &Cache,
77 pkgCache::PkgIterator const &Pkg);
78
79 bool showErrors() const { return ShowError; }
80 bool showErrors(bool const newValue) { if (ShowError == newValue) return ShowError; else return ((ShowError = newValue) == false); }
81 GlobalError::MsgType errorType() const { return ErrorType; }
82 GlobalError::MsgType errorType(GlobalError::MsgType const &newValue)
83 {
84 if (ErrorType == newValue) return ErrorType;
85 else {
86 GlobalError::MsgType const &oldValue = ErrorType;
87 ErrorType = newValue;
88 return oldValue;
89 }
90 }
91
92 /*}}}*/
93 protected:
94 bool ShowError;
95 GlobalError::MsgType ErrorType;
96 }; /*}}}*/
97 class PackageContainerInterface { /*{{{*/
98 /** \class PackageContainerInterface
99
100 * Interface ensuring that all operations can be executed on the yet to
101 * define concrete PackageContainer - access to all methods is possible,
102 * but in general the wrappers provided by the PackageContainer template
103 * are nicer to use.
104
105 * This class mostly protects use from the need to write all implementation
106 * of the methods working on containers in the template */
107 public:
108 class const_iterator { /*{{{*/
109 public:
110 virtual pkgCache::PkgIterator getPkg() const = 0;
111 operator pkgCache::PkgIterator(void) const { return getPkg(); }
112
113 inline const char *Name() const {return getPkg().Name(); }
114 inline std::string FullName(bool const Pretty) const { return getPkg().FullName(Pretty); }
115 inline std::string FullName() const { return getPkg().FullName(); }
116 inline const char *Section() const {return getPkg().Section(); }
117 inline bool Purge() const {return getPkg().Purge(); }
118 inline const char *Arch() const {return getPkg().Arch(); }
119 inline pkgCache::GrpIterator Group() const { return getPkg().Group(); }
120 inline pkgCache::VerIterator VersionList() const { return getPkg().VersionList(); }
121 inline pkgCache::VerIterator CurrentVer() const { return getPkg().CurrentVer(); }
122 inline pkgCache::DepIterator RevDependsList() const { return getPkg().RevDependsList(); }
123 inline pkgCache::PrvIterator ProvidesList() const { return getPkg().ProvidesList(); }
124 inline pkgCache::PkgIterator::OkState State() const { return getPkg().State(); }
125 inline const char *CandVersion() const { return getPkg().CandVersion(); }
126 inline const char *CurVersion() const { return getPkg().CurVersion(); }
127 inline pkgCache *Cache() const { return getPkg().Cache(); }
128 inline unsigned long Index() const {return getPkg().Index();}
129 // we have only valid iterators here
130 inline bool end() const { return false; }
131
132 inline pkgCache::Package const * operator->() const {return &*getPkg();}
133 };
134 /*}}}*/
135
136 virtual bool insert(pkgCache::PkgIterator const &P) = 0;
137 virtual bool empty() const = 0;
138 virtual void clear() = 0;
139
140 enum Constructor { UNKNOWN, REGEX, TASK, FNMATCH };
141 virtual void setConstructor(Constructor const &con) = 0;
142 virtual Constructor getConstructor() const = 0;
143
144 static bool FromTask(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper);
145 static bool FromRegEx(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper);
146 static pkgCache::PkgIterator FromName(pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper);
147 static bool FromFnmatch(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper);
148 static bool FromGroup(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper);
149 static bool FromString(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper);
150 static bool FromCommandLine(PackageContainerInterface * const pci, pkgCacheFile &Cache, const char **cmdline, CacheSetHelper &helper);
151
152 struct Modifier {
153 enum Position { NONE, PREFIX, POSTFIX };
154 unsigned short ID;
155 const char * const Alias;
156 Position Pos;
157 Modifier (unsigned short const &id, const char * const alias, Position const &pos) : ID(id), Alias(alias), Pos(pos) {}
158 };
159
160 static bool FromModifierCommandLine(unsigned short &modID, PackageContainerInterface * const pci,
161 pkgCacheFile &Cache, const char * cmdline,
162 std::list<Modifier> const &mods, CacheSetHelper &helper);
163 };
164 /*}}}*/
165 template<class Container> class PackageContainer : public PackageContainerInterface {/*{{{*/
166 /** \class APT::PackageContainer
167
168 Simple wrapper around a container class like std::set to provide a similar
169 interface to a set of packages as to the complete set of all packages in the
170 pkgCache. */
171 Container _cont;
172 public: /*{{{*/
173 /** \brief smell like a pkgCache::PkgIterator */
174 class const_iterator : public PackageContainerInterface::const_iterator,/*{{{*/
175 public std::iterator<std::forward_iterator_tag, typename Container::const_iterator> {
176 typename Container::const_iterator _iter;
177 public:
178 const_iterator(typename Container::const_iterator i) : _iter(i) {}
179 pkgCache::PkgIterator getPkg(void) const { return *_iter; }
180 inline pkgCache::PkgIterator operator*(void) const { return *_iter; }
181 operator typename Container::const_iterator(void) const { return _iter; }
182 inline const_iterator& operator++() { ++_iter; return *this; }
183 inline const_iterator operator++(int) { const_iterator tmp(*this); operator++(); return tmp; }
184 inline bool operator!=(const_iterator const &i) const { return _iter != i._iter; }
185 inline bool operator==(const_iterator const &i) const { return _iter == i._iter; }
186 friend std::ostream& operator<<(std::ostream& out, const_iterator i) { return operator<<(out, *i); }
187 };
188 class iterator : public PackageContainerInterface::const_iterator,
189 public std::iterator<std::forward_iterator_tag, typename Container::iterator> {
190 typename Container::iterator _iter;
191 public:
192 iterator(typename Container::iterator i) : _iter(i) {}
193 pkgCache::PkgIterator getPkg(void) const { return *_iter; }
194 inline pkgCache::PkgIterator operator*(void) const { return *_iter; }
195 operator typename Container::iterator(void) const { return _iter; }
196 operator typename PackageContainer<Container>::const_iterator() { return typename PackageContainer<Container>::const_iterator(_iter); }
197 inline iterator& operator++() { ++_iter; return *this; }
198 inline iterator operator++(int) { iterator tmp(*this); operator++(); return tmp; }
199 inline bool operator!=(iterator const &i) const { return _iter != i._iter; }
200 inline bool operator==(iterator const &i) const { return _iter == i._iter; }
201 inline iterator& operator=(iterator const &i) { _iter = i._iter; return *this; }
202 inline iterator& operator=(typename Container::iterator const &i) { _iter = i; return *this; }
203 friend std::ostream& operator<<(std::ostream& out, iterator i) { return operator<<(out, *i); }
204 };
205 /*}}}*/
206
207 bool insert(pkgCache::PkgIterator const &P) { if (P.end() == true) return false; _cont.insert(P); return true; }
208 template<class Cont> void insert(PackageContainer<Cont> const &pkgcont) { _cont.insert((typename Cont::const_iterator)pkgcont.begin(), (typename Cont::const_iterator)pkgcont.end()); }
209 void insert(const_iterator begin, const_iterator end) { _cont.insert(begin, end); }
210
211 bool empty() const { return _cont.empty(); }
212 void clear() { return _cont.clear(); }
213 //FIXME: on ABI break, replace the first with the second without bool
214 void erase(iterator position) { _cont.erase((typename Container::iterator)position); }
215 iterator& erase(iterator &position, bool) { return position = _cont.erase((typename Container::iterator)position); }
216 size_t erase(const pkgCache::PkgIterator x) { return _cont.erase(x); }
217 void erase(iterator first, iterator last) { _cont.erase(first, last); }
218 size_t size() const { return _cont.size(); }
219
220 const_iterator begin() const { return const_iterator(_cont.begin()); }
221 const_iterator end() const { return const_iterator(_cont.end()); }
222 iterator begin() { return iterator(_cont.begin()); }
223 iterator end() { return iterator(_cont.end()); }
224 const_iterator find(pkgCache::PkgIterator const &P) const { return const_iterator(_cont.find(P)); }
225
226 void setConstructor(Constructor const &by) { ConstructedBy = by; }
227 Constructor getConstructor() const { return ConstructedBy; }
228
229 PackageContainer() : ConstructedBy(UNKNOWN) {}
230 PackageContainer(Constructor const &by) : ConstructedBy(by) {}
231
232 /** \brief returns all packages in the cache who belong to the given task
233
234 A simple helper responsible for search for all members of a task
235 in the cache. Optional it prints a a notice about the
236 packages chosen cause of the given task.
237 \param Cache the packages are in
238 \param pattern name of the task
239 \param helper responsible for error and message handling */
240 static PackageContainer FromTask(pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper) {
241 PackageContainer cont(TASK);
242 PackageContainerInterface::FromTask(&cont, Cache, pattern, helper);
243 return cont;
244 }
245 static PackageContainer FromTask(pkgCacheFile &Cache, std::string const &pattern) {
246 CacheSetHelper helper;
247 return FromTask(Cache, pattern, helper);
248 }
249
250 /** \brief returns all packages in the cache whose name matchs a given pattern
251
252 A simple helper responsible for executing a regular expression on all
253 package names in the cache. Optional it prints a a notice about the
254 packages chosen cause of the given package.
255 \param Cache the packages are in
256 \param pattern regular expression for package names
257 \param helper responsible for error and message handling */
258 static PackageContainer FromRegEx(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) {
259 PackageContainer cont(REGEX);
260 PackageContainerInterface::FromRegEx(&cont, Cache, pattern, helper);
261 return cont;
262 }
263
264 static PackageContainer FromRegEx(pkgCacheFile &Cache, std::string const &pattern) {
265 CacheSetHelper helper;
266 return FromRegEx(Cache, pattern, helper);
267 }
268
269 static PackageContainer FromFnmatch(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) {
270 PackageContainer cont(FNMATCH);
271 PackageContainerInterface::FromFnmatch(&cont, Cache, pattern, helper);
272 return cont;
273 }
274 static PackageContainer FromFnMatch(pkgCacheFile &Cache, std::string const &pattern) {
275 CacheSetHelper helper;
276 return FromFnmatch(Cache, pattern, helper);
277 }
278
279 /** \brief returns a package specified by a string
280
281 \param Cache the package is in
282 \param pattern String the package name should be extracted from
283 \param helper responsible for error and message handling */
284 static pkgCache::PkgIterator FromName(pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper) {
285 return PackageContainerInterface::FromName(Cache, pattern, helper);
286 }
287 static pkgCache::PkgIterator FromName(pkgCacheFile &Cache, std::string const &pattern) {
288 CacheSetHelper helper;
289 return PackageContainerInterface::FromName(Cache, pattern, helper);
290 }
291
292 /** \brief returns all packages specified by a string
293
294 \param Cache the packages are in
295 \param pattern String the package name(s) should be extracted from
296 \param helper responsible for error and message handling */
297 static PackageContainer FromString(pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper) {
298 PackageContainer cont;
299 PackageContainerInterface::FromString(&cont, Cache, pattern, helper);
300 return cont;
301 }
302 static PackageContainer FromString(pkgCacheFile &Cache, std::string const &pattern) {
303 CacheSetHelper helper;
304 return FromString(Cache, pattern, helper);
305 }
306
307 /** \brief returns all packages specified on the commandline
308
309 Get all package names from the commandline and executes regex's if needed.
310 No special package command is supported, just plain names.
311 \param Cache the packages are in
312 \param cmdline Command line the package names should be extracted from
313 \param helper responsible for error and message handling */
314 static PackageContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline, CacheSetHelper &helper) {
315 PackageContainer cont;
316 PackageContainerInterface::FromCommandLine(&cont, Cache, cmdline, helper);
317 return cont;
318 }
319 static PackageContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline) {
320 CacheSetHelper helper;
321 return FromCommandLine(Cache, cmdline, helper);
322 }
323
324 /** \brief group packages by a action modifiers
325
326 At some point it is needed to get from the same commandline
327 different package sets grouped by a modifier. Take
328 apt-get install apt awesome-
329 as an example.
330 \param Cache the packages are in
331 \param cmdline Command line the package names should be extracted from
332 \param mods list of modifiers the method should accept
333 \param fallback the default modifier group for a package
334 \param helper responsible for error and message handling */
335 static std::map<unsigned short, PackageContainer> GroupedFromCommandLine(
336 pkgCacheFile &Cache,
337 const char **cmdline,
338 std::list<Modifier> const &mods,
339 unsigned short const &fallback,
340 CacheSetHelper &helper) {
341 std::map<unsigned short, PackageContainer> pkgsets;
342 for (const char **I = cmdline; *I != 0; ++I) {
343 unsigned short modID = fallback;
344 PackageContainer pkgset;
345 PackageContainerInterface::FromModifierCommandLine(modID, &pkgset, Cache, *I, mods, helper);
346 pkgsets[modID].insert(pkgset);
347 }
348 return pkgsets;
349 }
350 static std::map<unsigned short, PackageContainer> GroupedFromCommandLine(
351 pkgCacheFile &Cache,
352 const char **cmdline,
353 std::list<Modifier> const &mods,
354 unsigned short const &fallback) {
355 CacheSetHelper helper;
356 return GroupedFromCommandLine(Cache, cmdline,
357 mods, fallback, helper);
358 }
359 /*}}}*/
360 private: /*{{{*/
361 Constructor ConstructedBy;
362 /*}}}*/
363 }; /*}}}*/
364
365 template<> template<class Cont> void PackageContainer<std::list<pkgCache::PkgIterator> >::insert(PackageContainer<Cont> const &pkgcont) {
366 for (typename PackageContainer<Cont>::const_iterator p = pkgcont.begin(); p != pkgcont.end(); ++p)
367 _cont.push_back(*p);
368 }
369 // these two are 'inline' as otherwise the linker has problems with seeing these untemplated
370 // specializations again and again - but we need to see them, so that library users can use them
371 template<> inline bool PackageContainer<std::list<pkgCache::PkgIterator> >::insert(pkgCache::PkgIterator const &P) {
372 if (P.end() == true)
373 return false;
374 _cont.push_back(P);
375 return true;
376 }
377 template<> inline void PackageContainer<std::list<pkgCache::PkgIterator> >::insert(const_iterator begin, const_iterator end) {
378 for (const_iterator p = begin; p != end; ++p)
379 _cont.push_back(*p);
380 }
381 typedef PackageContainer<std::set<pkgCache::PkgIterator> > PackageSet;
382 typedef PackageContainer<std::list<pkgCache::PkgIterator> > PackageList;
383
384 class VersionContainerInterface { /*{{{*/
385 /** \class APT::VersionContainerInterface
386
387 Same as APT::PackageContainerInterface, just for Versions */
388 public:
389 /** \brief smell like a pkgCache::VerIterator */
390 class const_iterator { /*{{{*/
391 public:
392 virtual pkgCache::VerIterator getVer() const = 0;
393 operator pkgCache::VerIterator(void) { return getVer(); }
394
395 inline pkgCache *Cache() const { return getVer().Cache(); }
396 inline unsigned long Index() const {return getVer().Index();}
397 inline int CompareVer(const pkgCache::VerIterator &B) const { return getVer().CompareVer(B); }
398 inline const char *VerStr() const { return getVer().VerStr(); }
399 inline const char *Section() const { return getVer().Section(); }
400 inline const char *Arch() const { return getVer().Arch(); }
401 inline pkgCache::PkgIterator ParentPkg() const { return getVer().ParentPkg(); }
402 inline pkgCache::DescIterator DescriptionList() const { return getVer().DescriptionList(); }
403 inline pkgCache::DescIterator TranslatedDescription() const { return getVer().TranslatedDescription(); }
404 inline pkgCache::DepIterator DependsList() const { return getVer().DependsList(); }
405 inline pkgCache::PrvIterator ProvidesList() const { return getVer().ProvidesList(); }
406 inline pkgCache::VerFileIterator FileList() const { return getVer().FileList(); }
407 inline bool Downloadable() const { return getVer().Downloadable(); }
408 inline const char *PriorityType() const { return getVer().PriorityType(); }
409 inline std::string RelStr() const { return getVer().RelStr(); }
410 inline bool Automatic() const { return getVer().Automatic(); }
411 inline pkgCache::VerFileIterator NewestFile() const { return getVer().NewestFile(); }
412 // we have only valid iterators here
413 inline bool end() const { return false; }
414
415 inline pkgCache::Version const * operator->() const { return &*getVer(); }
416 };
417 /*}}}*/
418
419 virtual bool insert(pkgCache::VerIterator const &V) = 0;
420 virtual bool empty() const = 0;
421 virtual void clear() = 0;
422
423 /** \brief specifies which version(s) will be returned if non is given */
424 enum Version {
425 /** All versions */
426 ALL,
427 /** Candidate and installed version */
428 CANDANDINST,
429 /** Candidate version */
430 CANDIDATE,
431 /** Installed version */
432 INSTALLED,
433 /** Candidate or if non installed version */
434 CANDINST,
435 /** Installed or if non candidate version */
436 INSTCAND,
437 /** Newest version */
438 NEWEST
439 };
440
441 struct Modifier {
442 enum Position { NONE, PREFIX, POSTFIX };
443 unsigned short ID;
444 const char * const Alias;
445 Position Pos;
446 Version SelectVersion;
447 Modifier (unsigned short const &id, const char * const alias, Position const &pos,
448 Version const &select) : ID(id), Alias(alias), Pos(pos),
449 SelectVersion(select) {}
450 };
451
452 static bool FromCommandLine(VersionContainerInterface * const vci, pkgCacheFile &Cache,
453 const char **cmdline, Version const &fallback,
454 CacheSetHelper &helper);
455
456 static bool FromString(VersionContainerInterface * const vci, pkgCacheFile &Cache,
457 std::string pkg, Version const &fallback, CacheSetHelper &helper,
458 bool const onlyFromName = false);
459
460 static bool FromPackage(VersionContainerInterface * const vci, pkgCacheFile &Cache,
461 pkgCache::PkgIterator const &P, Version const &fallback,
462 CacheSetHelper &helper);
463
464 static bool FromModifierCommandLine(unsigned short &modID,
465 VersionContainerInterface * const vci,
466 pkgCacheFile &Cache, const char * cmdline,
467 std::list<Modifier> const &mods,
468 CacheSetHelper &helper);
469
470
471 static bool FromDependency(VersionContainerInterface * const vci,
472 pkgCacheFile &Cache,
473 pkgCache::DepIterator const &D,
474 Version const &selector,
475 CacheSetHelper &helper);
476
477 protected: /*{{{*/
478
479 /** \brief returns the candidate version of the package
480
481 \param Cache to be used to query for information
482 \param Pkg we want the candidate version from this package
483 \param helper used in this container instance */
484 static pkgCache::VerIterator getCandidateVer(pkgCacheFile &Cache,
485 pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper);
486
487 /** \brief returns the installed version of the package
488
489 \param Cache to be used to query for information
490 \param Pkg we want the installed version from this package
491 \param helper used in this container instance */
492 static pkgCache::VerIterator getInstalledVer(pkgCacheFile &Cache,
493 pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper);
494 /*}}}*/
495 };
496 /*}}}*/
497 template<class Container> class VersionContainer : public VersionContainerInterface {/*{{{*/
498 /** \class APT::VersionContainer
499
500 Simple wrapper around a container class like std::set to provide a similar
501 interface to a set of versions as to the complete set of all versions in the
502 pkgCache. */
503 Container _cont;
504 public: /*{{{*/
505 /** \brief smell like a pkgCache::VerIterator */
506 class const_iterator : public VersionContainerInterface::const_iterator,
507 public std::iterator<std::forward_iterator_tag, typename Container::const_iterator> {/*{{{*/
508 typename Container::const_iterator _iter;
509 public:
510 const_iterator(typename Container::const_iterator i) : _iter(i) {}
511 pkgCache::VerIterator getVer(void) const { return *_iter; }
512 inline pkgCache::VerIterator operator*(void) const { return *_iter; }
513 operator typename Container::const_iterator(void) const { return _iter; }
514 inline const_iterator& operator++() { ++_iter; return *this; }
515 inline const_iterator operator++(int) { const_iterator tmp(*this); operator++(); return tmp; }
516 inline bool operator!=(const_iterator const &i) const { return _iter != i._iter; }
517 inline bool operator==(const_iterator const &i) const { return _iter == i._iter; }
518 friend std::ostream& operator<<(std::ostream& out, const_iterator i) { return operator<<(out, *i); }
519 };
520 class iterator : public VersionContainerInterface::const_iterator,
521 public std::iterator<std::forward_iterator_tag, typename Container::iterator> {
522 typename Container::iterator _iter;
523 public:
524 iterator(typename Container::iterator i) : _iter(i) {}
525 pkgCache::VerIterator getVer(void) const { return *_iter; }
526 inline pkgCache::VerIterator operator*(void) const { return *_iter; }
527 operator typename Container::iterator(void) const { return _iter; }
528 operator typename VersionContainer<Container>::const_iterator() { return typename VersionContainer<Container>::const_iterator(_iter); }
529 inline iterator& operator++() { ++_iter; return *this; }
530 inline iterator operator++(int) { iterator tmp(*this); operator++(); return tmp; }
531 inline bool operator!=(iterator const &i) const { return _iter != i._iter; }
532 inline bool operator==(iterator const &i) const { return _iter == i._iter; }
533 inline iterator& operator=(iterator const &i) { _iter = i._iter; return *this; }
534 inline iterator& operator=(typename Container::iterator const &i) { _iter = i; return *this; }
535 friend std::ostream& operator<<(std::ostream& out, iterator i) { return operator<<(out, *i); }
536 };
537 /*}}}*/
538
539 bool insert(pkgCache::VerIterator const &V) { if (V.end() == true) return false; _cont.insert(V); return true; }
540 template<class Cont> void insert(VersionContainer<Cont> const &vercont) { _cont.insert((typename Cont::const_iterator)vercont.begin(), (typename Cont::const_iterator)vercont.end()); }
541 void insert(const_iterator begin, const_iterator end) { _cont.insert(begin, end); }
542 bool empty() const { return _cont.empty(); }
543 void clear() { return _cont.clear(); }
544 //FIXME: on ABI break, replace the first with the second without bool
545 void erase(iterator position) { _cont.erase((typename Container::iterator)position); }
546 iterator& erase(iterator &position, bool) { return position = _cont.erase((typename Container::iterator)position); }
547 size_t erase(const pkgCache::VerIterator x) { return _cont.erase(x); }
548 void erase(iterator first, iterator last) { _cont.erase(first, last); }
549 size_t size() const { return _cont.size(); }
550
551 const_iterator begin() const { return const_iterator(_cont.begin()); }
552 const_iterator end() const { return const_iterator(_cont.end()); }
553 iterator begin() { return iterator(_cont.begin()); }
554 iterator end() { return iterator(_cont.end()); }
555 const_iterator find(pkgCache::VerIterator const &V) const { return const_iterator(_cont.find(V)); }
556
557 /** \brief returns all versions specified on the commandline
558
559 Get all versions from the commandline, uses given default version if
560 non specifically requested and executes regex's if needed on names.
561 \param Cache the packages and versions are in
562 \param cmdline Command line the versions should be extracted from
563 \param fallback version specification
564 \param helper responsible for error and message handling */
565 static VersionContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
566 Version const &fallback, CacheSetHelper &helper) {
567 VersionContainer vercon;
568 VersionContainerInterface::FromCommandLine(&vercon, Cache, cmdline, fallback, helper);
569 return vercon;
570 }
571 static VersionContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
572 Version const &fallback) {
573 CacheSetHelper helper;
574 return FromCommandLine(Cache, cmdline, fallback, helper);
575 }
576 static VersionContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline) {
577 return FromCommandLine(Cache, cmdline, CANDINST);
578 }
579
580 static VersionContainer FromString(pkgCacheFile &Cache, std::string const &pkg,
581 Version const &fallback, CacheSetHelper &helper,
582 bool const onlyFromName = false) {
583 VersionContainer vercon;
584 VersionContainerInterface::FromString(&vercon, Cache, pkg, fallback, helper);
585 return vercon;
586 }
587 static VersionContainer FromString(pkgCacheFile &Cache, std::string pkg,
588 Version const &fallback) {
589 CacheSetHelper helper;
590 return FromString(Cache, pkg, fallback, helper);
591 }
592 static VersionContainer FromString(pkgCacheFile &Cache, std::string pkg) {
593 return FromString(Cache, pkg, CANDINST);
594 }
595
596 /** \brief returns all versions specified for the package
597
598 \param Cache the package and versions are in
599 \param P the package in question
600 \param fallback the version(s) you want to get
601 \param helper the helper used for display and error handling */
602 static VersionContainer FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P,
603 Version const &fallback, CacheSetHelper &helper) {
604 VersionContainer vercon;
605 VersionContainerInterface::FromPackage(&vercon, Cache, P, fallback, helper);
606 return vercon;
607 }
608 static VersionContainer FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P,
609 Version const &fallback) {
610 CacheSetHelper helper;
611 return FromPackage(Cache, P, fallback, helper);
612 }
613 static VersionContainer FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P) {
614 return FromPackage(Cache, P, CANDIDATE);
615 }
616
617 static std::map<unsigned short, VersionContainer> GroupedFromCommandLine(
618 pkgCacheFile &Cache,
619 const char **cmdline,
620 std::list<Modifier> const &mods,
621 unsigned short const fallback,
622 CacheSetHelper &helper) {
623 std::map<unsigned short, VersionContainer> versets;
624 for (const char **I = cmdline; *I != 0; ++I) {
625 unsigned short modID = fallback;
626 VersionContainer verset;
627 VersionContainerInterface::FromModifierCommandLine(modID, &verset, Cache, *I, mods, helper);
628 versets[modID].insert(verset);
629 }
630 return versets;
631
632 }
633 static std::map<unsigned short, VersionContainer> GroupedFromCommandLine(
634 pkgCacheFile &Cache, const char **cmdline,
635 std::list<Modifier> const &mods,
636 unsigned short const fallback) {
637 CacheSetHelper helper;
638 return GroupedFromCommandLine(Cache, cmdline,
639 mods, fallback, helper);
640 }
641
642 static VersionContainer FromDependency(pkgCacheFile &Cache, pkgCache::DepIterator const &D,
643 Version const &selector, CacheSetHelper &helper) {
644 VersionContainer vercon;
645 VersionContainerInterface::FromDependency(&vercon, Cache, D, selector, helper);
646 return vercon;
647 }
648 static VersionContainer FromDependency(pkgCacheFile &Cache, pkgCache::DepIterator const &D,
649 Version const &selector) {
650 CacheSetHelper helper;
651 return FromPackage(Cache, D, selector, helper);
652 }
653 static VersionContainer FromDependency(pkgCacheFile &Cache, pkgCache::DepIterator const &D) {
654 return FromPackage(Cache, D, CANDIDATE);
655 }
656 /*}}}*/
657 }; /*}}}*/
658
659 template<> template<class Cont> void VersionContainer<std::list<pkgCache::VerIterator> >::insert(VersionContainer<Cont> const &vercont) {
660 for (typename VersionContainer<Cont>::const_iterator v = vercont.begin(); v != vercont.end(); ++v)
661 _cont.push_back(*v);
662 }
663 // these two are 'inline' as otherwise the linker has problems with seeing these untemplated
664 // specializations again and again - but we need to see them, so that library users can use them
665 template<> inline bool VersionContainer<std::list<pkgCache::VerIterator> >::insert(pkgCache::VerIterator const &V) {
666 if (V.end() == true)
667 return false;
668 _cont.push_back(V);
669 return true;
670 }
671 template<> inline void VersionContainer<std::list<pkgCache::VerIterator> >::insert(const_iterator begin, const_iterator end) {
672 for (const_iterator v = begin; v != end; ++v)
673 _cont.push_back(*v);
674 }
675 typedef VersionContainer<std::set<pkgCache::VerIterator> > VersionSet;
676 typedef VersionContainer<std::list<pkgCache::VerIterator> > VersionList;
677 }
678 #endif