Imported Upstream version 16.4.0
[platform/upstream/libzypp.git] / zypp / sat / Solvable.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/sat/Solvable.h
10  *
11 */
12 #ifndef ZYPP_SAT_SOLVABLE_H
13 #define ZYPP_SAT_SOLVABLE_H
14
15 #include <iosfwd>
16
17 #include "zypp/sat/detail/PoolMember.h"
18 #include "zypp/sat/SolvAttr.h"
19 #include "zypp/ResTraits.h"
20 #include "zypp/IdString.h"
21 #include "zypp/Edition.h"
22 #include "zypp/Arch.h"
23 #include "zypp/Dep.h"
24 #include "zypp/Capabilities.h"
25 #include "zypp/Capability.h"
26 #include "zypp/Locale.h"
27
28 ///////////////////////////////////////////////////////////////////
29 namespace zypp
30 {
31   class ByteCount;
32   class CheckSum;
33   class CpeId;
34   class Date;
35   class OnMediaLocation;
36   ///////////////////////////////////////////////////////////////////
37   namespace sat
38   {
39     ///////////////////////////////////////////////////////////////////
40     /// \class Solvable
41     /// \brief  A \ref Solvable object within the sat \ref Pool.
42     ///
43     /// \note Unfortunately libsolv combines the objects kind and
44     /// name in a single identifier \c "pattern:kde_multimedia",
45     /// \b except for packages and source packes. They are not prefixed
46     /// by any kind string. Instead the architecture is abused to store
47     /// \c "src" and \c "nosrc" values.
48     ///
49     /// \ref Solvable will hide this inconsistency by treating source
50     /// packages as an own kind of solvable and map their arch to
51     /// \ref Arch_noarch.
52     ///////////////////////////////////////////////////////////////////
53     class Solvable : protected detail::PoolMember
54     {
55     public:
56       typedef sat::detail::SolvableIdType IdType;
57
58     public:
59       /** Default ctor creates \ref noSolvable.*/
60       Solvable()
61       : _id( detail::noSolvableId )
62       {}
63
64       /** \ref PoolImpl ctor. */
65       explicit Solvable( IdType id_r )
66       : _id( id_r )
67       {}
68
69     public:
70       /** Represents no \ref Solvable. */
71       static const Solvable noSolvable;
72
73       /** Evaluate \ref Solvable in a boolean context (\c != \c noSolvable). */
74       explicit operator bool() const
75       { return get(); }
76
77     public:
78       /** The identifier.
79        * This is the solvables \ref name, \b except for packages and
80        * source packes, prefixed by it's \ref kind.
81        */
82       IdString ident()const;
83
84       /** The Solvables ResKind. */
85       ResKind kind()const;
86
87       /** Test whether a Solvable is of a certain \ref ResKind.
88        * The test is far cheaper than actually retrieving and
89        * comparing the \ref kind.
90        */
91       bool isKind( const ResKind & kind_r ) const;
92       /** \overload */
93       template<class TRes>
94       bool isKind() const
95       { return isKind( resKind<TRes>() ); }
96       /** \overload Extend the test to a range of \ref ResKind. */
97       template<class TIterator>
98       bool isKind( TIterator begin, TIterator end ) const
99       { for_( it, begin, end ) if ( isKind( *it ) ) return true; return false; }
100
101       /** The name (without any ResKind prefix). */
102       std::string name() const;
103
104       /** The edition (version-release). */
105       Edition edition() const;
106
107       /** The architecture. */
108       Arch arch() const;
109
110       /** The vendor. */
111       IdString vendor() const;
112
113       /** The \ref Repository this \ref Solvable belongs to. */
114       Repository repository() const;
115       /** The repositories \ref RepoInfo. */
116       RepoInfo repoInfo() const;
117
118       /** Return whether this \ref Solvable belongs to the system repo.
119        * \note This includes the otherwise hidden systemSolvable.
120        */
121       bool isSystem() const;
122
123       /** Whether this is known to be installed on behalf of a user request.
124        * \note Returns \c false for non-system (uninstalled) solvables.
125        */
126       bool onSystemByUser() const;
127
128       /** Whether this is known to be automatically installed (as dependency of a user request package).
129        * \note Returns \c false for non-system (uninstalled) solvables.
130        */
131       bool onSystemByAuto() const;
132
133       /** Whether an installed solvable with the same ident is flagged as AutoInstalled. */
134       bool identIsAutoInstalled() const;
135
136       /** Whether different versions of this package can be installed at the same time.
137        * Per default \c false. \see also \ref ZConfig::multiversion.
138        */
139       bool multiversionInstall() const;
140
141       /** The items build time. */
142       Date buildtime() const;
143
144       /** The items install time (\c false if not installed). */
145       Date installtime() const;
146
147     public:
148       /** String representation <tt>"ident-edition.arch"</tt> or \c "noSolvable"
149        * \code
150        *   product:openSUSE-11.1.x86_64
151        *   autoyast2-2.16.19-0.1.src
152        *   noSolvable
153        * \endcode
154        */
155       std::string asString() const;
156
157       /** String representation <tt>"ident-edition.arch(repo)"</tt> or \c "noSolvable" */
158       std::string asUserString() const;
159
160       /** Test whether two Solvables have the same content.
161        * Basically the same name, edition, arch, vendor and buildtime.
162        */
163       bool identical( const Solvable & rhs ) const;
164
165       /** Test for same name-version-release.arch */
166       bool sameNVRA( const Solvable & rhs ) const
167       { return( get() == rhs.get() || ( ident() == rhs.ident() && edition() == rhs.edition() && arch() == rhs.arch() ) ); }
168
169     public:
170       /** \name Access to the \ref Solvable dependencies.
171        *
172        * \note Prerequires are a subset of requires.
173        */
174       //@{
175       Capabilities provides()    const;
176       Capabilities requires()    const;
177       Capabilities conflicts()   const;
178       Capabilities obsoletes()   const;
179       Capabilities recommends()  const;
180       Capabilities suggests()    const;
181       Capabilities enhances()    const;
182       Capabilities supplements() const;
183       Capabilities prerequires() const;
184
185       /** Return \ref Capabilities selected by \ref Dep constant. */
186       Capabilities dep( Dep which_r ) const
187       {
188         switch( which_r.inSwitch() )
189         {
190           case Dep::PROVIDES_e:    return provides();    break;
191           case Dep::REQUIRES_e:    return requires();    break;
192           case Dep::CONFLICTS_e:   return conflicts();   break;
193           case Dep::OBSOLETES_e:   return obsoletes();   break;
194           case Dep::RECOMMENDS_e:  return recommends();  break;
195           case Dep::SUGGESTS_e:    return suggests();    break;
196           case Dep::ENHANCES_e:    return enhances();    break;
197           case Dep::SUPPLEMENTS_e: return supplements(); break;
198           case Dep::PREREQUIRES_e: return prerequires(); break;
199         }
200         return Capabilities();
201       }
202       /** \overload operator[] */
203       Capabilities operator[]( Dep which_r ) const
204       { return dep( which_r ); }
205
206
207       /** Return the namespaced provides <tt>'namespace([value])[ op edition]'</tt> of this Solvable. */
208       CapabilitySet providesNamespace( const std::string & namespace_r ) const;
209
210       /** Return <tt>'value[ op edition]'</tt> for namespaced provides <tt>'namespace(value)[ op edition]'</tt>.
211        * Similar to \ref providesNamespace, but the namespace is stripped from the
212        * dependencies. This is convenient if the namespace denotes packages that
213        * should be looked up. E.g. the \c weakremover namespace used in a products
214        * release package denotes the packages that were dropped from the distribution.
215        * \see \ref Product::droplist
216        */
217       CapabilitySet valuesOfNamespace( const std::string & namespace_r ) const;
218       //@}
219
220     public:
221       /** \name Locale support. */
222       //@{
223       /** Whether this \c Solvable claims to support locales. */
224       bool supportsLocales() const;
225       /** Whether this \c Solvable supports a specific \ref Locale. */
226       bool supportsLocale( const Locale & locale_r ) const;
227       /** Whether this \c Solvable supports at least one of the specified locales. */
228       bool supportsLocale( const LocaleSet & locales_r ) const;
229       /** Whether this \c Solvable supports at least one requested locale.
230        * \see \ref Pool::setRequestedLocales
231        */
232       bool supportsRequestedLocales() const;
233       /** Return the supported locales. */
234       LocaleSet getSupportedLocales() const;
235       /** \overload Legacy return via arg \a locales_r */
236       void getSupportedLocales( LocaleSet & locales_r ) const
237       { locales_r = getSupportedLocales(); }
238       //@}
239
240     public:
241       /** The solvables CpeId if available. */
242       CpeId cpeId() const;
243
244       /** Media number the solvable is located on (\c 0 if no media access required). */
245       unsigned mediaNr() const;
246
247       /** Installed (unpacked) size.
248        * This is just a total number. Many objects provide even more detailed
249        * disk usage data. You can use \ref DiskUsageCounter to find out
250        * how objects data are distributed across partitions/directories.
251        * \code
252        *   // Load directory set into ducounter
253        *   DiskUsageCounter ducounter( { "/", "/usr", "/var" } );
254        *
255        *   // see how noch space the packages use
256        *   for ( const PoolItem & pi : pool )
257        *   {
258        *     cout << pi << ducounter.disk_usage( pi ) << endl;
259        *     // I__s_(7)GeoIP-1.4.8-3.1.2.x86_64(@System) {
260        *     // dir:[/] [ bs: 0 B ts: 0 B us: 0 B (+-: 1.0 KiB)]
261        *     // dir:[/usr] [ bs: 0 B ts: 0 B us: 0 B (+-: 133.0 KiB)]
262        *     // dir:[/var] [ bs: 0 B ts: 0 B us: 0 B (+-: 1.1 MiB)]
263        *     // }
264        *   }
265        * \endcode
266        * \see \ref DiskUsageCounter
267        */
268       ByteCount installSize() const;
269
270       /** Download size. */
271       ByteCount downloadSize() const;
272
273       /** The distribution string. */
274       std::string distribution() const;
275
276       /** Short (singleline) text describing the solvable (opt. translated). */
277       std::string summary( const Locale & lang_r = Locale() ) const;
278
279       /** Long (multiline) text describing the solvable (opt. translated). */
280       std::string description( const Locale & lang_r = Locale() ) const;
281
282       /** UI hint text when selecting the solvable for install (opt. translated). */
283       std::string insnotify( const Locale & lang_r = Locale() ) const;
284       /** UI hint text when selecting the solvable for uninstall (opt. translated).*/
285       std::string delnotify( const Locale & lang_r = Locale() ) const;
286
287       /** License or agreement to accept before installing the solvable (opt. translated). */
288       std::string licenseToConfirm( const Locale & lang_r = Locale() ) const;
289       /** \c True except for well known exceptions (i.e show license but no need to accept it). */
290       bool needToAcceptLicense() const;
291
292     public:
293       /** Helper that splits an identifier into kind and name or vice versa.
294        * \note In case \c name_r is preceded by a well known kind spec, the
295        * \c kind_r argument is ignored, and kind is derived from name.
296        * \see \ref ident
297        */
298       class SplitIdent
299       {
300       public:
301         SplitIdent() {}
302         SplitIdent( IdString ident_r );
303         SplitIdent( const char * ident_r );
304         SplitIdent( const std::string & ident_r );
305         SplitIdent( ResKind kind_r, IdString name_r );
306         SplitIdent( ResKind kind_r, const C_Str & name_r );
307
308         IdString ident() const { return _ident; }
309         ResKind  kind()  const { return _kind; }
310         IdString name()  const { return _name; }
311
312       private:
313         IdString  _ident;
314         ResKind   _kind;
315         IdString  _name;
316       };
317
318     public:
319       /** \name Attribute lookup.
320        * \see \ref LookupAttr and  \ref ArrayAttr providing a general, more
321        * query like interface for attribute retrieval.
322        */
323       //@{
324       /**
325        * returns the string attribute value for \ref attr
326        * or an empty string if it does not exists.
327        */
328       std::string lookupStrAttribute( const SolvAttr & attr ) const;
329       /** \overload Trying to look up a translated string attribute.
330        *
331        * Returns the translation for \c lang_r.
332        *
333        * Passing an empty \ref Locale will return the string for the
334        * current default locale (\see \ref ZConfig::TextLocale),
335        * \b considering all fallback locales.
336        *
337        * Returns an empty string if no translation is available.
338        */
339       std::string lookupStrAttribute( const SolvAttr & attr, const Locale & lang_r ) const;
340
341       /**
342        * returns the numeric attribute value for \ref attr
343        * or 0 if it does not exists.
344        */
345       unsigned long long lookupNumAttribute( const SolvAttr & attr ) const;
346       /** \overload returning custom notfound_r value */
347       unsigned long long lookupNumAttribute( const SolvAttr & attr, unsigned long long notfound_r ) const;
348
349       /**
350        * returns the boolean attribute value for \ref attr
351        * or \c false if it does not exists.
352        */
353       bool lookupBoolAttribute( const SolvAttr & attr ) const;
354
355       /**
356        * returns the id attribute value for \ref attr
357        * or \ref detail::noId if it does not exists.
358        */
359       detail::IdType lookupIdAttribute( const SolvAttr & attr ) const;
360
361       /**
362        * returns the CheckSum attribute value for \ref attr
363        * or an empty CheckSum if ir does not exist.
364        */
365       CheckSum lookupCheckSumAttribute( const SolvAttr & attr ) const;
366
367       /**
368        * returns OnMediaLocation data: This is everything we need to
369        * download e.g. an rpm (path, checksum, downloadsize, etc.).
370        */
371       OnMediaLocation lookupLocation() const;
372       //@}
373
374     public:
375       /** Return next Solvable in \ref Pool (or \ref noSolvable). */
376       Solvable nextInPool() const;
377       /** Return next Solvable in \ref Repo (or \ref noSolvable). */
378       Solvable nextInRepo() const;
379       /** Expert backdoor. */
380       detail::CSolvable * get() const;
381       /** Expert backdoor. */
382       IdType id() const { return _id; }
383
384     private:
385       IdType _id;
386     };
387     ///////////////////////////////////////////////////////////////////
388
389     /** \relates Solvable Stream output */
390     std::ostream & operator<<( std::ostream & str, const Solvable & obj );
391
392     /** \relates Solvable More verbose stream output including dependencies */
393     std::ostream & dumpOn( std::ostream & str, const Solvable & obj );
394
395     /** \relates Solvable XML output */
396     std::ostream & dumpAsXmlOn( std::ostream & str, const Solvable & obj );
397
398     /** \relates Solvable */
399     inline bool operator==( const Solvable & lhs, const Solvable & rhs )
400     { return lhs.get() == rhs.get(); }
401
402     /** \relates Solvable */
403     inline bool operator!=( const Solvable & lhs, const Solvable & rhs )
404     { return lhs.get() != rhs.get(); }
405
406     /** \relates Solvable */
407     inline bool operator<( const Solvable & lhs, const Solvable & rhs )
408     { return lhs.get() < rhs.get(); }
409
410     /** \relates Solvable Test whether a \ref Solvable is of a certain Kind. */
411     template<class TRes>
412     inline bool isKind( const Solvable & solvable_r )
413     { return solvable_r.isKind( ResTraits<TRes>::kind ); }
414
415     /** \relates Solvable Test for same content. */
416     inline bool identical( const Solvable & lhs, const Solvable & rhs )
417     { return lhs.identical( rhs ); }
418
419     /** \relates Solvable Test for same name version release and arch. */
420     inline bool sameNVRA( const Solvable & lhs, const Solvable & rhs )
421     { return lhs.sameNVRA( rhs ); }
422
423
424     /** \relates Solvable Compare according to \a kind and \a name. */
425     inline int compareByN( const Solvable & lhs, const Solvable & rhs )
426     {
427       int res = 0;
428       if ( lhs != rhs )
429       {
430         if ( (res = lhs.kind().compare( rhs.kind() )) == 0 )
431           res = lhs.name().compare( rhs.name() );
432       }
433       return res;
434     }
435
436     /** \relates Solvable Compare according to \a kind, \a name and \a edition. */
437     inline int compareByNVR( const Solvable & lhs, const Solvable & rhs )
438     {
439       int res = compareByN( lhs, rhs );
440       if ( res == 0 )
441         res = lhs.edition().compare( rhs.edition() );
442       return res;
443     }
444
445     /** \relates Solvable Compare according to \a kind, \a name, \a edition and \a arch. */
446     inline int compareByNVRA( const Solvable & lhs, const Solvable & rhs )
447     {
448       int res = compareByNVR( lhs, rhs );
449       if ( res == 0 )
450         res = lhs.arch().compare( rhs.arch() );
451       return res;
452     }
453
454     ///////////////////////////////////////////////////////////////////
455     namespace detail
456     {
457       ///////////////////////////////////////////////////////////////////
458       //
459       //        CLASS NAME : SolvableIterator
460       //
461       /** */
462       class SolvableIterator : public boost::iterator_adaptor<
463           SolvableIterator                   // Derived
464           , CSolvable*                       // Base
465           , const Solvable                   // Value
466           , boost::forward_traversal_tag     // CategoryOrTraversal
467           , const Solvable                   // Reference
468           >
469       {
470         public:
471           SolvableIterator()
472           : SolvableIterator::iterator_adaptor_( 0 )
473           {}
474
475           explicit SolvableIterator( const Solvable & val_r )
476           : SolvableIterator::iterator_adaptor_( 0 )
477           { assignVal( val_r ); }
478
479           explicit SolvableIterator( SolvableIdType id_r )
480           : SolvableIterator::iterator_adaptor_( 0 )
481           { assignVal( Solvable( id_r ) ); }
482
483         private:
484           friend class boost::iterator_core_access;
485
486           Solvable dereference() const
487           { return _val; }
488
489           void increment()
490           { assignVal( _val.nextInPool() ); }
491
492         private:
493           void assignVal( const Solvable & val_r )
494           { _val = val_r; base_reference() = _val.get(); }
495
496           Solvable _val;
497       };
498     } // namespace detail
499     ///////////////////////////////////////////////////////////////////
500   } // namespace sat
501   ///////////////////////////////////////////////////////////////////
502
503   class PoolItem;
504   ///////////////////////////////////////////////////////////////////
505   namespace sat
506   {
507     /** To Solvable transform functor.
508      * \relates Solvable
509      * \relates sat::SolvIterMixin
510      */
511     struct asSolvable
512     {
513       typedef Solvable result_type;
514
515       Solvable operator()( const Solvable & solv_r ) const
516       { return solv_r; }
517
518       Solvable operator()( const PoolItem & pi_r ) const;
519
520       Solvable operator()( const ResObject_constPtr & res_r ) const;
521     };
522   } // namespace sat
523   ///////////////////////////////////////////////////////////////////
524 } // namespace zypp
525 ///////////////////////////////////////////////////////////////////
526
527 ZYPP_DEFINE_ID_HASHABLE( ::zypp::sat::Solvable );
528
529 #endif // ZYPP_SAT_SOLVABLE_H