]>
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 | 205 | private: |
6c55f07a | 206 | void * const 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); |
6c55f07a | 269 | PackageContainerInterface& operator=(PackageContainerInterface const &other); |
c8a4ce6c | 270 | virtual ~PackageContainerInterface(); |
15fc8636 | 271 | |
1e064088 DK |
272 | APT_DEPRECATED static bool FromTask(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) { |
273 | return helper.PackageFrom(CacheSetHelper::TASK, pci, Cache, pattern); } | |
274 | APT_DEPRECATED static bool FromRegEx(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) { | |
275 | return helper.PackageFrom(CacheSetHelper::REGEX, pci, Cache, pattern); } | |
276 | APT_DEPRECATED static bool FromFnmatch(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) { | |
277 | return helper.PackageFrom(CacheSetHelper::FNMATCH, pci, Cache, pattern); } | |
278 | APT_DEPRECATED static bool FromGroup(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) { | |
279 | return helper.PackageFrom(CacheSetHelper::PACKAGENAME, pci, Cache, pattern); } | |
280 | APT_DEPRECATED static bool FromString(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper) { | |
281 | return helper.PackageFrom(CacheSetHelper::STRING, pci, Cache, pattern); } | |
282 | APT_DEPRECATED static bool FromCommandLine(PackageContainerInterface * const pci, pkgCacheFile &Cache, const char **cmdline, CacheSetHelper &helper) { | |
283 | return helper.PackageFromCommandLine(pci, Cache, cmdline); } | |
284 | ||
285 | APT_DEPRECATED typedef CacheSetHelper::PkgModifier Modifier; | |
15fc8636 | 286 | |
586d8704 | 287 | APT_IGNORE_DEPRECATED_PUSH |
1e064088 DK |
288 | APT_DEPRECATED static pkgCache::PkgIterator FromName(pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper) { |
289 | return helper.PackageFromName(Cache, pattern); } | |
290 | APT_DEPRECATED static bool FromModifierCommandLine(unsigned short &modID, PackageContainerInterface * const pci, | |
291 | pkgCacheFile &Cache, const char * cmdline, | |
292 | std::list<Modifier> const &mods, CacheSetHelper &helper) { | |
293 | return helper.PackageFromModifierCommandLine(modID, pci, Cache, cmdline, mods); } | |
586d8704 | 294 | APT_IGNORE_DEPRECATED_POP |
fdba4d53 DK |
295 | |
296 | private: | |
297 | CacheSetHelper::PkgSelector ConstructedBy; | |
6c55f07a | 298 | void * const d; |
15fc8636 DK |
299 | }; |
300 | /*}}}*/ | |
301 | template<class Container> class PackageContainer : public PackageContainerInterface {/*{{{*/ | |
302 | /** \class APT::PackageContainer | |
e1dbde8d | 303 | |
15fc8636 DK |
304 | Simple wrapper around a container class like std::set to provide a similar |
305 | interface to a set of packages as to the complete set of all packages in the | |
306 | pkgCache. */ | |
307 | Container _cont; | |
308 | public: /*{{{*/ | |
309 | /** \brief smell like a pkgCache::PkgIterator */ | |
c4cca791 DK |
310 | class const_iterator : public PackageContainerInterface::const_iterator,/*{{{*/ |
311 | public std::iterator<std::forward_iterator_tag, typename Container::const_iterator> { | |
15fc8636 DK |
312 | typename Container::const_iterator _iter; |
313 | public: | |
e8afd168 | 314 | explicit const_iterator(typename Container::const_iterator i) : _iter(i) {} |
15fc8636 | 315 | pkgCache::PkgIterator getPkg(void) const { return *_iter; } |
d3e8fbb3 | 316 | inline pkgCache::PkgIterator operator*(void) const { return *_iter; } |
15fc8636 | 317 | operator typename Container::const_iterator(void) const { return _iter; } |
63b70b21 DK |
318 | inline const_iterator& operator++() { ++_iter; return *this; } |
319 | inline const_iterator operator++(int) { const_iterator tmp(*this); operator++(); return tmp; } | |
d3e8fbb3 DK |
320 | inline bool operator!=(const_iterator const &i) const { return _iter != i._iter; } |
321 | inline bool operator==(const_iterator const &i) const { return _iter == i._iter; } | |
15fc8636 | 322 | friend std::ostream& operator<<(std::ostream& out, const_iterator i) { return operator<<(out, *i); } |
e1dbde8d | 323 | }; |
c4cca791 DK |
324 | class iterator : public PackageContainerInterface::const_iterator, |
325 | public std::iterator<std::forward_iterator_tag, typename Container::iterator> { | |
326 | typename Container::iterator _iter; | |
327 | public: | |
e8afd168 | 328 | explicit iterator(typename Container::iterator i) : _iter(i) {} |
c4cca791 | 329 | pkgCache::PkgIterator getPkg(void) const { return *_iter; } |
d3e8fbb3 | 330 | inline pkgCache::PkgIterator operator*(void) const { return *_iter; } |
c4cca791 | 331 | operator typename Container::iterator(void) const { return _iter; } |
bce0e0ff | 332 | operator typename PackageContainer<Container>::const_iterator() { return typename PackageContainer<Container>::const_iterator(_iter); } |
63b70b21 DK |
333 | inline iterator& operator++() { ++_iter; return *this; } |
334 | inline iterator operator++(int) { iterator tmp(*this); operator++(); return tmp; } | |
d3e8fbb3 DK |
335 | inline bool operator!=(iterator const &i) const { return _iter != i._iter; } |
336 | inline bool operator==(iterator const &i) const { return _iter == i._iter; } | |
337 | inline iterator& operator=(iterator const &i) { _iter = i._iter; return *this; } | |
338 | inline iterator& operator=(typename Container::iterator const &i) { _iter = i; return *this; } | |
c4cca791 DK |
339 | friend std::ostream& operator<<(std::ostream& out, iterator i) { return operator<<(out, *i); } |
340 | }; | |
dc0f01f7 | 341 | /*}}}*/ |
ffee1c2b | 342 | |
d3e8fbb3 DK |
343 | bool insert(pkgCache::PkgIterator const &P) { if (P.end() == true) return false; _cont.insert(P); return true; } |
344 | template<class Cont> void insert(PackageContainer<Cont> const &pkgcont) { _cont.insert((typename Cont::const_iterator)pkgcont.begin(), (typename Cont::const_iterator)pkgcont.end()); } | |
345 | void insert(const_iterator begin, const_iterator end) { _cont.insert(begin, end); } | |
c4cca791 | 346 | |
d3e8fbb3 DK |
347 | bool empty() const { return _cont.empty(); } |
348 | void clear() { return _cont.clear(); } | |
d3e8fbb3 | 349 | size_t size() const { return _cont.size(); } |
c687384b DK |
350 | iterator erase( iterator pos ) { return iterator(_cont.erase(pos)); } |
351 | iterator erase( iterator first, iterator last ) { return iterator(_cont.erase(first, last)); } | |
352 | size_t erase(pkgCache::PkgIterator const & P) { size_t oldsize = size(); _cont.erase(std::remove(_cont.begin(), _cont.end(), P), _cont.end()); return oldsize - size(); } | |
d3e8fbb3 DK |
353 | const_iterator begin() const { return const_iterator(_cont.begin()); } |
354 | const_iterator end() const { return const_iterator(_cont.end()); } | |
355 | iterator begin() { return iterator(_cont.begin()); } | |
356 | iterator end() { return iterator(_cont.end()); } | |
357 | const_iterator find(pkgCache::PkgIterator const &P) const { return const_iterator(_cont.find(P)); } | |
15fc8636 | 358 | |
c8a4ce6c | 359 | PackageContainer() : PackageContainerInterface(CacheSetHelper::UNKNOWN) {} |
e8afd168 | 360 | explicit PackageContainer(CacheSetHelper::PkgSelector const &by) : PackageContainerInterface(by) {} |
586d8704 | 361 | APT_IGNORE_DEPRECATED_PUSH |
fdba4d53 | 362 | APT_DEPRECATED PackageContainer(Constructor const &by) : PackageContainerInterface((CacheSetHelper::PkgSelector)by) {} |
586d8704 | 363 | APT_IGNORE_DEPRECATED_POP |
c45f2d19 | 364 | |
5c8c7321 DK |
365 | /** \brief sort all included versions with given comparer |
366 | ||
367 | Some containers are sorted by default, some are not and can't be, | |
368 | but a few like std::vector can be sorted if need be, so this can be | |
369 | specialized in later on. The default is that this will fail though. | |
370 | Specifically, already sorted containers like std::set will return | |
371 | false as well as there is no easy way to check that the given comparer | |
372 | would sort in the same way the set is currently sorted | |
373 | ||
374 | \return \b true if the set was sorted, \b false if not. */ | |
375 | template<class Compare> bool sort(Compare /*Comp*/) { return false; } | |
376 | ||
dc0f01f7 DK |
377 | /** \brief returns all packages in the cache who belong to the given task |
378 | ||
379 | A simple helper responsible for search for all members of a task | |
380 | in the cache. Optional it prints a a notice about the | |
381 | packages chosen cause of the given task. | |
382 | \param Cache the packages are in | |
383 | \param pattern name of the task | |
c8db3fff | 384 | \param helper responsible for error and message handling */ |
15fc8636 | 385 | static PackageContainer FromTask(pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper) { |
fdba4d53 | 386 | PackageContainer cont(CacheSetHelper::TASK); |
1e064088 | 387 | helper.PackageFrom(CacheSetHelper::TASK, &cont, Cache, pattern); |
15fc8636 DK |
388 | return cont; |
389 | } | |
390 | static PackageContainer FromTask(pkgCacheFile &Cache, std::string const &pattern) { | |
70e706ad | 391 | CacheSetHelper helper; |
446bbcf4 | 392 | return FromTask(Cache, pattern, helper); |
dc0f01f7 DK |
393 | } |
394 | ||
ffee1c2b DK |
395 | /** \brief returns all packages in the cache whose name matchs a given pattern |
396 | ||
397 | A simple helper responsible for executing a regular expression on all | |
398 | package names in the cache. Optional it prints a a notice about the | |
399 | packages chosen cause of the given package. | |
400 | \param Cache the packages are in | |
401 | \param pattern regular expression for package names | |
c8db3fff | 402 | \param helper responsible for error and message handling */ |
fdba4d53 DK |
403 | static PackageContainer FromRegEx(pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper) { |
404 | PackageContainer cont(CacheSetHelper::REGEX); | |
1e064088 | 405 | helper.PackageFrom(CacheSetHelper::REGEX, &cont, Cache, pattern); |
15fc8636 DK |
406 | return cont; |
407 | } | |
408 | ||
409 | static PackageContainer FromRegEx(pkgCacheFile &Cache, std::string const &pattern) { | |
70e706ad | 410 | CacheSetHelper helper; |
446bbcf4 | 411 | return FromRegEx(Cache, pattern, helper); |
ffee1c2b DK |
412 | } |
413 | ||
fdba4d53 DK |
414 | static PackageContainer FromFnmatch(pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper) { |
415 | PackageContainer cont(CacheSetHelper::FNMATCH); | |
1e064088 | 416 | helper.PackageFrom(CacheSetHelper::FNMATCH, &cont, Cache, pattern); |
b9179170 MV |
417 | return cont; |
418 | } | |
419 | static PackageContainer FromFnMatch(pkgCacheFile &Cache, std::string const &pattern) { | |
420 | CacheSetHelper helper; | |
421 | return FromFnmatch(Cache, pattern, helper); | |
422 | } | |
423 | ||
586d8704 | 424 | APT_IGNORE_DEPRECATED_PUSH |
15fc8636 | 425 | /** \brief returns a package specified by a string |
856d3b06 | 426 | |
15fc8636 DK |
427 | \param Cache the package is in |
428 | \param pattern String the package name should be extracted from | |
c8db3fff | 429 | \param helper responsible for error and message handling */ |
1e064088 DK |
430 | APT_DEPRECATED static pkgCache::PkgIterator FromName(pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper) { |
431 | return helper.PackageFromName(Cache, pattern); | |
15fc8636 | 432 | } |
1e064088 | 433 | APT_DEPRECATED static pkgCache::PkgIterator FromName(pkgCacheFile &Cache, std::string const &pattern) { |
70e706ad | 434 | CacheSetHelper helper; |
1e064088 | 435 | return FromName(Cache, pattern, helper); |
856d3b06 | 436 | } |
586d8704 | 437 | APT_IGNORE_DEPRECATED_POP |
856d3b06 | 438 | |
15fc8636 | 439 | /** \brief returns all packages specified by a string |
bd631595 | 440 | |
15fc8636 DK |
441 | \param Cache the packages are in |
442 | \param pattern String the package name(s) should be extracted from | |
c8db3fff | 443 | \param helper responsible for error and message handling */ |
15fc8636 DK |
444 | static PackageContainer FromString(pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper) { |
445 | PackageContainer cont; | |
1e064088 | 446 | helper.PackageFrom(CacheSetHelper::PACKAGENAME, &cont, Cache, pattern); |
15fc8636 DK |
447 | return cont; |
448 | } | |
449 | static PackageContainer FromString(pkgCacheFile &Cache, std::string const &pattern) { | |
bd631595 | 450 | CacheSetHelper helper; |
15fc8636 | 451 | return FromString(Cache, pattern, helper); |
bd631595 DK |
452 | } |
453 | ||
78c32596 DK |
454 | /** \brief returns all packages specified on the commandline |
455 | ||
456 | Get all package names from the commandline and executes regex's if needed. | |
457 | No special package command is supported, just plain names. | |
458 | \param Cache the packages are in | |
459 | \param cmdline Command line the package names should be extracted from | |
c8db3fff | 460 | \param helper responsible for error and message handling */ |
15fc8636 DK |
461 | static PackageContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline, CacheSetHelper &helper) { |
462 | PackageContainer cont; | |
1e064088 | 463 | helper.PackageFromCommandLine(&cont, Cache, cmdline); |
15fc8636 DK |
464 | return cont; |
465 | } | |
466 | static PackageContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline) { | |
70e706ad | 467 | CacheSetHelper helper; |
446bbcf4 | 468 | return FromCommandLine(Cache, cmdline, helper); |
78c32596 | 469 | } |
9cc83a6f | 470 | |
c8db3fff DK |
471 | /** \brief group packages by a action modifiers |
472 | ||
473 | At some point it is needed to get from the same commandline | |
474 | different package sets grouped by a modifier. Take | |
475 | apt-get install apt awesome- | |
476 | as an example. | |
477 | \param Cache the packages are in | |
478 | \param cmdline Command line the package names should be extracted from | |
479 | \param mods list of modifiers the method should accept | |
480 | \param fallback the default modifier group for a package | |
481 | \param helper responsible for error and message handling */ | |
15fc8636 DK |
482 | static std::map<unsigned short, PackageContainer> GroupedFromCommandLine( |
483 | pkgCacheFile &Cache, | |
484 | const char **cmdline, | |
1e064088 | 485 | std::list<CacheSetHelper::PkgModifier> const &mods, |
15fc8636 DK |
486 | unsigned short const &fallback, |
487 | CacheSetHelper &helper) { | |
488 | std::map<unsigned short, PackageContainer> pkgsets; | |
489 | for (const char **I = cmdline; *I != 0; ++I) { | |
490 | unsigned short modID = fallback; | |
491 | PackageContainer pkgset; | |
1e064088 | 492 | helper.PackageFromModifierCommandLine(modID, &pkgset, Cache, *I, mods); |
15fc8636 DK |
493 | pkgsets[modID].insert(pkgset); |
494 | } | |
495 | return pkgsets; | |
496 | } | |
497 | static std::map<unsigned short, PackageContainer> GroupedFromCommandLine( | |
498 | pkgCacheFile &Cache, | |
499 | const char **cmdline, | |
1e064088 | 500 | std::list<CacheSetHelper::PkgModifier> const &mods, |
15fc8636 | 501 | unsigned short const &fallback) { |
70e706ad | 502 | CacheSetHelper helper; |
446bbcf4 | 503 | return GroupedFromCommandLine(Cache, cmdline, |
70e706ad | 504 | mods, fallback, helper); |
9cc83a6f | 505 | } |
c8db3fff | 506 | /*}}}*/ |
d4489d49 | 507 | }; /*}}}*/ |
5c8c7321 | 508 | // specialisations for push_back containers: std::list & std::vector /*{{{*/ |
c4cca791 DK |
509 | template<> template<class Cont> void PackageContainer<std::list<pkgCache::PkgIterator> >::insert(PackageContainer<Cont> const &pkgcont) { |
510 | for (typename PackageContainer<Cont>::const_iterator p = pkgcont.begin(); p != pkgcont.end(); ++p) | |
511 | _cont.push_back(*p); | |
d3e8fbb3 | 512 | } |
5c8c7321 DK |
513 | template<> template<class Cont> void PackageContainer<std::vector<pkgCache::PkgIterator> >::insert(PackageContainer<Cont> const &pkgcont) { |
514 | for (typename PackageContainer<Cont>::const_iterator p = pkgcont.begin(); p != pkgcont.end(); ++p) | |
515 | _cont.push_back(*p); | |
516 | } | |
c4cca791 DK |
517 | // these two are 'inline' as otherwise the linker has problems with seeing these untemplated |
518 | // specializations again and again - but we need to see them, so that library users can use them | |
519 | template<> inline bool PackageContainer<std::list<pkgCache::PkgIterator> >::insert(pkgCache::PkgIterator const &P) { | |
520 | if (P.end() == true) | |
521 | return false; | |
522 | _cont.push_back(P); | |
523 | return true; | |
d3e8fbb3 | 524 | } |
5c8c7321 DK |
525 | template<> inline bool PackageContainer<std::vector<pkgCache::PkgIterator> >::insert(pkgCache::PkgIterator const &P) { |
526 | if (P.end() == true) | |
527 | return false; | |
528 | _cont.push_back(P); | |
529 | return true; | |
530 | } | |
c4cca791 DK |
531 | template<> inline void PackageContainer<std::list<pkgCache::PkgIterator> >::insert(const_iterator begin, const_iterator end) { |
532 | for (const_iterator p = begin; p != end; ++p) | |
533 | _cont.push_back(*p); | |
d3e8fbb3 | 534 | } |
5c8c7321 DK |
535 | template<> inline void PackageContainer<std::vector<pkgCache::PkgIterator> >::insert(const_iterator begin, const_iterator end) { |
536 | for (const_iterator p = begin; p != end; ++p) | |
537 | _cont.push_back(*p); | |
538 | } | |
539 | /*}}}*/ | |
540 | ||
541 | template<> template<class Compare> inline bool PackageContainer<std::vector<pkgCache::PkgIterator> >::sort(Compare Comp) { | |
542 | std::sort(_cont.begin(), _cont.end(), Comp); | |
543 | return true; | |
544 | } | |
545 | ||
840ca971 DK |
546 | // class PackageUniverse - pkgCache as PackageContainerInterface /*{{{*/ |
547 | /** \class PackageUniverse | |
548 | ||
549 | Wraps around our usual pkgCache, so that it can be stuffed into methods | |
550 | expecting a PackageContainer. | |
551 | ||
552 | The wrapping is read-only in practice modeled by making erase and co | |
553 | private methods. */ | |
3809194b | 554 | class APT_HIDDEN PackageUniverse : public PackageContainerInterface { |
840ca971 | 555 | pkgCache * const _cont; |
6c55f07a | 556 | void * const d; |
840ca971 DK |
557 | public: |
558 | typedef pkgCache::PkgIterator iterator; | |
559 | typedef pkgCache::PkgIterator const_iterator; | |
560 | ||
3809194b DK |
561 | APT_PUBLIC bool empty() const { return false; } |
562 | APT_PUBLIC size_t size() const { return _cont->Head().PackageCount; } | |
840ca971 | 563 | |
3809194b DK |
564 | APT_PUBLIC const_iterator begin() const { return _cont->PkgBegin(); } |
565 | APT_PUBLIC const_iterator end() const { return _cont->PkgEnd(); } | |
566 | APT_PUBLIC iterator begin() { return _cont->PkgBegin(); } | |
567 | APT_PUBLIC iterator end() { return _cont->PkgEnd(); } | |
840ca971 | 568 | |
e8afd168 | 569 | explicit APT_PUBLIC PackageUniverse(pkgCache * const Owner); |
c8a4ce6c | 570 | APT_PUBLIC virtual ~PackageUniverse(); |
840ca971 DK |
571 | |
572 | private: | |
573 | bool insert(pkgCache::PkgIterator const &) { return true; } | |
574 | template<class Cont> void insert(PackageContainer<Cont> const &) { } | |
575 | void insert(const_iterator, const_iterator) { } | |
576 | ||
577 | void clear() { } | |
c687384b DK |
578 | iterator erase( iterator pos ); |
579 | iterator erase( iterator first, iterator last ); | |
580 | size_t erase(pkgCache::PkgIterator const & P); | |
840ca971 DK |
581 | }; |
582 | /*}}}*/ | |
15fc8636 | 583 | typedef PackageContainer<std::set<pkgCache::PkgIterator> > PackageSet; |
c4cca791 | 584 | typedef PackageContainer<std::list<pkgCache::PkgIterator> > PackageList; |
5c8c7321 | 585 | typedef PackageContainer<std::vector<pkgCache::PkgIterator> > PackageVector; |
78c32596 | 586 | |
15fc8636 DK |
587 | class VersionContainerInterface { /*{{{*/ |
588 | /** \class APT::VersionContainerInterface | |
589 | ||
590 | Same as APT::PackageContainerInterface, just for Versions */ | |
591 | public: | |
d4489d49 | 592 | /** \brief smell like a pkgCache::VerIterator */ |
15fc8636 | 593 | class const_iterator { /*{{{*/ |
d4489d49 | 594 | public: |
15fc8636 DK |
595 | virtual pkgCache::VerIterator getVer() const = 0; |
596 | operator pkgCache::VerIterator(void) { return getVer(); } | |
597 | ||
d3e8fbb3 DK |
598 | inline pkgCache *Cache() const { return getVer().Cache(); } |
599 | inline unsigned long Index() const {return getVer().Index();} | |
600 | inline int CompareVer(const pkgCache::VerIterator &B) const { return getVer().CompareVer(B); } | |
601 | inline const char *VerStr() const { return getVer().VerStr(); } | |
602 | inline const char *Section() const { return getVer().Section(); } | |
603 | inline const char *Arch() const { return getVer().Arch(); } | |
604 | inline pkgCache::PkgIterator ParentPkg() const { return getVer().ParentPkg(); } | |
605 | inline pkgCache::DescIterator DescriptionList() const { return getVer().DescriptionList(); } | |
606 | inline pkgCache::DescIterator TranslatedDescription() const { return getVer().TranslatedDescription(); } | |
607 | inline pkgCache::DepIterator DependsList() const { return getVer().DependsList(); } | |
608 | inline pkgCache::PrvIterator ProvidesList() const { return getVer().ProvidesList(); } | |
609 | inline pkgCache::VerFileIterator FileList() const { return getVer().FileList(); } | |
610 | inline bool Downloadable() const { return getVer().Downloadable(); } | |
611 | inline const char *PriorityType() const { return getVer().PriorityType(); } | |
612 | inline std::string RelStr() const { return getVer().RelStr(); } | |
613 | inline bool Automatic() const { return getVer().Automatic(); } | |
614 | inline pkgCache::VerFileIterator NewestFile() const { return getVer().NewestFile(); } | |
d4489d49 | 615 | // we have only valid iterators here |
d3e8fbb3 | 616 | inline bool end() const { return false; } |
d4489d49 | 617 | |
d3e8fbb3 | 618 | inline pkgCache::Version const * operator->() const { return &*getVer(); } |
d4489d49 | 619 | }; |
dc0f01f7 | 620 | /*}}}*/ |
78c32596 | 621 | |
15fc8636 DK |
622 | virtual bool insert(pkgCache::VerIterator const &V) = 0; |
623 | virtual bool empty() const = 0; | |
624 | virtual void clear() = 0; | |
c45f2d19 | 625 | |
856d3b06 | 626 | /** \brief specifies which version(s) will be returned if non is given */ |
fdba4d53 DK |
627 | enum APT_DEPRECATED Version { |
628 | ALL = CacheSetHelper::ALL, | |
629 | CANDANDINST = CacheSetHelper::CANDANDINST, | |
630 | CANDIDATE = CacheSetHelper::CANDIDATE, | |
631 | INSTALLED = CacheSetHelper::INSTALLED, | |
632 | CANDINST = CacheSetHelper::CANDINST, | |
633 | INSTCAND = CacheSetHelper::INSTCAND, | |
634 | NEWEST = CacheSetHelper::NEWEST | |
856d3b06 DK |
635 | }; |
636 | ||
15fc8636 | 637 | struct Modifier { |
fdba4d53 | 638 | unsigned short const ID; |
15fc8636 | 639 | const char * const Alias; |
fdba4d53 DK |
640 | enum Position { NONE, PREFIX, POSTFIX } const Pos; |
641 | enum CacheSetHelper::VerSelector const SelectVersion; | |
15fc8636 | 642 | Modifier (unsigned short const &id, const char * const alias, Position const &pos, |
fdba4d53 | 643 | enum CacheSetHelper::VerSelector const select) : ID(id), Alias(alias), Pos(pos), |
d3e8fbb3 | 644 | SelectVersion(select) {} |
586d8704 | 645 | APT_IGNORE_DEPRECATED_PUSH |
fdba4d53 DK |
646 | APT_DEPRECATED Modifier(unsigned short const &id, const char * const alias, Position const &pos, |
647 | Version const &select) : ID(id), Alias(alias), Pos(pos), | |
648 | SelectVersion((CacheSetHelper::VerSelector)select) {} | |
586d8704 | 649 | APT_IGNORE_DEPRECATED_POP |
15fc8636 DK |
650 | }; |
651 | ||
652 | static bool FromCommandLine(VersionContainerInterface * const vci, pkgCacheFile &Cache, | |
fdba4d53 | 653 | const char **cmdline, CacheSetHelper::VerSelector const fallback, |
15fc8636 | 654 | CacheSetHelper &helper); |
586d8704 | 655 | APT_IGNORE_DEPRECATED_PUSH |
fdba4d53 DK |
656 | APT_DEPRECATED static bool FromCommandLine(VersionContainerInterface * const vci, pkgCacheFile &Cache, |
657 | const char **cmdline, Version const &fallback, | |
658 | CacheSetHelper &helper) { | |
659 | return FromCommandLine(vci, Cache, cmdline, (CacheSetHelper::VerSelector)fallback, helper); | |
660 | } | |
586d8704 | 661 | APT_IGNORE_DEPRECATED_POP |
15fc8636 DK |
662 | |
663 | static bool FromString(VersionContainerInterface * const vci, pkgCacheFile &Cache, | |
fdba4d53 | 664 | std::string pkg, CacheSetHelper::VerSelector const fallback, CacheSetHelper &helper, |
15fc8636 | 665 | bool const onlyFromName = false); |
586d8704 | 666 | APT_IGNORE_DEPRECATED_PUSH |
fdba4d53 DK |
667 | APT_DEPRECATED static bool FromString(VersionContainerInterface * const vci, pkgCacheFile &Cache, |
668 | std::string pkg, Version const &fallback, CacheSetHelper &helper, | |
669 | bool const onlyFromName = false) { | |
670 | return FromString(vci, Cache, pkg, (CacheSetHelper::VerSelector)fallback, helper, onlyFromName); | |
671 | } | |
586d8704 | 672 | APT_IGNORE_DEPRECATED_POP |
15fc8636 DK |
673 | |
674 | static bool FromPackage(VersionContainerInterface * const vci, pkgCacheFile &Cache, | |
fdba4d53 | 675 | pkgCache::PkgIterator const &P, CacheSetHelper::VerSelector const fallback, |
15fc8636 | 676 | CacheSetHelper &helper); |
586d8704 | 677 | APT_IGNORE_DEPRECATED_PUSH |
fdba4d53 DK |
678 | APT_DEPRECATED static bool FromPackage(VersionContainerInterface * const vci, pkgCacheFile &Cache, |
679 | pkgCache::PkgIterator const &P, Version const &fallback, | |
680 | CacheSetHelper &helper) { | |
681 | return FromPackage(vci, Cache, P, (CacheSetHelper::VerSelector)fallback, helper); | |
682 | } | |
586d8704 | 683 | APT_IGNORE_DEPRECATED_POP |
15fc8636 DK |
684 | |
685 | static bool FromModifierCommandLine(unsigned short &modID, | |
686 | VersionContainerInterface * const vci, | |
687 | pkgCacheFile &Cache, const char * cmdline, | |
688 | std::list<Modifier> const &mods, | |
689 | CacheSetHelper &helper); | |
690 | ||
c4cca791 DK |
691 | |
692 | static bool FromDependency(VersionContainerInterface * const vci, | |
693 | pkgCacheFile &Cache, | |
694 | pkgCache::DepIterator const &D, | |
fdba4d53 | 695 | CacheSetHelper::VerSelector const selector, |
c4cca791 | 696 | CacheSetHelper &helper); |
586d8704 | 697 | APT_IGNORE_DEPRECATED_PUSH |
fdba4d53 DK |
698 | APT_DEPRECATED static bool FromDependency(VersionContainerInterface * const vci, |
699 | pkgCacheFile &Cache, | |
700 | pkgCache::DepIterator const &D, | |
701 | Version const &selector, | |
702 | CacheSetHelper &helper) { | |
703 | return FromDependency(vci, Cache, D, (CacheSetHelper::VerSelector)selector, helper); | |
704 | } | |
586d8704 | 705 | APT_IGNORE_DEPRECATED_POP |
c4cca791 | 706 | |
c8a4ce6c | 707 | VersionContainerInterface(); |
6c55f07a | 708 | VersionContainerInterface& operator=(VersionContainerInterface const &other); |
c8a4ce6c DK |
709 | virtual ~VersionContainerInterface(); |
710 | private: | |
6c55f07a | 711 | void * const d; |
c8a4ce6c | 712 | |
15fc8636 DK |
713 | protected: /*{{{*/ |
714 | ||
715 | /** \brief returns the candidate version of the package | |
716 | ||
717 | \param Cache to be used to query for information | |
255c9e4b DK |
718 | \param Pkg we want the candidate version from this package |
719 | \param helper used in this container instance */ | |
15fc8636 DK |
720 | static pkgCache::VerIterator getCandidateVer(pkgCacheFile &Cache, |
721 | pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper); | |
722 | ||
723 | /** \brief returns the installed version of the package | |
724 | ||
725 | \param Cache to be used to query for information | |
255c9e4b DK |
726 | \param Pkg we want the installed version from this package |
727 | \param helper used in this container instance */ | |
15fc8636 DK |
728 | static pkgCache::VerIterator getInstalledVer(pkgCacheFile &Cache, |
729 | pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper); | |
730 | /*}}}*/ | |
731 | }; | |
732 | /*}}}*/ | |
733 | template<class Container> class VersionContainer : public VersionContainerInterface {/*{{{*/ | |
c4cca791 | 734 | /** \class APT::VersionContainer |
15fc8636 DK |
735 | |
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 | |
738 | pkgCache. */ | |
739 | Container _cont; | |
740 | public: /*{{{*/ | |
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; | |
745 | public: | |
e8afd168 | 746 | explicit const_iterator(typename Container::const_iterator i) : _iter(i) {} |
15fc8636 | 747 | pkgCache::VerIterator getVer(void) const { return *_iter; } |
d3e8fbb3 | 748 | inline pkgCache::VerIterator operator*(void) const { return *_iter; } |
15fc8636 | 749 | operator typename Container::const_iterator(void) const { return _iter; } |
63b70b21 DK |
750 | inline const_iterator& operator++() { ++_iter; return *this; } |
751 | inline const_iterator operator++(int) { const_iterator tmp(*this); operator++(); return tmp; } | |
d3e8fbb3 DK |
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; } | |
15fc8636 DK |
754 | friend std::ostream& operator<<(std::ostream& out, const_iterator i) { return operator<<(out, *i); } |
755 | }; | |
c4cca791 DK |
756 | class iterator : public VersionContainerInterface::const_iterator, |
757 | public std::iterator<std::forward_iterator_tag, typename Container::iterator> { | |
758 | typename Container::iterator _iter; | |
759 | public: | |
e8afd168 | 760 | explicit iterator(typename Container::iterator i) : _iter(i) {} |
c4cca791 | 761 | pkgCache::VerIterator getVer(void) const { return *_iter; } |
d3e8fbb3 | 762 | inline pkgCache::VerIterator operator*(void) const { return *_iter; } |
c4cca791 | 763 | operator typename Container::iterator(void) const { return _iter; } |
bce0e0ff | 764 | operator typename VersionContainer<Container>::const_iterator() { return typename VersionContainer<Container>::const_iterator(_iter); } |
63b70b21 DK |
765 | inline iterator& operator++() { ++_iter; return *this; } |
766 | inline iterator operator++(int) { iterator tmp(*this); operator++(); return tmp; } | |
d3e8fbb3 DK |
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; } | |
c4cca791 DK |
771 | friend std::ostream& operator<<(std::ostream& out, iterator i) { return operator<<(out, *i); } |
772 | }; | |
15fc8636 DK |
773 | /*}}}*/ |
774 | ||
d3e8fbb3 DK |
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(); } | |
d3e8fbb3 | 780 | size_t size() const { return _cont.size(); } |
c687384b DK |
781 | iterator erase( iterator pos ) { return iterator(_cont.erase(pos)); } |
782 | iterator erase( iterator first, iterator last ) { return iterator(_cont.erase(first, last)); } | |
783 | size_t erase(pkgCache::VerIterator const & V) { size_t oldsize = size(); _cont.erase(std::remove(_cont.begin(), _cont.end(), V), _cont.end()); return oldsize - size(); } | |
d3e8fbb3 DK |
784 | |
785 | const_iterator begin() const { return const_iterator(_cont.begin()); } | |
786 | const_iterator end() const { return const_iterator(_cont.end()); } | |
787 | iterator begin() { return iterator(_cont.begin()); } | |
788 | iterator end() { return iterator(_cont.end()); } | |
789 | const_iterator find(pkgCache::VerIterator const &V) const { return const_iterator(_cont.find(V)); } | |
15fc8636 | 790 | |
5c8c7321 DK |
791 | /** \brief sort all included versions with given comparer |
792 | ||
793 | Some containers are sorted by default, some are not and can't be, | |
794 | but a few like std::vector can be sorted if need be, so this can be | |
795 | specialized in later on. The default is that this will fail though. | |
796 | Specifically, already sorted containers like std::set will return | |
797 | false as well as there is no easy way to check that the given comparer | |
798 | would sort in the same way the set is currently sorted | |
799 | ||
800 | \return \b true if the set was sorted, \b false if not. */ | |
801 | template<class Compare> bool sort(Compare /*Comp*/) { return false; } | |
802 | ||
856d3b06 DK |
803 | /** \brief returns all versions specified on the commandline |
804 | ||
805 | Get all versions from the commandline, uses given default version if | |
806 | non specifically requested and executes regex's if needed on names. | |
807 | \param Cache the packages and versions are in | |
808 | \param cmdline Command line the versions should be extracted from | |
255c9e4b | 809 | \param fallback version specification |
c8db3fff | 810 | \param helper responsible for error and message handling */ |
15fc8636 | 811 | static VersionContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline, |
fdba4d53 | 812 | CacheSetHelper::VerSelector const fallback, CacheSetHelper &helper) { |
15fc8636 DK |
813 | VersionContainer vercon; |
814 | VersionContainerInterface::FromCommandLine(&vercon, Cache, cmdline, fallback, helper); | |
815 | return vercon; | |
816 | } | |
817 | static VersionContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline, | |
fdba4d53 | 818 | CacheSetHelper::VerSelector const fallback) { |
70e706ad | 819 | CacheSetHelper helper; |
446bbcf4 | 820 | return FromCommandLine(Cache, cmdline, fallback, helper); |
856d3b06 | 821 | } |
15fc8636 | 822 | static VersionContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline) { |
fdba4d53 | 823 | return FromCommandLine(Cache, cmdline, CacheSetHelper::CANDINST); |
856d3b06 | 824 | } |
15fc8636 | 825 | static VersionContainer FromString(pkgCacheFile &Cache, std::string const &pkg, |
fdba4d53 | 826 | CacheSetHelper::VerSelector const fallback, CacheSetHelper &helper, |
80624be7 | 827 | bool const /*onlyFromName = false*/) { |
15fc8636 DK |
828 | VersionContainer vercon; |
829 | VersionContainerInterface::FromString(&vercon, Cache, pkg, fallback, helper); | |
830 | return vercon; | |
831 | } | |
832 | static VersionContainer FromString(pkgCacheFile &Cache, std::string pkg, | |
fdba4d53 | 833 | CacheSetHelper::VerSelector const fallback) { |
70e706ad | 834 | CacheSetHelper helper; |
446bbcf4 | 835 | return FromString(Cache, pkg, fallback, helper); |
55c59998 | 836 | } |
15fc8636 | 837 | static VersionContainer FromString(pkgCacheFile &Cache, std::string pkg) { |
fdba4d53 DK |
838 | return FromString(Cache, pkg, CacheSetHelper::CANDINST); |
839 | } | |
586d8704 | 840 | APT_IGNORE_DEPRECATED_PUSH |
fdba4d53 DK |
841 | static VersionContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline, |
842 | Version const &fallback, CacheSetHelper &helper) { | |
843 | VersionContainer vercon; | |
844 | VersionContainerInterface::FromCommandLine(&vercon, Cache, cmdline, (CacheSetHelper::VerSelector)fallback, helper); | |
845 | return vercon; | |
846 | } | |
847 | static VersionContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline, | |
848 | Version const &fallback) { | |
849 | CacheSetHelper helper; | |
850 | return FromCommandLine(Cache, cmdline, (CacheSetHelper::VerSelector)fallback, helper); | |
55c59998 | 851 | } |
fdba4d53 DK |
852 | static VersionContainer FromString(pkgCacheFile &Cache, std::string const &pkg, |
853 | Version const &fallback, CacheSetHelper &helper, | |
854 | bool const /*onlyFromName = false*/) { | |
855 | VersionContainer vercon; | |
856 | VersionContainerInterface::FromString(&vercon, Cache, pkg, (CacheSetHelper::VerSelector)fallback, helper); | |
857 | return vercon; | |
858 | } | |
859 | static VersionContainer FromString(pkgCacheFile &Cache, std::string pkg, | |
860 | Version const &fallback) { | |
861 | CacheSetHelper helper; | |
862 | return FromString(Cache, pkg, (CacheSetHelper::VerSelector)fallback, helper); | |
863 | } | |
586d8704 | 864 | APT_IGNORE_DEPRECATED_POP |
55c59998 | 865 | |
fb83c1d0 DK |
866 | /** \brief returns all versions specified for the package |
867 | ||
868 | \param Cache the package and versions are in | |
869 | \param P the package in question | |
870 | \param fallback the version(s) you want to get | |
871 | \param helper the helper used for display and error handling */ | |
15fc8636 | 872 | static VersionContainer FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P, |
fdba4d53 | 873 | CacheSetHelper::VerSelector const fallback, CacheSetHelper &helper) { |
15fc8636 DK |
874 | VersionContainer vercon; |
875 | VersionContainerInterface::FromPackage(&vercon, Cache, P, fallback, helper); | |
876 | return vercon; | |
877 | } | |
878 | static VersionContainer FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P, | |
fdba4d53 | 879 | CacheSetHelper::VerSelector const fallback) { |
c8db3fff | 880 | CacheSetHelper helper; |
446bbcf4 | 881 | return FromPackage(Cache, P, fallback, helper); |
c8db3fff | 882 | } |
586d8704 | 883 | APT_IGNORE_DEPRECATED_PUSH |
fdba4d53 DK |
884 | static VersionContainer FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P, |
885 | Version const &fallback, CacheSetHelper &helper) { | |
886 | VersionContainer vercon; | |
887 | VersionContainerInterface::FromPackage(&vercon, Cache, P, (CacheSetHelper::VerSelector)fallback, helper); | |
888 | return vercon; | |
889 | } | |
890 | static VersionContainer FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P, | |
891 | Version const &fallback) { | |
892 | CacheSetHelper helper; | |
893 | return FromPackage(Cache, P, (CacheSetHelper::VerSelector)fallback, helper); | |
894 | } | |
586d8704 | 895 | APT_IGNORE_DEPRECATED_POP |
15fc8636 | 896 | static VersionContainer FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P) { |
fdba4d53 | 897 | return FromPackage(Cache, P, CacheSetHelper::CANDIDATE); |
c8db3fff | 898 | } |
fb83c1d0 | 899 | |
15fc8636 DK |
900 | static std::map<unsigned short, VersionContainer> GroupedFromCommandLine( |
901 | pkgCacheFile &Cache, | |
902 | const char **cmdline, | |
903 | std::list<Modifier> const &mods, | |
904 | unsigned short const fallback, | |
905 | CacheSetHelper &helper) { | |
906 | std::map<unsigned short, VersionContainer> versets; | |
907 | for (const char **I = cmdline; *I != 0; ++I) { | |
908 | unsigned short modID = fallback; | |
909 | VersionContainer verset; | |
910 | VersionContainerInterface::FromModifierCommandLine(modID, &verset, Cache, *I, mods, helper); | |
911 | versets[modID].insert(verset); | |
912 | } | |
913 | return versets; | |
55c59998 | 914 | |
15fc8636 DK |
915 | } |
916 | static std::map<unsigned short, VersionContainer> GroupedFromCommandLine( | |
55c59998 | 917 | pkgCacheFile &Cache, const char **cmdline, |
15fc8636 DK |
918 | std::list<Modifier> const &mods, |
919 | unsigned short const fallback) { | |
70e706ad | 920 | CacheSetHelper helper; |
446bbcf4 | 921 | return GroupedFromCommandLine(Cache, cmdline, |
70e706ad | 922 | mods, fallback, helper); |
55c59998 | 923 | } |
c4cca791 DK |
924 | |
925 | static VersionContainer FromDependency(pkgCacheFile &Cache, pkgCache::DepIterator const &D, | |
fdba4d53 | 926 | CacheSetHelper::VerSelector const selector, CacheSetHelper &helper) { |
c4cca791 DK |
927 | VersionContainer vercon; |
928 | VersionContainerInterface::FromDependency(&vercon, Cache, D, selector, helper); | |
929 | return vercon; | |
930 | } | |
931 | static VersionContainer FromDependency(pkgCacheFile &Cache, pkgCache::DepIterator const &D, | |
fdba4d53 | 932 | CacheSetHelper::VerSelector const selector) { |
c4cca791 DK |
933 | CacheSetHelper helper; |
934 | return FromPackage(Cache, D, selector, helper); | |
935 | } | |
586d8704 | 936 | APT_IGNORE_DEPRECATED_PUSH |
fdba4d53 DK |
937 | static VersionContainer FromDependency(pkgCacheFile &Cache, pkgCache::DepIterator const &D, |
938 | Version const &selector, CacheSetHelper &helper) { | |
939 | VersionContainer vercon; | |
940 | VersionContainerInterface::FromDependency(&vercon, Cache, D, (CacheSetHelper::VerSelector)selector, helper); | |
941 | return vercon; | |
942 | } | |
943 | static VersionContainer FromDependency(pkgCacheFile &Cache, pkgCache::DepIterator const &D, | |
944 | Version const &selector) { | |
945 | CacheSetHelper helper; | |
946 | return FromPackage(Cache, D, (CacheSetHelper::VerSelector)selector, helper); | |
947 | } | |
586d8704 | 948 | APT_IGNORE_DEPRECATED_POP |
c4cca791 | 949 | static VersionContainer FromDependency(pkgCacheFile &Cache, pkgCache::DepIterator const &D) { |
fdba4d53 | 950 | return FromPackage(Cache, D, CacheSetHelper::CANDIDATE); |
c4cca791 | 951 | } |
856d3b06 | 952 | /*}}}*/ |
d4489d49 | 953 | }; /*}}}*/ |
5c8c7321 | 954 | // specialisations for push_back containers: std::list & std::vector /*{{{*/ |
c4cca791 DK |
955 | template<> template<class Cont> void VersionContainer<std::list<pkgCache::VerIterator> >::insert(VersionContainer<Cont> const &vercont) { |
956 | for (typename VersionContainer<Cont>::const_iterator v = vercont.begin(); v != vercont.end(); ++v) | |
957 | _cont.push_back(*v); | |
d3e8fbb3 | 958 | } |
5c8c7321 DK |
959 | template<> template<class Cont> void VersionContainer<std::vector<pkgCache::VerIterator> >::insert(VersionContainer<Cont> const &vercont) { |
960 | for (typename VersionContainer<Cont>::const_iterator v = vercont.begin(); v != vercont.end(); ++v) | |
961 | _cont.push_back(*v); | |
962 | } | |
c4cca791 DK |
963 | // these two are 'inline' as otherwise the linker has problems with seeing these untemplated |
964 | // specializations again and again - but we need to see them, so that library users can use them | |
965 | template<> inline bool VersionContainer<std::list<pkgCache::VerIterator> >::insert(pkgCache::VerIterator const &V) { | |
966 | if (V.end() == true) | |
967 | return false; | |
968 | _cont.push_back(V); | |
969 | return true; | |
d3e8fbb3 | 970 | } |
5c8c7321 DK |
971 | template<> inline bool VersionContainer<std::vector<pkgCache::VerIterator> >::insert(pkgCache::VerIterator const &V) { |
972 | if (V.end() == true) | |
973 | return false; | |
974 | _cont.push_back(V); | |
975 | return true; | |
976 | } | |
c4cca791 DK |
977 | template<> inline void VersionContainer<std::list<pkgCache::VerIterator> >::insert(const_iterator begin, const_iterator end) { |
978 | for (const_iterator v = begin; v != end; ++v) | |
979 | _cont.push_back(*v); | |
d3e8fbb3 | 980 | } |
5c8c7321 DK |
981 | template<> inline void VersionContainer<std::vector<pkgCache::VerIterator> >::insert(const_iterator begin, const_iterator end) { |
982 | for (const_iterator v = begin; v != end; ++v) | |
983 | _cont.push_back(*v); | |
984 | } | |
985 | /*}}}*/ | |
986 | ||
c687384b DK |
987 | template<> inline size_t PackageContainer<std::set<pkgCache::PkgIterator> >::erase(pkgCache::PkgIterator const &P) { |
988 | return _cont.erase(P); | |
989 | } | |
990 | template<> inline size_t VersionContainer<std::set<pkgCache::VerIterator> >::erase(pkgCache::VerIterator const &V) { | |
991 | return _cont.erase(V); | |
992 | } | |
993 | ||
5c8c7321 DK |
994 | template<> template<class Compare> inline bool VersionContainer<std::vector<pkgCache::VerIterator> >::sort(Compare Comp) { |
995 | std::sort(_cont.begin(), _cont.end(), Comp); | |
996 | return true; | |
997 | } | |
998 | ||
15fc8636 | 999 | typedef VersionContainer<std::set<pkgCache::VerIterator> > VersionSet; |
c4cca791 | 1000 | typedef VersionContainer<std::list<pkgCache::VerIterator> > VersionList; |
5c8c7321 | 1001 | typedef VersionContainer<std::vector<pkgCache::VerIterator> > VersionVector; |
e1dbde8d DK |
1002 | } |
1003 | #endif |