1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/Capability.h
12 #ifndef ZYPP_CAPABILITY_H
13 #define ZYPP_CAPABILITY_H
17 #include "zypp/APIConfig.h"
18 #include "zypp/sat/detail/PoolMember.h"
20 #include "zypp/IdString.h"
21 #include "zypp/Edition.h"
23 #include "zypp/ResTraits.h"
25 #include "zypp/CapMatch.h"
27 ///////////////////////////////////////////////////////////////////
29 { /////////////////////////////////////////////////////////////////
35 typedef std::tr1::unordered_set<Capability> CapabilitySet;
37 ///////////////////////////////////////////////////////////////////
39 // CLASS NAME : Capability
43 * A Capability: <tt>"name[.arch] [op edition]"</tt>
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:
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'
59 class Capability: protected sat::detail::PoolMember
62 enum CtorFlag { PARSED, UNPARSED };
65 /** Default ctor, \ref Empty capability. */
66 Capability() : _id( sat::detail::emptyId ) {}
69 explicit Capability( sat::detail::IdType id_r ) : _id( id_r ) {}
71 /** \name Ctors parsing a Capability: <tt>"name[.arch] [op edition]"</tt> or <tt>( arch, "name [op edition]")</tt>
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>
79 explicit Capability( const char * str_r, const ResKind & prefix_r = ResKind(), CtorFlag flag_r = UNPARSED );
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 );
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() );
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() );
94 Capability( const Arch & arch_r, const std::string & str_r, CtorFlag flag_r, const ResKind & prefix_r = ResKind() );
98 /** \name Ctors parsing a broken down Capability: <tt>( "name[.arch]", op, edition )</tt>
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() );
104 Capability( const std::string & name_r, Rel op_r, const std::string & ed_r, const ResKind & prefix_r = ResKind() );
106 Capability( const std::string & name_r, Rel op_r, const Edition & ed_r, const ResKind & prefix_r = ResKind() );
109 /** \name Ctors taking a broken down Capability: <tt>( arch, name, op, edition )</tt>
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() );
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() );
117 Capability( const std::string & arch_r, const std::string & name_r, Rel op_r, const Edition & ed_r, const ResKind & prefix_r = ResKind() );
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() );
121 Capability( const Arch & arch_r, const std::string & name_r, Rel op_r, const std::string & ed_r, const ResKind & prefix_r = ResKind() );
123 Capability( const Arch & arch_r, const std::string & name_r, Rel op_r, const Edition & ed_r, const ResKind & prefix_r = ResKind() );
127 /** No or Null \ref Capability ( Id \c 0 ). */
128 static const Capability Null;
130 /** Empty Capability. */
131 static const Capability Empty;
134 /** Evaluate in a boolean context <tt>( ! empty() )</tt>. */
135 explicit operator bool() const
136 { return ! empty(); }
138 /** Whether the \ref Capability is empty.
139 * This is true for \ref Null and \ref Empty.
142 { return( _id == sat::detail::emptyId || _id == sat::detail::noId ); }
145 /** Conversion to <tt>const char *</tt> */
146 const char * c_str() const;
149 std::string asString() const
153 /** Helper providing more detailed information about a \ref Capability. */
154 CapDetail detail() const;
157 /** \name Match two simple capabilities.
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.
163 * If a capability expression is involved, \ref matches returns
164 * \ref CapMatch::irrelevant.
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() );}
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() ); }
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() ); }
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() ); }
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() ); }
193 /** \ref matches functor.
195 struct Matches: public std::binary_function<Capability,Capability,CapMatch>
197 CapMatch operator()( const Capability & lhs, const Capability & rhs ) const
198 { return Capability::matches( lhs, rhs ); }
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.
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 );
211 /** \ref Capability parser also guessing \c "libzypp-1.2.3-4.5.x86_64" formats.
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"
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
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.
229 * Otherwise we return the Capability originally created from
232 * \note: As this method will access the global pool, the returned
233 * result depends on the pools content.
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).
240 static Capability guessPackageSpec( const std::string & str_r, bool & rewrote_r );
243 /** Expert backdoor. */
244 sat::detail::IdType id() const
247 /** Match two Capabilities */
248 static CapMatch _doMatch( sat::detail::IdType lhs, sat::detail::IdType rhs );
250 sat::detail::IdType _id;
252 ///////////////////////////////////////////////////////////////////
254 /** \relates Capability Stream output */
255 std::ostream & operator<<( std::ostream & str, const Capability & obj );
257 /** \relates Capability Detailed stream output */
258 std::ostream & dumpOn( std::ostream & str, const Capability & obj );
260 /** \relates Capability */
261 inline bool operator==( const Capability & lhs, const Capability & rhs )
262 { return lhs.id() == rhs.id(); }
264 /** \relates Capability */
265 inline bool operator!=( const Capability & lhs, const Capability & rhs )
266 { return lhs.id() != rhs.id(); }
268 /** \relates Capability Arbitrary order. */
269 inline bool operator<( const Capability & lhs, const Capability & rhs )
270 { return lhs.id() < rhs.id(); }
272 ///////////////////////////////////////////////////////////////////
274 // CLASS NAME : CapDetail
276 /** Helper providing more detailed information about a \ref Capability.
278 * Capabilities are classified to be either \c SIMPLE:
280 * name[.arch] [op edition]
281 * with op := <|<=|=|>=|>|!=
283 * or formed by some \c EXPRESSION:
285 * left_cap op right_cap
286 * with op := AND|OR|WITH|NAMESPACE
289 class CapDetail: protected sat::detail::PoolMember
300 /** Enum values corresponding with libsolv defines.
301 * \note MPL check in PoolImpl.cc
315 : _kind( NOCAP ), _lhs( 0 ), _rhs( 0 ), _flag( 0 ), _archIfSimple( 0 )
317 explicit CapDetail( const Capability & cap_r )
318 : _kind( NOCAP ), _lhs( cap_r.id() ), _rhs( 0 ), _flag( 0 ), _archIfSimple( 0 )
320 explicit CapDetail( sat::detail::IdType id_r )
321 : _kind( NOCAP ), _lhs( id_r ), _rhs( 0 ), _flag( 0 ), _archIfSimple( 0 )
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; }
332 /** \name Is simple: <tt>name[.arch] [op edition]</tt> */
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(); }
341 /** \name Is expression <tt>cap op cap</tt> */
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; }
352 sat::detail::IdType _lhs;
353 sat::detail::IdType _rhs;
355 sat::detail::IdType _archIfSimple;
357 ///////////////////////////////////////////////////////////////////
359 /** \relates CapDetail Stream output */
360 std::ostream & operator<<( std::ostream & str, const CapDetail & obj );
362 /** \relates CapDetail Stream output */
363 std::ostream & operator<<( std::ostream & str, CapDetail::Kind obj );
365 /** \relates CapDetail Stream output */
366 std::ostream & operator<<( std::ostream & str, CapDetail::CapRel obj );
368 ///////////////////////////////////////////////////////////////////
370 inline CapDetail Capability::detail() const { return CapDetail( _id ); }
372 /////////////////////////////////////////////////////////////////
374 ///////////////////////////////////////////////////////////////////
376 ZYPP_DEFINE_ID_HASHABLE( ::zypp::Capability );
378 #endif // ZYPP_CAPABILITY_H