Deprecate MediaAccess::downloads (accidentally deleted)
[platform/upstream/libzypp.git] / zypp / Resolvable.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/Resolvable.h
10  *
11 */
12 #ifndef ZYPP_RESOLVABLE_H
13 #define ZYPP_RESOLVABLE_H
14
15 #include <iosfwd>
16 #include <string>
17
18 #include "zypp/base/Deprecated.h"
19
20 #include "zypp/base/ReferenceCounted.h"
21 #include "zypp/base/NonCopyable.h"
22 #include "zypp/base/PtrTypes.h"
23
24 #include "zypp/sat/Solvable.h"
25
26 ///////////////////////////////////////////////////////////////////
27 namespace zypp
28 { /////////////////////////////////////////////////////////////////
29
30   class PoolItem;
31
32   ///////////////////////////////////////////////////////////////////
33   //
34   //    CLASS NAME : Resolvable
35   //
36   /** Interface base for resolvable objects (identification and dependencies).
37    * \todo Merge with ResObject
38   */
39   class Resolvable : protected sat::Solvable,
40                      public base::ReferenceCounted, private base::NonCopyable
41   {
42     friend std::ostream & operator<<( std::ostream & str, const Resolvable & obj );
43
44   public:
45     typedef Resolvable               Self;
46     typedef ResTraits<Self>          TraitsType;
47     typedef TraitsType::KindType     Kind;
48     typedef TraitsType::PtrType      Ptr;
49     typedef TraitsType::constPtrType constPtr;
50
51   public:
52 #ifndef SWIG // Swig treats it as syntax error
53     /** Whether this represents a valid- or no-solvable. */
54     using sat::Solvable::operator bool_type;
55 #endif
56     /** Whether this represents an installed solvable. */
57     bool isSystem() const
58     { return sat::Solvable::isSystem(); }
59
60     IdString ident() const
61     { return sat::Solvable::ident(); }
62
63     ResKind kind() const
64     { return sat::Solvable::kind(); }
65
66     bool isKind( const ResKind & kind_r ) const
67     { return sat::Solvable::isKind( kind_r ); }
68
69     template<class _Res>
70     bool isKind() const
71     { return sat::Solvable::isKind<_Res>(); }
72
73     std::string name() const
74     { return sat::Solvable::name(); }
75
76     Edition edition() const
77     { return sat::Solvable::edition(); }
78
79     Arch arch() const
80     { return sat::Solvable::arch(); }
81
82     /**
83      * Flag in the metadata indicating this should be
84      * installed unsing '-i' (not -U).
85      */
86     bool installOnly() const
87     { return sat::Solvable::installOnly(); }
88
89     /** \name Dependencies. */
90     //@{
91     /** Select by Dep. */
92     Capabilities dep( Dep which_r ) const
93     { return operator[]( which_r ); }
94
95     Capabilities operator[]( Dep which_r ) const
96     { return sat::Solvable::operator[]( which_r ); }
97
98     Capabilities provides()    const
99     { return sat::Solvable::provides(); }
100
101     Capabilities requires()    const
102     { return sat::Solvable::requires(); }
103
104     Capabilities conflicts()   const
105     { return sat::Solvable::conflicts(); }
106
107     Capabilities obsoletes()   const
108     { return sat::Solvable::obsoletes(); }
109
110     Capabilities recommends()  const
111     { return sat::Solvable::recommends(); }
112
113     Capabilities suggests()    const
114     { return sat::Solvable::suggests(); }
115
116     Capabilities enhances()    const
117     { return sat::Solvable::enhances(); }
118
119     Capabilities supplements() const
120     { return sat::Solvable::supplements(); }
121
122     Capabilities prerequires() const
123     { return sat::Solvable::prerequires(); }
124     //@}
125
126   public:
127     /** Access the corresponding \ref sat:::Solvable. */
128     const sat::Solvable & satSolvable() const { return *this; }
129
130     /** Access the corresponding \ref PoolItem. */
131     PoolItem poolItem() const;
132
133   protected:
134     /** Ctor */
135     Resolvable( const sat::Solvable & solvable_r );
136     /** Dtor */
137     virtual ~Resolvable();
138     /** Helper for stream output */
139     virtual std::ostream & dumpOn( std::ostream & str ) const;
140  };
141  ///////////////////////////////////////////////////////////////////
142
143  /** \relates Resolvable Stream output */
144  inline std::ostream & operator<<( std::ostream & str, const Resolvable & obj )
145  { return obj.dumpOn( str ); }
146
147  /** \relates Resolvable More verbose stream output including dependencies */
148  inline std::ostream & dumpOn( std::ostream & str, const Resolvable & obj )
149  { return dumpOn( str, obj.satSolvable() ); }
150
151   /** Test whether a Resolvable::Ptr is of a certain Kind.
152    * \return \c Ture iff \a p is not \c NULL and points to a Resolvable
153    * of the specified Kind.
154    * \relates Resolvable
155    * \code
156    * isKind<Package>(resPtr);
157    * \endcode
158   */
159   template<class _Res>
160     inline bool isKind( const Resolvable::constPtr & p )
161     { return p && p->kind() == ResTraits<_Res>::kind; }
162
163   // Specialization for Resolvable: Always true.
164   template<>
165     inline bool isKind<Resolvable>( const Resolvable::constPtr & p )
166     { return p; }
167
168   // Specialization for ResObject: Always true.
169   template<>
170     inline bool isKind<ResObject>( const Resolvable::constPtr & p )
171     { return p; }
172
173
174   /** Convert Resolvable::Ptr into Ptr of a certain Kind.
175    * \return \c NULL iff \a p is \c NULL or points to a Resolvable
176    * not of the specified Kind.
177    * \relates Resolvable
178    * \code
179    * asKind<Package>(resPtr);
180    * \endcode
181   */
182   template<class _Res>
183     inline typename ResTraits<_Res>::PtrType asKind( const Resolvable::Ptr & p )
184     { return dynamic_pointer_cast<_Res>(p); }
185
186   template<class _Res>
187     inline typename ResTraits<_Res>::constPtrType asKind( const Resolvable::constPtr & p )
188     { return dynamic_pointer_cast<const _Res>(p); }
189
190   ///////////////////////////////////////////////////////////////////
191
192   /** \relates Resolvable Compare Resolvable::constPtr according to
193    *  \a kind and \a name.
194   */
195   inline int compareByN( const Resolvable::constPtr & lhs,
196                          const Resolvable::constPtr & rhs )
197   {
198     if ( lhs == rhs )
199       return 0;
200     if ( ! (lhs && rhs) )
201       return lhs ? 1 : -1;
202     int res = 0;
203     if ( (res = lhs->kind().compare( rhs->kind() )) )
204       return res;
205     return lhs->name().compare( rhs->name() );
206   }
207
208   /** \relates Resolvable Compare Resolvable::constPtr according to
209    *  \a kind, \a name and \a edition(compare!).
210   */
211   inline int compareByNVR( const Resolvable::constPtr & lhs,
212                            const Resolvable::constPtr & rhs )
213   {
214     if ( lhs == rhs )
215       return 0;
216     if ( ! (lhs && rhs) )
217       return lhs ? 1 : -1;
218     int res = 0;
219     if ( (res = lhs->kind().compare( rhs->kind() )) )
220       return res;
221     if ( (res = lhs->name().compare( rhs->name() )) )
222       return res;
223     return lhs->edition().compare( rhs->edition() );
224   }
225
226   /** \relates Resolvable Compare Resolvable::constPtr according to
227    *  \a kind, \a name, \a edition(compare!) and \a arch.
228   */
229   inline int compareByNVRA( const Resolvable::constPtr & lhs,
230                             const Resolvable::constPtr & rhs )
231   {
232     if ( lhs == rhs )
233       return 0;
234     if ( ! (lhs && rhs) )
235       return lhs ? 1 : -1;
236     int res = 0;
237     if ( (res = lhs->kind().compare( rhs->kind() )) )
238       return res;
239     if ( (res = lhs->name().compare( rhs->name() )) )
240       return res;
241     if ( (res = lhs->edition().compare( rhs->edition() )) )
242       return res;
243     return lhs->arch().compare( rhs->arch() );
244   }
245
246   /////////////////////////////////////////////////////////////////
247 } // namespace zypp
248 ///////////////////////////////////////////////////////////////////
249 #endif // ZYPP_RESOLVABLE_H