1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/sat/LookupAttr.h
12 #ifndef ZYPP_SAT_LOOKUPATTR_H
13 #define ZYPP_SAT_LOOKUPATTR_H
21 #include "zypp/base/PtrTypes.h"
22 #include "zypp/base/DefaultIntegral.h"
24 #include "zypp/sat/Pool.h"
26 ///////////////////////////////////////////////////////////////////
28 { /////////////////////////////////////////////////////////////////
29 ///////////////////////////////////////////////////////////////////
31 { /////////////////////////////////////////////////////////////////
33 ///////////////////////////////////////////////////////////////////
35 // CLASS NAME : LookupAttr
37 /** Lightweight attribute value lookup.
39 * Search for an attribute in \ref Pool, one \ref Repository
40 * or one \ref Solvable. \ref LookupAttr builds the query,
41 * \ref LookupAttr::iterator iterates over the result.
43 * Modifying the query will not affect any running
46 * Use \ref SolvAttr::allAttr to search all attributes.
49 * // look for all attributes of one solvable
50 * void ditest( sat::Solvable slv_r )
52 * sat::LookupAttr q( sat::SolvAttr::allAttr, slv_r );
53 * MIL << q << ": " << endl;
54 * for_( it, q.begin(), q.end() )
56 * MIL << " " << it.inSolvAttr() << " = " << it.asString() << endl;
62 * // look for an attribute in the pool.
63 * sat::LookupAttr q( sat::SolvAttr("susetags:datadir") );
64 * MIL << q << ": " << endl;
65 * for_( it, q.begin(), q.end() )
67 * MIL << " " << it << endl;
74 /** Default ctor finds nothing. */
77 /** Lookup \ref SolvAttr in \ref Pool (all repositories). */
79 LookupAttr( SolvAttr attr_r )
82 /** Lookup \ref SolvAttr in one\ref Repository. */
84 LookupAttr( SolvAttr attr_r, Repository repo_r )
85 : _attr( attr_r ), _repo( repo_r )
87 /** Lookup \ref SolvAttr in one \ref Solvable. */
88 LookupAttr( SolvAttr attr_r, Solvable solv_r )
89 : _attr( attr_r ), _solv( solv_r )
93 /** \name Search result. */
95 /** Result iterator. */
98 /** Iterator to the begin of query results. */
99 iterator begin() const;
101 /** Iterator behind the end of query results. */
102 iterator end() const;
104 /** Whether the query is empty. */
107 /** TransformIterator returning an \ref iterator vaue of type \c _ResultT. */
108 template<class _ResultT, class _AttrT> class transformIterator;
112 /** \name What to search. */
115 /** The \ref SolvAttr to search. */
116 SolvAttr attr() const
119 /** Set the \ref SolvAttr to search. */
120 void setAttr( SolvAttr attr_r )
125 /** \name Where to search. */
127 /** Wheter to search in \ref Pool. */
129 { return ! (_repo || _solv); }
131 /** Set search in \ref Pool (all repositories). */
134 _repo = Repository::noRepository;
135 _solv = Solvable::noSolvable;
138 /** Wheter to search in one \ref Repository. */
139 Repository repo() const
142 /** Set search in one \ref Repository. */
143 void setRepo( Repository repo_r )
146 _solv = Solvable::noSolvable;
149 /** Wheter to search in one \ref Solvable. */
150 Solvable solvable() const
153 /** Set search in one \ref Solvable. */
154 void setSolvable( Solvable solv_r )
156 _repo = Repository::noRepository;
166 ///////////////////////////////////////////////////////////////////
168 /** \relates LookupAttr Stream output. */
169 std::ostream & operator<<( std::ostream & str, const LookupAttr & obj );
171 /** \relates LookupAttr Verbose stream output including the query result. */
172 std::ostream & dumpOn( std::ostream & str, const LookupAttr & obj );
174 ///////////////////////////////////////////////////////////////////
176 // CLASS NAME : LookupAttr::iterator
179 * Extended iterator methods valid only if not @end.
180 * \note Implementation: Keep iterator_adaptor base and _dip in sync!
182 class LookupAttr::iterator : public boost::iterator_adaptor<
184 , ::_Dataiterator * // Base
185 , detail::IdType // Value
186 , boost::forward_traversal_tag // CategoryOrTraversal
187 , detail::IdType // Reference
191 /** \name Moving fast forward. */
193 /** On the next call to \ref operator++ advance to the next \ref SolvAttr. */
194 void nextSkipSolvAttr();
196 /** On the next call to \ref operator++ advance to the next \ref Solvable. */
197 void nextSkipSolvable();
199 /** On the next call to \ref operator++ advance to the next \ref Repository. */
202 /** Immediately advance to the next \ref SolvAttr. */
204 { nextSkipSolvAttr(); increment(); }
206 /** Immediately advance to the next \ref Solvable. */
208 { nextSkipSolvable(); increment(); }
210 /** Immediately advance to the next \ref Repository. */
212 { nextSkipRepo(); increment(); }
215 /** \name Current position info. */
217 /** The current \ref Repository. */
218 Repository inRepo() const;
220 /** The current \ref Solvabele. */
221 Solvable inSolvable() const;
223 /** The current \ref SolvAttr. */
224 SolvAttr inSolvAttr() const;
227 /** \name Test attribute value type. */
229 /** The current \ref SolvAttr type. */
230 detail::IdType solvAttrType() const;
232 /** Whether this is a numeric attribute (incl. boolean). */
233 bool solvAttrNumeric() const;
235 /** Whether this is a string attribute. */
236 bool solvAttrString() const;
238 /** *Whether this string attribute is available as \ref IdString. */
239 bool solvAttrIdString() const;
241 /** Whether this is a CheckSum attribute.*/
242 bool solvAttrCheckSum() const;
245 /** \name Retrieving attribute values. */
247 /** Conversion to numeric types. */
250 unsigned asUnsigned() const;
254 /** Conversion to string types. */
255 const char * c_str() const;
257 * If used with non-string types, this method tries to create
258 * some appropriate string representation.
260 std::string asString() const;
263 * This is only done for poolized string types. Large strings like
264 * summary or descriptions are not available via \ref IdStr, only
265 * via \ref c_str and \ref asString.
267 IdString idStr() const;
269 /** As \ref CheckSum. */
270 CheckSum asCheckSum() const;
272 /** Templated return type.
273 * Specialized for supported types.
275 template<class _Tp> _Tp asType() const;
278 ///////////////////////////////////////////////////////////////////
279 // internal stuff below
280 ///////////////////////////////////////////////////////////////////
284 iterator( const iterator & rhs );
286 iterator & operator=( const iterator & rhs );
291 friend class LookupAttr;
292 iterator( scoped_ptr< ::_Dataiterator> & dip_r, bool chain_r );
294 ::_Dataiterator * cloneFrom( const ::_Dataiterator * rhs );
297 friend class boost::iterator_core_access;
299 template <class OtherDerived, class OtherIterator, class V, class C, class R, class D>
300 bool equal( const boost::iterator_adaptor<OtherDerived, OtherIterator, V, C, R, D> & rhs ) const
302 return ( bool(base()) == bool(rhs.base()) )
303 && ( ! base() || dip_equal( *base(), *rhs.base() ) );
306 bool dip_equal( const ::_Dataiterator & lhs, const ::_Dataiterator & rhs ) const;
308 detail::IdType dereference() const;
313 /** Expert backdoor. */
314 const ::_Dataiterator * get() const
315 { return _dip.get(); }
317 scoped_ptr< ::_Dataiterator> _dip;
318 DefaultIntegral<bool,false> _chainRepos;
320 ///////////////////////////////////////////////////////////////////
322 /** \relates LookupAttr::iterator Stream output. */
323 std::ostream & operator<<( std::ostream & str, const LookupAttr::iterator & obj );
325 template<> inline int LookupAttr::iterator::asType<int>() const { return asInt(); }
326 template<> inline unsigned LookupAttr::iterator::asType<unsigned>() const { return asUnsigned(); }
327 template<> inline bool LookupAttr::iterator::asType<bool>() const { return asBool(); }
328 template<> inline const char * LookupAttr::iterator::asType<const char *>() const { return c_str(); }
329 template<> inline std::string LookupAttr::iterator::asType<std::string>() const { return asString(); }
330 template<> inline IdString LookupAttr::iterator::asType<IdString>() const { return idStr(); }
332 ///////////////////////////////////////////////////////////////////
334 // CLASS NAME : LookupAttr::transformIterator
336 /** TransformIterator returning an \ref iterator value of type \c _ResultT.
338 * The underlying LookupAttr::iterators value is retrieved \ref asType<_AttrT>
339 * and the returned \ref ResultT is constructed fron that value.
345 * Keywords( sat::Solvable solv_r )
346 * : _q( sat::SolvAttr::keywords, solv_r )
350 * typedef sat::LookupAttr::transformIterator<PackageKeyword,IdString> iterator;
352 * iterator begin() const { return iterator( _q.begin() ); }
353 * iterator end() const { return iterator( _q.end() ); }
356 * sat::LookupAttr _q;
360 * \see \ref ArrayAttr.
362 template<class _ResultT, class _AttrT>
363 class LookupAttr::transformIterator : public boost::iterator_adaptor<
364 transformIterator<_ResultT,_AttrT> // Derived
365 , LookupAttr::iterator // Base
367 , boost::forward_traversal_tag // CategoryOrTraversal
368 , _ResultT // Reference
376 transformIterator( const LookupAttr::iterator & val_r )
377 { this->base_reference() = val_r; }
381 /** \name Moving fast forward. */
383 /** On the next call to \ref operator++ advance to the next \ref SolvAttr. */
384 void nextSkipSolvAttr()
385 { this->base_reference().nextSkipSolvAttr(); }
387 /** On the next call to \ref operator++ advance to the next \ref Solvable. */
388 void nextSkipSolvable()
389 { this->base_reference().nextSkipSolvable(); }
391 /** On the next call to \ref operator++ advance to the next \ref Repository. */
393 { this->base_reference().nextSkipRepo(); }
395 /** Immediately advance to the next \ref SolvAttr. */
397 { this->base_reference().skipSolvAttr(); }
399 /** Immediately advance to the next \ref Solvable. */
401 { this->base_reference().skipSolvable(); }
403 /** Immediately advance to the next \ref Repository. */
405 { this->base_reference().skipRepo(); }
408 /** \name Current position info. */
410 /** The current \ref Repository. */
411 Repository inRepo() const
412 { return this->base_reference().inRepo(); }
414 /** The current \ref Solvabele. */
415 Solvable inSolvable() const
416 { return this->base_reference().inSolvable(); }
418 /** The current \ref SolvAttr. */
419 SolvAttr inSolvAttr() const
420 { return this->base_reference().inSolvAttr(); }
424 friend class boost::iterator_core_access;
426 _ResultT dereference() const
428 const LookupAttr::iterator lit( this->base_reference() );
429 return _ResultT( lit.asType<_AttrT>() );
432 ///////////////////////////////////////////////////////////////////
434 template<class _ResultT, class _AttrT>
437 template<class _ResultT, class _AttrT>
438 std::ostream & operator<<( std::ostream & str, const ArrayAttr<_ResultT,_AttrT> & obj );
440 ///////////////////////////////////////////////////////////////////
442 // CLASS NAME : ArrayAttr
444 /** \ref LookupAttr::transformIterator based container to retrieve list attributes.
447 * typedef ArrayAttr<PackageKeyword,IdString> Keywords;
448 * Keywords k( sat::SolvAttr::keywords );
449 * dumpRange( MIL << "All Keywords: ", k.begin(), k.end() ) << endl;
452 * \todo Maybe add some way to unify the result.
454 template<class _ResultT, class _AttrT>
457 friend std::ostream & operator<< <_ResultT,_AttrT>( std::ostream & str, const ArrayAttr<_ResultT,_AttrT> & obj );
463 ArrayAttr( SolvAttr attr_r )
467 ArrayAttr( SolvAttr attr_r, Repository repo_r )
468 : _q( attr_r, repo_r )
471 ArrayAttr( SolvAttr attr_r, Solvable solv_r )
472 : _q( attr_r, solv_r )
476 typedef sat::LookupAttr::transformIterator<_ResultT,_AttrT> iterator;
478 iterator begin() const
479 { return iterator( _q.begin() ); }
482 { return iterator( _q.end() ); }
485 { return _q.empty(); }
490 ///////////////////////////////////////////////////////////////////
492 /** \relates LookupAttr::iterator Stream output. */
493 template<class _ResultT, class _AttrT>
494 inline std::ostream & operator<<( std::ostream & str, const ArrayAttr<_ResultT,_AttrT> & obj )
495 { return dumpOn( str, obj._q); }
497 /////////////////////////////////////////////////////////////////
499 ///////////////////////////////////////////////////////////////////
500 /////////////////////////////////////////////////////////////////
502 ///////////////////////////////////////////////////////////////////
503 #endif // ZYPP_SAT_LOOKUPATTR_H