Imported Upstream version 14.45.0
[platform/upstream/libzypp.git] / zypp / Capability.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/Capability.h
10  *
11 */
12 #ifndef ZYPP_CAPABILITY_H
13 #define ZYPP_CAPABILITY_H
14
15 #include <iosfwd>
16
17 #include "zypp/APIConfig.h"
18 #include "zypp/sat/detail/PoolMember.h"
19
20 #include "zypp/IdString.h"
21 #include "zypp/Edition.h"
22 #include "zypp/Rel.h"
23 #include "zypp/ResTraits.h"
24
25 #include "zypp/CapMatch.h"
26
27 ///////////////////////////////////////////////////////////////////
28 namespace zypp
29 { /////////////////////////////////////////////////////////////////
30
31   class Capability;
32   class CapDetail;
33   class Arch;
34
35   typedef std::tr1::unordered_set<Capability> CapabilitySet;
36
37   ///////////////////////////////////////////////////////////////////
38   //
39   //    CLASS NAME : Capability
40   //
41   /** A sat capability.
42    *
43    * A Capability: <tt>"name[.arch] [op edition]"</tt>
44    *
45    * If a certain \ref ResKind is specified upon construction, the
46    * capabilities name part is prefixed, unless it already conatins a
47    * well known kind spec. If no \ref ResKind is specified, it's assumed
48    * you refer to a package or the name is already prefixed:
49    * \code
50    * Capability( "foo" )                   ==> 'foo'
51    * Capability( "foo", ResKind::package ) ==> 'foo'
52    * Capability( "foo", ResKind::pattern ) ==> 'pattern:foo'
53    * Capability( "pattern:foo" )           ==> 'pattern:foo'
54    * // in doubt an explicit name prefix wins:
55    * Capability( "pattern:foo", ResKind::package ) ==> 'pattern:foo'
56    * Capability( "package:foo", ResKind::pattern ) ==> 'foo'
57    * \endcode
58    */
59   class Capability: protected sat::detail::PoolMember
60   {
61     public:
62       enum CtorFlag { PARSED, UNPARSED };
63
64     public:
65       /** Default ctor, \ref Empty capability. */
66       Capability() : _id( sat::detail::emptyId ) {}
67
68       /** Ctor from id. */
69       explicit Capability( sat::detail::IdType id_r ) : _id( id_r ) {}
70
71       /** \name Ctors parsing a Capability: <tt>"name[.arch] [op edition]"</tt> or <tt>( arch, "name [op edition]")</tt>
72       */
73       //@{
74       /** Ctor from string.
75        * \a str_r is parsed to check whether it contains an <tt>[op edition]</tt> part,
76        * unless the \ref PARSED flag is passed to the ctor. In that case <tt>"name[.arch]"</tt>
77        * is assumed.
78       */
79       explicit Capability( const char * str_r, const ResKind & prefix_r = ResKind(), CtorFlag flag_r = UNPARSED );
80       /** \overload */
81       explicit Capability( const std::string & str_r, const ResKind & prefix_r = ResKind(), CtorFlag flag_r = UNPARSED );
82       /** \overload Explicitly specify the \c arch. */
83       Capability( const Arch & arch_r, const char * str_r, const ResKind & prefix_r = ResKind(), CtorFlag flag_r = UNPARSED );
84       /** \overload Explicitly specify the \c arch. */
85       Capability( const Arch & arch_r, const std::string & str_r, const ResKind & prefix_r = ResKind(), CtorFlag flag_r = UNPARSED );
86
87       /** \overload Convenience for parsed (name only, no <tt>"[op edition]</tt>) packages: <tt>Capability( "glibc", PARSED ); */
88       Capability( const char * str_r, CtorFlag flag_r, const ResKind & prefix_r = ResKind() );
89       /** \overload */
90       Capability( const std::string & str_r, CtorFlag flag_r, const ResKind & prefix_r = ResKind() );
91       /** \overload Explicitly specify the \c arch. */
92       Capability( const Arch & arch_r, const char * str_r, CtorFlag flag_r, const ResKind & prefix_r = ResKind() );
93       /** \overload */
94       Capability( const Arch & arch_r, const std::string & str_r, CtorFlag flag_r, const ResKind & prefix_r = ResKind() );
95       //@}
96
97
98       /** \name Ctors parsing a broken down Capability: <tt>( "name[.arch]", op, edition )</tt>
99       */
100       //@{
101       /** Ctor from <tt>name[.arch] op edition</tt>. */
102       Capability( const std::string & name_r, const std::string & op_r, const std::string & ed_r, const ResKind & prefix_r = ResKind() );
103       /** \overload */
104       Capability( const std::string & name_r, Rel op_r, const std::string & ed_r, const ResKind & prefix_r = ResKind() );
105       /** \overload */
106       Capability( const std::string & name_r, Rel op_r, const Edition & ed_r, const ResKind & prefix_r = ResKind() );
107       //@}
108
109       /** \name Ctors taking a broken down Capability: <tt>( arch, name, op, edition )</tt>
110       */
111       //@{
112       /** Ctor from <tt>arch name op edition</tt>. */
113       Capability( const std::string & arch_r, const std::string & name_r, const std::string & op_r, const std::string & ed_r, const ResKind & prefix_r = ResKind() );
114       /** \overload */
115       Capability( const std::string & arch_r, const std::string & name_r, Rel op_r, const std::string & ed_r, const ResKind & prefix_r = ResKind() );
116       /** \overload */
117       Capability( const std::string & arch_r, const std::string & name_r, Rel op_r, const Edition & ed_r, const ResKind & prefix_r = ResKind() );
118       /** \overload */
119       Capability( const Arch & arch_r, const std::string & name_r, const std::string & op_r, const std::string & ed_r, const ResKind & prefix_r = ResKind() );
120       /** \overload */
121       Capability( const Arch & arch_r, const std::string & name_r, Rel op_r, const std::string & ed_r, const ResKind & prefix_r = ResKind() );
122       /** \overload */
123       Capability( const Arch & arch_r, const std::string & name_r, Rel op_r, const Edition & ed_r, const ResKind & prefix_r = ResKind() );
124       //@}
125
126     public:
127       /** No or Null \ref Capability ( Id \c 0 ). */
128       static const Capability Null;
129
130       /** Empty Capability. */
131       static const Capability Empty;
132
133     public:
134       /** Evaluate in a boolean context <tt>( ! empty() )</tt>. */
135       explicit operator bool() const
136       { return ! empty(); }
137
138       /** Whether the \ref Capability is empty.
139        * This is true for \ref Null and \ref Empty.
140        */
141       bool empty() const
142       { return( _id == sat::detail::emptyId || _id == sat::detail::noId ); }
143
144     public:
145       /** Conversion to <tt>const char *</tt> */
146       const char * c_str() const;
147
148       /** \overload */
149       std::string asString() const
150       { return c_str(); }
151
152     public:
153       /** Helper providing more detailed information about a \ref Capability. */
154       CapDetail detail() const;
155
156     public:
157       /** \name Match two simple capabilities.
158        *
159        * Two simple capabilities match if they have the same \c name
160        * and their \c edition ranges overlap. Where no edition matches
161        * ANY edition. \see \ref Edition::match.
162        *
163        * If a capability expression is involved, \ref matches returns
164        * \ref CapMatch::irrelevant.
165        */
166       //@{
167       static CapMatch matches( const Capability & lhs,  const Capability & rhs )     { return _doMatch( lhs.id(), rhs.id() ); }
168       static CapMatch matches( const Capability & lhs,  const IdString & rhs )       { return _doMatch( lhs.id(), rhs.id() ); }
169       static CapMatch matches( const Capability & lhs,  const std::string & rhs )    { return _doMatch( lhs.id(), Capability(rhs).id() ); }
170       static CapMatch matches( const Capability & lhs,  const char * rhs )           { return _doMatch( lhs.id(), Capability(rhs).id() );}
171
172       static CapMatch matches( const IdString & lhs,    const Capability & rhs )     { return _doMatch( lhs.id(), rhs.id() ); }
173       static CapMatch matches( const IdString & lhs,    const IdString & rhs )       { return _doMatch( lhs.id(), rhs.id() ); }
174       static CapMatch matches( const IdString & lhs,    const std::string & rhs )    { return _doMatch( lhs.id(), Capability(rhs).id() ); }
175       static CapMatch matches( const IdString & lhs,    const char * rhs )           { return _doMatch( lhs.id(), Capability(rhs).id() ); }
176
177       static CapMatch matches( const std::string & lhs, const Capability & rhs )     { return _doMatch( Capability(lhs).id(), rhs.id() );}
178       static CapMatch matches( const std::string & lhs, const IdString & rhs )       { return _doMatch( Capability(lhs).id(), rhs.id() ); }
179       static CapMatch matches( const std::string & lhs, const std::string & rhs )    { return _doMatch( Capability(lhs).id(), Capability(rhs).id() ); }
180       static CapMatch matches( const std::string & lhs, const char * rhs )           { return _doMatch( Capability(lhs).id(), Capability(rhs).id() ); }
181
182       static CapMatch matches( const char * lhs,        const Capability & rhs )     { return _doMatch( Capability(lhs).id(), rhs.id() );}
183       static CapMatch matches( const char * lhs,        const IdString & rhs )       { return _doMatch( Capability(lhs).id(), rhs.id() ); }
184       static CapMatch matches( const char * lhs,        const std::string & rhs )    { return _doMatch( Capability(lhs).id(), Capability(rhs).id() ); }
185       static CapMatch matches( const char * lhs,        const char * rhs )           { return _doMatch( Capability(lhs).id(), Capability(rhs).id() ); }
186
187       CapMatch matches( const Capability & rhs )  const { return _doMatch( id(), rhs.id() ); }
188       CapMatch matches( const IdString & rhs )    const { return _doMatch( id(), rhs.id() ); }
189       CapMatch matches( const std::string & rhs ) const { return _doMatch( id(), Capability(rhs).id() ); }
190       CapMatch matches( const char * rhs )        const { return _doMatch( id(), Capability(rhs).id() ); }
191       //@}
192
193       /** \ref matches functor.
194        */
195       struct Matches: public std::binary_function<Capability,Capability,CapMatch>
196       {
197         CapMatch operator()( const Capability & lhs, const Capability & rhs ) const
198         { return Capability::matches( lhs, rhs ); }
199       };
200
201     public:
202       /** Test for a filename that is likely being REQUIRED.
203        * Files below \c /bin , \c /sbin ,  \c /lib etc. Scanning a
204        * packages filelist, an \e interesting filename might be worth
205        * being remembered in PROVIDES.
206        */
207       static bool isInterestingFileSpec( const IdString & name_r )    { return isInterestingFileSpec( name_r.c_str() ); }
208       static bool isInterestingFileSpec( const std::string & name_r ) { return isInterestingFileSpec( name_r.c_str() ); }
209       static bool isInterestingFileSpec( const char * name_r );
210
211       /** \ref Capability parser also guessing \c "libzypp-1.2.3-4.5.x86_64" formats.
212        *
213        * The argument might be in the form \c "libzypp-devel-1.2.3.x86_64".
214        * Passed to the Capability ctor, this would correctly be parsed as name
215        * capability, because actually the edition part had to be separated by a
216        * \c '=', and the architecture had to be appended to the name.
217        * So this is how it actually had to look like: \c "libzypp-devel.x86_64=1.2.3"
218        *
219        * Obviously we have to guess if, and where to split name and edition. In
220        * fact \c "devel" could also be the version and \c "1.2.3" would be the
221        * release then.
222        *
223        * Assuming this Capability should be provided by some package in
224        * the \ref ResPool, we check this. If unprovided, we substitute the last,
225        * (or one but last) \c '-' by a \c '='. If the name part (without version)
226        * of the resulting Capability matches a package name (not provides!) in
227        * the \ref ResPool, this Capability is returned.
228        *
229        * Otherwise we return the Capability originally created from
230        * \a str_r.
231        *
232        * \note: As this method will access the global pool, the returned
233        * result depends on the pools content.
234        */
235       static Capability guessPackageSpec( const std::string & str_r );
236       /** \overload Taking an additional bool indicating whether \c str_r made
237        * a valid \ref Capability (\c true) or the result was was guessed by
238        * rewiting a \c '-' to \c '='. (\c false).
239        */
240       static Capability guessPackageSpec( const std::string & str_r, bool & rewrote_r );
241
242     public:
243       /** Expert backdoor. */
244       sat::detail::IdType id() const
245       { return _id; }
246     private:
247       /** Match two Capabilities */
248       static CapMatch _doMatch( sat::detail::IdType lhs,  sat::detail::IdType rhs );
249     private:
250       sat::detail::IdType _id;
251   };
252   ///////////////////////////////////////////////////////////////////
253
254   /** \relates Capability Stream output */
255   std::ostream & operator<<( std::ostream & str, const Capability & obj );
256
257   /** \relates Capability Detailed stream output */
258   std::ostream & dumpOn( std::ostream & str, const Capability & obj );
259
260   /** \relates Capability */
261   inline bool operator==( const Capability & lhs, const Capability & rhs )
262   { return lhs.id() == rhs.id(); }
263
264   /** \relates Capability */
265   inline bool operator!=( const Capability & lhs, const Capability & rhs )
266   { return lhs.id() != rhs.id(); }
267
268   /** \relates Capability Arbitrary order. */
269   inline bool operator<( const Capability & lhs, const Capability & rhs )
270   { return lhs.id() < rhs.id(); }
271
272   ///////////////////////////////////////////////////////////////////
273   //
274   //    CLASS NAME : CapDetail
275   //
276   /** Helper providing more detailed information about a \ref Capability.
277    *
278    * Capabilities are classified to be either \c SIMPLE:
279    * \code
280    *   name[.arch] [op edition]
281    *   with op := <|<=|=|>=|>|!=
282    * \endcode
283    * or formed by some \c EXPRESSION:
284    * \code
285    *   left_cap op right_cap
286    *   with op := AND|OR|WITH|NAMESPACE
287    * \endcode
288    */
289   class CapDetail: protected sat::detail::PoolMember
290   {
291     public:
292       enum Kind
293       {
294         NOCAP      = 0x00,
295         NAMED      = 0x01,
296         VERSIONED  = 0x02,
297         EXPRESSION = 0x04
298       };
299
300       /** Enum values corresponding with libsolv defines.
301        * \note MPL check in PoolImpl.cc
302       */
303       enum CapRel
304       {
305         REL_NONE      = 0,
306         CAP_AND       = 16,
307         CAP_OR        = 17,
308         CAP_WITH      = 18,
309         CAP_NAMESPACE = 19,
310         CAP_ARCH      = 20
311       };
312
313     public:
314       CapDetail()
315       : _kind( NOCAP ), _lhs( 0 ), _rhs( 0 ), _flag( 0 ), _archIfSimple( 0 )
316       {}
317       explicit CapDetail( const Capability & cap_r )
318       : _kind( NOCAP ), _lhs( cap_r.id() ), _rhs( 0 ), _flag( 0 ), _archIfSimple( 0 )
319       { _init(); }
320       explicit CapDetail( sat::detail::IdType id_r )
321       : _kind( NOCAP ), _lhs( id_r ), _rhs( 0 ), _flag( 0 ), _archIfSimple( 0 )
322       { _init(); }
323
324     public:
325       Kind kind()         const { return _kind; }
326       bool isNull()       const { return _kind == NOCAP; }
327       bool isNamed()      const { return _kind == NAMED; }
328       bool isVersioned()  const { return _kind == VERSIONED; }
329       bool isSimple()     const { return _kind & (NAMED|VERSIONED); }
330       bool isExpression() const { return _kind == EXPRESSION; }
331
332       /** \name Is simple: <tt>name[.arch] [op edition]</tt> */
333       //@{
334       bool     hasArch()  const { return _archIfSimple; }
335       IdString arch()     const { return _archIfSimple ? IdString( _archIfSimple ) : IdString(); }
336       IdString name()     const { return isSimple()    ? IdString( _lhs ) : IdString(); }
337       Rel      op()       const { return isVersioned() ? Rel( _flag )     : Rel::ANY; }
338       Edition  ed()       const { return isVersioned() ? Edition( _rhs )  : Edition(); }
339       //@}
340
341       /** \name Is expression <tt>cap op cap</tt> */
342       //@{
343       Capability lhs()    const { return isExpression() ? Capability( _lhs ) : Capability::Null; }
344       CapRel     capRel() const { return isExpression() ? CapRel(_flag)      : REL_NONE; }
345       Capability rhs()    const { return isExpression() ? Capability( _rhs ) : Capability::Null; }
346      //@}
347
348     private:
349       void _init();
350     private:
351       Kind                _kind;
352       sat::detail::IdType _lhs;
353       sat::detail::IdType _rhs;
354       unsigned            _flag;
355       sat::detail::IdType _archIfSimple;
356   };
357   ///////////////////////////////////////////////////////////////////
358
359   /** \relates CapDetail Stream output */
360   std::ostream & operator<<( std::ostream & str, const CapDetail & obj );
361
362   /** \relates CapDetail Stream output */
363   std::ostream & operator<<( std::ostream & str, CapDetail::Kind obj );
364
365   /** \relates CapDetail Stream output */
366   std::ostream & operator<<( std::ostream & str, CapDetail::CapRel obj );
367
368   ///////////////////////////////////////////////////////////////////
369
370   inline CapDetail Capability::detail() const { return CapDetail( _id ); }
371
372   /////////////////////////////////////////////////////////////////
373 } // namespace zypp
374 ///////////////////////////////////////////////////////////////////
375
376 ZYPP_DEFINE_ID_HASHABLE( ::zypp::Capability );
377
378 #endif // ZYPP_CAPABILITY_H