1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/Capability.h
12 #ifndef ZYPP_CAPABILITY_H
13 #define ZYPP_CAPABILITY_H
17 #include "zypp/base/SafeBool.h"
18 #include "zypp/APIConfig.h"
20 #include "zypp/sat/detail/PoolMember.h"
22 #include "zypp/IdString.h"
23 #include "zypp/Edition.h"
25 #include "zypp/ResTraits.h"
27 #include "zypp/CapMatch.h"
29 ///////////////////////////////////////////////////////////////////
31 { /////////////////////////////////////////////////////////////////
37 typedef std::tr1::unordered_set<Capability> CapabilitySet;
39 ///////////////////////////////////////////////////////////////////
41 // CLASS NAME : Capability
45 * A Capability: <tt>"name[.arch] [op edition]"</tt>
47 * If a certain \ref ResKind is specified upon construction, the
48 * capabilities name part is prefixed, unless it already conatins a
49 * well known kind spec. If no \ref ResKind is specified, it's assumed
50 * you refer to a package or the name is already prefixed:
52 * Capability( "foo" ) ==> 'foo'
53 * Capability( "foo", ResKind::package ) ==> 'foo'
54 * Capability( "foo", ResKind::pattern ) ==> 'pattern:foo'
55 * Capability( "pattern:foo" ) ==> 'pattern:foo'
56 * // in doubt an explicit name prefix wins:
57 * Capability( "pattern:foo", ResKind::package ) ==> 'pattern:foo'
58 * Capability( "package:foo", ResKind::pattern ) ==> 'foo'
61 class Capability: protected sat::detail::PoolMember,
62 private base::SafeBool<Capability>
65 enum CtorFlag { PARSED, UNPARSED };
68 /** Default ctor, \ref Empty capability. */
69 Capability() : _id( sat::detail::emptyId ) {}
72 explicit Capability( sat::detail::IdType id_r ) : _id( id_r ) {}
74 /** \name Ctors parsing a Capability: <tt>"name[.arch] [op edition]"</tt> or <tt>( arch, "name [op edition]")</tt>
78 * \a str_r is parsed to check whether it contains an <tt>[op edition]</tt> part,
79 * unless the \ref PARSED flag is passed to the ctor. In that case <tt>"name[.arch]"</tt>
82 explicit Capability( const char * str_r, const ResKind & prefix_r = ResKind(), CtorFlag flag_r = UNPARSED );
84 explicit Capability( const std::string & str_r, const ResKind & prefix_r = ResKind(), CtorFlag flag_r = UNPARSED );
85 /** \overload Explicitly specify the \c arch. */
86 Capability( const Arch & arch_r, const char * str_r, const ResKind & prefix_r = ResKind(), CtorFlag flag_r = UNPARSED );
87 /** \overload Explicitly specify the \c arch. */
88 Capability( const Arch & arch_r, const std::string & str_r, const ResKind & prefix_r = ResKind(), CtorFlag flag_r = UNPARSED );
90 /** \overload Convenience for parsed (name only, no <tt>"[op edition]</tt>) packages: <tt>Capability( "glibc", PARSED ); */
91 Capability( const char * str_r, CtorFlag flag_r, const ResKind & prefix_r = ResKind() );
93 Capability( const std::string & str_r, CtorFlag flag_r, const ResKind & prefix_r = ResKind() );
94 /** \overload Explicitly specify the \c arch. */
95 Capability( const Arch & arch_r, const char * str_r, CtorFlag flag_r, const ResKind & prefix_r = ResKind() );
97 Capability( const Arch & arch_r, const std::string & str_r, CtorFlag flag_r, const ResKind & prefix_r = ResKind() );
101 /** \name Ctors parsing a broken down Capability: <tt>( "name[.arch]", op, edition )</tt>
104 /** Ctor from <tt>name[.arch] op edition</tt>. */
105 Capability( const std::string & name_r, const std::string & op_r, const std::string & ed_r, const ResKind & prefix_r = ResKind() );
107 Capability( const std::string & name_r, Rel op_r, const std::string & ed_r, const ResKind & prefix_r = ResKind() );
109 Capability( const std::string & name_r, Rel op_r, const Edition & ed_r, const ResKind & prefix_r = ResKind() );
112 /** \name Ctors taking a broken down Capability: <tt>( arch, name, op, edition )</tt>
115 /** Ctor from <tt>arch name op edition</tt>. */
116 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() );
118 Capability( const std::string & arch_r, const std::string & name_r, Rel op_r, const std::string & ed_r, const ResKind & prefix_r = ResKind() );
120 Capability( const std::string & arch_r, const std::string & name_r, Rel op_r, const Edition & ed_r, const ResKind & prefix_r = ResKind() );
122 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() );
124 Capability( const Arch & arch_r, const std::string & name_r, Rel op_r, const std::string & ed_r, const ResKind & prefix_r = ResKind() );
126 Capability( const Arch & arch_r, const std::string & name_r, Rel op_r, const Edition & ed_r, const ResKind & prefix_r = ResKind() );
130 /** No or Null \ref Capability ( Id \c 0 ). */
131 static const Capability Null;
133 /** Empty Capability. */
134 static const Capability Empty;
137 #ifndef SWIG // Swig treats it as syntax error
138 /** Evaluate in a boolean context <tt>( ! empty() )</tt>. */
139 using base::SafeBool<Capability>::operator bool_type;
141 /** Whether the \ref Capability is empty.
142 * This is true for \ref Null and \ref Empty.
145 { return( _id == sat::detail::emptyId || _id == sat::detail::noId ); }
148 /** Conversion to <tt>const char *</tt> */
149 const char * c_str() const;
152 std::string asString() const
156 /** Helper providing more detailed information about a \ref Capability. */
157 CapDetail detail() const;
160 /** \name Match two simple capabilities.
162 * Two simple capabilities match if they have the same \c name
163 * and their \c edition ranges overlap. Where no edition matches
164 * ANY edition. \see \ref Edition::match.
166 * If a capability expression is involved, \ref matches returns
167 * \ref CapMatch::irrelevant.
170 static CapMatch matches( const Capability & lhs, const Capability & rhs ) { return _doMatch( lhs.id(), rhs.id() ); }
171 static CapMatch matches( const Capability & lhs, const IdString & rhs ) { return _doMatch( lhs.id(), rhs.id() ); }
172 static CapMatch matches( const Capability & lhs, const std::string & rhs ) { return _doMatch( lhs.id(), Capability(rhs).id() ); }
173 static CapMatch matches( const Capability & lhs, const char * rhs ) { return _doMatch( lhs.id(), Capability(rhs).id() );}
175 static CapMatch matches( const IdString & lhs, const Capability & rhs ) { return _doMatch( lhs.id(), rhs.id() ); }
176 static CapMatch matches( const IdString & lhs, const IdString & rhs ) { return _doMatch( lhs.id(), rhs.id() ); }
177 static CapMatch matches( const IdString & lhs, const std::string & rhs ) { return _doMatch( lhs.id(), Capability(rhs).id() ); }
178 static CapMatch matches( const IdString & lhs, const char * rhs ) { return _doMatch( lhs.id(), Capability(rhs).id() ); }
180 static CapMatch matches( const std::string & lhs, const Capability & rhs ) { return _doMatch( Capability(lhs).id(), rhs.id() );}
181 static CapMatch matches( const std::string & lhs, const IdString & rhs ) { return _doMatch( Capability(lhs).id(), rhs.id() ); }
182 static CapMatch matches( const std::string & lhs, const std::string & rhs ) { return _doMatch( Capability(lhs).id(), Capability(rhs).id() ); }
183 static CapMatch matches( const std::string & lhs, const char * rhs ) { return _doMatch( Capability(lhs).id(), Capability(rhs).id() ); }
185 static CapMatch matches( const char * lhs, const Capability & rhs ) { return _doMatch( Capability(lhs).id(), rhs.id() );}
186 static CapMatch matches( const char * lhs, const IdString & rhs ) { return _doMatch( Capability(lhs).id(), rhs.id() ); }
187 static CapMatch matches( const char * lhs, const std::string & rhs ) { return _doMatch( Capability(lhs).id(), Capability(rhs).id() ); }
188 static CapMatch matches( const char * lhs, const char * rhs ) { return _doMatch( Capability(lhs).id(), Capability(rhs).id() ); }
190 CapMatch matches( const Capability & rhs ) const { return _doMatch( id(), rhs.id() ); }
191 CapMatch matches( const IdString & rhs ) const { return _doMatch( id(), rhs.id() ); }
192 CapMatch matches( const std::string & rhs ) const { return _doMatch( id(), Capability(rhs).id() ); }
193 CapMatch matches( const char * rhs ) const { return _doMatch( id(), Capability(rhs).id() ); }
196 /** \ref matches functor.
198 struct Matches: public std::binary_function<Capability,Capability,CapMatch>
200 CapMatch operator()( const Capability & lhs, const Capability & rhs ) const
201 { return Capability::matches( lhs, rhs ); }
205 /** Test for a filename that is likely being REQUIRED.
206 * Files below \c /bin , \c /sbin , \c /lib etc. Scanning a
207 * packages filelist, an \e interesting filename might be worth
208 * being remembered in PROVIDES.
210 static bool isInterestingFileSpec( const IdString & name_r ) { return isInterestingFileSpec( name_r.c_str() ); }
211 static bool isInterestingFileSpec( const std::string & name_r ) { return isInterestingFileSpec( name_r.c_str() ); }
212 static bool isInterestingFileSpec( const char * name_r );
214 /** \ref Capability parser also guessing \c "libzypp-1.2.3-4.5.x86_64" formats.
216 * The argument might be in the form \c "libzypp-devel-1.2.3.x86_64".
217 * Passed to the Capability ctor, this would correctly be parsed as name
218 * capability, because actually the edition part had to be separated by a
219 * \c '=', and the architecture had to be appended to the name.
220 * So this is how it actually had to look like: \c "libzypp-devel.x86_64=1.2.3"
222 * Obviously we have to guess if, and where to split name and edition. In
223 * fact \c "devel" could also be the version and \c "1.2.3" would be the
226 * Assuming this Capability should be provided by some package in
227 * the \ref ResPool, we check this. If unprovided, we substitute the last,
228 * (or one but last) \c '-' by a \c '='. If the name part (without version)
229 * of the resulting Capability matches a package name (not provides!) in
230 * the \ref ResPool, this Capability is returned.
232 * Otherwise we return the Capability originally created from
235 * \note: As this method will access the global pool, the returned
236 * result depends on the pools content.
238 static Capability guessPackageSpec( const std::string & str_r );
239 /** \overload Taking an additional bool indicating whether \c str_r made
240 * a valid \ref Capability (\c true) or the result was was guessed by
241 * rewiting a \c '-' to \c '='. (\c false).
243 static Capability guessPackageSpec( const std::string & str_r, bool & rewrote_r );
246 /** Expert backdoor. */
247 sat::detail::IdType id() const
250 /** Match two Capabilities */
251 static CapMatch _doMatch( sat::detail::IdType lhs, sat::detail::IdType rhs );
253 #ifndef SWIG // Swig treats it as syntax error
254 friend base::SafeBool<Capability>::operator bool_type() const;
256 bool boolTest() const { return ! empty(); }
258 sat::detail::IdType _id;
260 ///////////////////////////////////////////////////////////////////
262 /** \relates Capability Stream output */
263 std::ostream & operator<<( std::ostream & str, const Capability & obj );
265 /** \relates Capability Detailed stream output */
266 std::ostream & dumpOn( std::ostream & str, const Capability & obj );
268 /** \relates Capability */
269 inline bool operator==( const Capability & lhs, const Capability & rhs )
270 { return lhs.id() == rhs.id(); }
272 /** \relates Capability */
273 inline bool operator!=( const Capability & lhs, const Capability & rhs )
274 { return lhs.id() != rhs.id(); }
276 /** \relates Capability Arbitrary order. */
277 inline bool operator<( const Capability & lhs, const Capability & rhs )
278 { return lhs.id() < rhs.id(); }
280 ///////////////////////////////////////////////////////////////////
282 // CLASS NAME : CapDetail
284 /** Helper providing more detailed information about a \ref Capability.
286 * Capabilities are classified to be either \c SIMPLE:
288 * name[.arch] [op edition]
289 * with op := <|<=|=|>=|>|!=
291 * or formed by some \c EXPRESSION:
293 * left_cap op right_cap
294 * with op := AND|OR|WITH|NAMESPACE
297 class CapDetail: protected sat::detail::PoolMember
308 /** Enum values corresponding with libsolv defines.
309 * \note MPL check in PoolImpl.cc
323 : _kind( NOCAP ), _lhs( 0 ), _rhs( 0 ), _flag( 0 ), _archIfSimple( 0 )
325 explicit CapDetail( const Capability & cap_r )
326 : _kind( NOCAP ), _lhs( cap_r.id() ), _rhs( 0 ), _flag( 0 ), _archIfSimple( 0 )
328 explicit CapDetail( sat::detail::IdType id_r )
329 : _kind( NOCAP ), _lhs( id_r ), _rhs( 0 ), _flag( 0 ), _archIfSimple( 0 )
333 Kind kind() const { return _kind; }
334 bool isNull() const { return _kind == NOCAP; }
335 bool isNamed() const { return _kind == NAMED; }
336 bool isVersioned() const { return _kind == VERSIONED; }
337 bool isSimple() const { return _kind & (NAMED|VERSIONED); }
338 bool isExpression() const { return _kind == EXPRESSION; }
340 /** \name Is simple: <tt>name[.arch] [op edition]</tt> */
342 bool hasArch() const { return _archIfSimple; }
343 IdString arch() const { return _archIfSimple ? IdString( _archIfSimple ) : IdString(); }
344 IdString name() const { return isSimple() ? IdString( _lhs ) : IdString(); }
345 Rel op() const { return isVersioned() ? Rel( _flag ) : Rel::ANY; }
346 Edition ed() const { return isVersioned() ? Edition( _rhs ) : Edition(); }
349 /** \name Is expression <tt>cap op cap</tt> */
351 Capability lhs() const { return isExpression() ? Capability( _lhs ) : Capability::Null; }
352 CapRel capRel() const { return isExpression() ? CapRel(_flag) : REL_NONE; }
353 Capability rhs() const { return isExpression() ? Capability( _rhs ) : Capability::Null; }
360 sat::detail::IdType _lhs;
361 sat::detail::IdType _rhs;
363 sat::detail::IdType _archIfSimple;
365 ///////////////////////////////////////////////////////////////////
367 /** \relates CapDetail Stream output */
368 std::ostream & operator<<( std::ostream & str, const CapDetail & obj );
370 /** \relates CapDetail Stream output */
371 std::ostream & operator<<( std::ostream & str, CapDetail::Kind obj );
373 /** \relates CapDetail Stream output */
374 std::ostream & operator<<( std::ostream & str, CapDetail::CapRel obj );
376 ///////////////////////////////////////////////////////////////////
378 inline CapDetail Capability::detail() const { return CapDetail( _id ); }
380 /////////////////////////////////////////////////////////////////
382 ///////////////////////////////////////////////////////////////////
384 ZYPP_DEFINE_ID_HASHABLE( ::zypp::Capability );
386 #endif // ZYPP_CAPABILITY_H