added isRelevant
[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/base/SafeBool.h"
18
19 #include "zypp/sat/detail/PoolMember.h"
20 #include "zypp/sat/SolvAttr.h"
21 #include "zypp/ResTraits.h"
22 #include "zypp/IdString.h"
23 #include "zypp/Edition.h"
24 #include "zypp/Arch.h"
25 #include "zypp/Dep.h"
26 #include "zypp/Capabilities.h"
27 #include "zypp/Capability.h"
28 #include "zypp/Locale.h"
29
30 ///////////////////////////////////////////////////////////////////
31 namespace zypp
32 { /////////////////////////////////////////////////////////////////
33
34   class CheckSum;
35   class OnMediaLocation;
36
37   ///////////////////////////////////////////////////////////////////
38   namespace sat
39   { /////////////////////////////////////////////////////////////////
40
41     ///////////////////////////////////////////////////////////////////
42     //
43     //  CLASS NAME : Solvable
44     //
45     /** A \ref Solvable object within the sat \ref Pool.
46      *
47      * \note Unfortunately libsatsolver combines the objects kind and
48      * name in a single identifier \c "pattern:kde_multimedia",
49      * \b except for packages and source packes. They are not prefixed
50      * by any kind string. Instead the architecture is abused to store
51      * \c "src" and \c "nosrc" values.
52      *
53      * \ref Solvable will hide this inconsistency by treating source
54      * packages as an own kind of solvable and map their arch to
55      * \ref Arch_noarch.
56      */
57     class Solvable : protected detail::PoolMember,
58                      private base::SafeBool<Solvable>
59     {
60       public:
61         typedef sat::detail::SolvableIdType IdType;
62
63       public:
64         /** Default ctor creates \ref noSolvable.*/
65         Solvable()
66         : _id( detail::noSolvableId ) {}
67
68         /** \ref PoolImpl ctor. */
69         explicit Solvable( IdType id_r )
70         : _id( id_r ) {}
71
72       public:
73         /** Represents no \ref Solvable. */
74         static const Solvable noSolvable;
75
76         /** Evaluate \ref Solvable in a boolean context (\c != \c noSolvable). */
77         using base::SafeBool<Solvable>::operator bool_type;
78
79         /** Return whether this \ref Solvable belongs to the system repo.
80          * \note This includes the otherwise hidden systemSolvable.
81         */
82         bool isSystem() const;
83
84         /** The \ref Repository this \ref Solvable belongs to. */
85         Repository repository() const;
86
87       public:
88
89         /** \name Attribute lookup.
90          * \see \ref LookupAttr and  \ref ArrayAttr providing a general, more
91          * query like interface for attribute retrieval.
92         */
93         //@{
94         /**
95          * returns the string attribute value for \ref attr
96          * or an empty string if it does not exists.
97          */
98         std::string lookupStrAttribute( const SolvAttr & attr ) const;
99         /** \overload Trying to look up a translated string attribute.
100          *
101          * Returns the translation for \c lang_r (\b no fallback locales).
102          *
103          * Passing an empty \ref Locale will return the string for the
104          * current default locale (\see \ref ZConfig::defaultTextLocale),
105          * \b considering all fallback locales.
106          *
107          * Returns an empty string if no translation is available.
108         */
109         std::string lookupStrAttribute( const SolvAttr & attr, const Locale & lang_r ) const;
110
111         /**
112          * returns the numeric attribute value for \ref attr
113          * or 0 if it does not exists.
114          */
115         unsigned lookupNumAttribute( const SolvAttr & attr ) const;
116
117         /**
118          * returns the boolean attribute value for \ref attr
119          * or \c false if it does not exists.
120          */
121         bool lookupBoolAttribute( const SolvAttr & attr ) const;
122
123        /**
124          * returns the id attribute value for \ref attr
125          * or \ref detail::noId if it does not exists.
126          */
127         detail::IdType lookupIdAttribute( const SolvAttr & attr ) const;
128
129        /**
130          * returns the CheckSum attribute value for \ref attr
131          * or an empty CheckSum if ir does not exist.
132          */
133         CheckSum lookupCheckSumAttribute( const SolvAttr & attr ) const;
134
135         /**
136          * returns OnMediaLocation data: This is everything we need to
137          * download e.g. an rpm (path, checksum, downloadsize, etc.).
138          */
139         OnMediaLocation lookupLocation() const;
140
141         //@}
142       public:
143         /** The identifier.
144          * This is the solvables \ref name, \b except for packages and
145          * source packes, prefixed by it's \ref kind.
146          */
147         IdString     ident()    const;
148
149         ResKind      kind()     const;
150         /** Test whether a Solvable is of a certain \ref ResKind. */
151         bool         isKind( const ResKind & kind_r ) const;
152
153         std::string  name()     const;
154         Edition      edition()  const;
155         Arch         arch()     const;
156
157         IdString     vendor()   const;
158
159       public:
160
161         /** \name Access to the \ref Solvable dependencies.
162          *
163          * \note Prerequires are a subset of requires.
164          */
165         //@{
166         Capabilities operator[]( Dep which_r ) const;
167
168         Capabilities provides()    const;
169         Capabilities requires()    const;
170         Capabilities conflicts()   const;
171         Capabilities obsoletes()   const;
172         Capabilities recommends()  const;
173         Capabilities suggests()    const;
174         Capabilities freshens()    const;
175         Capabilities enhances()    const;
176         Capabilities supplements() const;
177         Capabilities prerequires() const;
178         //@}
179
180       public:
181         /** Returns true if the solvable is satisfied */
182         bool isSatisfied() const;
183         /** Returns true if the solvable is satisfied */
184         bool isBroken() const { return !isSatisfied(); }
185         /** Returns true if the solvable is relevant which means e.G. for patches
186          *  that at least one package of the patch is installed.
187          */
188         bool isRelevant() const;
189
190       public:
191         /** \name Locale support. */
192         //@{
193         /** Whether this \c Solvable claims to support locales. */
194         bool supportsLocales() const;
195         /** Whether this \c Solvable supports a specific \ref Locale. */
196         bool supportsLocale( const Locale & locale_r ) const;
197         /** Whether this \c Solvable supports at least one of the specified locales. */
198         bool supportsLocale( const LocaleSet & locales_r ) const;
199         /** Whether this \c Solvable supports at least one requested locale.
200          * \see \ref Pool::setRequestedLocales
201         */
202         bool supportsRequestedLocales() const;
203         /** Return the supported locales via locales_r. */
204         void getSupportedLocales( LocaleSet & locales_r ) const;
205         /** \overload */
206         LocaleSet getSupportedLocales() const
207         { LocaleSet ret; getSupportedLocales( ret ); return ret; }
208         //@}
209
210       public:
211         /** Return next Solvable in \ref Pool (or \ref noSolvable). */
212         Solvable nextInPool() const;
213         /** Return next Solvable in \ref Repo (or \ref noSolvable). */
214         Solvable nextInRepo() const;
215
216         /** Helper that splits an identifier into kind and name.
217          * \see \ref ident
218         */
219         class SplitIdent
220         {
221           public:
222             SplitIdent() {}
223             SplitIdent( IdString ident_r );
224             ResKind      kind() const { return _kind; }
225             std::string  name() const { return _name; }
226           private:
227             ResKind      _kind;
228             std::string  _name;
229         };
230
231       public:
232         /** Expert backdoor. */
233         ::_Solvable * get() const;
234         /** Expert backdoor. */
235         IdType id() const { return _id; }
236       private:
237         friend base::SafeBool<Solvable>::operator bool_type() const;
238         bool boolTest() const { return get(); }
239       private:
240         IdType _id;
241     };
242     ///////////////////////////////////////////////////////////////////
243
244     /** \relates Solvable Stream output */
245     std::ostream & operator<<( std::ostream & str, const Solvable & obj );
246
247     /** \relates Solvable More verbose stream output including dependencies */
248     std::ostream & dumpOn( std::ostream & str, const Solvable & obj );
249
250     /** \relates Solvable */
251     inline bool operator==( const Solvable & lhs, const Solvable & rhs )
252     { return lhs.get() == rhs.get(); }
253
254     /** \relates Solvable */
255     inline bool operator!=( const Solvable & lhs, const Solvable & rhs )
256     { return lhs.get() != rhs.get(); }
257
258     /** \relates Solvable */
259     inline bool operator<( const Solvable & lhs, const Solvable & rhs )
260     { return lhs.get() < rhs.get(); }
261
262     ///////////////////////////////////////////////////////////////////
263     namespace detail
264     { /////////////////////////////////////////////////////////////////
265       ///////////////////////////////////////////////////////////////////
266       //
267       //        CLASS NAME : SolvableIterator
268       //
269       /** */
270       class SolvableIterator : public boost::iterator_adaptor<
271           SolvableIterator                   // Derived
272           , ::_Solvable*                     // Base
273           , const Solvable                   // Value
274           , boost::forward_traversal_tag     // CategoryOrTraversal
275           , const Solvable                   // Reference
276           >
277       {
278         public:
279           SolvableIterator()
280           : SolvableIterator::iterator_adaptor_( 0 )
281           {}
282
283           explicit SolvableIterator( const Solvable & val_r )
284           : SolvableIterator::iterator_adaptor_( 0 )
285           { assignVal( val_r ); }
286
287           explicit SolvableIterator( SolvableIdType id_r )
288           : SolvableIterator::iterator_adaptor_( 0 )
289           { assignVal( Solvable( id_r ) ); }
290
291         private:
292           friend class boost::iterator_core_access;
293
294           Solvable dereference() const
295           { return _val; }
296
297           void increment()
298           { assignVal( _val.nextInPool() ); }
299
300         private:
301           void assignVal( const Solvable & val_r )
302           { _val = val_r; base_reference() = _val.get(); }
303
304           Solvable _val;
305       };
306       ///////////////////////////////////////////////////////////////////
307       /////////////////////////////////////////////////////////////////
308     } // namespace detail
309     ///////////////////////////////////////////////////////////////////
310
311    /////////////////////////////////////////////////////////////////
312   } // namespace sat
313   ///////////////////////////////////////////////////////////////////
314
315   /** \relates sat::Solvable Test whether a \ref sat::Solvable is of a certain Kind. */
316   template<class _Res>
317   inline bool isKind( const sat::Solvable & solvable_r )
318   { return solvable_r.isKind( ResTraits<_Res>::kind ); }
319
320   /////////////////////////////////////////////////////////////////
321 } // namespace zypp
322 ///////////////////////////////////////////////////////////////////
323
324 ZYPP_DEFINE_ID_HASHABLE( ::zypp::sat::Solvable )
325
326 #endif // ZYPP_SAT_SOLVABLE_H