a27a5cba9d35d1d752da59c6fec60a63b5c73661
[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/APIConfig.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 zypp::sat::Solvable,     // Note: gcc bug #52841 prohibits using just 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     /** Whether this represents an installed solvable. */
53     bool isSystem() const
54     { return sat::Solvable::isSystem(); }
55
56     /** \copydoc sat::Solvable::onSystemByUser() */
57     bool onSystemByUser() const
58     { return sat::Solvable::onSystemByUser(); }
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     /** Whether different versions of this package can be installed at the same time.
83      * Per default \c false. \see also \ref ZConfig::multiversion.
84      */
85     bool multiversionInstall() const
86     { return sat::Solvable::multiversionInstall(); }
87
88     using sat::Solvable::asString;
89     using sat::Solvable::asUserString;
90
91     /** \name Dependencies. */
92     //@{
93     /** Select by Dep. */
94     Capabilities dep( Dep which_r ) const
95     { return operator[]( which_r ); }
96
97     Capabilities operator[]( Dep which_r ) const
98     { return sat::Solvable::operator[]( which_r ); }
99
100     Capabilities provides()    const
101     { return sat::Solvable::provides(); }
102
103     Capabilities requires()    const
104     { return sat::Solvable::requires(); }
105
106     Capabilities conflicts()   const
107     { return sat::Solvable::conflicts(); }
108
109     Capabilities obsoletes()   const
110     { return sat::Solvable::obsoletes(); }
111
112     Capabilities recommends()  const
113     { return sat::Solvable::recommends(); }
114
115     Capabilities suggests()    const
116     { return sat::Solvable::suggests(); }
117
118     Capabilities enhances()    const
119     { return sat::Solvable::enhances(); }
120
121     Capabilities supplements() const
122     { return sat::Solvable::supplements(); }
123
124     Capabilities prerequires() const
125     { return sat::Solvable::prerequires(); }
126
127     CapabilitySet providesNamespace( const std::string & namespace_r ) const
128     { return sat::Solvable::providesNamespace( namespace_r ); }
129
130     CapabilitySet valuesOfNamespace( const std::string & namespace_r ) const
131     { return sat::Solvable::valuesOfNamespace( namespace_r ); }
132     //@}
133
134   public:
135     /** Access the corresponding \ref sat:::Solvable. */
136     const sat::Solvable & satSolvable() const { return *this; }
137
138     /** Access the corresponding \ref PoolItem. */
139     PoolItem poolItem() const;
140
141   protected:
142     /** Ctor */
143     Resolvable( const sat::Solvable & solvable_r );
144     /** Dtor */
145     virtual ~Resolvable();
146     /** Helper for stream output */
147     virtual std::ostream & dumpOn( std::ostream & str ) const;
148  };
149  ///////////////////////////////////////////////////////////////////
150
151  /** \relates Resolvable Stream output */
152  inline std::ostream & operator<<( std::ostream & str, const Resolvable & obj )
153  { return obj.dumpOn( str ); }
154
155  /** \relates Resolvable More verbose stream output including dependencies */
156  inline std::ostream & dumpOn( std::ostream & str, const Resolvable & obj )
157  { return dumpOn( str, obj.satSolvable() ); }
158
159   /** Test whether a Resolvable::Ptr is of a certain Kind.
160    * \return \c Ture iff \a p is not \c NULL and points to a Resolvable
161    * of the specified Kind.
162    * \relates Resolvable
163    * \code
164    * isKind<Package>(resPtr);
165    * \endcode
166   */
167   template<class _Res>
168     inline bool isKind( const Resolvable::constPtr & p )
169     { return p && p->kind() == ResTraits<_Res>::kind; }
170
171   // Specialization for Resolvable: Always true.
172   template<>
173     inline bool isKind<Resolvable>( const Resolvable::constPtr & p )
174     { return !!p; }
175
176   // Specialization for ResObject: Always true.
177   template<>
178     inline bool isKind<ResObject>( const Resolvable::constPtr & p )
179     { return !!p; }
180
181
182   /** Convert Resolvable::Ptr into Ptr of a certain Kind.
183    * \return \c NULL iff \a p is \c NULL or points to a Resolvable
184    * not of the specified Kind.
185    * \relates Resolvable
186    * \code
187    * asKind<Package>(resPtr);
188    * \endcode
189   */
190   template<class _Res>
191     inline typename ResTraits<_Res>::PtrType asKind( const Resolvable::Ptr & p )
192     { return dynamic_pointer_cast<_Res>(p); }
193
194   template<class _Res>
195     inline typename ResTraits<_Res>::constPtrType asKind( const Resolvable::constPtr & p )
196     { return dynamic_pointer_cast<const _Res>(p); }
197
198   ///////////////////////////////////////////////////////////////////
199
200   /** \relates Resolvable Compare Resolvable::constPtr according to
201    *  \a kind and \a name.
202   */
203   inline int compareByN( const Resolvable::constPtr & lhs,
204                          const Resolvable::constPtr & rhs )
205   {
206     if ( lhs == rhs )
207       return 0;
208     if ( ! (lhs && rhs) )
209       return lhs ? 1 : -1;
210     int res = 0;
211     if ( (res = lhs->kind().compare( rhs->kind() )) )
212       return res;
213     return lhs->name().compare( rhs->name() );
214   }
215
216   /** \relates Resolvable Compare Resolvable::constPtr according to
217    *  \a kind, \a name and \a edition(compare!).
218   */
219   inline int compareByNVR( const Resolvable::constPtr & lhs,
220                            const Resolvable::constPtr & rhs )
221   {
222     if ( lhs == rhs )
223       return 0;
224     if ( ! (lhs && rhs) )
225       return lhs ? 1 : -1;
226     int res = 0;
227     if ( (res = lhs->kind().compare( rhs->kind() )) )
228       return res;
229     if ( (res = lhs->name().compare( rhs->name() )) )
230       return res;
231     return lhs->edition().compare( rhs->edition() );
232   }
233
234   /** \relates Resolvable Compare Resolvable::constPtr according to
235    *  \a kind, \a name, \a edition(compare!) and \a arch.
236   */
237   inline int compareByNVRA( const Resolvable::constPtr & lhs,
238                             const Resolvable::constPtr & rhs )
239   {
240     if ( lhs == rhs )
241       return 0;
242     if ( ! (lhs && rhs) )
243       return lhs ? 1 : -1;
244     int res = 0;
245     if ( (res = lhs->kind().compare( rhs->kind() )) )
246       return res;
247     if ( (res = lhs->name().compare( rhs->name() )) )
248       return res;
249     if ( (res = lhs->edition().compare( rhs->edition() )) )
250       return res;
251     return lhs->arch().compare( rhs->arch() );
252   }
253
254   /////////////////////////////////////////////////////////////////
255 } // namespace zypp
256 ///////////////////////////////////////////////////////////////////
257 #endif // ZYPP_RESOLVABLE_H