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