00001
00002
00003
00004
00005
00006
00007
00008
00012 #include <iostream>
00013 #include <functional>
00014 #include <set>
00015 #include <map>
00016
00017 #include <ext/hash_set>
00018 #include <ext/hash_fun.h>
00019
00020 #include "zypp/base/Logger.h"
00021 #include "zypp/base/Exception.h"
00022 #include "zypp/base/String.h"
00023 #include "zypp/base/Counter.h"
00024
00025 #include "zypp/CapFactory.h"
00026 #include "zypp/capability/Capabilities.h"
00027
00028 using std::endl;
00029
00031 namespace
00032 {
00033 using ::zypp::Resolvable;
00034 using ::zypp::capability::CapabilityImpl;
00035 using ::zypp::capability::CapImplOrder;
00036
00037 struct CapImplHashFun
00038 {
00039 size_t operator() ( const CapabilityImpl::Ptr & p ) const
00040 {
00041 return __gnu_cxx::hash<const char*>()( p->encode().c_str() );
00042 }
00043 };
00044
00045 struct CapImplHashEqual
00046 {
00047 bool operator() ( const CapabilityImpl::Ptr & lhs, const CapabilityImpl::Ptr & rhs ) const
00048 {
00049 return ( lhs->encode() == rhs->encode()
00050 && lhs->kind() == rhs->kind()
00051 && lhs->refers() == rhs->refers() );
00052 }
00053 };
00054
00056
00057 typedef __gnu_cxx::hash_set<CapabilityImpl::Ptr, CapImplHashFun, CapImplHashEqual> USet;
00058
00059
00066 USet _uset;
00067
00077 CapabilityImpl::Ptr usetInsert( CapabilityImpl::Ptr cap_ptr )
00078 {
00079 return *(_uset.insert( cap_ptr ).first);
00080 }
00081
00085 struct USetStatsCollect : public std::unary_function<CapabilityImpl::constPtr, void>
00086 {
00087 typedef ::zypp::Counter<unsigned> Counter;
00088
00089 Counter _caps;
00090 std::map<CapabilityImpl::Kind,Counter> _capKind;
00091 std::map<Resolvable::Kind,Counter> _capRefers;
00092
00093 void operator()( const CapabilityImpl::constPtr & cap_r )
00094 {
00095
00096 ++_caps;
00097 ++(_capKind[cap_r->kind()]);
00098 ++(_capRefers[cap_r->refers()]);
00099 }
00100
00101 std::ostream & dumpOn( std::ostream & str ) const
00102 {
00103 str << " Capabilities total: " << _caps << endl;
00104 str << " Capability kinds:" << endl;
00105 for ( std::map<CapabilityImpl::Kind,Counter>::const_iterator it = _capKind.begin();
00106 it != _capKind.end(); ++it )
00107 {
00108 str << " " << it->first << '\t' << it->second << endl;
00109 }
00110 str << " Capability refers:" << endl;
00111 for ( std::map<Resolvable::Kind,Counter>::const_iterator it = _capRefers.begin();
00112 it != _capRefers.end(); ++it )
00113 {
00114 str << " " << it->first << '\t' << it->second << endl;
00115 }
00116 return str;
00117 }
00118 };
00119
00121 }
00123
00125 namespace zypp
00126 {
00127
00129
00130
00131
00149 struct CapFactory::Impl
00150 {
00151
00152 };
00154
00156
00158
00159
00160
00162
00164
00165
00166
00167
00168 CapFactory::CapFactory()
00169 {}
00170
00172
00173
00174
00175
00176 CapFactory::~CapFactory()
00177 {}
00178
00180
00181
00182
00183
00184 Capability CapFactory::fromImpl( capability::CapabilityImpl::Ptr impl ) const
00185 try
00186 {
00187 return Capability( usetInsert( impl ) );
00188 }
00189 catch ( Exception & excpt )
00190 {
00191 ZYPP_RETHROW( excpt );
00192 return Capability();
00193 }
00194
00196
00197
00198
00199
00200 Capability CapFactory::parse( const Resolvable::Kind & refers_r,
00201 const std::string & strval_r ) const
00202 try
00203 {
00204 return Capability( usetInsert( ::zypp::capability::parse( refers_r, strval_r ) ) );
00205 }
00206 catch ( Exception & excpt )
00207 {
00208 ZYPP_RETHROW( excpt );
00209 return Capability();
00210 }
00211
00213
00214
00215
00216
00217 Capability CapFactory::parse( const Resolvable::Kind & refers_r,
00218 const std::string & name_r,
00219 const std::string & op_r,
00220 const std::string & edition_r ) const
00221 try
00222 {
00223 return Capability( usetInsert(::zypp::capability::parse( refers_r, name_r, op_r, edition_r ) ) );
00224 }
00225 catch ( Exception & excpt )
00226 {
00227 ZYPP_RETHROW( excpt );
00228 return Capability();
00229 }
00230
00232
00233
00234
00235
00236 Capability CapFactory::parse( const Resolvable::Kind & refers_r,
00237 const std::string & name_r,
00238 Rel op_r,
00239 const Edition & edition_r ) const
00240 try
00241 {
00242 return Capability( usetInsert(::zypp::capability::parse( refers_r, name_r, op_r, edition_r )) );
00243 }
00244 catch ( Exception & excpt )
00245 {
00246 ZYPP_RETHROW( excpt );
00247 return Capability();
00248 }
00249
00251
00252
00253
00254
00255 Capability CapFactory::halEvalCap() const
00256 try
00257 {
00258 return Capability( usetInsert( ::zypp::capability::buildHal( Resolvable::Kind(), "hal()" ) ) );
00259 }
00260 catch ( Exception & excpt )
00261 {
00262 ZYPP_RETHROW( excpt );
00263 return Capability();
00264 }
00265
00267
00268
00269
00270
00271 Capability CapFactory::modaliasEvalCap() const
00272 try
00273 {
00274 return Capability( usetInsert( ::zypp::capability::buildModalias( Resolvable::Kind(), "modalias()" ) ) );
00275 }
00276 catch ( Exception & excpt )
00277 {
00278 ZYPP_RETHROW( excpt );
00279 return Capability();
00280 }
00281
00283
00284
00285
00286
00287 Capability CapFactory::filesystemEvalCap() const
00288 try
00289 {
00290 return Capability( usetInsert( ::zypp::capability::buildFilesystem( Resolvable::Kind(), "filesystem()" ) ) );
00291 }
00292 catch ( Exception & excpt )
00293 {
00294 ZYPP_RETHROW( excpt );
00295 return Capability();
00296 }
00297
00299
00300
00301
00302
00303 std::string CapFactory::encode( const Capability & cap_r ) const
00304 {
00305 return cap_r._pimpl->encode();
00306 }
00307
00308
00309
00310
00311
00312
00313 std::ostream & operator<<( std::ostream & str, const CapFactory & obj )
00314 {
00315 str << "CapFactory stats:" << endl;
00316
00317 return for_each( _uset.begin(), _uset.end(), USetStatsCollect() ).dumpOn( str );
00318 }
00319
00321 }