Add ResObject construction from Solvable
authorMichael Andres <ma@suse.de>
Thu, 24 Jan 2008 12:15:50 +0000 (12:15 +0000)
committerMichael Andres <ma@suse.de>
Thu, 24 Jan 2008 12:15:50 +0000 (12:15 +0000)
- ResObject::Ptr makeResObject( const sat::Solvable & solvable_r )
- template<class _Res>
  ResTraits<_Res>::PtrType make( const sat::Solvable & solvable_r )

18 files changed:
zypp/Atom.h
zypp/CMakeLists.txt
zypp/Language.h
zypp/Message.h
zypp/Package.h
zypp/Patch.h
zypp/Pattern.h
zypp/Product.h
zypp/ResObject.cc
zypp/ResObject.h
zypp/ResObjects.h [new file with mode: 0644]
zypp/Resolvable.cc
zypp/Script.h
zypp/Selection.h
zypp/SrcPackage.h
zypp/sat/Solvable.cc
zypp/sat/Solvable.h
zypp/target/store/serialize.cc

index d07a49b..b244018 100644 (file)
@@ -31,6 +31,8 @@ namespace zypp
     typedef TraitsType::constPtrType constPtr;
 
   protected:
+    friend Ptr make<Self>( const sat::Solvable & solvable_r );
+    /** Ctor */
     Atom( const sat::Solvable & solvable_r );
     /** Dtor */
     virtual ~Atom();
index 4258583..67e202a 100644 (file)
@@ -143,6 +143,7 @@ SET( zypp_HEADERS
   ResKind.h
   ResFilters.h
   ResObject.h
+  ResObjectsh
   ResPool.h
   ResPoolManager.h
   ResPoolProxy.h
index 73a64c4..5bd1749 100644 (file)
@@ -41,6 +41,7 @@ namespace zypp
     static Ptr availableInstance( const Locale & locale_r );
 
   protected:
+    friend Ptr make<Self>( const sat::Solvable & solvable_r );
     /** Ctor */
     Language( const sat::Solvable & solvable_r );
     /** Dtor */
index 856f724..0d70b53 100644 (file)
@@ -39,6 +39,8 @@ namespace zypp
     ResTraits<Patch>::constPtrType patch() const;
 
   protected:
+    friend Ptr make<Self>( const sat::Solvable & solvable_r );
+    /** Ctor */
     Message( const sat::Solvable & solvable_r );
     /** Dtor */
     virtual ~Message();
index 1cc1ef3..4e6b8e5 100644 (file)
@@ -98,6 +98,8 @@ namespace zypp
     OnMediaLocation location() const;
 
   protected:
+    friend Ptr make<Self>( const sat::Solvable & solvable_r );
+    /** Ctor */
     Package( const sat::Solvable & solvable_r );
     /** Dtor */
     virtual ~Package();
index fbbe563..89cc887 100644 (file)
@@ -56,6 +56,7 @@ namespace zypp
     bool interactive() const;
 
   protected:
+    friend Ptr make<Self>( const sat::Solvable & solvable_r );
     /** Ctor */
     Patch( const sat::Solvable & solvable_r );
     /** Dtor */
index 2564629..9f68c32 100644 (file)
@@ -58,6 +58,7 @@ namespace zypp
     const Capabilities & extends() const;
 
   protected:
+    friend Ptr make<Self>( const sat::Solvable & solvable_r );
     /** Ctor */
     Pattern( const sat::Solvable & solvable_r );
     /** Dtor */
index a7e9daa..9e7b016 100644 (file)
@@ -82,6 +82,7 @@ namespace zypp
     Edition distributionEdition() const;
 
   protected:
+    friend Ptr make<Self>( const sat::Solvable & solvable_r );
     /** Ctor */
     Product( const sat::Solvable & solvable_r );
     /** Dtor */
index fe9185d..7d47624 100644 (file)
@@ -42,8 +42,7 @@ namespace zypp
   //
   std::ostream & ResObject::dumpOn( std::ostream & str ) const
   {
-    str << "[S" << repository().numericId() << ":" << mediaNr() << "]";
-    return Resolvable::dumpOn( str );
+    return Resolvable::dumpOn( str << "[S" << repository().numericId() << ":" << mediaNr() << "]" );
   }
 
   ///////////////////////////////////////////////////////////////////
@@ -97,6 +96,34 @@ namespace zypp
     return _du;
   }
 
+   /////////////////////////////////////////////////////////////////
+} // namespace zypp
+///////////////////////////////////////////////////////////////////
+
+#include "zypp/ResObjects.h"
+
+///////////////////////////////////////////////////////////////////
+namespace zypp
+{ /////////////////////////////////////////////////////////////////
+
+  ResObject::Ptr makeResObject( const sat::Solvable & solvable_r )
+  {
+    ResKind kind( solvable_r.kind() );
+#define OUTS(X)  if ( kind == ResTraits<X>::kind ) return make<X>( solvable_r );
+    OUTS( Atom );
+    OUTS( Language );
+    OUTS( Message );
+    OUTS( Package );
+    OUTS( Patch );
+    OUTS( Pattern );
+    OUTS( Product );
+    OUTS( Script );
+    OUTS( Selection );
+    OUTS( SrcPackage );
+#undef OUTS
+    return 0;
+  }
+
   /////////////////////////////////////////////////////////////////
 } // namespace zypp
 ///////////////////////////////////////////////////////////////////
index e2b5bd2..c3b9e0d 100644 (file)
@@ -96,13 +96,6 @@ namespace zypp
     ByteCount downloadSize() const;
 
     /**
-     * \short Download size
-     * \deprecated Use downloadSize()
-     */
-    ZYPP_DEPRECATED ByteCount archivesize() const
-    { return downloadSize(); }
-
-    /**
      * Source providing this resolvable
      */
     Repository repository() const;
@@ -159,13 +152,30 @@ namespace zypp
    * \code
    * sat::Solvable s;
    * ResObject::Ptr p( makeResObject( s ) );
-   * if ( p )
-   * {
+   * ResObject::Ptr q( make<ResObject>( s ) );
+   * Package::Ptr   pkg( make<Package>( s ) );
+   * \endcode
+  */
+  ResObject::Ptr makeResObject( const sat::Solvable & solvable_r );
+
+  /** Directly create a certain kind of ResObject from \ref sat::Solvable.
    *
-   * }
+   * If the sat::Solvables kind is not appropriate, a NULL
+   * pointer is returned.
+    * \code
+   * sat::Solvable s;
+   * ResObject::Ptr p( makeResObject( s ) );
+   * ResObject::Ptr q( make<ResObject>( s ) );
+   * Package::Ptr   pkg( make<Package>( s ) );
    * \endcode
   */
-  inline ResObject::Ptr makeResObject( const sat::Solvable & solvable_r ) { return 0; }
+  template<class _Res>
+  inline typename ResTraits<_Res>::PtrType make( const sat::Solvable & solvable_r )
+  { return( isKind<_Res>( solvable_r ) ? new _Res( solvable_r ) : 0 ); }
+  /** \overload Specialisation for ResObject autodetecting the kind of resolvable. */
+  template<>
+  inline ResObject::Ptr make<ResObject>( const sat::Solvable & solvable_r )
+  { return makeResObject( solvable_r ); }
 
   /** Convert ResObject::Ptr into Ptr of a certain Kind.
    * \return \c NULL iff \a p is \c NULL or points to a Resolvable
@@ -176,12 +186,12 @@ namespace zypp
    * \endcode
   */
   template<class _Res>
-    inline typename ResTraits<_Res>::PtrType asKind( const ResObject::Ptr & p )
-    { return dynamic_pointer_cast<_Res>(p); }
+  inline typename ResTraits<_Res>::PtrType asKind( const ResObject::Ptr & p )
+  { return dynamic_pointer_cast<_Res>(p); }
 
   template<class _Res>
-    inline typename ResTraits<_Res>::constPtrType asKind( const ResObject::constPtr & p )
-    { return dynamic_pointer_cast<const _Res>(p); }
+  inline typename ResTraits<_Res>::constPtrType asKind( const ResObject::constPtr & p )
+  { return dynamic_pointer_cast<const _Res>(p); }
 
   /////////////////////////////////////////////////////////////////
 } // namespace zypp
diff --git a/zypp/ResObjects.h b/zypp/ResObjects.h
new file mode 100644 (file)
index 0000000..647ad58
--- /dev/null
@@ -0,0 +1,26 @@
+/*---------------------------------------------------------------------\
+|                          ____ _   __ __ ___                          |
+|                         |__  / \ / / . \ . \                         |
+|                           / / \ V /|  _/  _/                         |
+|                          / /__ | | | | | |                           |
+|                         /_____||_| |_| |_|                           |
+|                                                                      |
+\---------------------------------------------------------------------*/
+/** \file      zypp/ResObjects.h
+ *
+*/
+#ifndef ZYPP_RESOBJECTS_H
+#define ZYPP_RESOBJECTS_H
+
+#include "zypp/Atom.h"
+#include "zypp/Language.h"
+#include "zypp/Message.h"
+#include "zypp/Package.h"
+#include "zypp/Patch.h"
+#include "zypp/Pattern.h"
+#include "zypp/Product.h"
+#include "zypp/Script.h"
+#include "zypp/Selection.h"
+#include "zypp/SrcPackage.h"
+
+#endif // ZYPP_RESOBJECTS_H
index a2534df..0097113 100644 (file)
@@ -35,13 +35,13 @@ namespace zypp
   Resolvable::~Resolvable()
   {}
 
+  ///////////////////////////////////////////////////////////////////
+  //
+  //   METHOD NAME : Resolvable::dumpOn
+  //   METHOD TYPE : std::ostream &
+  //
   std::ostream & Resolvable::dumpOn( std::ostream & str ) const
-  {
-    //::dumpOn( str, *(sat::Solvable*)(this) );
-    //return str << sat::Solvable::dumpOn(str);
-    return str;
-  }
-
+  { return str << satSolvable(); }
 
   /////////////////////////////////////////////////////////////////
 } // namespace zypp
index ff2e168..e37a43c 100644 (file)
@@ -60,6 +60,7 @@ namespace zypp
     OnMediaLocation undoScriptLocation() const;
 
   protected:
+    friend Ptr make<Self>( const sat::Solvable & solvable_r );
     /** Ctor */
     Script( const sat::Solvable & solvable_r );
     /** Dtor */
index 6384449..de0780d 100644 (file)
@@ -49,6 +49,7 @@ namespace zypp
     const std::set<std::string> install_packages( const Locale & lang = Locale("") ) const;
 
   protected:
+    friend Ptr make<Self>( const sat::Solvable & solvable_r );
     /** Ctor */
     Selection( const sat::Solvable & solvable_r );
     /** Dtor */
index 18bc42f..2ce1a31 100644 (file)
@@ -40,6 +40,8 @@ namespace zypp
     OnMediaLocation location() const;
 
   protected:
+    friend Ptr make<Self>( const sat::Solvable & solvable_r );
+    /** Ctor */
     SrcPackage( const sat::Solvable & solvable_r );
     /** Dtor */
     virtual ~SrcPackage();
index 8a4a029..4432738 100644 (file)
@@ -115,6 +115,30 @@ namespace zypp
       return ResKind( std::string( ident, sep-ident ) );
     }
 
+    bool Solvable::isKind( const ResKind & kind_r ) const
+    {
+      NO_SOLVABLE_RETURN( false );
+
+      // detect srcpackages by 'arch'
+      if ( kind_r == ResKind::srcpackage )
+      {
+        return( _solvable->arch == ARCH_SRC || _solvable->arch == ARCH_NOSRC );
+      }
+
+      // no ':' in package names (hopefully)
+      const char * ident = IdString( _solvable->name ).c_str();
+      if ( kind_r == ResKind::package )
+      {
+        return( ::strchr( ident, ':' ) == 0 );
+      }
+
+      // look for a 'kind:' prefix
+      const char * kind = kind_r.c_str();
+      unsigned     ksize = ::strlen( kind );
+      return( ::strncmp( ident, kind, ksize ) == 0
+              && ident[ksize] == ':' );
+    }
+
     std::string Solvable::name() const
     {
       NO_SOLVABLE_RETURN( std::string() );
index 9cfc7a2..0091a6a 100644 (file)
@@ -84,6 +84,9 @@ namespace zypp
         IdString     ident()    const;
 
         ResKind      kind()     const;
+        /** Test whether a Solvable is of a certain \ref ResKind. */
+        bool         isKind( const ResKind & kind_r ) const;
+
         std::string  name()     const;
         Edition      edition()  const;
         Arch         arch()     const;
@@ -199,6 +202,12 @@ namespace zypp
    /////////////////////////////////////////////////////////////////
   } // namespace sat
   ///////////////////////////////////////////////////////////////////
+
+  /** \relates sat::Solvable Test whether a \ref sat::Solvable is of a certain Kind. */
+  template<class _Res>
+  inline bool isKind( const sat::Solvable & solvable_r )
+  { return solvable_r.isKind( ResTraits<_Res>::kind ); }
+
   /////////////////////////////////////////////////////////////////
 } // namespace zypp
 ///////////////////////////////////////////////////////////////////
index d86fe3a..ea68c71 100644 (file)
@@ -166,7 +166,6 @@ string toXML( const Dependencies &dep )
     out << "    " << xml_tag_enclose(toXML(dep[Dep::CONFLICTS]), "conflicts") << endl;
   if ( dep[Dep::OBSOLETES].size() > 0 )
     out << "    " << xml_tag_enclose(toXML(dep[Dep::OBSOLETES]), "obsoletes") << endl;
-  // why the YUM tag is freshen without s????
   if ( dep[Dep::FRESHENS].size() > 0 )
     out << "    " << xml_tag_enclose(toXML(dep[Dep::FRESHENS]), "freshens") << endl;
   if ( dep[Dep::REQUIRES].size() > 0 )