CapFactory.cc

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
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   //typedef std::set<CapabilityImpl::Ptr,CapImplOrder> USet;
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       //DBG << *cap_r << endl;
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 } // namespace
00123 
00125 namespace zypp
00126 { 
00127 
00129   //
00130   //    CLASS NAME : CapFactoryImpl
00131   //
00149   struct CapFactory::Impl
00150   {
00151 
00152   };
00154 
00156 
00158   //
00159   //    CLASS NAME : CapFactory
00160   //
00162 
00164   //
00165   //    METHOD NAME : CapFactory::CapFactory
00166   //    METHOD TYPE : Ctor
00167   //
00168   CapFactory::CapFactory()
00169   {}
00170 
00172   //
00173   //    METHOD NAME : CapFactory::~CapFactory
00174   //    METHOD TYPE : Dtor
00175   //
00176   CapFactory::~CapFactory()
00177   {}
00178 
00180   //
00181   //    METHOD NAME : CapFactory::fromImpl
00182   //    METHOD TYPE : Capability
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(); // not reached
00193   }
00194 
00196   //
00197   //    METHOD NAME : CapFactory::parse
00198   //    METHOD TYPE : Capability
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(); // not reached
00210   }
00211 
00213   //
00214   //    METHOD NAME : CapFactory::parse
00215   //    METHOD TYPE : Capability
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(); // not reached
00229   }
00230 
00232   //
00233   //    METHOD NAME : CapFactory::parse
00234   //    METHOD TYPE : Capability
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(); // not reached
00248     }
00249 
00251   //
00252   //    METHOD NAME : CapFactory::halEvalCap
00253   //    METHOD TYPE : Capability
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(); // not reached
00264     }
00265 
00267   //
00268   //    METHOD NAME : CapFactory::modaliasEvalCap
00269   //    METHOD TYPE : Capability
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(); // not reached
00280     }
00281 
00283   //
00284   //    METHOD NAME : CapFactory::filesystemEvalCap
00285   //    METHOD TYPE : Capability
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(); // not reached
00296   }
00297 
00299   //
00300   //    METHOD NAME : CapFactory::encode
00301   //    METHOD TYPE : std::string
00302   //
00303   std::string CapFactory::encode( const Capability & cap_r ) const
00304   {
00305     return cap_r._pimpl->encode();
00306   }
00307 
00308   /******************************************************************
00309   **
00310   **    FUNCTION NAME : operator<<
00311   **    FUNCTION TYPE : std::ostream &
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 } // namespace zypp

Generated on Tue Sep 25 19:23:00 2007 for libzypp by  doxygen 1.5.3