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