]> git.saurik.com Git - apt.git/blob - apt-pkg/cacheset.h
Fix typos in documentation (codespell)
[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 static pkgCache::VerIterator getCandidateVer(pkgCacheFile &Cache,
484 pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper);
485
486 /** \brief returns the installed version of the package
487
488 \param Cache to be used to query for information
489 \param Pkg we want the installed version from this package */
490 static pkgCache::VerIterator getInstalledVer(pkgCacheFile &Cache,
491 pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper);
492 /*}}}*/
493 };
494 /*}}}*/
495 template<class Container> class VersionContainer : public VersionContainerInterface {/*{{{*/
496 /** \class APT::VersionContainer
497
498 Simple wrapper around a container class like std::set to provide a similar
499 interface to a set of versions as to the complete set of all versions in the
500 pkgCache. */
501 Container _cont;
502 public: /*{{{*/
503 /** \brief smell like a pkgCache::VerIterator */
504 class const_iterator : public VersionContainerInterface::const_iterator,
505 public std::iterator<std::forward_iterator_tag, typename Container::const_iterator> {/*{{{*/
506 typename Container::const_iterator _iter;
507 public:
508 const_iterator(typename Container::const_iterator i) : _iter(i) {}
509 pkgCache::VerIterator getVer(void) const { return *_iter; }
510 inline pkgCache::VerIterator operator*(void) const { return *_iter; };
511 operator typename Container::const_iterator(void) const { return _iter; }
512 inline const_iterator& operator++() { ++_iter; return *this; }
513 inline const_iterator operator++(int) { const_iterator tmp(*this); operator++(); return tmp; }
514 inline bool operator!=(const_iterator const &i) const { return _iter != i._iter; };
515 inline bool operator==(const_iterator const &i) const { return _iter == i._iter; };
516 friend std::ostream& operator<<(std::ostream& out, const_iterator i) { return operator<<(out, *i); }
517 };
518 class iterator : public VersionContainerInterface::const_iterator,
519 public std::iterator<std::forward_iterator_tag, typename Container::iterator> {
520 typename Container::iterator _iter;
521 public:
522 iterator(typename Container::iterator i) : _iter(i) {}
523 pkgCache::VerIterator getVer(void) const { return *_iter; }
524 inline pkgCache::VerIterator operator*(void) const { return *_iter; };
525 operator typename Container::iterator(void) const { return _iter; }
526 operator typename VersionContainer<Container>::const_iterator() { return typename VersionContainer<Container>::const_iterator(_iter); }
527 inline iterator& operator++() { ++_iter; return *this; }
528 inline iterator operator++(int) { iterator tmp(*this); operator++(); return tmp; }
529 inline bool operator!=(iterator const &i) const { return _iter != i._iter; };
530 inline bool operator==(iterator const &i) const { return _iter == i._iter; };
531 inline iterator& operator=(iterator const &i) { _iter = i._iter; return *this; };
532 inline iterator& operator=(typename Container::iterator const &i) { _iter = i; return *this; };
533 friend std::ostream& operator<<(std::ostream& out, iterator i) { return operator<<(out, *i); }
534 };
535 /*}}}*/
536
537 bool insert(pkgCache::VerIterator const &V) { if (V.end() == true) return false; _cont.insert(V); return true; };
538 template<class Cont> void insert(VersionContainer<Cont> const &vercont) { _cont.insert((typename Cont::const_iterator)vercont.begin(), (typename Cont::const_iterator)vercont.end()); };
539 void insert(const_iterator begin, const_iterator end) { _cont.insert(begin, end); };
540 bool empty() const { return _cont.empty(); };
541 void clear() { return _cont.clear(); };
542 //FIXME: on ABI break, replace the first with the second without bool
543 void erase(iterator position) { _cont.erase((typename Container::iterator)position); };
544 iterator& erase(iterator &position, bool) { return position = _cont.erase((typename Container::iterator)position); };
545 size_t erase(const pkgCache::VerIterator x) { return _cont.erase(x); };
546 void erase(iterator first, iterator last) { _cont.erase(first, last); };
547 size_t size() const { return _cont.size(); };
548
549 const_iterator begin() const { return const_iterator(_cont.begin()); };
550 const_iterator end() const { return const_iterator(_cont.end()); };
551 iterator begin() { return iterator(_cont.begin()); };
552 iterator end() { return iterator(_cont.end()); };
553 const_iterator find(pkgCache::VerIterator const &V) const { return const_iterator(_cont.find(V)); };
554
555 /** \brief returns all versions specified on the commandline
556
557 Get all versions from the commandline, uses given default version if
558 non specifically requested and executes regex's if needed on names.
559 \param Cache the packages and versions are in
560 \param cmdline Command line the versions should be extracted from
561 \param helper responsible for error and message handling */
562 static VersionContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
563 Version const &fallback, CacheSetHelper &helper) {
564 VersionContainer vercon;
565 VersionContainerInterface::FromCommandLine(&vercon, Cache, cmdline, fallback, helper);
566 return vercon;
567 }
568 static VersionContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
569 Version const &fallback) {
570 CacheSetHelper helper;
571 return FromCommandLine(Cache, cmdline, fallback, helper);
572 }
573 static VersionContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline) {
574 return FromCommandLine(Cache, cmdline, CANDINST);
575 }
576
577 static VersionContainer FromString(pkgCacheFile &Cache, std::string const &pkg,
578 Version const &fallback, CacheSetHelper &helper,
579 bool const onlyFromName = false) {
580 VersionContainer vercon;
581 VersionContainerInterface::FromString(&vercon, Cache, pkg, fallback, helper);
582 return vercon;
583 }
584 static VersionContainer FromString(pkgCacheFile &Cache, std::string pkg,
585 Version const &fallback) {
586 CacheSetHelper helper;
587 return FromString(Cache, pkg, fallback, helper);
588 }
589 static VersionContainer FromString(pkgCacheFile &Cache, std::string pkg) {
590 return FromString(Cache, pkg, CANDINST);
591 }
592
593 /** \brief returns all versions specified for the package
594
595 \param Cache the package and versions are in
596 \param P the package in question
597 \param fallback the version(s) you want to get
598 \param helper the helper used for display and error handling */
599 static VersionContainer FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P,
600 Version const &fallback, CacheSetHelper &helper) {
601 VersionContainer vercon;
602 VersionContainerInterface::FromPackage(&vercon, Cache, P, fallback, helper);
603 return vercon;
604 }
605 static VersionContainer FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P,
606 Version const &fallback) {
607 CacheSetHelper helper;
608 return FromPackage(Cache, P, fallback, helper);
609 }
610 static VersionContainer FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P) {
611 return FromPackage(Cache, P, CANDIDATE);
612 }
613
614 static std::map<unsigned short, VersionContainer> GroupedFromCommandLine(
615 pkgCacheFile &Cache,
616 const char **cmdline,
617 std::list<Modifier> const &mods,
618 unsigned short const fallback,
619 CacheSetHelper &helper) {
620 std::map<unsigned short, VersionContainer> versets;
621 for (const char **I = cmdline; *I != 0; ++I) {
622 unsigned short modID = fallback;
623 VersionContainer verset;
624 VersionContainerInterface::FromModifierCommandLine(modID, &verset, Cache, *I, mods, helper);
625 versets[modID].insert(verset);
626 }
627 return versets;
628
629 }
630 static std::map<unsigned short, VersionContainer> GroupedFromCommandLine(
631 pkgCacheFile &Cache, const char **cmdline,
632 std::list<Modifier> const &mods,
633 unsigned short const fallback) {
634 CacheSetHelper helper;
635 return GroupedFromCommandLine(Cache, cmdline,
636 mods, fallback, helper);
637 }
638
639 static VersionContainer FromDependency(pkgCacheFile &Cache, pkgCache::DepIterator const &D,
640 Version const &selector, CacheSetHelper &helper) {
641 VersionContainer vercon;
642 VersionContainerInterface::FromDependency(&vercon, Cache, D, selector, helper);
643 return vercon;
644 }
645 static VersionContainer FromDependency(pkgCacheFile &Cache, pkgCache::DepIterator const &D,
646 Version const &selector) {
647 CacheSetHelper helper;
648 return FromPackage(Cache, D, selector, helper);
649 }
650 static VersionContainer FromDependency(pkgCacheFile &Cache, pkgCache::DepIterator const &D) {
651 return FromPackage(Cache, D, CANDIDATE);
652 }
653 /*}}}*/
654 }; /*}}}*/
655
656 template<> template<class Cont> void VersionContainer<std::list<pkgCache::VerIterator> >::insert(VersionContainer<Cont> const &vercont) {
657 for (typename VersionContainer<Cont>::const_iterator v = vercont.begin(); v != vercont.end(); ++v)
658 _cont.push_back(*v);
659 };
660 // these two are 'inline' as otherwise the linker has problems with seeing these untemplated
661 // specializations again and again - but we need to see them, so that library users can use them
662 template<> inline bool VersionContainer<std::list<pkgCache::VerIterator> >::insert(pkgCache::VerIterator const &V) {
663 if (V.end() == true)
664 return false;
665 _cont.push_back(V);
666 return true;
667 };
668 template<> inline void VersionContainer<std::list<pkgCache::VerIterator> >::insert(const_iterator begin, const_iterator end) {
669 for (const_iterator v = begin; v != end; ++v)
670 _cont.push_back(*v);
671 };
672 typedef VersionContainer<std::set<pkgCache::VerIterator> > VersionSet;
673 typedef VersionContainer<std::list<pkgCache::VerIterator> > VersionList;
674 }
675 #endif