add ResObject::as<Package>()
[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 #ifndef SWIG // Swig treats it as syntax error
77         /** Evaluate \ref Solvable in a boolean context (\c != \c noSolvable). */
78         using base::SafeBool<Solvable>::operator bool_type;
79 #endif
80
81         /** Return whether this \ref Solvable belongs to the system repo.
82          * \note This includes the otherwise hidden systemSolvable.
83         */
84         bool isSystem() const;
85
86         /** The \ref Repository this \ref Solvable belongs to. */
87         Repository repository() const;
88
89       public:
90
91         /** \name Attribute lookup.
92          * \see \ref LookupAttr and  \ref ArrayAttr providing a general, more
93          * query like interface for attribute retrieval.
94         */
95         //@{
96         /**
97          * returns the string attribute value for \ref attr
98          * or an empty string if it does not exists.
99          */
100         std::string lookupStrAttribute( const SolvAttr & attr ) const;
101         /** \overload Trying to look up a translated string attribute.
102          *
103          * Returns the translation for \c lang_r (\b no fallback locales).
104          *
105          * Passing an empty \ref Locale will return the string for the
106          * current default locale (\see \ref ZConfig::defaultTextLocale),
107          * \b considering all fallback locales.
108          *
109          * Returns an empty string if no translation is available.
110         */
111         std::string lookupStrAttribute( const SolvAttr & attr, const Locale & lang_r ) const;
112
113         /**
114          * returns the numeric attribute value for \ref attr
115          * or 0 if it does not exists.
116          */
117         unsigned lookupNumAttribute( const SolvAttr & attr ) const;
118
119         /**
120          * returns the boolean attribute value for \ref attr
121          * or \c false if it does not exists.
122          */
123         bool lookupBoolAttribute( const SolvAttr & attr ) const;
124
125        /**
126          * returns the id attribute value for \ref attr
127          * or \ref detail::noId if it does not exists.
128          */
129         detail::IdType lookupIdAttribute( const SolvAttr & attr ) const;
130
131        /**
132          * returns the CheckSum attribute value for \ref attr
133          * or an empty CheckSum if ir does not exist.
134          */
135         CheckSum lookupCheckSumAttribute( const SolvAttr & attr ) const;
136
137         /**
138          * returns OnMediaLocation data: This is everything we need to
139          * download e.g. an rpm (path, checksum, downloadsize, etc.).
140          */
141         OnMediaLocation lookupLocation() const;
142
143         //@}
144       public:
145         /** The identifier.
146          * This is the solvables \ref name, \b except for packages and
147          * source packes, prefixed by it's \ref kind.
148          */
149         IdString     ident()    const;
150
151         ResKind      kind()     const;
152         /** Test whether a Solvable is of a certain \ref ResKind. */
153         bool         isKind( const ResKind & kind_r ) const;
154         /** \overload */
155         template<class _Res>
156         bool isKind() const
157         { return isKind( resKind<_Res>() ); }
158
159         std::string  name()     const;
160         Edition      edition()  const;
161         Arch         arch()     const;
162
163         IdString     vendor()   const;
164
165         bool         installOnly() const;
166
167       public:
168
169         /** \name Access to the \ref Solvable dependencies.
170          *
171          * \note Prerequires are a subset of requires.
172          */
173         //@{
174         Capabilities operator[]( Dep which_r ) const;
175
176         Capabilities provides()    const;
177         Capabilities requires()    const;
178         Capabilities conflicts()   const;
179         Capabilities obsoletes()   const;
180         Capabilities recommends()  const;
181         Capabilities suggests()    const;
182         Capabilities enhances()    const;
183         Capabilities supplements() const;
184         Capabilities prerequires() const;
185         //@}
186
187       public:
188         /** \name Locale support. */
189         //@{
190         /** Whether this \c Solvable claims to support locales. */
191         bool supportsLocales() const;
192         /** Whether this \c Solvable supports a specific \ref Locale. */
193         bool supportsLocale( const Locale & locale_r ) const;
194         /** Whether this \c Solvable supports at least one of the specified locales. */
195         bool supportsLocale( const LocaleSet & locales_r ) const;
196         /** Whether this \c Solvable supports at least one requested locale.
197          * \see \ref Pool::setRequestedLocales
198         */
199         bool supportsRequestedLocales() const;
200         /** Return the supported locales via locales_r. */
201         void getSupportedLocales( LocaleSet & locales_r ) const;
202         /** \overload */
203         LocaleSet getSupportedLocales() const
204         { LocaleSet ret; getSupportedLocales( ret ); return ret; }
205         //@}
206
207       public:
208         /** Return next Solvable in \ref Pool (or \ref noSolvable). */
209         Solvable nextInPool() const;
210         /** Return next Solvable in \ref Repo (or \ref noSolvable). */
211         Solvable nextInRepo() const;
212
213         /** Helper that splits an identifier into kind and name or vice versa.
214          * \see \ref ident
215         */
216         class SplitIdent
217         {
218           public:
219             SplitIdent() {}
220             SplitIdent( IdString ident_r );
221             SplitIdent( ResKind kind_r, IdString name_r );
222             SplitIdent( ResKind kind_r, const C_Str & name_r );
223
224             IdString ident() const { return _ident; }
225             ResKind  kind()  const { return _kind; }
226             IdString name()  const { return _name; }
227           private:
228             IdString  _ident;
229             ResKind   _kind;
230             IdString  _name;
231         };
232
233       public:
234         /** Expert backdoor. */
235         ::_Solvable * get() const;
236         /** Expert backdoor. */
237         IdType id() const { return _id; }
238       private:
239 #ifndef SWIG // Swig treats it as syntax error
240         friend base::SafeBool<Solvable>::operator bool_type() const;
241 #endif
242         bool boolTest() const { return get(); }
243       private:
244         IdType _id;
245     };
246     ///////////////////////////////////////////////////////////////////
247
248     /** \relates Solvable Stream output */
249     std::ostream & operator<<( std::ostream & str, const Solvable & obj );
250
251     /** \relates Solvable More verbose stream output including dependencies */
252     std::ostream & dumpOn( std::ostream & str, const Solvable & obj );
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     /** \relates Solvable */
263     inline bool operator<( const Solvable & lhs, const Solvable & rhs )
264     { return lhs.get() < rhs.get(); }
265
266     ///////////////////////////////////////////////////////////////////
267     namespace detail
268     { /////////////////////////////////////////////////////////////////
269       ///////////////////////////////////////////////////////////////////
270       //
271       //        CLASS NAME : SolvableIterator
272       //
273       /** */
274       class SolvableIterator : public boost::iterator_adaptor<
275           SolvableIterator                   // Derived
276           , ::_Solvable*                     // Base
277           , const Solvable                   // Value
278           , boost::forward_traversal_tag     // CategoryOrTraversal
279           , const Solvable                   // Reference
280           >
281       {
282         public:
283           SolvableIterator()
284           : SolvableIterator::iterator_adaptor_( 0 )
285           {}
286
287           explicit SolvableIterator( const Solvable & val_r )
288           : SolvableIterator::iterator_adaptor_( 0 )
289           { assignVal( val_r ); }
290
291           explicit SolvableIterator( SolvableIdType id_r )
292           : SolvableIterator::iterator_adaptor_( 0 )
293           { assignVal( Solvable( id_r ) ); }
294
295         private:
296           friend class boost::iterator_core_access;
297
298           Solvable dereference() const
299           { return _val; }
300
301           void increment()
302           { assignVal( _val.nextInPool() ); }
303
304         private:
305           void assignVal( const Solvable & val_r )
306           { _val = val_r; base_reference() = _val.get(); }
307
308           Solvable _val;
309       };
310       ///////////////////////////////////////////////////////////////////
311       /////////////////////////////////////////////////////////////////
312     } // namespace detail
313     ///////////////////////////////////////////////////////////////////
314
315    /////////////////////////////////////////////////////////////////
316   } // namespace sat
317   ///////////////////////////////////////////////////////////////////
318
319   /** \relates sat::Solvable Test whether a \ref sat::Solvable is of a certain Kind. */
320   template<class _Res>
321   inline bool isKind( const sat::Solvable & solvable_r )
322   { return solvable_r.isKind( ResTraits<_Res>::kind ); }
323
324   /////////////////////////////////////////////////////////////////
325 } // namespace zypp
326 ///////////////////////////////////////////////////////////////////
327
328 ZYPP_DEFINE_ID_HASHABLE( ::zypp::sat::Solvable );
329
330 #endif // ZYPP_SAT_SOLVABLE_H