Imported Upstream version 14.45.0
[platform/upstream/libzypp.git] / zypp / sat / LookupAttrTools.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/sat/LookupAttrTools.h
10  *
11 */
12 #ifndef ZYPP_SAT_LOOKUPATTRTOOLS_H
13 #define ZYPP_SAT_LOOKUPATTRTOOLS_H
14
15 #include "zypp/sat/LookupAttr.h"
16 #include "zypp/Repository.h"
17
18 ///////////////////////////////////////////////////////////////////
19 namespace zypp
20 { /////////////////////////////////////////////////////////////////
21   ///////////////////////////////////////////////////////////////////
22   namespace sat
23   { /////////////////////////////////////////////////////////////////
24
25     ///////////////////////////////////////////////////////////////////
26     //
27     //  CLASS NAME : LookupAttr::transformIterator
28     //
29     /** TransformIterator returning an \ref iterator value of type \c _ResultT.
30      *
31      * The underlying LookupAttr::iterators value is retrieved \ref asType<_AttrT>
32      * and the returned \ref ResultT is constructed fron that value.
33      *
34      * \code
35      *   class Keywords
36      *   {
37      *     public:
38      *       Keywords( sat::Solvable solv_r )
39      *       : _q( sat::SolvAttr::keywords, solv_r )
40      *       {}
41      *
42      *     public:
43      *       typedef sat::LookupAttr::transformIterator<PackageKeyword,IdString> iterator;
44      *
45      *       iterator begin() const { return iterator( _q.begin() ); }
46      *       iterator end() const   { return iterator( _q.end() ); }
47      *
48      *     private:
49      *       sat::LookupAttr _q;
50      *   };
51      * \endcode
52      *
53      * \see \ref ArrayAttr.
54      */
55     template<class _ResultT, class _AttrT>
56     class LookupAttr::transformIterator : public boost::iterator_adaptor<
57           transformIterator<_ResultT,_AttrT> // Derived
58           , LookupAttr::iterator         // Base
59           , _ResultT                     // Value
60           , boost::forward_traversal_tag // CategoryOrTraversal
61           , _ResultT                     // Reference
62     >
63     {
64       public:
65         transformIterator()
66         {}
67
68         explicit
69         transformIterator( const LookupAttr::iterator & val_r )
70         { this->base_reference() = val_r; }
71
72       public:
73
74         /** \name Moving fast forward. */
75         //@{
76         /** On the next call to \ref operator++ advance to the next \ref SolvAttr. */
77         void nextSkipSolvAttr()
78         { this->base_reference().nextSkipSolvAttr(); }
79
80         /** On the next call to \ref operator++ advance to the next \ref Solvable. */
81         void nextSkipSolvable()
82         { this->base_reference().nextSkipSolvable(); }
83
84         /** On the next call to \ref operator++ advance to the next \ref Repository. */
85         void nextSkipRepo()
86         { this->base_reference().nextSkipRepo(); }
87
88         /** Immediately advance to the next \ref SolvAttr. */
89         void skipSolvAttr()
90         { this->base_reference().skipSolvAttr(); }
91
92         /** Immediately advance to the next \ref Solvable. */
93         void skipSolvable()
94         { this->base_reference().skipSolvable(); }
95
96         /** Immediately advance to the next \ref Repository. */
97         void skipRepo()
98         { this->base_reference().skipRepo(); }
99         //@}
100
101         /** \name Current position info. */
102         //@{
103         /** The current \ref Repository. */
104         Repository inRepo() const
105         { return this->base_reference().inRepo(); }
106
107         /** The current \ref Solvabele. */
108         Solvable inSolvable() const
109         { return this->base_reference().inSolvable(); }
110
111         /** The current \ref SolvAttr. */
112         SolvAttr inSolvAttr() const
113         { return this->base_reference().inSolvAttr(); }
114         //@}
115
116       private:
117         friend class boost::iterator_core_access;
118
119         _ResultT dereference() const
120         {
121           const LookupAttr::iterator lit( this->base_reference() );
122           return _ResultT( lit.asType<_AttrT>() );
123         }
124     };
125     ///////////////////////////////////////////////////////////////////
126
127     template<class _ResultT, class _AttrT>
128     class ArrayAttr;
129
130     template<class _ResultT, class _AttrT>
131     std::ostream & operator<<( std::ostream & str, const ArrayAttr<_ResultT,_AttrT> & obj );
132
133     ///////////////////////////////////////////////////////////////////
134     //
135     //  CLASS NAME : ArrayAttr
136     //
137     /** \ref LookupAttr::transformIterator based container to retrieve list attributes.
138      *
139      * You may pass \ref LookupAttr::REPO_ATTR as \ref LookupAttr::Location argument,
140      * to lookup attributes associated with the \ref Repository as a whole
141      * (e.g. repository keywords).
142      *
143      * \see \ref LookupAttr for details.
144      *
145      * \code
146      *  typedef ArrayAttr<PackageKeyword,IdString> Keywords;
147      *  Keywords k( sat::SolvAttr::keywords );
148      *  dumpRange( MIL << "All Keywords: ", k.begin(), k.end() ) << endl;
149      * \endcode
150      *
151      * \todo Maybe add some way to unify the result.
152      */
153     template<class _ResultT, class _AttrT>
154     class ArrayAttr
155     {
156       friend std::ostream & operator<< <_ResultT,_AttrT>( std::ostream & str, const ArrayAttr<_ResultT,_AttrT> & obj );
157
158       public:
159         ArrayAttr()
160         {}
161
162         ArrayAttr( SolvAttr attr_r, LookupAttr::Location loc_r = LookupAttr::SOLV_ATTR )
163         : _q( attr_r, loc_r )
164         {}
165
166         ArrayAttr( SolvAttr attr_r, Repository repo_r, LookupAttr::Location loc_r = LookupAttr::SOLV_ATTR )
167         : _q( attr_r, repo_r, loc_r )
168         {}
169
170         ArrayAttr( SolvAttr attr_r, Solvable solv_r )
171         : _q( attr_r, solv_r )
172         {}
173
174       public:
175         typedef LookupAttr::transformIterator<_ResultT,_AttrT> iterator;
176         typedef LookupAttr::size_type size_type;
177
178         iterator begin() const
179         { return iterator( _q.begin() ); }
180
181         iterator end() const
182         { return iterator( _q.end() ); }
183
184         bool empty() const
185         { return _q.empty(); }
186
187         size_type size() const
188         {
189           size_type count = 0;
190           for_( it, begin(), end() )
191             ++count;
192           return count;
193         }
194
195       public:
196
197         iterator find( const _ResultT & key_r ) const
198         {
199           for_( it, begin(), end() )
200           {
201             if ( *it == key_r )
202               return it;
203           }
204           return end();
205         }
206
207       private:
208         LookupAttr _q;
209     };
210     ///////////////////////////////////////////////////////////////////
211
212     /** \relates LookupAttr::iterator Stream output. */
213     template<class _ResultT, class _AttrT>
214     inline std::ostream & operator<<( std::ostream & str, const ArrayAttr<_ResultT,_AttrT> & obj )
215     { return dumpOn( str, obj._q ); }
216
217     /////////////////////////////////////////////////////////////////
218   } // namespace sat
219   ///////////////////////////////////////////////////////////////////
220   /////////////////////////////////////////////////////////////////
221 } // namespace zypp
222 ///////////////////////////////////////////////////////////////////
223 #endif // ZYPP_SAT_LOOKUPATTRTOOLS_H