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