ignore
[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/base/ReferenceCounted.h"
19 #include "zypp/base/NonCopyable.h"
20 #include "zypp/base/PtrTypes.h"
21 #include "zypp/ResTraits.h"
22
23 #include "zypp/Edition.h"
24 #include "zypp/Arch.h"
25 #include "zypp/CapSetFwd.h"
26 #include "zypp/Dep.h"
27
28 ///////////////////////////////////////////////////////////////////
29 namespace zypp
30 { /////////////////////////////////////////////////////////////////
31
32   struct NVRAD;
33   class Dependencies;
34
35   ///////////////////////////////////////////////////////////////////
36   //
37   //    CLASS NAME : Resolvable
38   //
39   /** Interface base for resolvable objects (identification and dependencies).
40    * \invariant \c provides <tt>name = edition</tt>
41    * \invariant \c prerequires is a subset of \c requires
42   */
43   class Resolvable : public base::ReferenceCounted, private base::NonCopyable
44   {
45   public:
46     typedef Resolvable               Self;
47     typedef ResTraits<Self>          TraitsType;
48     typedef TraitsType::KindType     Kind;
49     typedef TraitsType::PtrType      Ptr;
50     typedef TraitsType::constPtrType constPtr;
51
52   public:
53     /**  */
54     const Kind & kind() const;
55     /**  */
56     const std::string & name() const;
57     /**  */
58     const Edition & edition() const;
59     /**  */
60     const Arch & arch() const;
61
62     /** \name Dependencies. */
63     //@{
64     /** Select by Dep. */
65     const CapSet & dep( Dep which_r ) const;
66     /** All dependencies. */
67     const Dependencies & deps() const;
68     //@}
69
70     /** \name Deprecated. */
71     //@{
72     void injectProvides( const Capability & cap_r );
73     void injectRequires( const Capability & cap_r );
74     //@}
75
76   protected:
77     /** Ctor */
78     Resolvable( const Kind & kind_r,
79                 const NVRAD & nvrad_r );
80     /** Dtor */
81     virtual ~Resolvable();
82     /** Helper for stream output */
83     virtual std::ostream & dumpOn( std::ostream & str ) const;
84
85   private:
86     /** Implementation */
87     struct Impl;
88     /** Pointer to implementation */
89     RW_pointer<Impl> _pimpl;
90   };
91   ///////////////////////////////////////////////////////////////////
92
93   /** Test whether a Resolvable::Ptr is of a certain Kind.
94    * \return \c Ture iff \a p is not \c NULL and points to a Resolvable
95    * of the specified Kind.
96    * \relates Resolvable
97    * \code
98    * isKind<Package>(resPtr);
99    * \endcode
100   */
101   template<class _Res>
102     inline bool isKind( const Resolvable::constPtr & p )
103     { return p && p->kind() == ResTraits<_Res>::kind; }
104
105   // Specialization for Resolvable: Always true.
106   template<>
107     inline bool isKind<Resolvable>( const Resolvable::constPtr & p )
108     { return p; }
109
110   // Specialization for ResObject: Always true.
111   template<>
112     inline bool isKind<ResObject>( const Resolvable::constPtr & p )
113     { return p; }
114
115
116   /** Convert Resolvable::Ptr into Ptr of a certain Kind.
117    * \return \c NULL iff \a p is \c NULL or points to a Resolvable
118    * not of the specified Kind.
119    * \relates Resolvable
120    * \code
121    * asKind<Package>(resPtr);
122    * \endcode
123   */
124   template<class _Res>
125     inline typename ResTraits<_Res>::PtrType asKind( const Resolvable::Ptr & p )
126     { return dynamic_pointer_cast<_Res>(p); }
127
128   template<class _Res>
129     inline typename ResTraits<_Res>::constPtrType asKind( const Resolvable::constPtr & p )
130     { return dynamic_pointer_cast<const _Res>(p); }
131
132   ///////////////////////////////////////////////////////////////////
133
134   /** \relates Resolvable Compare Resolvable::constPtr according to
135    *  \a kind and \a name.
136   */
137   inline int compareByN( const Resolvable::constPtr & lhs,
138                          const Resolvable::constPtr & rhs )
139   {
140     if ( lhs == rhs )
141       return 0;
142     if ( ! (lhs && rhs) )
143       return lhs ? 1 : -1;
144     int res = 0;
145     if ( (res = lhs->kind().compare( rhs->kind() )) )
146       return res;
147     return lhs->name().compare( rhs->name() );
148   }
149
150   /** \relates Resolvable Compare Resolvable::constPtr according to
151    *  \a kind, \a name and \a edition(compare!).
152   */
153   inline int compareByNVR( const Resolvable::constPtr & lhs,
154                            const Resolvable::constPtr & rhs )
155   {
156     if ( lhs == rhs )
157       return 0;
158     if ( ! (lhs && rhs) )
159       return lhs ? 1 : -1;
160     int res = 0;
161     if ( (res = lhs->kind().compare( rhs->kind() )) )
162       return res;
163     if ( (res = lhs->name().compare( rhs->name() )) )
164       return res;
165     return lhs->edition().compare( rhs->edition() );
166   }
167
168   /** \relates Resolvable Compare Resolvable::constPtr according to
169    *  \a kind, \a name, \a edition(compare!) and \a arch.
170   */
171   inline int compareByNVRA( const Resolvable::constPtr & lhs,
172                             const Resolvable::constPtr & rhs )
173   {
174     if ( lhs == rhs )
175       return 0;
176     if ( ! (lhs && rhs) )
177       return lhs ? 1 : -1;
178     int res = 0;
179     if ( (res = lhs->kind().compare( rhs->kind() )) )
180       return res;
181     if ( (res = lhs->name().compare( rhs->name() )) )
182       return res;
183     if ( (res = lhs->edition().compare( rhs->edition() )) )
184       return res;
185     return lhs->arch().compare( rhs->arch() );
186   }
187
188   /////////////////////////////////////////////////////////////////
189 } // namespace zypp
190 ///////////////////////////////////////////////////////////////////
191 #endif // ZYPP_RESOLVABLE_H