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/base/Deprecated.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 { /////////////////////////////////////////////////////////////////
36 typedef std::tr1::unordered_set<Capability> CapabilitySet;
38 ///////////////////////////////////////////////////////////////////
40 // CLASS NAME : Capability
44 * A Capability: <tt>"name[.arch] [op edition]"</tt>
46 * If a certain \ref ResKind is specified upon construction, the
47 * capabilities name part is prefixed accordingly. If no \ref ResKind
48 * is specified, it's assumed you refer to a package or the name is
51 * Capability( "foo" ) ==> 'foo'
52 * Capability( "foo", ResKind::package ) ==> 'foo'
53 * Capability( "foo", ResKind::pattern ) ==> 'pattern:foo'
54 * Capability( "pattern:foo" ) ==> 'pattern:foo'
56 * Capability( "pattern:foo", ResKind::pattern ) ==> 'pattern:pattern:foo'
59 class Capability: protected sat::detail::PoolMember,
60 private base::SafeBool<Capability>
63 enum CtorFlag { PARSED, UNPARSED };
66 /** Default ctor, \ref Empty capability. */
67 Capability() : _id( sat::detail::emptyId ) {}
70 explicit Capability( sat::detail::IdType id_r ) : _id( id_r ) {}
72 /** \name Ctors parsing a Capability: <tt>"name[.arch] [op edition]"</tt>
76 * \a str_r is parsed to check whether it contains an <tt>[op edition]</tt> part,
77 * unless the \ref PARSED flag is passed to the ctor. In that case <tt>"name[.arch]"</tt>
80 explicit Capability( const char * str_r, const ResKind & prefix_r = ResKind(), CtorFlag flag_r = UNPARSED );
82 explicit Capability( const std::string & str_r, const ResKind & prefix_r = ResKind(), CtorFlag flag_r = UNPARSED );
83 /** \overload Convenience for parsed (name only) packages. */
84 Capability( const char * str_r, CtorFlag flag_r, const ResKind & prefix_r = ResKind() );
86 Capability( const std::string & str_r, CtorFlag flag_r, const ResKind & prefix_r = ResKind() );
89 /** \name Ctors parsing a broken down Capability: <tt>( "name[.arch]", op, edition )</tt>
92 /** Ctor from <tt>name[.arch] op edition</tt>. */
93 Capability( const std::string & name_r, const std::string & op_r, const std::string & ed_r, const ResKind & prefix_r = ResKind() );
95 Capability( const std::string & name_r, Rel op_r, const std::string & ed_r, const ResKind & prefix_r = ResKind() );
97 Capability( const std::string & name_r, Rel op_r, const Edition & ed_r, const ResKind & prefix_r = ResKind() );
101 /** \name Ctors taking a broken down Capability: <tt>( arch, name, op, edition )</tt>
104 /** Ctor from <tt>arch name op edition</tt>. */
105 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() );
107 Capability( const std::string & arch_r, const std::string & name_r, Rel op_r, const std::string & ed_r, const ResKind & prefix_r = ResKind() );
109 Capability( const std::string & arch_r, const std::string & name_r, Rel op_r, const Edition & ed_r, const ResKind & prefix_r = ResKind() );
111 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() );
113 Capability( const Arch & arch_r, const std::string & name_r, Rel op_r, const std::string & ed_r, const ResKind & prefix_r = ResKind() );
115 Capability( const Arch & arch_r, const std::string & name_r, Rel op_r, const Edition & ed_r, const ResKind & prefix_r = ResKind() );
120 /** No or Null \ref Capability ( Id \c 0 ). */
121 static const Capability Null;
123 /** Empty Capability. */
124 static const Capability Empty;
127 /** Evaluate in a boolean context <tt>( ! empty() )</tt>. */
128 using base::SafeBool<Capability>::operator bool_type;
130 /** Whether the \ref Capability is empty.
131 * This is true for \ref Null and \ref Empty.
134 { return( _id == sat::detail::emptyId || _id == sat::detail::noId ); }
137 /** Conversion to <tt>const char *</tt> */
138 const char * c_str() const;
141 std::string asString() const
145 /** Helper providing more detailed information about a \ref Capability. */
146 CapDetail detail() const;
149 /** \name Match two simple capabilities.
151 * Two simple capabilities match if they have the same \c name
152 * and their \c edition ranges overlap. Where no edition matches
153 * ANY edition. \see \ref Edition::match.
155 * If a capability expression is involved, \ref matches returns
156 * \ref CapMatch::irrelevant.
158 * \todo check whether we must promote string to Capability in order to match.
161 static CapMatch matches( const Capability & lhs, const Capability & rhs ) { return _doMatch( lhs.id(), rhs.id() ); }
162 static CapMatch matches( const Capability & lhs, const IdString & rhs ) { return _doMatch( lhs.id(), rhs.id() ); }
163 static CapMatch matches( const Capability & lhs, const std::string & rhs ) { return _doMatch( lhs.id(), Capability(rhs).id() ); }
164 static CapMatch matches( const Capability & lhs, const char * rhs ) { return _doMatch( lhs.id(), Capability(rhs).id() );}
166 static CapMatch matches( const IdString & lhs, const Capability & rhs ) { return _doMatch( lhs.id(), rhs.id() ); }
167 static CapMatch matches( const IdString & lhs, const IdString & rhs ) { return _doMatch( lhs.id(), rhs.id() ); }
168 static CapMatch matches( const IdString & lhs, const std::string & rhs ) { return _doMatch( lhs.id(), Capability(rhs).id() ); }
169 static CapMatch matches( const IdString & lhs, const char * rhs ) { return _doMatch( lhs.id(), Capability(rhs).id() ); }
171 static CapMatch matches( const std::string & lhs, const Capability & rhs ) { return _doMatch( Capability(lhs).id(), rhs.id() );}
172 static CapMatch matches( const std::string & lhs, const IdString & rhs ) { return _doMatch( Capability(lhs).id(), rhs.id() ); }
173 static CapMatch matches( const std::string & lhs, const std::string & rhs ) { return _doMatch( Capability(lhs).id(), Capability(rhs).id() ); }
174 static CapMatch matches( const std::string & lhs, const char * rhs ) { return _doMatch( Capability(lhs).id(), Capability(rhs).id() ); }
176 static CapMatch matches( const char * lhs, const Capability & rhs ) { return _doMatch( Capability(lhs).id(), rhs.id() );}
177 static CapMatch matches( const char * lhs, const IdString & rhs ) { return _doMatch( Capability(lhs).id(), rhs.id() ); }
178 static CapMatch matches( const char * lhs, const std::string & rhs ) { return _doMatch( Capability(lhs).id(), Capability(rhs).id() ); }
179 static CapMatch matches( const char * lhs, const char * rhs ) { return _doMatch( Capability(lhs).id(), Capability(rhs).id() ); }
181 CapMatch matches( const Capability & rhs ) const { return _doMatch( id(), rhs.id() ); }
182 CapMatch matches( const IdString & rhs ) const { return _doMatch( id(), rhs.id() ); }
183 CapMatch matches( const std::string & rhs ) const { return _doMatch( id(), Capability(rhs).id() ); }
184 CapMatch matches( const char * rhs ) const { return _doMatch( id(), Capability(rhs).id() ); }
187 /** \ref matches functor.
189 struct Matches: public std::binary_function<Capability,Capability,CapMatch>
191 CapMatch operator()( const Capability & lhs, const Capability & rhs ) const
192 { return Capability::matches( lhs, rhs ); }
196 /** Test for a filename that is likely being REQUIRED.
197 * Files below \c /bin , \c /sbin , \c /lib etc. Scanning a
198 * packages filelist, an \e interesting filename might be worth
199 * being remembered in PROVIDES.
201 static bool isInterestingFileSpec( const IdString & name_r ) { return isInterestingFileSpec( name_r.c_str() ); }
202 static bool isInterestingFileSpec( const std::string & name_r ) { return isInterestingFileSpec( name_r.c_str() ); }
203 static bool isInterestingFileSpec( const char * name_r );
206 /** Expert backdoor. */
207 sat::detail::IdType id() const
210 /** Match two Capabilities */
211 static CapMatch _doMatch( sat::detail::IdType lhs, sat::detail::IdType rhs );
213 friend base::SafeBool<Capability>::operator bool_type() const;
214 bool boolTest() const { return ! empty(); }
216 sat::detail::IdType _id;
218 ///////////////////////////////////////////////////////////////////
220 /** \relates Capability Stream output */
221 std::ostream & operator<<( std::ostream & str, const Capability & obj );
223 /** \relates Capability Detailed stream output */
224 std::ostream & dumpOn( std::ostream & str, const Capability & obj );
226 /** \relates Capability */
227 inline bool operator==( const Capability & lhs, const Capability & rhs )
228 { return lhs.id() == rhs.id(); }
230 /** \relates Capability */
231 inline bool operator!=( const Capability & lhs, const Capability & rhs )
232 { return lhs.id() != rhs.id(); }
234 /** \relates Capability Arbitrary order. */
235 inline bool operator<( const Capability & lhs, const Capability & rhs )
236 { return lhs.id() < rhs.id(); }
238 ///////////////////////////////////////////////////////////////////
240 // CLASS NAME : CapDetail
242 /** Helper providing more detailed information about a \ref Capability.
244 class CapDetail: protected sat::detail::PoolMember
255 /** Enum values corresponding with libsatsolver defines.
256 * MPL check in PoolImpl.cc
269 : _kind( NOCAP ), _lhs( 0 ), _rhs( 0 ), _flag( 0 )
271 explicit CapDetail( const Capability & cap_r )
272 : _kind( NOCAP ), _lhs( cap_r.id() ), _rhs( 0 ), _flag( 0 )
274 explicit CapDetail( sat::detail::IdType id_r )
275 : _kind( NOCAP ), _lhs( id_r ), _rhs( 0 ), _flag( 0 )
279 Kind kind() const { return _kind; }
280 bool isNull() const { return _kind == NOCAP; }
281 bool isNamed() const { return _kind == NAMED; }
282 bool isVersioned() const { return _kind == VERSIONED; }
283 bool isSimple() const { return _kind & (NAMED|VERSIONED); }
284 bool isExpression() const { return _kind == EXPRESSION; }
286 /** \name Is simple: <tt>name [op edition]</tt> */
288 IdString name() const { return isSimple() ? IdString( _lhs ) : IdString(); }
289 Rel op() const { return isVersioned() ? Rel( _flag ) : Rel::ANY; }
290 Edition ed() const { return isVersioned() ? Edition( _rhs ) : Edition(); }
293 /** \name Is expression <tt>cap op cap</tt> */
295 Capability lhs() const { return isExpression() ? Capability( _lhs ) : Capability::Null; }
296 CapRel capRel() const { return isExpression() ? CapRel(_flag) : REL_NONE; }
297 Capability rhs() const { return isExpression() ? Capability( _rhs ) : Capability::Null; }
304 sat::detail::IdType _lhs;
305 sat::detail::IdType _rhs;
308 ///////////////////////////////////////////////////////////////////
310 /** \relates CapDetail Stream output */
311 std::ostream & operator<<( std::ostream & str, const CapDetail & obj );
313 /** \relates CapDetail Stream output */
314 std::ostream & operator<<( std::ostream & str, CapDetail::Kind obj );
316 /** \relates CapDetail Stream output */
317 std::ostream & operator<<( std::ostream & str, CapDetail::CapRel obj );
319 ///////////////////////////////////////////////////////////////////
321 inline CapDetail Capability::detail() const { return CapDetail( _id ); }
323 /////////////////////////////////////////////////////////////////
325 ///////////////////////////////////////////////////////////////////
327 ZYPP_DEFINE_ID_HASHABLE( ::zypp::Capability )
329 #endif // ZYPP_CAPABILITY_H