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