add ResObject::as<Package>()
authorMichael Andres <ma@suse.de>
Tue, 23 Sep 2008 10:25:00 +0000 (10:25 +0000)
committerMichael Andres <ma@suse.de>
Tue, 23 Sep 2008 10:25:00 +0000 (10:25 +0000)
devel/devel.ma/NewPool.cc
zypp/ResObject.h
zypp/Resolvable.h
zypp/sat/Solvable.h

index 34e5c42..239d133 100644 (file)
@@ -430,7 +430,6 @@ void testCMP( const L & lhs, const R & rhs )
 #undef OUTS
 }
 
-
 /******************************************************************
 **
 **      FUNCTION NAME : main
@@ -444,16 +443,6 @@ try {
   INT << "===[START]==========================================" << endl;
   ZConfig::instance();
 
-  Edition a("4.21.3-2");
-  DBG << a << endl;
-  DBG << (a == a) << endl;
-  DBG << (a.match(a)) << endl;
-  ///////////////////////////////////////////////////////////////////
-  INT << "===[END]============================================" << endl << endl;
-  zypp::base::LogControl::instance().logNothing();
-  return 0;
-
-
 #if 0
 
   RepoManager repoManager( makeRepoManager( sysRoot ) );
@@ -625,7 +614,14 @@ try {
   }
 
 
-  //////////////////////////////////////////////////////////////////
+  PoolItem pi ( getPi<Package>("amarok") );
+  MIL << pi << endl;
+  MIL << pi->as<Package>() << endl;
+  MIL << pi->as<Product>() << endl;
+  if ( pi->isKind<Package>() )
+    SEC << pi->as<Package>() << endl;
+
+ //////////////////////////////////////////////////////////////////
   INT << "===[END]============================================" << endl << endl;
   zypp::base::LogControl::instance().logNothing();
   return 0;
index 0faa960..8a4e7da 100644 (file)
@@ -50,9 +50,29 @@ namespace zypp
     typedef TraitsType::constPtrType constPtr;
 
   public:
+
+    /** Convert \c this into a Ptr of a certain Kind.
+     * This is a convenience to access type specific
+     * attributes.
+     * \return \c NULL if \c this is not of the specified kind.
+     * \code
+     *  PoolItem pi;
+     *  Package::constPtr pkg = pi->as<Package>();
+     *
+     *  if ( pi->isKind<Package>() )
+     *     DBG << pi->as<Package>()->keywords() << endl;
+     * \endcode
+     */
+    template<class _Res>
+    inline typename ResTraits<_Res>::constPtrType as() const;
+
+    template<class _Res>
+    inline typename ResTraits<_Res>::PtrType as();
+
+  public:
     /** \name Locale support.
      * \see \ref sat::Solvable
-    */
+     */
     //@{
     /** \see \ref sat::Solvable::supportsLocales */
     bool supportsLocales() const
@@ -240,6 +260,14 @@ namespace zypp
   inline typename ResTraits<_Res>::constPtrType asKind( const ResObject::constPtr & p )
   { return dynamic_pointer_cast<const _Res>(p); }
 
+  template<class _Res>
+  inline typename ResTraits<_Res>::constPtrType ResObject::as() const
+  { return make<_Res>( *this ); }
+
+  template<class _Res>
+  inline typename ResTraits<_Res>::PtrType ResObject::as()
+  { return make<_Res>( *this ); }
+
   /////////////////////////////////////////////////////////////////
 } // namespace zypp
 ///////////////////////////////////////////////////////////////////
index 5984bbd..c697b8e 100644 (file)
@@ -66,6 +66,10 @@ namespace zypp
     bool isKind( const ResKind & kind_r ) const
     { return sat::Solvable::isKind( kind_r ); }
 
+    template<class _Res>
+    bool isKind() const
+    { return sat::Solvable::isKind<_Res>(); }
+
     std::string name() const
     { return sat::Solvable::name(); }
 
index 937b392..1a0ca19 100644 (file)
@@ -151,6 +151,10 @@ namespace zypp
         ResKind      kind()     const;
         /** Test whether a Solvable is of a certain \ref ResKind. */
         bool         isKind( const ResKind & kind_r ) const;
+        /** \overload */
+        template<class _Res>
+        bool isKind() const
+        { return isKind( resKind<_Res>() ); }
 
         std::string  name()     const;
         Edition      edition()  const;