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