1 // -*- mode: cpp; mode: fold -*-
4 Wrappers around std::set to have set::iterators which behave
5 similar to the Iterators of the cache structures.
7 Provides also a few helper methods which work with these sets */
10 #define APT_CACHESET_H
11 // Include Files /*{{{*/
23 #include <apt-pkg/error.h>
24 #include <apt-pkg/pkgcache.h>
25 #include <apt-pkg/cacheiterators.h>
27 #ifndef APT_8_CLEANER_HEADERS
28 #include <apt-pkg/cachefile.h>
30 #ifndef APT_10_CLEANER_HEADERS
38 class PackageContainerInterface
;
39 class VersionContainerInterface
;
41 class CacheSetHelper
{ /*{{{*/
42 /** \class APT::CacheSetHelper
43 Simple base class with a lot of virtual methods which can be overridden
44 to alter the behavior or the output of the CacheSets.
46 This helper is passed around by the static methods in the CacheSets and
47 used every time they hit an error condition or something could be
51 CacheSetHelper(bool const ShowError
= true,
52 GlobalError::MsgType ErrorType
= GlobalError::ERROR
) :
53 ShowError(ShowError
), ErrorType(ErrorType
) {}
54 virtual ~CacheSetHelper() {}
56 enum PkgSelector
{ UNKNOWN
, PACKAGENAME
, REGEX
, TASK
, FNMATCH
};
58 /** \brief be notified about the package being selected via pattern
60 * Main use is probably to show a message to the user what happened
62 * \param pkg is the package which was selected
63 * \param select is the selection method which choose the package
64 * \param pattern is the string used by the selection method to pick the package
66 virtual void showPackageSelection(pkgCache::PkgIterator
const &pkg
, PkgSelector
const select
, std::string
const &pattern
);
67 // use the method above instead, react only on the type you need and let the base handle the rest if need be
68 // this allows use to add new selection methods without breaking the ABI constantly with new virtual methods
69 APT_DEPRECATED
virtual void showTaskSelection(pkgCache::PkgIterator
const &pkg
, std::string
const &pattern
);
70 APT_DEPRECATED
virtual void showRegExSelection(pkgCache::PkgIterator
const &pkg
, std::string
const &pattern
);
71 APT_DEPRECATED
virtual void showFnmatchSelection(pkgCache::PkgIterator
const &pkg
, std::string
const &pattern
);
73 /** \brief be notified if a package can't be found via pattern
75 * Can be used to show a message as well as to try something else to make it match
77 * \param select is the method tried for selection
78 * \param pci is the container the package should be inserted in
79 * \param Cache is the package universe available
80 * \param pattern is the string not matching anything
82 virtual void canNotFindPackage(enum PkgSelector
const select
, PackageContainerInterface
* const pci
, pkgCacheFile
&Cache
, std::string
const &pattern
);
83 // same as above for showPackageSelection
84 APT_DEPRECATED
virtual void canNotFindTask(PackageContainerInterface
* const pci
, pkgCacheFile
&Cache
, std::string pattern
);
85 APT_DEPRECATED
virtual void canNotFindRegEx(PackageContainerInterface
* const pci
, pkgCacheFile
&Cache
, std::string pattern
);
86 APT_DEPRECATED
virtual void canNotFindFnmatch(PackageContainerInterface
* const pci
, pkgCacheFile
&Cache
, std::string pattern
);
87 APT_DEPRECATED
virtual void canNotFindPackage(PackageContainerInterface
* const pci
, pkgCacheFile
&Cache
, std::string
const &str
);
89 /** \brief specifies which version(s) we want to refer to */
91 /** by release string */
93 /** by version number string */
97 /** Candidate and installed version */
99 /** Candidate version */
101 /** Installed version */
103 /** Candidate or if non installed version */
105 /** Installed or if non candidate version */
107 /** Newest version */
111 /** \brief be notified about the version being selected via pattern
113 * Main use is probably to show a message to the user what happened
114 * Note that at the moment this method is only called for RELEASE
115 * and VERSION selections, not for the others.
117 * \param Pkg is the package which was selected for
118 * \param Ver is the version selected
119 * \param select is the selection method which choose the version
120 * \param pattern is the string used by the selection method to pick the version
122 virtual void showVersionSelection(pkgCache::PkgIterator
const &Pkg
, pkgCache::VerIterator
const &Ver
,
123 enum VerSelector
const select
, std::string
const &pattern
);
124 // renamed to have a similar interface to showPackageSelection
125 APT_DEPRECATED
virtual void showSelectedVersion(pkgCache::PkgIterator
const &Pkg
, pkgCache::VerIterator
const Ver
,
126 std::string
const &ver
, bool const verIsRel
);
128 /** \brief be notified if a version can't be found for a package
130 * Main use is probably to show a message to the user what happened
132 * \param select is the method tried for selection
133 * \param vci is the container the version should be inserted in
134 * \param Cache is the package universe available
135 * \param Pkg is the package we wanted a version from
137 virtual void canNotFindVersion(enum VerSelector
const select
, VersionContainerInterface
* const vci
, pkgCacheFile
&Cache
, pkgCache::PkgIterator
const &Pkg
);
138 // same as above for showPackageSelection
139 APT_DEPRECATED
virtual void canNotFindAllVer(VersionContainerInterface
* const vci
, pkgCacheFile
&Cache
, pkgCache::PkgIterator
const &Pkg
);
140 APT_DEPRECATED
virtual void canNotFindInstCandVer(VersionContainerInterface
* const vci
, pkgCacheFile
&Cache
,
141 pkgCache::PkgIterator
const &Pkg
);
142 APT_DEPRECATED
virtual void canNotFindCandInstVer(VersionContainerInterface
* const vci
,
144 pkgCache::PkgIterator
const &Pkg
);
146 // the difference between canNotFind and canNotGet is that the later is more low-level
147 // and called from other places: In this case looking into the code is the only real answer…
148 virtual pkgCache::VerIterator
canNotGetVersion(enum VerSelector
const select
, pkgCacheFile
&Cache
, pkgCache::PkgIterator
const &Pkg
);
149 // same as above for showPackageSelection
150 APT_DEPRECATED
virtual pkgCache::VerIterator
canNotFindNewestVer(pkgCacheFile
&Cache
,
151 pkgCache::PkgIterator
const &Pkg
);
152 APT_DEPRECATED
virtual pkgCache::VerIterator
canNotFindCandidateVer(pkgCacheFile
&Cache
,
153 pkgCache::PkgIterator
const &Pkg
);
154 APT_DEPRECATED
virtual pkgCache::VerIterator
canNotFindInstalledVer(pkgCacheFile
&Cache
,
155 pkgCache::PkgIterator
const &Pkg
);
157 virtual pkgCache::PkgIterator
canNotFindPkgName(pkgCacheFile
&Cache
, std::string
const &str
);
159 bool showErrors() const { return ShowError
; }
160 bool showErrors(bool const newValue
) { if (ShowError
== newValue
) return ShowError
; else return ((ShowError
= newValue
) == false); }
161 GlobalError::MsgType
errorType() const { return ErrorType
; }
162 GlobalError::MsgType
errorType(GlobalError::MsgType
const &newValue
)
164 if (ErrorType
== newValue
) return ErrorType
;
166 GlobalError::MsgType
const &oldValue
= ErrorType
;
167 ErrorType
= newValue
;
175 GlobalError::MsgType ErrorType
;
177 pkgCache::VerIterator
canNotGetInstCandVer(pkgCacheFile
&Cache
,
178 pkgCache::PkgIterator
const &Pkg
);
179 pkgCache::VerIterator
canNotGetCandInstVer(pkgCacheFile
&Cache
,
180 pkgCache::PkgIterator
const &Pkg
);
183 class PackageContainerInterface
{ /*{{{*/
184 /** \class PackageContainerInterface
186 * Interface ensuring that all operations can be executed on the yet to
187 * define concrete PackageContainer - access to all methods is possible,
188 * but in general the wrappers provided by the PackageContainer template
191 * This class mostly protects use from the need to write all implementation
192 * of the methods working on containers in the template */
194 class const_iterator
{ /*{{{*/
196 virtual pkgCache::PkgIterator
getPkg() const = 0;
197 operator pkgCache::PkgIterator(void) const { return getPkg(); }
199 inline const char *Name() const {return getPkg().Name(); }
200 inline std::string
FullName(bool const Pretty
) const { return getPkg().FullName(Pretty
); }
201 inline std::string
FullName() const { return getPkg().FullName(); }
202 APT_DEPRECATED
inline const char *Section() const {
204 #pragma GCC diagnostic push
205 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
207 return getPkg().Section();
209 #pragma GCC diagnostic pop
212 inline bool Purge() const {return getPkg().Purge(); }
213 inline const char *Arch() const {return getPkg().Arch(); }
214 inline pkgCache::GrpIterator
Group() const { return getPkg().Group(); }
215 inline pkgCache::VerIterator
VersionList() const { return getPkg().VersionList(); }
216 inline pkgCache::VerIterator
CurrentVer() const { return getPkg().CurrentVer(); }
217 inline pkgCache::DepIterator
RevDependsList() const { return getPkg().RevDependsList(); }
218 inline pkgCache::PrvIterator
ProvidesList() const { return getPkg().ProvidesList(); }
219 inline pkgCache::PkgIterator::OkState
State() const { return getPkg().State(); }
220 inline const char *CandVersion() const { return getPkg().CandVersion(); }
221 inline const char *CurVersion() const { return getPkg().CurVersion(); }
222 inline pkgCache
*Cache() const { return getPkg().Cache(); }
223 inline unsigned long Index() const {return getPkg().Index();}
224 // we have only valid iterators here
225 inline bool end() const { return false; }
227 inline pkgCache::Package
const * operator->() const {return &*getPkg();}
231 virtual bool insert(pkgCache::PkgIterator
const &P
) = 0;
232 virtual bool empty() const = 0;
233 virtual void clear() = 0;
235 // FIXME: This is a bloody hack removed soon. Use CacheSetHelper::PkgSelector !
236 enum APT_DEPRECATED Constructor
{ UNKNOWN
= CacheSetHelper::UNKNOWN
,
237 REGEX
= CacheSetHelper::REGEX
,
238 TASK
= CacheSetHelper::TASK
,
239 FNMATCH
= CacheSetHelper::FNMATCH
};
241 #pragma GCC diagnostic push
242 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
244 void setConstructor(Constructor
const by
) { ConstructedBy
= (CacheSetHelper::PkgSelector
)by
; }
246 #pragma GCC diagnostic pop
249 void setConstructor(CacheSetHelper::PkgSelector
const by
) { ConstructedBy
= by
; }
250 CacheSetHelper::PkgSelector
getConstructor() const { return ConstructedBy
; }
251 PackageContainerInterface() : ConstructedBy(CacheSetHelper::UNKNOWN
) {}
252 PackageContainerInterface(CacheSetHelper::PkgSelector
const by
) : ConstructedBy(by
) {}
254 static bool FromTask(PackageContainerInterface
* const pci
, pkgCacheFile
&Cache
, std::string pattern
, CacheSetHelper
&helper
);
255 static bool FromRegEx(PackageContainerInterface
* const pci
, pkgCacheFile
&Cache
, std::string pattern
, CacheSetHelper
&helper
);
256 static pkgCache::PkgIterator
FromName(pkgCacheFile
&Cache
, std::string
const &pattern
, CacheSetHelper
&helper
);
257 static bool FromFnmatch(PackageContainerInterface
* const pci
, pkgCacheFile
&Cache
, std::string pattern
, CacheSetHelper
&helper
);
258 static bool FromGroup(PackageContainerInterface
* const pci
, pkgCacheFile
&Cache
, std::string pattern
, CacheSetHelper
&helper
);
259 static bool FromString(PackageContainerInterface
* const pci
, pkgCacheFile
&Cache
, std::string
const &pattern
, CacheSetHelper
&helper
);
260 static bool FromCommandLine(PackageContainerInterface
* const pci
, pkgCacheFile
&Cache
, const char **cmdline
, CacheSetHelper
&helper
);
263 enum Position
{ NONE
, PREFIX
, POSTFIX
};
265 const char * const Alias
;
267 Modifier (unsigned short const &id
, const char * const alias
, Position
const &pos
) : ID(id
), Alias(alias
), Pos(pos
) {}
270 static bool FromModifierCommandLine(unsigned short &modID
, PackageContainerInterface
* const pci
,
271 pkgCacheFile
&Cache
, const char * cmdline
,
272 std::list
<Modifier
> const &mods
, CacheSetHelper
&helper
);
275 CacheSetHelper::PkgSelector ConstructedBy
;
278 template<class Container
> class PackageContainer
: public PackageContainerInterface
{/*{{{*/
279 /** \class APT::PackageContainer
281 Simple wrapper around a container class like std::set to provide a similar
282 interface to a set of packages as to the complete set of all packages in the
286 /** \brief smell like a pkgCache::PkgIterator */
287 class const_iterator
: public PackageContainerInterface::const_iterator
,/*{{{*/
288 public std::iterator
<std::forward_iterator_tag
, typename
Container::const_iterator
> {
289 typename
Container::const_iterator _iter
;
291 const_iterator(typename
Container::const_iterator i
) : _iter(i
) {}
292 pkgCache::PkgIterator
getPkg(void) const { return *_iter
; }
293 inline pkgCache::PkgIterator
operator*(void) const { return *_iter
; }
294 operator typename
Container::const_iterator(void) const { return _iter
; }
295 inline const_iterator
& operator++() { ++_iter
; return *this; }
296 inline const_iterator
operator++(int) { const_iterator
tmp(*this); operator++(); return tmp
; }
297 inline bool operator!=(const_iterator
const &i
) const { return _iter
!= i
._iter
; }
298 inline bool operator==(const_iterator
const &i
) const { return _iter
== i
._iter
; }
299 friend std::ostream
& operator<<(std::ostream
& out
, const_iterator i
) { return operator<<(out
, *i
); }
301 class iterator
: public PackageContainerInterface::const_iterator
,
302 public std::iterator
<std::forward_iterator_tag
, typename
Container::iterator
> {
303 typename
Container::iterator _iter
;
305 iterator(typename
Container::iterator i
) : _iter(i
) {}
306 pkgCache::PkgIterator
getPkg(void) const { return *_iter
; }
307 inline pkgCache::PkgIterator
operator*(void) const { return *_iter
; }
308 operator typename
Container::iterator(void) const { return _iter
; }
309 operator typename PackageContainer
<Container
>::const_iterator() { return typename PackageContainer
<Container
>::const_iterator(_iter
); }
310 inline iterator
& operator++() { ++_iter
; return *this; }
311 inline iterator
operator++(int) { iterator
tmp(*this); operator++(); return tmp
; }
312 inline bool operator!=(iterator
const &i
) const { return _iter
!= i
._iter
; }
313 inline bool operator==(iterator
const &i
) const { return _iter
== i
._iter
; }
314 inline iterator
& operator=(iterator
const &i
) { _iter
= i
._iter
; return *this; }
315 inline iterator
& operator=(typename
Container::iterator
const &i
) { _iter
= i
; return *this; }
316 friend std::ostream
& operator<<(std::ostream
& out
, iterator i
) { return operator<<(out
, *i
); }
320 bool insert(pkgCache::PkgIterator
const &P
) { if (P
.end() == true) return false; _cont
.insert(P
); return true; }
321 template<class Cont
> void insert(PackageContainer
<Cont
> const &pkgcont
) { _cont
.insert((typename
Cont::const_iterator
)pkgcont
.begin(), (typename
Cont::const_iterator
)pkgcont
.end()); }
322 void insert(const_iterator begin
, const_iterator end
) { _cont
.insert(begin
, end
); }
324 bool empty() const { return _cont
.empty(); }
325 void clear() { return _cont
.clear(); }
326 //FIXME: on ABI break, replace the first with the second without bool
327 void erase(iterator position
) { _cont
.erase((typename
Container::iterator
)position
); }
328 iterator
& erase(iterator
&position
, bool) { return position
= _cont
.erase((typename
Container::iterator
)position
); }
329 size_t erase(const pkgCache::PkgIterator x
) { return _cont
.erase(x
); }
330 void erase(iterator first
, iterator last
) { _cont
.erase(first
, last
); }
331 size_t size() const { return _cont
.size(); }
333 const_iterator
begin() const { return const_iterator(_cont
.begin()); }
334 const_iterator
end() const { return const_iterator(_cont
.end()); }
335 iterator
begin() { return iterator(_cont
.begin()); }
336 iterator
end() { return iterator(_cont
.end()); }
337 const_iterator
find(pkgCache::PkgIterator
const &P
) const { return const_iterator(_cont
.find(P
)); }
339 PackageContainer() : PackageContainerInterface() {}
340 PackageContainer(CacheSetHelper::PkgSelector
const &by
) : PackageContainerInterface(by
) {}
342 #pragma GCC diagnostic push
343 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
345 APT_DEPRECATED
PackageContainer(Constructor
const &by
) : PackageContainerInterface((CacheSetHelper::PkgSelector
)by
) {}
347 #pragma GCC diagnostic pop
350 /** \brief sort all included versions with given comparer
352 Some containers are sorted by default, some are not and can't be,
353 but a few like std::vector can be sorted if need be, so this can be
354 specialized in later on. The default is that this will fail though.
355 Specifically, already sorted containers like std::set will return
356 false as well as there is no easy way to check that the given comparer
357 would sort in the same way the set is currently sorted
359 \return \b true if the set was sorted, \b false if not. */
360 template<class Compare
> bool sort(Compare
/*Comp*/) { return false; }
362 /** \brief returns all packages in the cache who belong to the given task
364 A simple helper responsible for search for all members of a task
365 in the cache. Optional it prints a a notice about the
366 packages chosen cause of the given task.
367 \param Cache the packages are in
368 \param pattern name of the task
369 \param helper responsible for error and message handling */
370 static PackageContainer
FromTask(pkgCacheFile
&Cache
, std::string
const &pattern
, CacheSetHelper
&helper
) {
371 PackageContainer
cont(CacheSetHelper::TASK
);
372 PackageContainerInterface::FromTask(&cont
, Cache
, pattern
, helper
);
375 static PackageContainer
FromTask(pkgCacheFile
&Cache
, std::string
const &pattern
) {
376 CacheSetHelper helper
;
377 return FromTask(Cache
, pattern
, helper
);
380 /** \brief returns all packages in the cache whose name matchs a given pattern
382 A simple helper responsible for executing a regular expression on all
383 package names in the cache. Optional it prints a a notice about the
384 packages chosen cause of the given package.
385 \param Cache the packages are in
386 \param pattern regular expression for package names
387 \param helper responsible for error and message handling */
388 static PackageContainer
FromRegEx(pkgCacheFile
&Cache
, std::string
const &pattern
, CacheSetHelper
&helper
) {
389 PackageContainer
cont(CacheSetHelper::REGEX
);
390 PackageContainerInterface::FromRegEx(&cont
, Cache
, pattern
, helper
);
394 static PackageContainer
FromRegEx(pkgCacheFile
&Cache
, std::string
const &pattern
) {
395 CacheSetHelper helper
;
396 return FromRegEx(Cache
, pattern
, helper
);
399 static PackageContainer
FromFnmatch(pkgCacheFile
&Cache
, std::string
const &pattern
, CacheSetHelper
&helper
) {
400 PackageContainer
cont(CacheSetHelper::FNMATCH
);
401 PackageContainerInterface::FromFnmatch(&cont
, Cache
, pattern
, helper
);
404 static PackageContainer
FromFnMatch(pkgCacheFile
&Cache
, std::string
const &pattern
) {
405 CacheSetHelper helper
;
406 return FromFnmatch(Cache
, pattern
, helper
);
409 /** \brief returns a package specified by a string
411 \param Cache the package is in
412 \param pattern String the package name should be extracted from
413 \param helper responsible for error and message handling */
414 static pkgCache::PkgIterator
FromName(pkgCacheFile
&Cache
, std::string
const &pattern
, CacheSetHelper
&helper
) {
415 return PackageContainerInterface::FromName(Cache
, pattern
, helper
);
417 static pkgCache::PkgIterator
FromName(pkgCacheFile
&Cache
, std::string
const &pattern
) {
418 CacheSetHelper helper
;
419 return PackageContainerInterface::FromName(Cache
, pattern
, helper
);
422 /** \brief returns all packages specified by a string
424 \param Cache the packages are in
425 \param pattern String the package name(s) should be extracted from
426 \param helper responsible for error and message handling */
427 static PackageContainer
FromString(pkgCacheFile
&Cache
, std::string
const &pattern
, CacheSetHelper
&helper
) {
428 PackageContainer cont
;
429 PackageContainerInterface::FromString(&cont
, Cache
, pattern
, helper
);
432 static PackageContainer
FromString(pkgCacheFile
&Cache
, std::string
const &pattern
) {
433 CacheSetHelper helper
;
434 return FromString(Cache
, pattern
, helper
);
437 /** \brief returns all packages specified on the commandline
439 Get all package names from the commandline and executes regex's if needed.
440 No special package command is supported, just plain names.
441 \param Cache the packages are in
442 \param cmdline Command line the package names should be extracted from
443 \param helper responsible for error and message handling */
444 static PackageContainer
FromCommandLine(pkgCacheFile
&Cache
, const char **cmdline
, CacheSetHelper
&helper
) {
445 PackageContainer cont
;
446 PackageContainerInterface::FromCommandLine(&cont
, Cache
, cmdline
, helper
);
449 static PackageContainer
FromCommandLine(pkgCacheFile
&Cache
, const char **cmdline
) {
450 CacheSetHelper helper
;
451 return FromCommandLine(Cache
, cmdline
, helper
);
454 /** \brief group packages by a action modifiers
456 At some point it is needed to get from the same commandline
457 different package sets grouped by a modifier. Take
458 apt-get install apt awesome-
460 \param Cache the packages are in
461 \param cmdline Command line the package names should be extracted from
462 \param mods list of modifiers the method should accept
463 \param fallback the default modifier group for a package
464 \param helper responsible for error and message handling */
465 static std::map
<unsigned short, PackageContainer
> GroupedFromCommandLine(
467 const char **cmdline
,
468 std::list
<Modifier
> const &mods
,
469 unsigned short const &fallback
,
470 CacheSetHelper
&helper
) {
471 std::map
<unsigned short, PackageContainer
> pkgsets
;
472 for (const char **I
= cmdline
; *I
!= 0; ++I
) {
473 unsigned short modID
= fallback
;
474 PackageContainer pkgset
;
475 PackageContainerInterface::FromModifierCommandLine(modID
, &pkgset
, Cache
, *I
, mods
, helper
);
476 pkgsets
[modID
].insert(pkgset
);
480 static std::map
<unsigned short, PackageContainer
> GroupedFromCommandLine(
482 const char **cmdline
,
483 std::list
<Modifier
> const &mods
,
484 unsigned short const &fallback
) {
485 CacheSetHelper helper
;
486 return GroupedFromCommandLine(Cache
, cmdline
,
487 mods
, fallback
, helper
);
491 // specialisations for push_back containers: std::list & std::vector /*{{{*/
492 template<> template<class Cont
> void PackageContainer
<std::list
<pkgCache::PkgIterator
> >::insert(PackageContainer
<Cont
> const &pkgcont
) {
493 for (typename PackageContainer
<Cont
>::const_iterator p
= pkgcont
.begin(); p
!= pkgcont
.end(); ++p
)
496 template<> template<class Cont
> void PackageContainer
<std::vector
<pkgCache::PkgIterator
> >::insert(PackageContainer
<Cont
> const &pkgcont
) {
497 for (typename PackageContainer
<Cont
>::const_iterator p
= pkgcont
.begin(); p
!= pkgcont
.end(); ++p
)
500 // these two are 'inline' as otherwise the linker has problems with seeing these untemplated
501 // specializations again and again - but we need to see them, so that library users can use them
502 template<> inline bool PackageContainer
<std::list
<pkgCache::PkgIterator
> >::insert(pkgCache::PkgIterator
const &P
) {
508 template<> inline bool PackageContainer
<std::vector
<pkgCache::PkgIterator
> >::insert(pkgCache::PkgIterator
const &P
) {
514 template<> inline void PackageContainer
<std::list
<pkgCache::PkgIterator
> >::insert(const_iterator begin
, const_iterator end
) {
515 for (const_iterator p
= begin
; p
!= end
; ++p
)
518 template<> inline void PackageContainer
<std::vector
<pkgCache::PkgIterator
> >::insert(const_iterator begin
, const_iterator end
) {
519 for (const_iterator p
= begin
; p
!= end
; ++p
)
524 template<> template<class Compare
> inline bool PackageContainer
<std::vector
<pkgCache::PkgIterator
> >::sort(Compare Comp
) {
525 std::sort(_cont
.begin(), _cont
.end(), Comp
);
529 // class PackageUniverse - pkgCache as PackageContainerInterface /*{{{*/
530 /** \class PackageUniverse
532 Wraps around our usual pkgCache, so that it can be stuffed into methods
533 expecting a PackageContainer.
535 The wrapping is read-only in practice modeled by making erase and co
537 class PackageUniverse
: public PackageContainerInterface
{
538 pkgCache
* const _cont
;
540 typedef pkgCache::PkgIterator iterator
;
541 typedef pkgCache::PkgIterator const_iterator
;
543 bool empty() const { return false; }
544 size_t size() const { return _cont
->Head().PackageCount
; }
546 const_iterator
begin() const { return _cont
->PkgBegin(); }
547 const_iterator
end() const { return _cont
->PkgEnd(); }
548 iterator
begin() { return _cont
->PkgBegin(); }
549 iterator
end() { return _cont
->PkgEnd(); }
551 PackageUniverse(pkgCache
* const Owner
) : _cont(Owner
) { }
554 bool insert(pkgCache::PkgIterator
const &) { return true; }
555 template<class Cont
> void insert(PackageContainer
<Cont
> const &) { }
556 void insert(const_iterator
, const_iterator
) { }
559 iterator
& erase(iterator
&iter
) { return iter
; }
560 size_t erase(const pkgCache::PkgIterator
) { return 0; }
561 void erase(iterator
, iterator
) { }
564 typedef PackageContainer
<std::set
<pkgCache::PkgIterator
> > PackageSet
;
565 typedef PackageContainer
<std::list
<pkgCache::PkgIterator
> > PackageList
;
566 typedef PackageContainer
<std::vector
<pkgCache::PkgIterator
> > PackageVector
;
568 class VersionContainerInterface
{ /*{{{*/
569 /** \class APT::VersionContainerInterface
571 Same as APT::PackageContainerInterface, just for Versions */
573 /** \brief smell like a pkgCache::VerIterator */
574 class const_iterator
{ /*{{{*/
576 virtual pkgCache::VerIterator
getVer() const = 0;
577 operator pkgCache::VerIterator(void) { return getVer(); }
579 inline pkgCache
*Cache() const { return getVer().Cache(); }
580 inline unsigned long Index() const {return getVer().Index();}
581 inline int CompareVer(const pkgCache::VerIterator
&B
) const { return getVer().CompareVer(B
); }
582 inline const char *VerStr() const { return getVer().VerStr(); }
583 inline const char *Section() const { return getVer().Section(); }
584 inline const char *Arch() const { return getVer().Arch(); }
585 inline pkgCache::PkgIterator
ParentPkg() const { return getVer().ParentPkg(); }
586 inline pkgCache::DescIterator
DescriptionList() const { return getVer().DescriptionList(); }
587 inline pkgCache::DescIterator
TranslatedDescription() const { return getVer().TranslatedDescription(); }
588 inline pkgCache::DepIterator
DependsList() const { return getVer().DependsList(); }
589 inline pkgCache::PrvIterator
ProvidesList() const { return getVer().ProvidesList(); }
590 inline pkgCache::VerFileIterator
FileList() const { return getVer().FileList(); }
591 inline bool Downloadable() const { return getVer().Downloadable(); }
592 inline const char *PriorityType() const { return getVer().PriorityType(); }
593 inline std::string
RelStr() const { return getVer().RelStr(); }
594 inline bool Automatic() const { return getVer().Automatic(); }
595 inline pkgCache::VerFileIterator
NewestFile() const { return getVer().NewestFile(); }
596 // we have only valid iterators here
597 inline bool end() const { return false; }
599 inline pkgCache::Version
const * operator->() const { return &*getVer(); }
603 virtual bool insert(pkgCache::VerIterator
const &V
) = 0;
604 virtual bool empty() const = 0;
605 virtual void clear() = 0;
607 /** \brief specifies which version(s) will be returned if non is given */
608 enum APT_DEPRECATED Version
{
609 ALL
= CacheSetHelper::ALL
,
610 CANDANDINST
= CacheSetHelper::CANDANDINST
,
611 CANDIDATE
= CacheSetHelper::CANDIDATE
,
612 INSTALLED
= CacheSetHelper::INSTALLED
,
613 CANDINST
= CacheSetHelper::CANDINST
,
614 INSTCAND
= CacheSetHelper::INSTCAND
,
615 NEWEST
= CacheSetHelper::NEWEST
619 unsigned short const ID
;
620 const char * const Alias
;
621 enum Position
{ NONE
, PREFIX
, POSTFIX
} const Pos
;
622 enum CacheSetHelper::VerSelector
const SelectVersion
;
623 Modifier (unsigned short const &id
, const char * const alias
, Position
const &pos
,
624 enum CacheSetHelper::VerSelector
const select
) : ID(id
), Alias(alias
), Pos(pos
),
625 SelectVersion(select
) {}
627 #pragma GCC diagnostic push
628 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
630 APT_DEPRECATED
Modifier(unsigned short const &id
, const char * const alias
, Position
const &pos
,
631 Version
const &select
) : ID(id
), Alias(alias
), Pos(pos
),
632 SelectVersion((CacheSetHelper::VerSelector
)select
) {}
634 #pragma GCC diagnostic pop
638 static bool FromCommandLine(VersionContainerInterface
* const vci
, pkgCacheFile
&Cache
,
639 const char **cmdline
, CacheSetHelper::VerSelector
const fallback
,
640 CacheSetHelper
&helper
);
642 #pragma GCC diagnostic push
643 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
645 APT_DEPRECATED
static bool FromCommandLine(VersionContainerInterface
* const vci
, pkgCacheFile
&Cache
,
646 const char **cmdline
, Version
const &fallback
,
647 CacheSetHelper
&helper
) {
648 return FromCommandLine(vci
, Cache
, cmdline
, (CacheSetHelper::VerSelector
)fallback
, helper
);
651 #pragma GCC diagnostic pop
654 static bool FromString(VersionContainerInterface
* const vci
, pkgCacheFile
&Cache
,
655 std::string pkg
, CacheSetHelper::VerSelector
const fallback
, CacheSetHelper
&helper
,
656 bool const onlyFromName
= false);
658 #pragma GCC diagnostic push
659 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
661 APT_DEPRECATED
static bool FromString(VersionContainerInterface
* const vci
, pkgCacheFile
&Cache
,
662 std::string pkg
, Version
const &fallback
, CacheSetHelper
&helper
,
663 bool const onlyFromName
= false) {
664 return FromString(vci
, Cache
, pkg
, (CacheSetHelper::VerSelector
)fallback
, helper
, onlyFromName
);
667 #pragma GCC diagnostic pop
670 static bool FromPackage(VersionContainerInterface
* const vci
, pkgCacheFile
&Cache
,
671 pkgCache::PkgIterator
const &P
, CacheSetHelper::VerSelector
const fallback
,
672 CacheSetHelper
&helper
);
674 #pragma GCC diagnostic push
675 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
677 APT_DEPRECATED
static bool FromPackage(VersionContainerInterface
* const vci
, pkgCacheFile
&Cache
,
678 pkgCache::PkgIterator
const &P
, Version
const &fallback
,
679 CacheSetHelper
&helper
) {
680 return FromPackage(vci
, Cache
, P
, (CacheSetHelper::VerSelector
)fallback
, helper
);
683 #pragma GCC diagnostic pop
686 static bool FromModifierCommandLine(unsigned short &modID
,
687 VersionContainerInterface
* const vci
,
688 pkgCacheFile
&Cache
, const char * cmdline
,
689 std::list
<Modifier
> const &mods
,
690 CacheSetHelper
&helper
);
693 static bool FromDependency(VersionContainerInterface
* const vci
,
695 pkgCache::DepIterator
const &D
,
696 CacheSetHelper::VerSelector
const selector
,
697 CacheSetHelper
&helper
);
699 #pragma GCC diagnostic push
700 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
702 APT_DEPRECATED
static bool FromDependency(VersionContainerInterface
* const vci
,
704 pkgCache::DepIterator
const &D
,
705 Version
const &selector
,
706 CacheSetHelper
&helper
) {
707 return FromDependency(vci
, Cache
, D
, (CacheSetHelper::VerSelector
)selector
, helper
);
710 #pragma GCC diagnostic pop
715 /** \brief returns the candidate version of the package
717 \param Cache to be used to query for information
718 \param Pkg we want the candidate version from this package
719 \param helper used in this container instance */
720 static pkgCache::VerIterator
getCandidateVer(pkgCacheFile
&Cache
,
721 pkgCache::PkgIterator
const &Pkg
, CacheSetHelper
&helper
);
723 /** \brief returns the installed version of the package
725 \param Cache to be used to query for information
726 \param Pkg we want the installed version from this package
727 \param helper used in this container instance */
728 static pkgCache::VerIterator
getInstalledVer(pkgCacheFile
&Cache
,
729 pkgCache::PkgIterator
const &Pkg
, CacheSetHelper
&helper
);
733 template<class Container
> class VersionContainer
: public VersionContainerInterface
{/*{{{*/
734 /** \class APT::VersionContainer
736 Simple wrapper around a container class like std::set to provide a similar
737 interface to a set of versions as to the complete set of all versions in the
741 /** \brief smell like a pkgCache::VerIterator */
742 class const_iterator
: public VersionContainerInterface::const_iterator
,
743 public std::iterator
<std::forward_iterator_tag
, typename
Container::const_iterator
> {/*{{{*/
744 typename
Container::const_iterator _iter
;
746 const_iterator(typename
Container::const_iterator i
) : _iter(i
) {}
747 pkgCache::VerIterator
getVer(void) const { return *_iter
; }
748 inline pkgCache::VerIterator
operator*(void) const { return *_iter
; }
749 operator typename
Container::const_iterator(void) const { return _iter
; }
750 inline const_iterator
& operator++() { ++_iter
; return *this; }
751 inline const_iterator
operator++(int) { const_iterator
tmp(*this); operator++(); return tmp
; }
752 inline bool operator!=(const_iterator
const &i
) const { return _iter
!= i
._iter
; }
753 inline bool operator==(const_iterator
const &i
) const { return _iter
== i
._iter
; }
754 friend std::ostream
& operator<<(std::ostream
& out
, const_iterator i
) { return operator<<(out
, *i
); }
756 class iterator
: public VersionContainerInterface::const_iterator
,
757 public std::iterator
<std::forward_iterator_tag
, typename
Container::iterator
> {
758 typename
Container::iterator _iter
;
760 iterator(typename
Container::iterator i
) : _iter(i
) {}
761 pkgCache::VerIterator
getVer(void) const { return *_iter
; }
762 inline pkgCache::VerIterator
operator*(void) const { return *_iter
; }
763 operator typename
Container::iterator(void) const { return _iter
; }
764 operator typename VersionContainer
<Container
>::const_iterator() { return typename VersionContainer
<Container
>::const_iterator(_iter
); }
765 inline iterator
& operator++() { ++_iter
; return *this; }
766 inline iterator
operator++(int) { iterator
tmp(*this); operator++(); return tmp
; }
767 inline bool operator!=(iterator
const &i
) const { return _iter
!= i
._iter
; }
768 inline bool operator==(iterator
const &i
) const { return _iter
== i
._iter
; }
769 inline iterator
& operator=(iterator
const &i
) { _iter
= i
._iter
; return *this; }
770 inline iterator
& operator=(typename
Container::iterator
const &i
) { _iter
= i
; return *this; }
771 friend std::ostream
& operator<<(std::ostream
& out
, iterator i
) { return operator<<(out
, *i
); }
775 bool insert(pkgCache::VerIterator
const &V
) { if (V
.end() == true) return false; _cont
.insert(V
); return true; }
776 template<class Cont
> void insert(VersionContainer
<Cont
> const &vercont
) { _cont
.insert((typename
Cont::const_iterator
)vercont
.begin(), (typename
Cont::const_iterator
)vercont
.end()); }
777 void insert(const_iterator begin
, const_iterator end
) { _cont
.insert(begin
, end
); }
778 bool empty() const { return _cont
.empty(); }
779 void clear() { return _cont
.clear(); }
780 //FIXME: on ABI break, replace the first with the second without bool
781 void erase(iterator position
) { _cont
.erase((typename
Container::iterator
)position
); }
782 iterator
& erase(iterator
&position
, bool) { return position
= _cont
.erase((typename
Container::iterator
)position
); }
783 size_t erase(const pkgCache::VerIterator x
) { return _cont
.erase(x
); }
784 void erase(iterator first
, iterator last
) { _cont
.erase(first
, last
); }
785 size_t size() const { return _cont
.size(); }
787 const_iterator
begin() const { return const_iterator(_cont
.begin()); }
788 const_iterator
end() const { return const_iterator(_cont
.end()); }
789 iterator
begin() { return iterator(_cont
.begin()); }
790 iterator
end() { return iterator(_cont
.end()); }
791 const_iterator
find(pkgCache::VerIterator
const &V
) const { return const_iterator(_cont
.find(V
)); }
793 /** \brief sort all included versions with given comparer
795 Some containers are sorted by default, some are not and can't be,
796 but a few like std::vector can be sorted if need be, so this can be
797 specialized in later on. The default is that this will fail though.
798 Specifically, already sorted containers like std::set will return
799 false as well as there is no easy way to check that the given comparer
800 would sort in the same way the set is currently sorted
802 \return \b true if the set was sorted, \b false if not. */
803 template<class Compare
> bool sort(Compare
/*Comp*/) { return false; }
805 /** \brief returns all versions specified on the commandline
807 Get all versions from the commandline, uses given default version if
808 non specifically requested and executes regex's if needed on names.
809 \param Cache the packages and versions are in
810 \param cmdline Command line the versions should be extracted from
811 \param fallback version specification
812 \param helper responsible for error and message handling */
813 static VersionContainer
FromCommandLine(pkgCacheFile
&Cache
, const char **cmdline
,
814 CacheSetHelper::VerSelector
const fallback
, CacheSetHelper
&helper
) {
815 VersionContainer vercon
;
816 VersionContainerInterface::FromCommandLine(&vercon
, Cache
, cmdline
, fallback
, helper
);
819 static VersionContainer
FromCommandLine(pkgCacheFile
&Cache
, const char **cmdline
,
820 CacheSetHelper::VerSelector
const fallback
) {
821 CacheSetHelper helper
;
822 return FromCommandLine(Cache
, cmdline
, fallback
, helper
);
824 static VersionContainer
FromCommandLine(pkgCacheFile
&Cache
, const char **cmdline
) {
825 return FromCommandLine(Cache
, cmdline
, CacheSetHelper::CANDINST
);
827 static VersionContainer
FromString(pkgCacheFile
&Cache
, std::string
const &pkg
,
828 CacheSetHelper::VerSelector
const fallback
, CacheSetHelper
&helper
,
829 bool const /*onlyFromName = false*/) {
830 VersionContainer vercon
;
831 VersionContainerInterface::FromString(&vercon
, Cache
, pkg
, fallback
, helper
);
834 static VersionContainer
FromString(pkgCacheFile
&Cache
, std::string pkg
,
835 CacheSetHelper::VerSelector
const fallback
) {
836 CacheSetHelper helper
;
837 return FromString(Cache
, pkg
, fallback
, helper
);
839 static VersionContainer
FromString(pkgCacheFile
&Cache
, std::string pkg
) {
840 return FromString(Cache
, pkg
, CacheSetHelper::CANDINST
);
843 #pragma GCC diagnostic push
844 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
846 static VersionContainer
FromCommandLine(pkgCacheFile
&Cache
, const char **cmdline
,
847 Version
const &fallback
, CacheSetHelper
&helper
) {
848 VersionContainer vercon
;
849 VersionContainerInterface::FromCommandLine(&vercon
, Cache
, cmdline
, (CacheSetHelper::VerSelector
)fallback
, helper
);
852 static VersionContainer
FromCommandLine(pkgCacheFile
&Cache
, const char **cmdline
,
853 Version
const &fallback
) {
854 CacheSetHelper helper
;
855 return FromCommandLine(Cache
, cmdline
, (CacheSetHelper::VerSelector
)fallback
, helper
);
857 static VersionContainer
FromString(pkgCacheFile
&Cache
, std::string
const &pkg
,
858 Version
const &fallback
, CacheSetHelper
&helper
,
859 bool const /*onlyFromName = false*/) {
860 VersionContainer vercon
;
861 VersionContainerInterface::FromString(&vercon
, Cache
, pkg
, (CacheSetHelper::VerSelector
)fallback
, helper
);
864 static VersionContainer
FromString(pkgCacheFile
&Cache
, std::string pkg
,
865 Version
const &fallback
) {
866 CacheSetHelper helper
;
867 return FromString(Cache
, pkg
, (CacheSetHelper::VerSelector
)fallback
, helper
);
870 #pragma GCC diagnostic pop
873 /** \brief returns all versions specified for the package
875 \param Cache the package and versions are in
876 \param P the package in question
877 \param fallback the version(s) you want to get
878 \param helper the helper used for display and error handling */
879 static VersionContainer
FromPackage(pkgCacheFile
&Cache
, pkgCache::PkgIterator
const &P
,
880 CacheSetHelper::VerSelector
const fallback
, CacheSetHelper
&helper
) {
881 VersionContainer vercon
;
882 VersionContainerInterface::FromPackage(&vercon
, Cache
, P
, fallback
, helper
);
885 static VersionContainer
FromPackage(pkgCacheFile
&Cache
, pkgCache::PkgIterator
const &P
,
886 CacheSetHelper::VerSelector
const fallback
) {
887 CacheSetHelper helper
;
888 return FromPackage(Cache
, P
, fallback
, helper
);
891 #pragma GCC diagnostic push
892 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
894 static VersionContainer
FromPackage(pkgCacheFile
&Cache
, pkgCache::PkgIterator
const &P
,
895 Version
const &fallback
, CacheSetHelper
&helper
) {
896 VersionContainer vercon
;
897 VersionContainerInterface::FromPackage(&vercon
, Cache
, P
, (CacheSetHelper::VerSelector
)fallback
, helper
);
900 static VersionContainer
FromPackage(pkgCacheFile
&Cache
, pkgCache::PkgIterator
const &P
,
901 Version
const &fallback
) {
902 CacheSetHelper helper
;
903 return FromPackage(Cache
, P
, (CacheSetHelper::VerSelector
)fallback
, helper
);
906 #pragma GCC diagnostic pop
908 static VersionContainer
FromPackage(pkgCacheFile
&Cache
, pkgCache::PkgIterator
const &P
) {
909 return FromPackage(Cache
, P
, CacheSetHelper::CANDIDATE
);
912 static std::map
<unsigned short, VersionContainer
> GroupedFromCommandLine(
914 const char **cmdline
,
915 std::list
<Modifier
> const &mods
,
916 unsigned short const fallback
,
917 CacheSetHelper
&helper
) {
918 std::map
<unsigned short, VersionContainer
> versets
;
919 for (const char **I
= cmdline
; *I
!= 0; ++I
) {
920 unsigned short modID
= fallback
;
921 VersionContainer verset
;
922 VersionContainerInterface::FromModifierCommandLine(modID
, &verset
, Cache
, *I
, mods
, helper
);
923 versets
[modID
].insert(verset
);
928 static std::map
<unsigned short, VersionContainer
> GroupedFromCommandLine(
929 pkgCacheFile
&Cache
, const char **cmdline
,
930 std::list
<Modifier
> const &mods
,
931 unsigned short const fallback
) {
932 CacheSetHelper helper
;
933 return GroupedFromCommandLine(Cache
, cmdline
,
934 mods
, fallback
, helper
);
937 static VersionContainer
FromDependency(pkgCacheFile
&Cache
, pkgCache::DepIterator
const &D
,
938 CacheSetHelper::VerSelector
const selector
, CacheSetHelper
&helper
) {
939 VersionContainer vercon
;
940 VersionContainerInterface::FromDependency(&vercon
, Cache
, D
, selector
, helper
);
943 static VersionContainer
FromDependency(pkgCacheFile
&Cache
, pkgCache::DepIterator
const &D
,
944 CacheSetHelper::VerSelector
const selector
) {
945 CacheSetHelper helper
;
946 return FromPackage(Cache
, D
, selector
, helper
);
949 #pragma GCC diagnostic push
950 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
952 static VersionContainer
FromDependency(pkgCacheFile
&Cache
, pkgCache::DepIterator
const &D
,
953 Version
const &selector
, CacheSetHelper
&helper
) {
954 VersionContainer vercon
;
955 VersionContainerInterface::FromDependency(&vercon
, Cache
, D
, (CacheSetHelper::VerSelector
)selector
, helper
);
958 static VersionContainer
FromDependency(pkgCacheFile
&Cache
, pkgCache::DepIterator
const &D
,
959 Version
const &selector
) {
960 CacheSetHelper helper
;
961 return FromPackage(Cache
, D
, (CacheSetHelper::VerSelector
)selector
, helper
);
964 #pragma GCC diagnostic pop
966 static VersionContainer
FromDependency(pkgCacheFile
&Cache
, pkgCache::DepIterator
const &D
) {
967 return FromPackage(Cache
, D
, CacheSetHelper::CANDIDATE
);
971 // specialisations for push_back containers: std::list & std::vector /*{{{*/
972 template<> template<class Cont
> void VersionContainer
<std::list
<pkgCache::VerIterator
> >::insert(VersionContainer
<Cont
> const &vercont
) {
973 for (typename VersionContainer
<Cont
>::const_iterator v
= vercont
.begin(); v
!= vercont
.end(); ++v
)
976 template<> template<class Cont
> void VersionContainer
<std::vector
<pkgCache::VerIterator
> >::insert(VersionContainer
<Cont
> const &vercont
) {
977 for (typename VersionContainer
<Cont
>::const_iterator v
= vercont
.begin(); v
!= vercont
.end(); ++v
)
980 // these two are 'inline' as otherwise the linker has problems with seeing these untemplated
981 // specializations again and again - but we need to see them, so that library users can use them
982 template<> inline bool VersionContainer
<std::list
<pkgCache::VerIterator
> >::insert(pkgCache::VerIterator
const &V
) {
988 template<> inline bool VersionContainer
<std::vector
<pkgCache::VerIterator
> >::insert(pkgCache::VerIterator
const &V
) {
994 template<> inline void VersionContainer
<std::list
<pkgCache::VerIterator
> >::insert(const_iterator begin
, const_iterator end
) {
995 for (const_iterator v
= begin
; v
!= end
; ++v
)
998 template<> inline void VersionContainer
<std::vector
<pkgCache::VerIterator
> >::insert(const_iterator begin
, const_iterator end
) {
999 for (const_iterator v
= begin
; v
!= end
; ++v
)
1000 _cont
.push_back(*v
);
1004 template<> template<class Compare
> inline bool VersionContainer
<std::vector
<pkgCache::VerIterator
> >::sort(Compare Comp
) {
1005 std::sort(_cont
.begin(), _cont
.end(), Comp
);
1009 typedef VersionContainer
<std::set
<pkgCache::VerIterator
> > VersionSet
;
1010 typedef VersionContainer
<std::list
<pkgCache::VerIterator
> > VersionList
;
1011 typedef VersionContainer
<std::vector
<pkgCache::VerIterator
> > VersionVector
;