1075a2c1bb3607702112acdafd234bef315b6267
[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
22 #include "zypp/ResTraits.h"
23 #include "zypp/IdString.h"
24 #include "zypp/Edition.h"
25 #include "zypp/Arch.h"
26 #include "zypp/Dep.h"
27 #include "zypp/Capabilities.h"
28 #include "zypp/Capability.h"
29
30 ///////////////////////////////////////////////////////////////////
31 namespace zypp
32 { /////////////////////////////////////////////////////////////////
33   ///////////////////////////////////////////////////////////////////
34   namespace sat
35   { /////////////////////////////////////////////////////////////////
36
37     ///////////////////////////////////////////////////////////////////
38     //
39     //  CLASS NAME : Solvable
40     //
41     /** A \ref Solvable object within the sat \ref Pool.
42      *
43      * \note Unfortunately libsatsolver 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      *
54      */
55     class Solvable : protected detail::PoolMember,
56                      private base::SafeBool<Solvable>
57     {
58       public:
59         /** Default ctor creates \ref nosolvable.*/
60         Solvable()
61         : _id( detail::noSolvableId ) {}
62
63         /** \ref PoolImpl ctor. */
64         explicit Solvable( detail::SolvableIdType id_r )
65         : _id( id_r ) {}
66
67       public:
68         /** Represents no \ref Solvable. */
69         static const Solvable nosolvable;
70
71         /** Evaluate \ref Solvable in a boolean context (\c != \c nosolvable). */
72         using base::SafeBool<Solvable>::operator bool_type;
73
74         /** Return whether this \ref Solvable belongs to the system repo. */
75         bool isSystem() const;
76
77         /** The \ref Repository this \ref Solvable belongs to. */
78         Repo repo() const;
79
80       public:
81
82         /**
83          * returns the string attribute value for \ref attr
84          * or an empty string if it does not exists.
85          */
86         std::string lookupStrAttribute( const SolvAttr &attr ) const;
87
88         /**
89          * returns the numeric attribute value for \ref attr
90          * or 0 if it does not exists.
91          */
92         int lookupNumAttribute( const SolvAttr &attr ) const;
93
94       public:
95         /** The identifier.
96          * This is the solvables \ref name, \b except for packages and
97          * source packes, prefixed by it's \ref kind.
98          */
99         IdString     ident()    const;
100
101         ResKind      kind()     const;
102         /** Test whether a Solvable is of a certain \ref ResKind. */
103         bool         isKind( const ResKind & kind_r ) const;
104
105         std::string  name()     const;
106         Edition      edition()  const;
107         Arch         arch()     const;
108
109         IdString     vendor()   const;
110
111       public:
112
113         /** \name Access to the \ref Solvable dependencies.
114          *
115          * \note Prerequires are a subset of requires.
116          */
117         //@{
118         Capabilities operator[]( Dep which_r ) const;
119
120         Capabilities provides()    const;
121         Capabilities requires()    const;
122         Capabilities conflicts()   const;
123         Capabilities obsoletes()   const;
124         Capabilities recommends()  const;
125         Capabilities suggests()    const;
126         Capabilities freshens()    const;
127         Capabilities enhances()    const;
128         Capabilities supplements() const;
129         Capabilities prerequires() const;
130         //@}
131
132       public:
133         /** Return next Solvable in \ref Pool (or \ref nosolvable). */
134         Solvable nextInPool() const;
135         /** Return next Solvable in \ref Repo (or \ref nosolvable). */
136         Solvable nextInRepo() const;
137
138       public:
139         /** Expert backdoor. */
140         ::_Solvable * get() const;
141         /** Expert backdoor. */
142         detail::SolvableIdType id() const { return _id; }
143       private:
144         friend base::SafeBool<Solvable>::operator bool_type() const;
145         bool boolTest() const { return get(); }
146       private:
147         detail::SolvableIdType _id;
148     };
149     ///////////////////////////////////////////////////////////////////
150
151     /** \relates Solvable Stream output */
152     std::ostream & operator<<( std::ostream & str, const Solvable & obj );
153
154     /** \relates Solvable More verbose stream output including dependencies */
155     std::ostream & dumpOn( std::ostream & str, const Solvable & obj );
156
157     /** \relates Solvable */
158     inline bool operator==( const Solvable & lhs, const Solvable & rhs )
159     { return lhs.get() == rhs.get(); }
160
161     /** \relates Solvable */
162     inline bool operator!=( const Solvable & lhs, const Solvable & rhs )
163     { return lhs.get() != rhs.get(); }
164
165     /** \relates Solvable */
166     inline bool operator<( const Solvable & lhs, const Solvable & rhs )
167     { return lhs.get() < rhs.get(); }
168
169     ///////////////////////////////////////////////////////////////////
170     namespace detail
171     { /////////////////////////////////////////////////////////////////
172       ///////////////////////////////////////////////////////////////////
173       //
174       //        CLASS NAME : SolvableIterator
175       //
176       /** */
177       class SolvableIterator : public boost::iterator_adaptor<
178           SolvableIterator                   // Derived
179           , ::_Solvable*                     // Base
180           , Solvable                         // Value
181           , boost::single_pass_traversal_tag // CategoryOrTraversal
182           , Solvable                         // Reference
183           >
184       {
185         public:
186           SolvableIterator()
187           : SolvableIterator::iterator_adaptor_( 0 )
188           {}
189
190           explicit SolvableIterator( const Solvable & val_r )
191           : SolvableIterator::iterator_adaptor_( 0 )
192           { assignVal( val_r ); }
193
194           explicit SolvableIterator( SolvableIdType id_r )
195           : SolvableIterator::iterator_adaptor_( 0 )
196           { assignVal( Solvable( id_r ) ); }
197
198         private:
199           friend class boost::iterator_core_access;
200
201           void increment()
202           { assignVal( _val.nextInPool() ); }
203
204           Solvable dereference() const
205           { return _val; }
206
207           void assignVal( const Solvable & val_r )
208           { _val = val_r; base_reference() = _val.get(); }
209
210           Solvable _val;
211       };
212       ///////////////////////////////////////////////////////////////////
213       /////////////////////////////////////////////////////////////////
214     } // namespace detail
215     ///////////////////////////////////////////////////////////////////
216
217    /////////////////////////////////////////////////////////////////
218   } // namespace sat
219   ///////////////////////////////////////////////////////////////////
220
221   /** \relates sat::Solvable Test whether a \ref sat::Solvable is of a certain Kind. */
222   template<class _Res>
223   inline bool isKind( const sat::Solvable & solvable_r )
224   { return solvable_r.isKind( ResTraits<_Res>::kind ); }
225
226   /////////////////////////////////////////////////////////////////
227 } // namespace zypp
228 ///////////////////////////////////////////////////////////////////
229 #endif // ZYPP_SAT_SOLVABLE_H