1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/sat/WhatProvides.h
12 #ifndef ZYPP_SAT_WHATPROVIDES_H
13 #define ZYPP_SAT_WHATPROVIDES_H
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>
23 ///////////////////////////////////////////////////////////////////
25 { /////////////////////////////////////////////////////////////////
26 ///////////////////////////////////////////////////////////////////
28 { /////////////////////////////////////////////////////////////////
32 class WhatProvidesIterator;
35 ///////////////////////////////////////////////////////////////////
37 // CLASS NAME : WhatProvides
39 /** Container of \ref Solvable providing a \ref Capability (read only).
42 * Capability cap("amarok < 1.13");
44 * WhatProvides q( cap );
45 * Solvable firstMatch;
49 * cout << "Found " << q.size() << " matches for " << cap << ":" << endl;
50 * firstMatch = *q.begin();
52 * for_( it, q.begin(), q.end() )
53 * cout << *it << endl;
58 * WhatProvides req( firstMatch.requires() );
59 * if ( ! req.empty() )
61 * cout << "Found " << req.size() << " items providing requirements of " << firstMatch << ":" << endl;
66 * \note Note that there are capabilities which are not provided by any \ref Solvable,
67 * but are system properties. For example:
69 * rpmlib(PayloadIsBzip2) <= 3.0.5-1
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.
75 * WhatProvides q( Capability("rpmlib(PayloadIsBzip2) <= 3.0.5-1") );
76 * for_( it, q.begin(), q.end() )
79 * cout << "Capability is provided by package " << *it << endl;
80 * else if ( it->isSystem() )
81 * cout << "Capability is a system property" << endl;
83 * ; // never reaching this \c else
87 class WhatProvides : public SolvIterMixin<WhatProvides,detail::WhatProvidesIterator>,
88 protected detail::PoolMember
91 typedef Solvable value_type;
92 typedef unsigned size_type;
98 /** Ctor from \ref Capability. */
100 WhatProvides( Capability cap_r );
102 /** Ctor collecting all providers of capabilities in \c caps_r. */
104 WhatProvides( Capabilities caps_r );
106 /** Ctor collecting all providers of capabilities in \c caps_r. */
108 WhatProvides( const CapabilitySet & caps_r );
111 /** Whether the container is empty. */
114 /** Number of solvables inside. */
115 size_type size() const;
118 typedef detail::WhatProvidesIterator const_iterator;
120 /** Iterator pointing to the first \ref Solvable. */
121 const_iterator begin() const;
123 /** Iterator pointing behind the last \ref Solvable. */
124 const_iterator end() const;
128 RW_pointer<Impl> _pimpl;
130 ///////////////////////////////////////////////////////////////////
132 /** \relates WhatProvides Stream output */
133 std::ostream & operator<<( std::ostream & str, const WhatProvides & obj );
137 ///////////////////////////////////////////////////////////////////
139 // CLASS NAME : WhatProvides::const_iterator
141 /** \ref WhatProvides iterator.
142 * Iterate a NULL terminated sat::detail::IdType array. Ctor gets
143 * the adress of a pointer to the array, and offset into the array.
144 * This is needed in case the array gets reallocated.
146 class WhatProvidesIterator : public boost::iterator_adaptor<
147 WhatProvidesIterator // Derived
148 , const detail::IdType * // Base
149 , const Solvable // Value
150 , boost::forward_traversal_tag // CategoryOrTraversal
151 , const Solvable // Reference
154 friend std::ostream & operator<<( std::ostream & str, const WhatProvidesIterator & obj );
156 WhatProvidesIterator()
157 : iterator_adaptor_( 0 ), _baseRef( 0 ), _offset( 0 )
160 /** Ctor with pointer to 1st elemment of an array.
161 * Use otherwise unused base as pointer for _baseRef.
163 explicit WhatProvidesIterator( const detail::IdType *const base_r, unsigned offset_r = 0 )
164 : iterator_adaptor_( base_r ), _baseRef( base_r ? &base_reference() : 0 ), _offset( offset_r )
167 /** Ctor with pointer to pointer to 1st elemment of an array.
168 * Required for arrays that might be relocated while iterating.
170 explicit WhatProvidesIterator( const detail::IdType *const* baseRef_r, unsigned offset_r )
171 : iterator_adaptor_( 0 ), _baseRef( baseRef_r ), _offset( offset_r )
174 /** Copy-ctor required to keep _baseRef adjusted. */
175 WhatProvidesIterator( const WhatProvidesIterator & rhs )
176 : iterator_adaptor_( rhs.base_reference() )
177 , _baseRef( base_reference() ? &base_reference() : rhs._baseRef )
178 , _offset( rhs._offset )
181 /** Assignment operator required to keep _baseRef adjusted. */
182 WhatProvidesIterator & operator=( const WhatProvidesIterator & rhs )
184 if ( this != &rhs ) // no self assign
186 base_reference() = rhs.base_reference();
187 _baseRef = ( base_reference() ? &base_reference() : rhs._baseRef );
188 _offset = rhs._offset;
194 friend class boost::iterator_core_access;
196 reference dereference() const
197 { return Solvable( getId() ); }
199 template <class OtherDerived, class OtherIterator, class V, class C, class R, class D>
200 bool equal( const boost::iterator_adaptor<OtherDerived, OtherIterator, V, C, R, D> & rhs ) const
202 bool equal( const WhatProvidesIterator & rhs ) const
204 if ( ! ( getId() || rhs.getId() ) )
205 return true; // both @end
206 if ( _offset != rhs._offset )
208 if ( base_reference() )
209 return( base_reference() == rhs.base_reference() );
210 return( _baseRef == rhs._baseRef );
216 detail::IdType getId() const
217 { return _baseRef ? (*_baseRef)[_offset] : detail::noId; }
220 const detail::IdType *const* _baseRef;
223 ///////////////////////////////////////////////////////////////////
226 inline WhatProvides::const_iterator WhatProvides::end() const
227 { return const_iterator(); }
229 /////////////////////////////////////////////////////////////////
231 ///////////////////////////////////////////////////////////////////
232 /////////////////////////////////////////////////////////////////
234 ///////////////////////////////////////////////////////////////////
235 #endif // ZYPP_SAT_WHATPROVIDES_H