Imported Upstream version 16.3.2
[platform/upstream/libzypp.git] / zypp / ResObject.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/ResObject.h
10  *
11 */
12 #ifndef ZYPP_RESOBJECT_H
13 #define ZYPP_RESOBJECT_H
14
15 #include "zypp/APIConfig.h"
16
17 #include "zypp/Resolvable.h"
18 #include "zypp/Vendor.h"
19
20 #include "zypp/sat/LookupAttr.h"
21 #include "zypp/sat/SolvableSet.h"
22
23 ///////////////////////////////////////////////////////////////////
24 namespace zypp
25 {
26   ///////////////////////////////////////////////////////////////////
27   /// \class ResObject
28   /// \brief Base for resolvable objects
29   ///
30   /// \note \ref Resolvable is a SolvableType, which provides direct
31   /// access to many of the underlying sat::Solvables properties.
32   /// Don't add common properties here, but in \ref sat::Solvable
33   /// and extend \ref sat::SolvableType.
34   ///
35   /// \see \ref makeResObject for how to construct ResObjects.
36   /// \todo Merge with Resolvable
37   ///////////////////////////////////////////////////////////////////
38   class ResObject : public Resolvable
39   {
40   public:
41     typedef ResObject                Self;
42     typedef ResTraits<Self>          TraitsType;
43     typedef TraitsType::PtrType      Ptr;
44     typedef TraitsType::constPtrType constPtr;
45
46   public:
47     /** Convert \c this into a Ptr of a certain Kind.
48      * This is a convenience to access type specific
49      * attributes.
50      * \return \c NULL if \c this is not of the specified kind.
51      * \code
52      *  PoolItem pi;
53      *  Package::constPtr pkg = pi->asKind<Package>();
54      *
55      *  if ( pi->isKind<Package>() )
56      *     DBG << pi->asKind<Package>()->keywords() << endl;
57      * \endcode
58      */
59     template<class TRes>
60     inline typename ResTraits<TRes>::constPtrType asKind() const;
61
62     template<class TRes>
63     inline typename ResTraits<TRes>::PtrType asKind();
64
65   public:
66     /**
67      * \short Vendor
68      * \deprecated Though typedef'ed to std::string, Vendor is actually an \ref IdString.
69      */
70     Vendor vendor() const
71     { return Resolvable::vendor().asString(); }
72
73   protected:
74     friend ResObject::Ptr makeResObject( const sat::Solvable & solvable_r );
75     /** Ctor */
76     ResObject( const sat::Solvable & solvable_r );
77     /** Dtor */
78     virtual ~ResObject();
79     /** Helper for stream output */
80     virtual std::ostream & dumpOn( std::ostream & str ) const;
81     /** This is a \ref sat::SolvableType (allow implicit conversion in derived classes). */
82     operator sat::Solvable() const
83     { return satSolvable(); }
84   };
85   ///////////////////////////////////////////////////////////////////
86
87   /** Create \ref ResObject from \ref sat::Solvable.
88    *
89    * This function creates the apropriate kind of ResObject
90    * depending on the sat::Solvables kind, and returns a smart
91    * pointer to it.
92    *
93    * If the sat::Solvables kind is not convertible, a NULL
94    * pointer is returned.
95    *
96    * \code
97    * sat::Solvable s;
98    * ResObject::Ptr p( makeResObject( s ) );
99    * ResObject::Ptr q( make<ResObject>( s ) );
100    * Package::Ptr   pkg( make<Package>( s ) );
101    * \endcode
102   */
103   ResObject::Ptr makeResObject( const sat::Solvable & solvable_r );
104
105   /** Directly create a certain kind of ResObject from \ref sat::Solvable.
106    *
107    * If the sat::Solvables kind is not appropriate, a NULL
108    * pointer is returned.
109     * \code
110    * sat::Solvable s;
111    * ResObject::Ptr p( makeResObject( s ) );
112    * ResObject::Ptr q( make<ResObject>( s ) );
113    * Package::Ptr   pkg( make<Package>( s ) );
114    * \endcode
115    * \todo make<> was a poor choice (AFAIR because gcc had some trouble with
116    * asKind<>(sat::Solvable)). Remove it in favour of asKind<>(sat::Solvable)
117   */
118   template<class TRes>
119   inline typename ResTraits<TRes>::PtrType make( const sat::Solvable & solvable_r )
120   { return( isKind<TRes>( solvable_r ) ? new TRes( solvable_r ) : 0 ); }
121   /** \overload Specialisation for ResObject autodetecting the kind of resolvable. */
122   template<>
123   inline ResObject::Ptr make<ResObject>( const sat::Solvable & solvable_r )
124   { return makeResObject( solvable_r ); }
125
126   /** Directly create a certain kind of ResObject from \ref sat::Solvable. */
127   template<class TRes>
128   inline typename ResTraits<TRes>::PtrType asKind( const sat::Solvable & solvable_r )
129   { return make<TRes>( solvable_r ); }
130
131   /** Convert ResObject::Ptr into Ptr of a certain Kind.
132    * \return \c NULL iff \a p is \c NULL or points to a Resolvable
133    * not of the specified Kind.
134    * \relates ResObject
135    * \code
136    * asKind<Package>(resPtr);
137    * \endcode
138   */
139   template<class TRes>
140   inline typename ResTraits<TRes>::PtrType asKind( const ResObject::Ptr & p )
141   { return dynamic_pointer_cast<TRes>(p); }
142
143   template<class TRes>
144   inline typename ResTraits<TRes>::constPtrType asKind( const ResObject::constPtr & p )
145   { return dynamic_pointer_cast<const TRes>(p); }
146
147   template<class TRes>
148   inline typename ResTraits<TRes>::constPtrType ResObject::asKind() const
149   { return dynamic_cast<const TRes *>( this ); }
150
151   template<class TRes>
152   inline typename ResTraits<TRes>::PtrType ResObject::asKind()
153   { return dynamic_cast<TRes *>( this ); }
154
155 } // namespace zypp
156 ///////////////////////////////////////////////////////////////////
157 #endif // ZYPP_RESOBJECT_H