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