]>
Commit | Line | Data |
---|---|---|
e1dbde8d DK |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
7959c5ed DK |
3 | /** \file cacheset.h |
4 | Wrappers around std::set to have set::iterators which behave | |
5 | similar to the Iterators of the cache structures. | |
e1dbde8d | 6 | |
7959c5ed | 7 | Provides also a few helper methods which work with these sets */ |
e1dbde8d | 8 | /*}}}*/ |
7959c5ed DK |
9 | #ifndef APT_CACHESET_H |
10 | #define APT_CACHESET_H | |
e1dbde8d | 11 | // Include Files /*{{{*/ |
ffee1c2b | 12 | #include <fstream> |
9cc83a6f | 13 | #include <map> |
ffee1c2b | 14 | #include <set> |
c4cca791 | 15 | #include <list> |
5c8c7321 | 16 | #include <vector> |
e1dbde8d | 17 | #include <string> |
15fc8636 | 18 | #include <iterator> |
5c8c7321 | 19 | #include <algorithm> |
ffee1c2b | 20 | |
453b82a3 DK |
21 | #include <stddef.h> |
22 | ||
472ff00e | 23 | #include <apt-pkg/error.h> |
e1dbde8d | 24 | #include <apt-pkg/pkgcache.h> |
453b82a3 | 25 | #include <apt-pkg/cacheiterators.h> |
586d8704 | 26 | #include <apt-pkg/macros.h> |
b9dadc24 DK |
27 | |
28 | #ifndef APT_8_CLEANER_HEADERS | |
29 | #include <apt-pkg/cachefile.h> | |
453b82a3 DK |
30 | #endif |
31 | #ifndef APT_10_CLEANER_HEADERS | |
32 | #include <iostream> | |
b9dadc24 | 33 | #endif |
e1dbde8d | 34 | /*}}}*/ |
472ff00e DK |
35 | |
36 | class pkgCacheFile; | |
37 | ||
e1dbde8d | 38 | namespace APT { |
15fc8636 DK |
39 | class PackageContainerInterface; |
40 | class VersionContainerInterface; | |
41 | ||
70e706ad DK |
42 | class CacheSetHelper { /*{{{*/ |
43 | /** \class APT::CacheSetHelper | |
44 | Simple base class with a lot of virtual methods which can be overridden | |
45 | to alter the behavior or the output of the CacheSets. | |
46 | ||
47 | This helper is passed around by the static methods in the CacheSets and | |
48 | used every time they hit an error condition or something could be | |
49 | printed out. | |
50 | */ | |
51 | public: /*{{{*/ | |
15fc8636 | 52 | CacheSetHelper(bool const ShowError = true, |
c8a4ce6c DK |
53 | GlobalError::MsgType ErrorType = GlobalError::ERROR); |
54 | virtual ~CacheSetHelper(); | |
70e706ad | 55 | |
1e064088 DK |
56 | enum PkgSelector { UNKNOWN, REGEX, TASK, FNMATCH, PACKAGENAME, STRING }; |
57 | ||
58 | virtual bool PackageFrom(enum PkgSelector const select, PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string const &pattern); | |
59 | ||
60 | virtual bool PackageFromCommandLine(PackageContainerInterface * const pci, pkgCacheFile &Cache, const char **cmdline); | |
61 | ||
62 | struct PkgModifier { | |
63 | enum Position { NONE, PREFIX, POSTFIX }; | |
64 | unsigned short ID; | |
65 | const char * const Alias; | |
66 | Position Pos; | |
67 | PkgModifier (unsigned short const &id, const char * const alias, Position const &pos) : ID(id), Alias(alias), Pos(pos) {} | |
68 | }; | |
69 | virtual bool PackageFromModifierCommandLine(unsigned short &modID, PackageContainerInterface * const pci, | |
70 | pkgCacheFile &Cache, const char * cmdline, | |
71 | std::list<PkgModifier> const &mods); | |
72 | ||
73 | // use PackageFrom(PACKAGENAME, …) instead | |
74 | APT_DEPRECATED pkgCache::PkgIterator PackageFromName(pkgCacheFile &Cache, std::string const &pattern); | |
fdba4d53 DK |
75 | |
76 | /** \brief be notified about the package being selected via pattern | |
77 | * | |
78 | * Main use is probably to show a message to the user what happened | |
79 | * | |
80 | * \param pkg is the package which was selected | |
81 | * \param select is the selection method which choose the package | |
82 | * \param pattern is the string used by the selection method to pick the package | |
83 | */ | |
84 | virtual void showPackageSelection(pkgCache::PkgIterator const &pkg, PkgSelector const select, std::string const &pattern); | |
85 | // use the method above instead, react only on the type you need and let the base handle the rest if need be | |
86 | // this allows use to add new selection methods without breaking the ABI constantly with new virtual methods | |
87 | APT_DEPRECATED virtual void showTaskSelection(pkgCache::PkgIterator const &pkg, std::string const &pattern); | |
88 | APT_DEPRECATED virtual void showRegExSelection(pkgCache::PkgIterator const &pkg, std::string const &pattern); | |
89 | APT_DEPRECATED virtual void showFnmatchSelection(pkgCache::PkgIterator const &pkg, std::string const &pattern); | |
90 | ||
91 | /** \brief be notified if a package can't be found via pattern | |
92 | * | |
93 | * Can be used to show a message as well as to try something else to make it match | |
94 | * | |
95 | * \param select is the method tried for selection | |
96 | * \param pci is the container the package should be inserted in | |
97 | * \param Cache is the package universe available | |
98 | * \param pattern is the string not matching anything | |
99 | */ | |
100 | virtual void canNotFindPackage(enum PkgSelector const select, PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string const &pattern); | |
101 | // same as above for showPackageSelection | |
102 | APT_DEPRECATED virtual void canNotFindTask(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern); | |
103 | APT_DEPRECATED virtual void canNotFindRegEx(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern); | |
104 | APT_DEPRECATED virtual void canNotFindFnmatch(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern); | |
105 | APT_DEPRECATED virtual void canNotFindPackage(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string const &str); | |
106 | ||
107 | /** \brief specifies which version(s) we want to refer to */ | |
108 | enum VerSelector { | |
109 | /** by release string */ | |
110 | RELEASE, | |
111 | /** by version number string */ | |
112 | VERSIONNUMBER, | |
113 | /** All versions */ | |
114 | ALL, | |
115 | /** Candidate and installed version */ | |
116 | CANDANDINST, | |
117 | /** Candidate version */ | |
118 | CANDIDATE, | |
119 | /** Installed version */ | |
120 | INSTALLED, | |
121 | /** Candidate or if non installed version */ | |
122 | CANDINST, | |
123 | /** Installed or if non candidate version */ | |
124 | INSTCAND, | |
125 | /** Newest version */ | |
126 | NEWEST | |
127 | }; | |
70e706ad | 128 | |
fdba4d53 DK |
129 | /** \brief be notified about the version being selected via pattern |
130 | * | |
131 | * Main use is probably to show a message to the user what happened | |
132 | * Note that at the moment this method is only called for RELEASE | |
133 | * and VERSION selections, not for the others. | |
134 | * | |
135 | * \param Pkg is the package which was selected for | |
136 | * \param Ver is the version selected | |
137 | * \param select is the selection method which choose the version | |
138 | * \param pattern is the string used by the selection method to pick the version | |
139 | */ | |
140 | virtual void showVersionSelection(pkgCache::PkgIterator const &Pkg, pkgCache::VerIterator const &Ver, | |
141 | enum VerSelector const select, std::string const &pattern); | |
142 | // renamed to have a similar interface to showPackageSelection | |
143 | APT_DEPRECATED virtual void showSelectedVersion(pkgCache::PkgIterator const &Pkg, pkgCache::VerIterator const Ver, | |
144 | std::string const &ver, bool const verIsRel); | |
15fc8636 | 145 | |
fdba4d53 DK |
146 | /** \brief be notified if a version can't be found for a package |
147 | * | |
148 | * Main use is probably to show a message to the user what happened | |
149 | * | |
150 | * \param select is the method tried for selection | |
151 | * \param vci is the container the version should be inserted in | |
152 | * \param Cache is the package universe available | |
153 | * \param Pkg is the package we wanted a version from | |
154 | */ | |
155 | virtual void canNotFindVersion(enum VerSelector const select, VersionContainerInterface * const vci, pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg); | |
156 | // same as above for showPackageSelection | |
157 | APT_DEPRECATED virtual void canNotFindAllVer(VersionContainerInterface * const vci, pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg); | |
158 | APT_DEPRECATED virtual void canNotFindInstCandVer(VersionContainerInterface * const vci, pkgCacheFile &Cache, | |
70e706ad | 159 | pkgCache::PkgIterator const &Pkg); |
fdba4d53 | 160 | APT_DEPRECATED virtual void canNotFindCandInstVer(VersionContainerInterface * const vci, |
15fc8636 | 161 | pkgCacheFile &Cache, |
cf28bcad | 162 | pkgCache::PkgIterator const &Pkg); |
15fc8636 | 163 | |
fdba4d53 DK |
164 | // the difference between canNotFind and canNotGet is that the later is more low-level |
165 | // and called from other places: In this case looking into the code is the only real answer… | |
166 | virtual pkgCache::VerIterator canNotGetVersion(enum VerSelector const select, pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg); | |
167 | // same as above for showPackageSelection | |
168 | APT_DEPRECATED virtual pkgCache::VerIterator canNotFindNewestVer(pkgCacheFile &Cache, | |
70e706ad | 169 | pkgCache::PkgIterator const &Pkg); |
fdba4d53 | 170 | APT_DEPRECATED virtual pkgCache::VerIterator canNotFindCandidateVer(pkgCacheFile &Cache, |
70e706ad | 171 | pkgCache::PkgIterator const &Pkg); |
fdba4d53 | 172 | APT_DEPRECATED virtual pkgCache::VerIterator canNotFindInstalledVer(pkgCacheFile &Cache, |
70e706ad DK |
173 | pkgCache::PkgIterator const &Pkg); |
174 | ||
fdba4d53 DK |
175 | virtual pkgCache::PkgIterator canNotFindPkgName(pkgCacheFile &Cache, std::string const &str); |
176 | ||
d3e8fbb3 DK |
177 | bool showErrors() const { return ShowError; } |
178 | bool showErrors(bool const newValue) { if (ShowError == newValue) return ShowError; else return ((ShowError = newValue) == false); } | |
179 | GlobalError::MsgType errorType() const { return ErrorType; } | |
cd7bbc47 DK |
180 | GlobalError::MsgType errorType(GlobalError::MsgType const &newValue) |
181 | { | |
182 | if (ErrorType == newValue) return ErrorType; | |
183 | else { | |
184 | GlobalError::MsgType const &oldValue = ErrorType; | |
185 | ErrorType = newValue; | |
186 | return oldValue; | |
187 | } | |
d3e8fbb3 | 188 | } |
cd7bbc47 | 189 | |
70e706ad DK |
190 | /*}}}*/ |
191 | protected: | |
192 | bool ShowError; | |
cd7bbc47 | 193 | GlobalError::MsgType ErrorType; |
fdba4d53 DK |
194 | |
195 | pkgCache::VerIterator canNotGetInstCandVer(pkgCacheFile &Cache, | |
196 | pkgCache::PkgIterator const &Pkg); | |
197 | pkgCache::VerIterator canNotGetCandInstVer(pkgCacheFile &Cache, | |
198 | pkgCache::PkgIterator const &Pkg); | |
1e064088 DK |
199 | |
200 | bool PackageFromTask(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern); | |
201 | bool PackageFromRegEx(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern); | |
202 | bool PackageFromFnmatch(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern); | |
203 | bool PackageFromPackageName(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern); | |
204 | bool PackageFromString(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string const &pattern); | |
c8a4ce6c DK |
205 | private: |
206 | void *d; | |
70e706ad | 207 | }; /*}}}*/ |
5c8c7321 | 208 | |
15fc8636 DK |
209 | class PackageContainerInterface { /*{{{*/ |
210 | /** \class PackageContainerInterface | |
211 | ||
212 | * Interface ensuring that all operations can be executed on the yet to | |
213 | * define concrete PackageContainer - access to all methods is possible, | |
214 | * but in general the wrappers provided by the PackageContainer template | |
215 | * are nicer to use. | |
216 | ||
217 | * This class mostly protects use from the need to write all implementation | |
218 | * of the methods working on containers in the template */ | |
219 | public: | |
220 | class const_iterator { /*{{{*/ | |
e1dbde8d | 221 | public: |
15fc8636 DK |
222 | virtual pkgCache::PkgIterator getPkg() const = 0; |
223 | operator pkgCache::PkgIterator(void) const { return getPkg(); } | |
224 | ||
225 | inline const char *Name() const {return getPkg().Name(); } | |
226 | inline std::string FullName(bool const Pretty) const { return getPkg().FullName(Pretty); } | |
227 | inline std::string FullName() const { return getPkg().FullName(); } | |
7a669774 | 228 | APT_DEPRECATED inline const char *Section() const { |
2b4cead3 DK |
229 | APT_IGNORE_DEPRECATED_PUSH |
230 | return getPkg().Section(); | |
231 | APT_IGNORE_DEPRECATED_POP | |
7a669774 | 232 | } |
15fc8636 DK |
233 | inline bool Purge() const {return getPkg().Purge(); } |
234 | inline const char *Arch() const {return getPkg().Arch(); } | |
235 | inline pkgCache::GrpIterator Group() const { return getPkg().Group(); } | |
236 | inline pkgCache::VerIterator VersionList() const { return getPkg().VersionList(); } | |
237 | inline pkgCache::VerIterator CurrentVer() const { return getPkg().CurrentVer(); } | |
238 | inline pkgCache::DepIterator RevDependsList() const { return getPkg().RevDependsList(); } | |
239 | inline pkgCache::PrvIterator ProvidesList() const { return getPkg().ProvidesList(); } | |
240 | inline pkgCache::PkgIterator::OkState State() const { return getPkg().State(); } | |
241 | inline const char *CandVersion() const { return getPkg().CandVersion(); } | |
242 | inline const char *CurVersion() const { return getPkg().CurVersion(); } | |
d3e8fbb3 DK |
243 | inline pkgCache *Cache() const { return getPkg().Cache(); } |
244 | inline unsigned long Index() const {return getPkg().Index();} | |
78c32596 | 245 | // we have only valid iterators here |
d3e8fbb3 | 246 | inline bool end() const { return false; } |
e1dbde8d | 247 | |
d3e8fbb3 | 248 | inline pkgCache::Package const * operator->() const {return &*getPkg();} |
15fc8636 DK |
249 | }; |
250 | /*}}}*/ | |
251 | ||
252 | virtual bool insert(pkgCache::PkgIterator const &P) = 0; | |
253 | virtual bool empty() const = 0; | |
254 | virtual void clear() = 0; | |
255 | ||
fdba4d53 DK |
256 | // FIXME: This is a bloody hack removed soon. Use CacheSetHelper::PkgSelector ! |
257 | enum APT_DEPRECATED Constructor { UNKNOWN = CacheSetHelper::UNKNOWN, | |
258 | REGEX = CacheSetHelper::REGEX, | |
259 | TASK = CacheSetHelper::TASK, | |
260 | FNMATCH = CacheSetHelper::FNMATCH }; | |
586d8704 | 261 | APT_IGNORE_DEPRECATED_PUSH |
fdba4d53 | 262 | void setConstructor(Constructor const by) { ConstructedBy = (CacheSetHelper::PkgSelector)by; } |
586d8704 | 263 | APT_IGNORE_DEPRECATED_POP |
fdba4d53 DK |
264 | |
265 | void setConstructor(CacheSetHelper::PkgSelector const by) { ConstructedBy = by; } | |
266 | CacheSetHelper::PkgSelector getConstructor() const { return ConstructedBy; } | |
c8a4ce6c | 267 | PackageContainerInterface(); |
e8afd168 | 268 | explicit PackageContainerInterface(CacheSetHelper::PkgSelector const by); |
c8a4ce6c | 269 | virtual ~PackageContainerInterface(); |
15fc8636 | 270 | |
1e064088 DK |
271 | APT_DEPRECATED static bool FromTask(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) { |
272 | return helper.PackageFrom(CacheSetHelper::TASK, pci, Cache, pattern); } | |
273 | APT_DEPRECATED static bool FromRegEx(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) { | |
274 | return helper.PackageFrom(CacheSetHelper::REGEX, pci, Cache, pattern); } | |
275 | APT_DEPRECATED static bool FromFnmatch(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) { | |
276 | return helper.PackageFrom(CacheSetHelper::FNMATCH, pci, Cache, pattern); } | |
277 | APT_DEPRECATED static bool FromGroup(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) { | |
278 | return helper.PackageFrom(CacheSetHelper::PACKAGENAME, pci, Cache, pattern); } | |
279 | APT_DEPRECATED static bool FromString(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper) { | |
280 | return helper.PackageFrom(CacheSetHelper::STRING, pci, Cache, pattern); } | |
281 | APT_DEPRECATED static bool FromCommandLine(PackageContainerInterface * const pci, pkgCacheFile &Cache, const char **cmdline, CacheSetHelper &helper) { | |
282 | return helper.PackageFromCommandLine(pci, Cache, cmdline); } | |
283 | ||
284 | APT_DEPRECATED typedef CacheSetHelper::PkgModifier Modifier; | |
15fc8636 | 285 | |
586d8704 | 286 | APT_IGNORE_DEPRECATED_PUSH |
1e064088 DK |
287 | APT_DEPRECATED static pkgCache::PkgIterator FromName(pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper) { |
288 | return helper.PackageFromName(Cache, pattern); } | |
289 | APT_DEPRECATED static bool FromModifierCommandLine(unsigned short &modID, PackageContainerInterface * const pci, | |
290 | pkgCacheFile &Cache, const char * cmdline, | |
291 | std::list<Modifier> const &mods, CacheSetHelper &helper) { | |
292 | return helper.PackageFromModifierCommandLine(modID, pci, Cache, cmdline, mods); } | |
586d8704 | 293 | APT_IGNORE_DEPRECATED_POP |
fdba4d53 DK |
294 | |
295 | private: | |
296 | CacheSetHelper::PkgSelector ConstructedBy; | |
c8a4ce6c | 297 | void *d; |
15fc8636 DK |
298 | }; |
299 | /*}}}*/ | |
300 | template<class Container> class PackageContainer : public PackageContainerInterface {/*{{{*/ | |
301 | /** \class APT::PackageContainer | |
e1dbde8d | 302 | |
15fc8636 DK |
303 | Simple wrapper around a container class like std::set to provide a similar |
304 | interface to a set of packages as to the complete set of all packages in the | |
305 | pkgCache. */ | |
306 | Container _cont; | |
307 | public: /*{{{*/ | |
308 | /** \brief smell like a pkgCache::PkgIterator */ | |
c4cca791 DK |
309 | class const_iterator : public PackageContainerInterface::const_iterator,/*{{{*/ |
310 | public std::iterator<std::forward_iterator_tag, typename Container::const_iterator> { | |
15fc8636 DK |
311 | typename Container::const_iterator _iter; |
312 | public: | |
e8afd168 | 313 | explicit const_iterator(typename Container::const_iterator i) : _iter(i) {} |
15fc8636 | 314 | pkgCache::PkgIterator getPkg(void) const { return *_iter; } |
d3e8fbb3 | 315 | inline pkgCache::PkgIterator operator*(void) const { return *_iter; } |
15fc8636 | 316 | operator typename Container::const_iterator(void) const { return _iter; } |
63b70b21 DK |
317 | inline const_iterator& operator++() { ++_iter; return *this; } |
318 | inline const_iterator operator++(int) { const_iterator tmp(*this); operator++(); return tmp; } | |
d3e8fbb3 DK |
319 | inline bool operator!=(const_iterator const &i) const { return _iter != i._iter; } |
320 | inline bool operator==(const_iterator const &i) const { return _iter == i._iter; } | |
15fc8636 | 321 | friend std::ostream& operator<<(std::ostream& out, const_iterator i) { return operator<<(out, *i); } |
e1dbde8d | 322 | }; |
c4cca791 DK |
323 | class iterator : public PackageContainerInterface::const_iterator, |
324 | public std::iterator<std::forward_iterator_tag, typename Container::iterator> { | |
325 | typename Container::iterator _iter; | |
326 | public: | |
e8afd168 | 327 | explicit iterator(typename Container::iterator i) : _iter(i) {} |
c4cca791 | 328 | pkgCache::PkgIterator getPkg(void) const { return *_iter; } |
d3e8fbb3 | 329 | inline pkgCache::PkgIterator operator*(void) const { return *_iter; } |
c4cca791 | 330 | operator typename Container::iterator(void) const { return _iter; } |
bce0e0ff | 331 | operator typename PackageContainer<Container>::const_iterator() { return typename PackageContainer<Container>::const_iterator(_iter); } |
63b70b21 DK |
332 | inline iterator& operator++() { ++_iter; return *this; } |
333 | inline iterator operator++(int) { iterator tmp(*this); operator++(); return tmp; } | |
d3e8fbb3 DK |
334 | inline bool operator!=(iterator const &i) const { return _iter != i._iter; } |
335 | inline bool operator==(iterator const &i) const { return _iter == i._iter; } | |
336 | inline iterator& operator=(iterator const &i) { _iter = i._iter; return *this; } | |
337 | inline iterator& operator=(typename Container::iterator const &i) { _iter = i; return *this; } | |
c4cca791 DK |
338 | friend std::ostream& operator<<(std::ostream& out, iterator i) { return operator<<(out, *i); } |
339 | }; | |
dc0f01f7 | 340 | /*}}}*/ |
ffee1c2b | 341 | |
d3e8fbb3 DK |
342 | bool insert(pkgCache::PkgIterator const &P) { if (P.end() == true) return false; _cont.insert(P); return true; } |
343 | template<class Cont> void insert(PackageContainer<Cont> const &pkgcont) { _cont.insert((typename Cont::const_iterator)pkgcont.begin(), (typename Cont::const_iterator)pkgcont.end()); } | |
344 | void insert(const_iterator begin, const_iterator end) { _cont.insert(begin, end); } | |
c4cca791 | 345 | |
d3e8fbb3 DK |
346 | bool empty() const { return _cont.empty(); } |
347 | void clear() { return _cont.clear(); } | |
5eb9a474 | 348 | //FIXME: on ABI break, replace the first with the second without bool |
d3e8fbb3 DK |
349 | void erase(iterator position) { _cont.erase((typename Container::iterator)position); } |
350 | iterator& erase(iterator &position, bool) { return position = _cont.erase((typename Container::iterator)position); } | |
351 | size_t erase(const pkgCache::PkgIterator x) { return _cont.erase(x); } | |
352 | void erase(iterator first, iterator last) { _cont.erase(first, last); } | |
353 | size_t size() const { return _cont.size(); } | |
15fc8636 | 354 | |
d3e8fbb3 DK |
355 | const_iterator begin() const { return const_iterator(_cont.begin()); } |
356 | const_iterator end() const { return const_iterator(_cont.end()); } | |
357 | iterator begin() { return iterator(_cont.begin()); } | |
358 | iterator end() { return iterator(_cont.end()); } | |
359 | const_iterator find(pkgCache::PkgIterator const &P) const { return const_iterator(_cont.find(P)); } | |
15fc8636 | 360 | |
c8a4ce6c | 361 | PackageContainer() : PackageContainerInterface(CacheSetHelper::UNKNOWN) {} |
e8afd168 | 362 | explicit PackageContainer(CacheSetHelper::PkgSelector const &by) : PackageContainerInterface(by) {} |
586d8704 | 363 | APT_IGNORE_DEPRECATED_PUSH |
fdba4d53 | 364 | APT_DEPRECATED PackageContainer(Constructor const &by) : PackageContainerInterface((CacheSetHelper::PkgSelector)by) {} |
586d8704 | 365 | APT_IGNORE_DEPRECATED_POP |
c45f2d19 | 366 | |
5c8c7321 DK |
367 | /** \brief sort all included versions with given comparer |
368 | ||
369 | Some containers are sorted by default, some are not and can't be, | |
370 | but a few like std::vector can be sorted if need be, so this can be | |
371 | specialized in later on. The default is that this will fail though. | |
372 | Specifically, already sorted containers like std::set will return | |
373 | false as well as there is no easy way to check that the given comparer | |
374 | would sort in the same way the set is currently sorted | |
375 | ||
376 | \return \b true if the set was sorted, \b false if not. */ | |
377 | template<class Compare> bool sort(Compare /*Comp*/) { return false; } | |
378 | ||
dc0f01f7 DK |
379 | /** \brief returns all packages in the cache who belong to the given task |
380 | ||
381 | A simple helper responsible for search for all members of a task | |
382 | in the cache. Optional it prints a a notice about the | |
383 | packages chosen cause of the given task. | |
384 | \param Cache the packages are in | |
385 | \param pattern name of the task | |
c8db3fff | 386 | \param helper responsible for error and message handling */ |
15fc8636 | 387 | static PackageContainer FromTask(pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper) { |
fdba4d53 | 388 | PackageContainer cont(CacheSetHelper::TASK); |
1e064088 | 389 | helper.PackageFrom(CacheSetHelper::TASK, &cont, Cache, pattern); |
15fc8636 DK |
390 | return cont; |
391 | } | |
392 | static PackageContainer FromTask(pkgCacheFile &Cache, std::string const &pattern) { | |
70e706ad | 393 | CacheSetHelper helper; |
446bbcf4 | 394 | return FromTask(Cache, pattern, helper); |
dc0f01f7 DK |
395 | } |
396 | ||
ffee1c2b DK |
397 | /** \brief returns all packages in the cache whose name matchs a given pattern |
398 | ||
399 | A simple helper responsible for executing a regular expression on all | |
400 | package names in the cache. Optional it prints a a notice about the | |
401 | packages chosen cause of the given package. | |
402 | \param Cache the packages are in | |
403 | \param pattern regular expression for package names | |
c8db3fff | 404 | \param helper responsible for error and message handling */ |
fdba4d53 DK |
405 | static PackageContainer FromRegEx(pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper) { |
406 | PackageContainer cont(CacheSetHelper::REGEX); | |
1e064088 | 407 | helper.PackageFrom(CacheSetHelper::REGEX, &cont, Cache, pattern); |
15fc8636 DK |
408 | return cont; |
409 | } | |
410 | ||
411 | static PackageContainer FromRegEx(pkgCacheFile &Cache, std::string const &pattern) { | |
70e706ad | 412 | CacheSetHelper helper; |
446bbcf4 | 413 | return FromRegEx(Cache, pattern, helper); |
ffee1c2b DK |
414 | } |
415 | ||
fdba4d53 DK |
416 | static PackageContainer FromFnmatch(pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper) { |
417 | PackageContainer cont(CacheSetHelper::FNMATCH); | |
1e064088 | 418 | helper.PackageFrom(CacheSetHelper::FNMATCH, &cont, Cache, pattern); |
b9179170 MV |
419 | return cont; |
420 | } | |
421 | static PackageContainer FromFnMatch(pkgCacheFile &Cache, std::string const &pattern) { | |
422 | CacheSetHelper helper; | |
423 | return FromFnmatch(Cache, pattern, helper); | |
424 | } | |
425 | ||
586d8704 | 426 | APT_IGNORE_DEPRECATED_PUSH |
15fc8636 | 427 | /** \brief returns a package specified by a string |
856d3b06 | 428 | |
15fc8636 DK |
429 | \param Cache the package is in |
430 | \param pattern String the package name should be extracted from | |
c8db3fff | 431 | \param helper responsible for error and message handling */ |
1e064088 DK |
432 | APT_DEPRECATED static pkgCache::PkgIterator FromName(pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper) { |
433 | return helper.PackageFromName(Cache, pattern); | |
15fc8636 | 434 | } |
1e064088 | 435 | APT_DEPRECATED static pkgCache::PkgIterator FromName(pkgCacheFile &Cache, std::string const &pattern) { |
70e706ad | 436 | CacheSetHelper helper; |
1e064088 | 437 | return FromName(Cache, pattern, helper); |
856d3b06 | 438 | } |
586d8704 | 439 | APT_IGNORE_DEPRECATED_POP |
856d3b06 | 440 | |
15fc8636 | 441 | /** \brief returns all packages specified by a string |
bd631595 | 442 | |
15fc8636 DK |
443 | \param Cache the packages are in |
444 | \param pattern String the package name(s) should be extracted from | |
c8db3fff | 445 | \param helper responsible for error and message handling */ |
15fc8636 DK |
446 | static PackageContainer FromString(pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper) { |
447 | PackageContainer cont; | |
1e064088 | 448 | helper.PackageFrom(CacheSetHelper::PACKAGENAME, &cont, Cache, pattern); |
15fc8636 DK |
449 | return cont; |
450 | } | |
451 | static PackageContainer FromString(pkgCacheFile &Cache, std::string const &pattern) { | |
bd631595 | 452 | CacheSetHelper helper; |
15fc8636 | 453 | return FromString(Cache, pattern, helper); |
bd631595 DK |
454 | } |
455 | ||
78c32596 DK |
456 | /** \brief returns all packages specified on the commandline |
457 | ||
458 | Get all package names from the commandline and executes regex's if needed. | |
459 | No special package command is supported, just plain names. | |
460 | \param Cache the packages are in | |
461 | \param cmdline Command line the package names should be extracted from | |
c8db3fff | 462 | \param helper responsible for error and message handling */ |
15fc8636 DK |
463 | static PackageContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline, CacheSetHelper &helper) { |
464 | PackageContainer cont; | |
1e064088 | 465 | helper.PackageFromCommandLine(&cont, Cache, cmdline); |
15fc8636 DK |
466 | return cont; |
467 | } | |
468 | static PackageContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline) { | |
70e706ad | 469 | CacheSetHelper helper; |
446bbcf4 | 470 | return FromCommandLine(Cache, cmdline, helper); |
78c32596 | 471 | } |
9cc83a6f | 472 | |
c8db3fff DK |
473 | /** \brief group packages by a action modifiers |
474 | ||
475 | At some point it is needed to get from the same commandline | |
476 | different package sets grouped by a modifier. Take | |
477 | apt-get install apt awesome- | |
478 | as an example. | |
479 | \param Cache the packages are in | |
480 | \param cmdline Command line the package names should be extracted from | |
481 | \param mods list of modifiers the method should accept | |
482 | \param fallback the default modifier group for a package | |
483 | \param helper responsible for error and message handling */ | |
15fc8636 DK |
484 | static std::map<unsigned short, PackageContainer> GroupedFromCommandLine( |
485 | pkgCacheFile &Cache, | |
486 | const char **cmdline, | |
1e064088 | 487 | std::list<CacheSetHelper::PkgModifier> const &mods, |
15fc8636 DK |
488 | unsigned short const &fallback, |
489 | CacheSetHelper &helper) { | |
490 | std::map<unsigned short, PackageContainer> pkgsets; | |
491 | for (const char **I = cmdline; *I != 0; ++I) { | |
492 | unsigned short modID = fallback; | |
493 | PackageContainer pkgset; | |
1e064088 | 494 | helper.PackageFromModifierCommandLine(modID, &pkgset, Cache, *I, mods); |
15fc8636 DK |
495 | pkgsets[modID].insert(pkgset); |
496 | } | |
497 | return pkgsets; | |
498 | } | |
499 | static std::map<unsigned short, PackageContainer> GroupedFromCommandLine( | |
500 | pkgCacheFile &Cache, | |
501 | const char **cmdline, | |
1e064088 | 502 | std::list<CacheSetHelper::PkgModifier> const &mods, |
15fc8636 | 503 | unsigned short const &fallback) { |
70e706ad | 504 | CacheSetHelper helper; |
446bbcf4 | 505 | return GroupedFromCommandLine(Cache, cmdline, |
70e706ad | 506 | mods, fallback, helper); |
9cc83a6f | 507 | } |
c8db3fff | 508 | /*}}}*/ |
d4489d49 | 509 | }; /*}}}*/ |
5c8c7321 | 510 | // specialisations for push_back containers: std::list & std::vector /*{{{*/ |
c4cca791 DK |
511 | template<> template<class Cont> void PackageContainer<std::list<pkgCache::PkgIterator> >::insert(PackageContainer<Cont> const &pkgcont) { |
512 | for (typename PackageContainer<Cont>::const_iterator p = pkgcont.begin(); p != pkgcont.end(); ++p) | |
513 | _cont.push_back(*p); | |
d3e8fbb3 | 514 | } |
5c8c7321 DK |
515 | template<> template<class Cont> void PackageContainer<std::vector<pkgCache::PkgIterator> >::insert(PackageContainer<Cont> const &pkgcont) { |
516 | for (typename PackageContainer<Cont>::const_iterator p = pkgcont.begin(); p != pkgcont.end(); ++p) | |
517 | _cont.push_back(*p); | |
518 | } | |
c4cca791 DK |
519 | // these two are 'inline' as otherwise the linker has problems with seeing these untemplated |
520 | // specializations again and again - but we need to see them, so that library users can use them | |
521 | template<> inline bool PackageContainer<std::list<pkgCache::PkgIterator> >::insert(pkgCache::PkgIterator const &P) { | |
522 | if (P.end() == true) | |
523 | return false; | |
524 | _cont.push_back(P); | |
525 | return true; | |
d3e8fbb3 | 526 | } |
5c8c7321 DK |
527 | template<> inline bool PackageContainer<std::vector<pkgCache::PkgIterator> >::insert(pkgCache::PkgIterator const &P) { |
528 | if (P.end() == true) | |
529 | return false; | |
530 | _cont.push_back(P); | |
531 | return true; | |
532 | } | |
c4cca791 DK |
533 | template<> inline void PackageContainer<std::list<pkgCache::PkgIterator> >::insert(const_iterator begin, const_iterator end) { |
534 | for (const_iterator p = begin; p != end; ++p) | |
535 | _cont.push_back(*p); | |
d3e8fbb3 | 536 | } |
5c8c7321 DK |
537 | template<> inline void PackageContainer<std::vector<pkgCache::PkgIterator> >::insert(const_iterator begin, const_iterator end) { |
538 | for (const_iterator p = begin; p != end; ++p) | |
539 | _cont.push_back(*p); | |
540 | } | |
541 | /*}}}*/ | |
542 | ||
543 | template<> template<class Compare> inline bool PackageContainer<std::vector<pkgCache::PkgIterator> >::sort(Compare Comp) { | |
544 | std::sort(_cont.begin(), _cont.end(), Comp); | |
545 | return true; | |
546 | } | |
547 | ||
840ca971 DK |
548 | // class PackageUniverse - pkgCache as PackageContainerInterface /*{{{*/ |
549 | /** \class PackageUniverse | |
550 | ||
551 | Wraps around our usual pkgCache, so that it can be stuffed into methods | |
552 | expecting a PackageContainer. | |
553 | ||
554 | The wrapping is read-only in practice modeled by making erase and co | |
555 | private methods. */ | |
3809194b | 556 | class APT_HIDDEN PackageUniverse : public PackageContainerInterface { |
840ca971 | 557 | pkgCache * const _cont; |
c8a4ce6c | 558 | void *d; |
840ca971 DK |
559 | public: |
560 | typedef pkgCache::PkgIterator iterator; | |
561 | typedef pkgCache::PkgIterator const_iterator; | |
562 | ||
3809194b DK |
563 | APT_PUBLIC bool empty() const { return false; } |
564 | APT_PUBLIC size_t size() const { return _cont->Head().PackageCount; } | |
840ca971 | 565 | |
3809194b DK |
566 | APT_PUBLIC const_iterator begin() const { return _cont->PkgBegin(); } |
567 | APT_PUBLIC const_iterator end() const { return _cont->PkgEnd(); } | |
568 | APT_PUBLIC iterator begin() { return _cont->PkgBegin(); } | |
569 | APT_PUBLIC iterator end() { return _cont->PkgEnd(); } | |
840ca971 | 570 | |
e8afd168 | 571 | explicit APT_PUBLIC PackageUniverse(pkgCache * const Owner); |
c8a4ce6c | 572 | APT_PUBLIC virtual ~PackageUniverse(); |
840ca971 DK |
573 | |
574 | private: | |
575 | bool insert(pkgCache::PkgIterator const &) { return true; } | |
576 | template<class Cont> void insert(PackageContainer<Cont> const &) { } | |
577 | void insert(const_iterator, const_iterator) { } | |
578 | ||
579 | void clear() { } | |
580 | iterator& erase(iterator &iter) { return iter; } | |
581 | size_t erase(const pkgCache::PkgIterator) { return 0; } | |
582 | void erase(iterator, iterator) { } | |
840ca971 DK |
583 | }; |
584 | /*}}}*/ | |
15fc8636 | 585 | typedef PackageContainer<std::set<pkgCache::PkgIterator> > PackageSet; |
c4cca791 | 586 | typedef PackageContainer<std::list<pkgCache::PkgIterator> > PackageList; |
5c8c7321 | 587 | typedef PackageContainer<std::vector<pkgCache::PkgIterator> > PackageVector; |
78c32596 | 588 | |
15fc8636 DK |
589 | class VersionContainerInterface { /*{{{*/ |
590 | /** \class APT::VersionContainerInterface | |
591 | ||
592 | Same as APT::PackageContainerInterface, just for Versions */ | |
593 | public: | |
d4489d49 | 594 | /** \brief smell like a pkgCache::VerIterator */ |
15fc8636 | 595 | class const_iterator { /*{{{*/ |
d4489d49 | 596 | public: |
15fc8636 DK |
597 | virtual pkgCache::VerIterator getVer() const = 0; |
598 | operator pkgCache::VerIterator(void) { return getVer(); } | |
599 | ||
d3e8fbb3 DK |
600 | inline pkgCache *Cache() const { return getVer().Cache(); } |
601 | inline unsigned long Index() const {return getVer().Index();} | |
602 | inline int CompareVer(const pkgCache::VerIterator &B) const { return getVer().CompareVer(B); } | |
603 | inline const char *VerStr() const { return getVer().VerStr(); } | |
604 | inline const char *Section() const { return getVer().Section(); } | |
605 | inline const char *Arch() const { return getVer().Arch(); } | |
606 | inline pkgCache::PkgIterator ParentPkg() const { return getVer().ParentPkg(); } | |
607 | inline pkgCache::DescIterator DescriptionList() const { return getVer().DescriptionList(); } | |
608 | inline pkgCache::DescIterator TranslatedDescription() const { return getVer().TranslatedDescription(); } | |
609 | inline pkgCache::DepIterator DependsList() const { return getVer().DependsList(); } | |
610 | inline pkgCache::PrvIterator ProvidesList() const { return getVer().ProvidesList(); } | |
611 | inline pkgCache::VerFileIterator FileList() const { return getVer().FileList(); } | |
612 | inline bool Downloadable() const { return getVer().Downloadable(); } | |
613 | inline const char *PriorityType() const { return getVer().PriorityType(); } | |
614 | inline std::string RelStr() const { return getVer().RelStr(); } | |
615 | inline bool Automatic() const { return getVer().Automatic(); } | |
616 | inline pkgCache::VerFileIterator NewestFile() const { return getVer().NewestFile(); } | |
d4489d49 | 617 | // we have only valid iterators here |
d3e8fbb3 | 618 | inline bool end() const { return false; } |
d4489d49 | 619 | |
d3e8fbb3 | 620 | inline pkgCache::Version const * operator->() const { return &*getVer(); } |
d4489d49 | 621 | }; |
dc0f01f7 | 622 | /*}}}*/ |
78c32596 | 623 | |
15fc8636 DK |
624 | virtual bool insert(pkgCache::VerIterator const &V) = 0; |
625 | virtual bool empty() const = 0; | |
626 | virtual void clear() = 0; | |
c45f2d19 | 627 | |
856d3b06 | 628 | /** \brief specifies which version(s) will be returned if non is given */ |
fdba4d53 DK |
629 | enum APT_DEPRECATED Version { |
630 | ALL = CacheSetHelper::ALL, | |
631 | CANDANDINST = CacheSetHelper::CANDANDINST, | |
632 | CANDIDATE = CacheSetHelper::CANDIDATE, | |
633 | INSTALLED = CacheSetHelper::INSTALLED, | |
634 | CANDINST = CacheSetHelper::CANDINST, | |
635 | INSTCAND = CacheSetHelper::INSTCAND, | |
636 | NEWEST = CacheSetHelper::NEWEST | |
856d3b06 DK |
637 | }; |
638 | ||
15fc8636 | 639 | struct Modifier { |
fdba4d53 | 640 | unsigned short const ID; |
15fc8636 | 641 | const char * const Alias; |
fdba4d53 DK |
642 | enum Position { NONE, PREFIX, POSTFIX } const Pos; |
643 | enum CacheSetHelper::VerSelector const SelectVersion; | |
15fc8636 | 644 | Modifier (unsigned short const &id, const char * const alias, Position const &pos, |
fdba4d53 | 645 | enum CacheSetHelper::VerSelector const select) : ID(id), Alias(alias), Pos(pos), |
d3e8fbb3 | 646 | SelectVersion(select) {} |
586d8704 | 647 | APT_IGNORE_DEPRECATED_PUSH |
fdba4d53 DK |
648 | APT_DEPRECATED Modifier(unsigned short const &id, const char * const alias, Position const &pos, |
649 | Version const &select) : ID(id), Alias(alias), Pos(pos), | |
650 | SelectVersion((CacheSetHelper::VerSelector)select) {} | |
586d8704 | 651 | APT_IGNORE_DEPRECATED_POP |
15fc8636 DK |
652 | }; |
653 | ||
654 | static bool FromCommandLine(VersionContainerInterface * const vci, pkgCacheFile &Cache, | |
fdba4d53 | 655 | const char **cmdline, CacheSetHelper::VerSelector const fallback, |
15fc8636 | 656 | CacheSetHelper &helper); |
586d8704 | 657 | APT_IGNORE_DEPRECATED_PUSH |
fdba4d53 DK |
658 | APT_DEPRECATED static bool FromCommandLine(VersionContainerInterface * const vci, pkgCacheFile &Cache, |
659 | const char **cmdline, Version const &fallback, | |
660 | CacheSetHelper &helper) { | |
661 | return FromCommandLine(vci, Cache, cmdline, (CacheSetHelper::VerSelector)fallback, helper); | |
662 | } | |
586d8704 | 663 | APT_IGNORE_DEPRECATED_POP |
15fc8636 DK |
664 | |
665 | static bool FromString(VersionContainerInterface * const vci, pkgCacheFile &Cache, | |
fdba4d53 | 666 | std::string pkg, CacheSetHelper::VerSelector const fallback, CacheSetHelper &helper, |
15fc8636 | 667 | bool const onlyFromName = false); |
586d8704 | 668 | APT_IGNORE_DEPRECATED_PUSH |
fdba4d53 DK |
669 | APT_DEPRECATED static bool FromString(VersionContainerInterface * const vci, pkgCacheFile &Cache, |
670 | std::string pkg, Version const &fallback, CacheSetHelper &helper, | |
671 | bool const onlyFromName = false) { | |
672 | return FromString(vci, Cache, pkg, (CacheSetHelper::VerSelector)fallback, helper, onlyFromName); | |
673 | } | |
586d8704 | 674 | APT_IGNORE_DEPRECATED_POP |
15fc8636 DK |
675 | |
676 | static bool FromPackage(VersionContainerInterface * const vci, pkgCacheFile &Cache, | |
fdba4d53 | 677 | pkgCache::PkgIterator const &P, CacheSetHelper::VerSelector const fallback, |
15fc8636 | 678 | CacheSetHelper &helper); |
586d8704 | 679 | APT_IGNORE_DEPRECATED_PUSH |
fdba4d53 DK |
680 | APT_DEPRECATED static bool FromPackage(VersionContainerInterface * const vci, pkgCacheFile &Cache, |
681 | pkgCache::PkgIterator const &P, Version const &fallback, | |
682 | CacheSetHelper &helper) { | |
683 | return FromPackage(vci, Cache, P, (CacheSetHelper::VerSelector)fallback, helper); | |
684 | } | |
586d8704 | 685 | APT_IGNORE_DEPRECATED_POP |
15fc8636 DK |
686 | |
687 | static bool FromModifierCommandLine(unsigned short &modID, | |
688 | VersionContainerInterface * const vci, | |
689 | pkgCacheFile &Cache, const char * cmdline, | |
690 | std::list<Modifier> const &mods, | |
691 | CacheSetHelper &helper); | |
692 | ||
c4cca791 DK |
693 | |
694 | static bool FromDependency(VersionContainerInterface * const vci, | |
695 | pkgCacheFile &Cache, | |
696 | pkgCache::DepIterator const &D, | |
fdba4d53 | 697 | CacheSetHelper::VerSelector const selector, |
c4cca791 | 698 | CacheSetHelper &helper); |
586d8704 | 699 | APT_IGNORE_DEPRECATED_PUSH |
fdba4d53 DK |
700 | APT_DEPRECATED static bool FromDependency(VersionContainerInterface * const vci, |
701 | pkgCacheFile &Cache, | |
702 | pkgCache::DepIterator const &D, | |
703 | Version const &selector, | |
704 | CacheSetHelper &helper) { | |
705 | return FromDependency(vci, Cache, D, (CacheSetHelper::VerSelector)selector, helper); | |
706 | } | |
586d8704 | 707 | APT_IGNORE_DEPRECATED_POP |
c4cca791 | 708 | |
c8a4ce6c DK |
709 | VersionContainerInterface(); |
710 | virtual ~VersionContainerInterface(); | |
711 | private: | |
712 | void *d; | |
713 | ||
15fc8636 DK |
714 | protected: /*{{{*/ |
715 | ||
716 | /** \brief returns the candidate version of the package | |
717 | ||
718 | \param Cache to be used to query for information | |
255c9e4b DK |
719 | \param Pkg we want the candidate version from this package |
720 | \param helper used in this container instance */ | |
15fc8636 DK |
721 | static pkgCache::VerIterator getCandidateVer(pkgCacheFile &Cache, |
722 | pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper); | |
723 | ||
724 | /** \brief returns the installed version of the package | |
725 | ||
726 | \param Cache to be used to query for information | |
255c9e4b DK |
727 | \param Pkg we want the installed version from this package |
728 | \param helper used in this container instance */ | |
15fc8636 DK |
729 | static pkgCache::VerIterator getInstalledVer(pkgCacheFile &Cache, |
730 | pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper); | |
731 | /*}}}*/ | |
732 | }; | |
733 | /*}}}*/ | |
734 | template<class Container> class VersionContainer : public VersionContainerInterface {/*{{{*/ | |
c4cca791 | 735 | /** \class APT::VersionContainer |
15fc8636 DK |
736 | |
737 | Simple wrapper around a container class like std::set to provide a similar | |
738 | interface to a set of versions as to the complete set of all versions in the | |
739 | pkgCache. */ | |
740 | Container _cont; | |
741 | public: /*{{{*/ | |
742 | /** \brief smell like a pkgCache::VerIterator */ | |
743 | class const_iterator : public VersionContainerInterface::const_iterator, | |
744 | public std::iterator<std::forward_iterator_tag, typename Container::const_iterator> {/*{{{*/ | |
745 | typename Container::const_iterator _iter; | |
746 | public: | |
e8afd168 | 747 | explicit const_iterator(typename Container::const_iterator i) : _iter(i) {} |
15fc8636 | 748 | pkgCache::VerIterator getVer(void) const { return *_iter; } |
d3e8fbb3 | 749 | inline pkgCache::VerIterator operator*(void) const { return *_iter; } |
15fc8636 | 750 | operator typename Container::const_iterator(void) const { return _iter; } |
63b70b21 DK |
751 | inline const_iterator& operator++() { ++_iter; return *this; } |
752 | inline const_iterator operator++(int) { const_iterator tmp(*this); operator++(); return tmp; } | |
d3e8fbb3 DK |
753 | inline bool operator!=(const_iterator const &i) const { return _iter != i._iter; } |
754 | inline bool operator==(const_iterator const &i) const { return _iter == i._iter; } | |
15fc8636 DK |
755 | friend std::ostream& operator<<(std::ostream& out, const_iterator i) { return operator<<(out, *i); } |
756 | }; | |
c4cca791 DK |
757 | class iterator : public VersionContainerInterface::const_iterator, |
758 | public std::iterator<std::forward_iterator_tag, typename Container::iterator> { | |
759 | typename Container::iterator _iter; | |
760 | public: | |
e8afd168 | 761 | explicit iterator(typename Container::iterator i) : _iter(i) {} |
c4cca791 | 762 | pkgCache::VerIterator getVer(void) const { return *_iter; } |
d3e8fbb3 | 763 | inline pkgCache::VerIterator operator*(void) const { return *_iter; } |
c4cca791 | 764 | operator typename Container::iterator(void) const { return _iter; } |
bce0e0ff | 765 | operator typename VersionContainer<Container>::const_iterator() { return typename VersionContainer<Container>::const_iterator(_iter); } |
63b70b21 DK |
766 | inline iterator& operator++() { ++_iter; return *this; } |
767 | inline iterator operator++(int) { iterator tmp(*this); operator++(); return tmp; } | |
d3e8fbb3 DK |
768 | inline bool operator!=(iterator const &i) const { return _iter != i._iter; } |
769 | inline bool operator==(iterator const &i) const { return _iter == i._iter; } | |
770 | inline iterator& operator=(iterator const &i) { _iter = i._iter; return *this; } | |
771 | inline iterator& operator=(typename Container::iterator const &i) { _iter = i; return *this; } | |
c4cca791 DK |
772 | friend std::ostream& operator<<(std::ostream& out, iterator i) { return operator<<(out, *i); } |
773 | }; | |
15fc8636 DK |
774 | /*}}}*/ |
775 | ||
d3e8fbb3 DK |
776 | bool insert(pkgCache::VerIterator const &V) { if (V.end() == true) return false; _cont.insert(V); return true; } |
777 | template<class Cont> void insert(VersionContainer<Cont> const &vercont) { _cont.insert((typename Cont::const_iterator)vercont.begin(), (typename Cont::const_iterator)vercont.end()); } | |
778 | void insert(const_iterator begin, const_iterator end) { _cont.insert(begin, end); } | |
779 | bool empty() const { return _cont.empty(); } | |
780 | void clear() { return _cont.clear(); } | |
5eb9a474 | 781 | //FIXME: on ABI break, replace the first with the second without bool |
d3e8fbb3 DK |
782 | void erase(iterator position) { _cont.erase((typename Container::iterator)position); } |
783 | iterator& erase(iterator &position, bool) { return position = _cont.erase((typename Container::iterator)position); } | |
784 | size_t erase(const pkgCache::VerIterator x) { return _cont.erase(x); } | |
785 | void erase(iterator first, iterator last) { _cont.erase(first, last); } | |
786 | size_t size() const { return _cont.size(); } | |
787 | ||
788 | const_iterator begin() const { return const_iterator(_cont.begin()); } | |
789 | const_iterator end() const { return const_iterator(_cont.end()); } | |
790 | iterator begin() { return iterator(_cont.begin()); } | |
791 | iterator end() { return iterator(_cont.end()); } | |
792 | const_iterator find(pkgCache::VerIterator const &V) const { return const_iterator(_cont.find(V)); } | |
15fc8636 | 793 | |
5c8c7321 DK |
794 | /** \brief sort all included versions with given comparer |
795 | ||
796 | Some containers are sorted by default, some are not and can't be, | |
797 | but a few like std::vector can be sorted if need be, so this can be | |
798 | specialized in later on. The default is that this will fail though. | |
799 | Specifically, already sorted containers like std::set will return | |
800 | false as well as there is no easy way to check that the given comparer | |
801 | would sort in the same way the set is currently sorted | |
802 | ||
803 | \return \b true if the set was sorted, \b false if not. */ | |
804 | template<class Compare> bool sort(Compare /*Comp*/) { return false; } | |
805 | ||
856d3b06 DK |
806 | /** \brief returns all versions specified on the commandline |
807 | ||
808 | Get all versions from the commandline, uses given default version if | |
809 | non specifically requested and executes regex's if needed on names. | |
810 | \param Cache the packages and versions are in | |
811 | \param cmdline Command line the versions should be extracted from | |
255c9e4b | 812 | \param fallback version specification |
c8db3fff | 813 | \param helper responsible for error and message handling */ |
15fc8636 | 814 | static VersionContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline, |
fdba4d53 | 815 | CacheSetHelper::VerSelector const fallback, CacheSetHelper &helper) { |
15fc8636 DK |
816 | VersionContainer vercon; |
817 | VersionContainerInterface::FromCommandLine(&vercon, Cache, cmdline, fallback, helper); | |
818 | return vercon; | |
819 | } | |
820 | static VersionContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline, | |
fdba4d53 | 821 | CacheSetHelper::VerSelector const fallback) { |
70e706ad | 822 | CacheSetHelper helper; |
446bbcf4 | 823 | return FromCommandLine(Cache, cmdline, fallback, helper); |
856d3b06 | 824 | } |
15fc8636 | 825 | static VersionContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline) { |
fdba4d53 | 826 | return FromCommandLine(Cache, cmdline, CacheSetHelper::CANDINST); |
856d3b06 | 827 | } |
15fc8636 | 828 | static VersionContainer FromString(pkgCacheFile &Cache, std::string const &pkg, |
fdba4d53 | 829 | CacheSetHelper::VerSelector const fallback, CacheSetHelper &helper, |
80624be7 | 830 | bool const /*onlyFromName = false*/) { |
15fc8636 DK |
831 | VersionContainer vercon; |
832 | VersionContainerInterface::FromString(&vercon, Cache, pkg, fallback, helper); | |
833 | return vercon; | |
834 | } | |
835 | static VersionContainer FromString(pkgCacheFile &Cache, std::string pkg, | |
fdba4d53 | 836 | CacheSetHelper::VerSelector const fallback) { |
70e706ad | 837 | CacheSetHelper helper; |
446bbcf4 | 838 | return FromString(Cache, pkg, fallback, helper); |
55c59998 | 839 | } |
15fc8636 | 840 | static VersionContainer FromString(pkgCacheFile &Cache, std::string pkg) { |
fdba4d53 DK |
841 | return FromString(Cache, pkg, CacheSetHelper::CANDINST); |
842 | } | |
586d8704 | 843 | APT_IGNORE_DEPRECATED_PUSH |
fdba4d53 DK |
844 | static VersionContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline, |
845 | Version const &fallback, CacheSetHelper &helper) { | |
846 | VersionContainer vercon; | |
847 | VersionContainerInterface::FromCommandLine(&vercon, Cache, cmdline, (CacheSetHelper::VerSelector)fallback, helper); | |
848 | return vercon; | |
849 | } | |
850 | static VersionContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline, | |
851 | Version const &fallback) { | |
852 | CacheSetHelper helper; | |
853 | return FromCommandLine(Cache, cmdline, (CacheSetHelper::VerSelector)fallback, helper); | |
55c59998 | 854 | } |
fdba4d53 DK |
855 | static VersionContainer FromString(pkgCacheFile &Cache, std::string const &pkg, |
856 | Version const &fallback, CacheSetHelper &helper, | |
857 | bool const /*onlyFromName = false*/) { | |
858 | VersionContainer vercon; | |
859 | VersionContainerInterface::FromString(&vercon, Cache, pkg, (CacheSetHelper::VerSelector)fallback, helper); | |
860 | return vercon; | |
861 | } | |
862 | static VersionContainer FromString(pkgCacheFile &Cache, std::string pkg, | |
863 | Version const &fallback) { | |
864 | CacheSetHelper helper; | |
865 | return FromString(Cache, pkg, (CacheSetHelper::VerSelector)fallback, helper); | |
866 | } | |
586d8704 | 867 | APT_IGNORE_DEPRECATED_POP |
55c59998 | 868 | |
fb83c1d0 DK |
869 | /** \brief returns all versions specified for the package |
870 | ||
871 | \param Cache the package and versions are in | |
872 | \param P the package in question | |
873 | \param fallback the version(s) you want to get | |
874 | \param helper the helper used for display and error handling */ | |
15fc8636 | 875 | static VersionContainer FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P, |
fdba4d53 | 876 | CacheSetHelper::VerSelector const fallback, CacheSetHelper &helper) { |
15fc8636 DK |
877 | VersionContainer vercon; |
878 | VersionContainerInterface::FromPackage(&vercon, Cache, P, fallback, helper); | |
879 | return vercon; | |
880 | } | |
881 | static VersionContainer FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P, | |
fdba4d53 | 882 | CacheSetHelper::VerSelector const fallback) { |
c8db3fff | 883 | CacheSetHelper helper; |
446bbcf4 | 884 | return FromPackage(Cache, P, fallback, helper); |
c8db3fff | 885 | } |
586d8704 | 886 | APT_IGNORE_DEPRECATED_PUSH |
fdba4d53 DK |
887 | static VersionContainer FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P, |
888 | Version const &fallback, CacheSetHelper &helper) { | |
889 | VersionContainer vercon; | |
890 | VersionContainerInterface::FromPackage(&vercon, Cache, P, (CacheSetHelper::VerSelector)fallback, helper); | |
891 | return vercon; | |
892 | } | |
893 | static VersionContainer FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P, | |
894 | Version const &fallback) { | |
895 | CacheSetHelper helper; | |
896 | return FromPackage(Cache, P, (CacheSetHelper::VerSelector)fallback, helper); | |
897 | } | |
586d8704 | 898 | APT_IGNORE_DEPRECATED_POP |
15fc8636 | 899 | static VersionContainer FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P) { |
fdba4d53 | 900 | return FromPackage(Cache, P, CacheSetHelper::CANDIDATE); |
c8db3fff | 901 | } |
fb83c1d0 | 902 | |
15fc8636 DK |
903 | static std::map<unsigned short, VersionContainer> GroupedFromCommandLine( |
904 | pkgCacheFile &Cache, | |
905 | const char **cmdline, | |
906 | std::list<Modifier> const &mods, | |
907 | unsigned short const fallback, | |
908 | CacheSetHelper &helper) { | |
909 | std::map<unsigned short, VersionContainer> versets; | |
910 | for (const char **I = cmdline; *I != 0; ++I) { | |
911 | unsigned short modID = fallback; | |
912 | VersionContainer verset; | |
913 | VersionContainerInterface::FromModifierCommandLine(modID, &verset, Cache, *I, mods, helper); | |
914 | versets[modID].insert(verset); | |
915 | } | |
916 | return versets; | |
55c59998 | 917 | |
15fc8636 DK |
918 | } |
919 | static std::map<unsigned short, VersionContainer> GroupedFromCommandLine( | |
55c59998 | 920 | pkgCacheFile &Cache, const char **cmdline, |
15fc8636 DK |
921 | std::list<Modifier> const &mods, |
922 | unsigned short const fallback) { | |
70e706ad | 923 | CacheSetHelper helper; |
446bbcf4 | 924 | return GroupedFromCommandLine(Cache, cmdline, |
70e706ad | 925 | mods, fallback, helper); |
55c59998 | 926 | } |
c4cca791 DK |
927 | |
928 | static VersionContainer FromDependency(pkgCacheFile &Cache, pkgCache::DepIterator const &D, | |
fdba4d53 | 929 | CacheSetHelper::VerSelector const selector, CacheSetHelper &helper) { |
c4cca791 DK |
930 | VersionContainer vercon; |
931 | VersionContainerInterface::FromDependency(&vercon, Cache, D, selector, helper); | |
932 | return vercon; | |
933 | } | |
934 | static VersionContainer FromDependency(pkgCacheFile &Cache, pkgCache::DepIterator const &D, | |
fdba4d53 | 935 | CacheSetHelper::VerSelector const selector) { |
c4cca791 DK |
936 | CacheSetHelper helper; |
937 | return FromPackage(Cache, D, selector, helper); | |
938 | } | |
586d8704 | 939 | APT_IGNORE_DEPRECATED_PUSH |
fdba4d53 DK |
940 | static VersionContainer FromDependency(pkgCacheFile &Cache, pkgCache::DepIterator const &D, |
941 | Version const &selector, CacheSetHelper &helper) { | |
942 | VersionContainer vercon; | |
943 | VersionContainerInterface::FromDependency(&vercon, Cache, D, (CacheSetHelper::VerSelector)selector, helper); | |
944 | return vercon; | |
945 | } | |
946 | static VersionContainer FromDependency(pkgCacheFile &Cache, pkgCache::DepIterator const &D, | |
947 | Version const &selector) { | |
948 | CacheSetHelper helper; | |
949 | return FromPackage(Cache, D, (CacheSetHelper::VerSelector)selector, helper); | |
950 | } | |
586d8704 | 951 | APT_IGNORE_DEPRECATED_POP |
c4cca791 | 952 | static VersionContainer FromDependency(pkgCacheFile &Cache, pkgCache::DepIterator const &D) { |
fdba4d53 | 953 | return FromPackage(Cache, D, CacheSetHelper::CANDIDATE); |
c4cca791 | 954 | } |
856d3b06 | 955 | /*}}}*/ |
d4489d49 | 956 | }; /*}}}*/ |
5c8c7321 | 957 | // specialisations for push_back containers: std::list & std::vector /*{{{*/ |
c4cca791 DK |
958 | template<> template<class Cont> void VersionContainer<std::list<pkgCache::VerIterator> >::insert(VersionContainer<Cont> const &vercont) { |
959 | for (typename VersionContainer<Cont>::const_iterator v = vercont.begin(); v != vercont.end(); ++v) | |
960 | _cont.push_back(*v); | |
d3e8fbb3 | 961 | } |
5c8c7321 DK |
962 | template<> template<class Cont> void VersionContainer<std::vector<pkgCache::VerIterator> >::insert(VersionContainer<Cont> const &vercont) { |
963 | for (typename VersionContainer<Cont>::const_iterator v = vercont.begin(); v != vercont.end(); ++v) | |
964 | _cont.push_back(*v); | |
965 | } | |
c4cca791 DK |
966 | // these two are 'inline' as otherwise the linker has problems with seeing these untemplated |
967 | // specializations again and again - but we need to see them, so that library users can use them | |
968 | template<> inline bool VersionContainer<std::list<pkgCache::VerIterator> >::insert(pkgCache::VerIterator const &V) { | |
969 | if (V.end() == true) | |
970 | return false; | |
971 | _cont.push_back(V); | |
972 | return true; | |
d3e8fbb3 | 973 | } |
5c8c7321 DK |
974 | template<> inline bool VersionContainer<std::vector<pkgCache::VerIterator> >::insert(pkgCache::VerIterator const &V) { |
975 | if (V.end() == true) | |
976 | return false; | |
977 | _cont.push_back(V); | |
978 | return true; | |
979 | } | |
c4cca791 DK |
980 | template<> inline void VersionContainer<std::list<pkgCache::VerIterator> >::insert(const_iterator begin, const_iterator end) { |
981 | for (const_iterator v = begin; v != end; ++v) | |
982 | _cont.push_back(*v); | |
d3e8fbb3 | 983 | } |
5c8c7321 DK |
984 | template<> inline void VersionContainer<std::vector<pkgCache::VerIterator> >::insert(const_iterator begin, const_iterator end) { |
985 | for (const_iterator v = begin; v != end; ++v) | |
986 | _cont.push_back(*v); | |
987 | } | |
988 | /*}}}*/ | |
989 | ||
990 | template<> template<class Compare> inline bool VersionContainer<std::vector<pkgCache::VerIterator> >::sort(Compare Comp) { | |
991 | std::sort(_cont.begin(), _cont.end(), Comp); | |
992 | return true; | |
993 | } | |
994 | ||
15fc8636 | 995 | typedef VersionContainer<std::set<pkgCache::VerIterator> > VersionSet; |
c4cca791 | 996 | typedef VersionContainer<std::list<pkgCache::VerIterator> > VersionList; |
5c8c7321 | 997 | typedef VersionContainer<std::vector<pkgCache::VerIterator> > VersionVector; |
e1dbde8d DK |
998 | } |
999 | #endif |