- Add method Product::replacedProducts to identify installed
[platform/upstream/libzypp.git] / zypp / sat / WhatProvides.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/sat/WhatProvides.h
10  *
11 */
12 #ifndef ZYPP_SAT_WHATPROVIDES_H
13 #define ZYPP_SAT_WHATPROVIDES_H
14
15 #include <iosfwd>
16 #include <vector>
17
18 #include "zypp/base/PtrTypes.h"
19 #include "zypp/sat/detail/PoolMember.h"
20 #include "zypp/sat/Solvable.h"
21 #include "zypp/sat/SolvIterMixin.h"
22
23 ///////////////////////////////////////////////////////////////////
24 namespace zypp
25 { /////////////////////////////////////////////////////////////////
26   ///////////////////////////////////////////////////////////////////
27   namespace sat
28   { /////////////////////////////////////////////////////////////////
29
30     namespace detail
31     {
32       class WhatProvidesIterator;
33     }
34
35     ///////////////////////////////////////////////////////////////////
36     //
37     //  CLASS NAME : WhatProvides
38     //
39     /** Container of \ref Solvable providing a \ref Capability (read only).
40      *
41      * \code
42      * Capability cap("amarok < 1.13");
43      *
44      * WhatProvides q( cap );
45      * Solvable firstMatch;
46      *
47      * if ( ! q.empty() )
48      * {
49      *   cout << "Found " << q.size() << " matches for " << cap << ":" << endl;
50      *   firstMatch = *q.begin();
51      *
52      *   for_( it, q.begin(), q.end() )
53      *     cout << *it << endl;
54      * }
55      *
56      * if ( firstMatch )
57      * {
58      *   WhatProvides req( firstMatch.requires() );
59      *   if ( ! req.empty() )
60      *   {
61      *      cout << "Found " << req.size() << " items providing requirements of " << firstMatch << ":" << endl;
62      *   }
63      * }
64      * \endcode
65      *
66      * \note Note that there are capabilities which are not provided by any \ref Solvable,
67      * but are system properties. For example:
68      * \code
69      *   rpmlib(PayloadIsBzip2) <= 3.0.5-1
70      * \endcode
71      * In that case a \ref Solvable::noSolvable is returned, which has \c isSystem set \c true, although
72      * there should never be a \ref Solvable::noSolvable returned with \c isSystem set \c false. If so,
73      * please file a bugreport.
74      * \code
75      * WhatProvides q( Capability("rpmlib(PayloadIsBzip2) <= 3.0.5-1") );
76      * for_( it, q.begin(), q.end() )
77      * {
78      *   if ( *it )
79      *     cout << "Capability is provided by package " << *it << endl;
80      *   else if ( it->isSystem() )
81      *     cout << "Capability is a system property" << endl;
82      *   else
83      *     ; // never reaching this \c else
84      * }
85      * \endcode
86      */
87     class WhatProvides : public SolvIterMixin<WhatProvides,detail::WhatProvidesIterator>,
88                          protected detail::PoolMember
89     {
90       public:
91         typedef Solvable  value_type;
92         typedef unsigned  size_type;
93
94       public:
95         /** Default ctor */
96         WhatProvides()
97         : _begin( 0 )
98         {}
99
100         /** Ctor from \ref Capability. */
101         explicit
102         WhatProvides( Capability cap_r );
103
104         /** Ctor collecting all providers of capabilities in \c caps_r. */
105         explicit
106         WhatProvides( Capabilities caps_r );
107
108         /** Ctor collecting all providers of capabilities in \c caps_r. */
109         explicit
110         WhatProvides( const CapabilitySet & caps_r );
111
112      public:
113         /** Whether the container is empty. */
114         bool empty() const
115         { return ! ( _begin && *_begin ); }
116
117         /** Number of solvables inside. */
118         size_type size() const;
119
120       public:
121         typedef detail::WhatProvidesIterator const_iterator;
122
123         /** Iterator pointing to the first \ref Solvable. */
124         const_iterator begin() const;
125
126         /** Iterator pointing behind the last \ref Solvable. */
127         const_iterator end() const;
128
129       private:
130         const sat::detail::IdType * _begin;
131         shared_ptr<void> _private;
132     };
133     ///////////////////////////////////////////////////////////////////
134
135     /** \relates WhatProvides Stream output */
136     std::ostream & operator<<( std::ostream & str, const WhatProvides & obj );
137
138     namespace detail
139     {
140     ///////////////////////////////////////////////////////////////////
141     //
142     //  CLASS NAME : WhatProvides::const_iterator
143     //
144     /** \ref WhatProvides iterator.
145      * Iterate a NULL terminated sat::detail::IdType array.
146      */
147     class WhatProvidesIterator : public boost::iterator_adaptor<
148           WhatProvidesIterator         // Derived
149         , const detail::IdType *       // Base
150         , const Solvable               // Value
151         , boost::forward_traversal_tag // CategoryOrTraversal
152         , const Solvable               // Reference
153         >
154     {
155       public:
156         WhatProvidesIterator()
157         : iterator_adaptor_( 0 )
158         {}
159
160         explicit WhatProvidesIterator( const sat::detail::IdType * _idx )
161         : iterator_adaptor_( _idx )
162         {}
163
164       private:
165         friend class boost::iterator_core_access;
166
167         reference dereference() const
168         { return ( base() ) ? Solvable( *base() ) : Solvable::noSolvable; }
169
170         template <class OtherDerived, class OtherIterator, class V, class C, class R, class D>
171         bool equal( const boost::iterator_adaptor<OtherDerived, OtherIterator, V, C, R, D> & rhs ) const
172         { // NULL pointer is eqal pointer to Id 0
173           return ( base() == rhs.base() // includes both NULL...
174               || ( !rhs.base() && !*base()     )
175               || ( !base()     && !*rhs.base() ) );
176         }
177
178         void increment()
179         { ++base_reference(); }
180     };
181     ///////////////////////////////////////////////////////////////////
182     }
183
184     inline WhatProvides::const_iterator WhatProvides::begin() const
185     { return const_iterator( _begin ); }
186
187     inline WhatProvides::const_iterator WhatProvides::end() const
188     { return const_iterator( 0 ); }
189
190     /////////////////////////////////////////////////////////////////
191   } // namespace sat
192   ///////////////////////////////////////////////////////////////////
193   /////////////////////////////////////////////////////////////////
194 } // namespace zypp
195 ///////////////////////////////////////////////////////////////////
196 #endif // ZYPP_SAT_WHATPROVIDES_H