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