Fix Werrors with GCC-14.1.0
[platform/upstream/libzypp.git] / zypp / Resolvable.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/Resolvable.h
10  *
11 */
12 #ifndef ZYPP_RESOLVABLE_H
13 #define ZYPP_RESOLVABLE_H
14
15 #include <iosfwd>
16 #include <string>
17
18 #include <zypp/APIConfig.h>
19
20 #include <zypp/base/ReferenceCounted.h>
21 #include <zypp/base/NonCopyable.h>
22 #include <zypp/base/PtrTypes.h>
23
24 #include <zypp/sat/SolvableType.h>
25
26 ///////////////////////////////////////////////////////////////////
27 namespace zypp
28 {
29   class PoolItem;
30   ///////////////////////////////////////////////////////////////////
31   /// \class Resolvable
32   /// \brief Base for resolvable objects
33   ///
34   /// \note \ref Resolvable is a SolvableType, which provides direct
35   /// access to many of the underlying sat::Solvables properties.
36   /// Don't add common properties here, but in \ref sat::Solvable
37   /// and extend \ref sat::SolvableType.
38   ///
39   /// In most cases you want to retrieve the common properties directly
40   /// from a \ref PoolItem or \ref sat::Solvable. Construction from and
41   /// explicit conversion to sat::Solvable are supported. Next goal is
42   /// to get rid of the smart pointer hierarchy. A Resolvable is actually
43   /// an unsigned and derived classes contain no data, so it makes little
44   /// sense to wrap this into ReferenceCounted smart pointer.
45   ///
46   /// \todo Merge with ResObject
47   /// \todo Get rid of refcout/smart_prt bloat, as this type is actually IdBased (i.e. sizeof(unsigned))
48   ///////////////////////////////////////////////////////////////////
49   class Resolvable : public sat::SolvableType<Resolvable>,
50                      public base::ReferenceCounted, private base::NonCopyable
51   {
52     friend std::ostream & operator<<( std::ostream & str, const Resolvable & obj );
53
54   public:
55     typedef Resolvable               Self;
56     typedef ResTraits<Self>          TraitsType;
57     typedef TraitsType::KindType     Kind;
58     typedef TraitsType::PtrType      Ptr;
59     typedef TraitsType::constPtrType constPtr;
60
61   public:
62     /** This is a \ref sat::SolvableType. */
63     explicit operator sat::Solvable() const
64     { return _solvable; }
65
66     /** Access the corresponding \ref PoolItem. */
67     PoolItem poolItem() const;
68
69   protected:
70     /** Ctor */
71     Resolvable( const sat::Solvable & solvable_r );
72     /** Dtor */
73     virtual ~Resolvable();
74     /** Helper for stream output */
75     virtual std::ostream & dumpOn( std::ostream & str ) const;
76
77   private:
78     sat::Solvable _solvable;
79  };
80  ///////////////////////////////////////////////////////////////////
81
82  /** \relates Resolvable Stream output */
83  inline std::ostream & operator<<( std::ostream & str, const Resolvable & obj )
84  { return obj.dumpOn( str ); }
85
86  /** \relates Resolvable More verbose stream output including dependencies */
87  inline std::ostream & dumpOn( std::ostream & str, const Resolvable & obj )
88  { return dumpOn( str, obj.satSolvable() ); }
89
90  /** Test whether a Resolvable::Ptr is of a certain Kind.
91   * \return \c Ture iff \a p is not \c NULL and points to a Resolvable
92   * of the specified Kind.
93   * \relates Resolvable
94   * \code
95   * isKind<Package>(resPtr);
96   * \endcode
97   */
98  template<class TRes>
99  inline bool isKind( const Resolvable::constPtr & p )
100  { return p && p->isKind<TRes>(); }
101
102  // Specialization for Resolvable: Always true.
103  template<>
104  inline bool isKind<Resolvable>( const Resolvable::constPtr & p )
105  { return !!p; }
106
107  // Specialization for ResObject: Always true.
108  template<>
109  inline bool isKind<ResObject>( const Resolvable::constPtr & p )
110  { return !!p; }
111
112
113  /** Convert Resolvable::Ptr into Ptr of a certain Kind.
114   * \return \c NULL iff \a p is \c NULL or points to a Resolvable
115   * not of the specified Kind.
116   * \relates Resolvable
117   * \code
118   * asKind<Package>(resPtr);
119   * \endcode
120   */
121  template<class TRes>
122  inline typename ResTraits<TRes>::PtrType asKind( const Resolvable::Ptr & p )
123  { return dynamic_pointer_cast<TRes>(p); }
124
125  template<class TRes>
126  inline typename ResTraits<TRes>::constPtrType asKind( const Resolvable::constPtr & p )
127  { return dynamic_pointer_cast<const TRes>(p); }
128
129   ///////////////////////////////////////////////////////////////////
130
131   /** \relates Resolvable Compare Resolvable::constPtr according to \a kind and \a name.
132    * \deprecated Get rid of refcout/smart_prt bloat, use
133    */
134   inline int compareByN( const Resolvable::constPtr & lhs, const Resolvable::constPtr & rhs )
135   {
136     int ret = 0;
137     if ( lhs != rhs )
138     {
139       if ( lhs && rhs )
140         ret = compareByN( *lhs, *rhs );
141       else
142         ret = lhs ? 1 : -1;
143     }
144     return ret;
145   }
146
147   /** \relates Resolvable Compare according to \a kind, \a name and \a edition. */
148   inline int compareByNVR( const Resolvable::constPtr & lhs, const Resolvable::constPtr & rhs )
149   {
150     int ret = 0;
151     if ( lhs != rhs )
152     {
153       if ( lhs && rhs )
154         ret = compareByNVR( *lhs, *rhs );
155       else
156         ret = lhs ? 1 : -1;
157     }
158     return ret;
159   }
160
161   /** \relates Resolvable Compare Resolvable::constPtr according to \a kind, \a name, \a edition and \a arch. */
162   inline int compareByNVRA( const Resolvable::constPtr & lhs, const Resolvable::constPtr & rhs )
163   {
164     int ret = 0;
165     if ( lhs != rhs )
166     {
167       if ( lhs && rhs )
168         ret = compareByNVRA( *lhs, *rhs );
169       else
170         ret = lhs ? 1 : -1;
171     }
172     return ret;
173   }
174 } // namespace zypp
175 ///////////////////////////////////////////////////////////////////
176 #endif // ZYPP_RESOLVABLE_H