- get rid of Impl * in the PoolQueryIterator, copy the necessary data
[platform/upstream/libzypp.git] / zypp / PoolQuery.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/PoolQuery.h
10  *
11 */
12 #ifndef ZYPP_POOLQUERY_H
13 #define ZYPP_POOLQUERY_H
14
15 #include "base/Regex.h"
16
17 #include "zypp/Resolvable.h"
18 #include "zypp/sat/SolvAttr.h"
19 #include "zypp/sat/SolvIterMixin.h"
20 #include "zypp/sat/LookupAttr.h"
21
22 #include "zypp/base/PtrTypes.h"
23 #include "zypp/base/Function.h"
24
25 extern "C"
26 {
27 struct _Dataiterator;
28 }
29
30 ///////////////////////////////////////////////////////////////////
31 namespace zypp
32 { /////////////////////////////////////////////////////////////////
33
34   namespace detail
35   {
36     class PoolQueryIterator;
37   }
38
39   ///////////////////////////////////////////////////////////////////
40   //
41   //  CLASS NAME : PoolQuery
42   //
43   /**
44    * Meta-data query API. Returns solvables of specified kinds from specified
45    * repositories with attributes matching the specified search strings.
46    *
47    * TODO: details, examples.
48    */
49   class PoolQuery : public sat::SolvIterMixin<PoolQuery, detail::PoolQueryIterator>
50   {
51   public:
52     typedef std::set<std::string>                           StrContainer;
53     typedef std::set<Resolvable::Kind>                      Kinds;
54     typedef std::map<sat::SolvAttr, StrContainer>           AttrRawStrMap;
55     typedef std::map<sat::SolvAttr, std::string>            AttrCompiledStrMap;
56     typedef std::map<sat::SolvAttr, str::regex>             AttrRegexMap;
57
58     typedef detail::PoolQueryIterator                       const_iterator;
59     typedef unsigned int                                    size_type;
60
61   public:
62     typedef function<bool( const sat::Solvable & )> ProcessResolvable;
63
64     PoolQuery();
65     ~PoolQuery();
66
67     /** Query result accessers. */
68     //@{
69     class PoolQueryIterator;
70
71     /** */
72     const_iterator begin() const;
73     /** */
74     const_iterator end() const;
75     /** */
76     bool empty();
77     /** */
78     size_type size();
79     //@}
80
81     /**
82      * executes the query with the current settings
83      * results are yielded on the callback passed on
84      * construction
85      */
86     void execute(ProcessResolvable fnc);
87
88     /**
89      * Filter by selectable kind.
90      *
91      * By default, all kinds will be returned. If addKind() is used,
92      * only the specified kinds will be returned (multiple kinds will be ORed).
93      *
94      * Pass ResTraits<T>::kind to this method, where T is one of the
95      * \ref Resolvable child classes (e.g. ResTraits<Pattern>::kind).
96      */
97     void addKind(const Resolvable::Kind &kind);
98
99     /**
100      * Filter by repo.
101      *
102      * By default, all repos will be returned. If addRepo() is used,
103      * only the specified repo will be returned (multiple repos will be ORed).
104      */
105     void addRepo(const std::string &repoalias);
106
107     /** Installed status filter setters. */
108     //@{
109
110     /**
111      * Filter by status (installed uninstalled)
112      */
113     enum StatusFilter {
114       ALL = 0, // both install filter and uninstall filter bits are 0
115       INSTALLED_ONLY = 1,
116       UNINSTALLED_ONLY = 2
117     };
118     void setInstalledOnly();
119     void setUninstalledOnly();
120     void setStatusFilterFlags( StatusFilter flags );
121
122     //@}
123
124     /**
125      * 
126      */
127     void addString(const std::string & value);
128
129     /**
130      * Filter by the \a value of any available solvable attribute.
131      *
132      * \note Solvables of a kind not supporting the specified attribute will
133      * <b>not</b> be returned.
134      * \todo check the above
135      *
136      * \param attr Attribute identfier. Use sat::Solvattr::* constants
137      * \param value What to search for.
138      */
139     void addAttribute(const sat::SolvAttr & attr, const std::string & value = "");
140
141     /**
142      * Filter by Selectable status.
143      *
144      * This should cover also plain 'is installed' and 'not installed' statuses.
145      *
146      * \param status Selectable status (zypp::ui::Status enum)
147      */
148     //void addStatus(const Status status);
149
150     /**
151      * Add dependency filter.
152      *
153      * \param dtype   depenedcy type
154      * \param name    depenency name
155      * \param edition edition for a versioned dependency
156      * \param rel     operand for a versioned dependency
157      *
158      * \todo maybe a isRegexp bool as in addName() for the name parameter would
159      *       be handy here as well.
160      * \todo add more addDependecy() variants
161      *//*
162     void addDependency(const Dep & dtype,
163                        const std::string & name,
164                        const Edition & edition = Edition(),
165                        const Rel & rel = Rel::EQ);
166 */
167
168
169     /** \name Text Matching Options */
170     //@{
171     /**
172      * Turn case sentitivity on or off (unsets or sets \ref SEARCH_NOCASE flag).
173      * PoolQuery defaults to case insensitive search unless this method
174      * is used.
175      *
176      * \param value Whether to turn the case sensitivity on (default) or off.
177      */
178     void setCaseSensitive(const bool value = true);
179
180     /** Set to match exact string instead of substring.*/
181     void setMatchExact();
182     /** Set to substring (the default). */
183     void setMatchSubstring();
184     /** Set to match globs. */
185     void setMatchGlob();
186     /** Set to use the query strings as regexes */
187     void setMatchRegex();
188     /** Set to match words (uses regex) */
189     void setMatchWord();
190     //void setLocale(const Locale & locale);
191     //@}
192
193
194     /**
195      * Require that all of the values set by addString, addAttribute, addDep
196      * match the values of respective attributes. 
197      */
198     void setRequireAll(const bool require_all = true);
199
200     /** \name getters */
201     //@{
202
203     /** Search strings added via addString() */
204     const StrContainer & strings() const;
205     /**
206      * Map (map<SolvAttr, StrContainer>) of attribute values added via
207      * addAttribute(), addDep in string form */
208     const AttrRawStrMap & attributes() const;
209
210     const Kinds & kinds() const;
211
212     const StrContainer & repos() const;
213
214     bool caseSensitive() const;
215
216     bool matchExact() const;
217     bool matchSubstring() const;
218     bool matchGlob() const;
219     bool matchRegex() const;
220     /**
221      * Returns currently used string matching type.
222      * \see satsolver/repo.h
223      */
224     int  matchType() const;
225
226     bool matchWord() const;
227
228     bool requireAll() const;
229
230     StatusFilter statusFilterFlags() const;
231     //@}
232
233     /**
234      * Reads from stream query. Attributes is sepated by delim. Query is
235      * separated by two delim.
236      *
237      * \param str input stream which contains query
238      * \param delim delimeter for attributes
239      * \return true if non-empty query is recovered
240      *
241      * \see readPoolQueriesFromFile
242      */
243     bool recover( std::istream &str, char delim = '\n' );
244
245     /**
246      * Writes a machine-readable string representation of the query to stream.
247      * Use \a delim as attribute delimiter.
248      *
249      * \param str output stream to write to
250      * \param delim delimiter for attributes
251      *
252      * \see writePoolQueriesToFile
253      */
254     void serialize( std::ostream &str, char delim = '\n' ) const;
255
256     /** Return a human-readable description of the query */
257     std::string asString() const;
258
259     bool operator==(const PoolQuery& b) const;
260     bool operator!=(const PoolQuery& b) const { return !(*this == b ); }
261
262     // low level API
263
264     /**
265      * Free function to set the satsolver repo search
266      * flags.
267      *
268      * \see SEARCH_STRINGMASK
269      * \see SEARCH_STRING
270      * \see SEARCH_SUBSTRING
271      * \see SEARCH_GLOB
272      * \see SEARCH_REGEX
273      * \see SEARCH_NOCASE
274      * \see SEARCH_NO_STORAGE_SOLVABLE
275      */
276     void setFlags(int flags);
277
278     class Impl;
279   private:
280     /** Pointer to implementation */
281     RW_pointer<Impl> _pimpl;
282   };
283   ///////////////////////////////////////////////////////////////////
284
285   /** \relates PoolQuery Stream output. */
286   std::ostream & operator<<( std::ostream & str, const PoolQuery & obj );
287
288
289
290   ///////////////////////////////////////////////////////////////////
291   namespace detail
292   { /////////////////////////////////////////////////////////////////
293
294
295   ///////////////////////////////////////////////////////////////////
296   //
297   //  CLASS NAME : PoolQuery::PoolQueryIterator
298   //
299   /**
300    * 
301    */
302   class PoolQueryIterator : public boost::iterator_adaptor<
303     PoolQueryIterator                  // Derived
304     , sat::LookupAttr::iterator        // Base
305     , const sat::Solvable              // Value
306     , boost::forward_traversal_tag     // CategoryOrTraversal
307     , const sat::Solvable              // Reference
308   >
309   {
310   public:
311     PoolQueryIterator();
312
313     PoolQueryIterator(const PoolQueryIterator &);
314
315     explicit
316     PoolQueryIterator( const sat::LookupAttr::iterator & val_r )
317     { this->base_reference() = val_r; }
318
319     ~PoolQueryIterator();
320
321     PoolQueryIterator & operator=( const PoolQueryIterator & rhs );
322
323   private:
324     friend class boost::iterator_core_access;
325     friend class PoolQuery::Impl;
326
327     PoolQueryIterator(
328         scoped_ptr< ::_Dataiterator> & dip_r,
329         const PoolQuery::Impl * pqimpl);
330
331     const sat::Solvable dereference() const
332     {
333       return _sid ? sat::Solvable(_sid) : sat::Solvable::noSolvable;
334     }
335
336     void increment();
337
338     bool matchSolvable();
339
340   private:
341     /** current matching solvable id */
342     int _sid;
343     /** whether there is a next solvable to check */
344     bool _has_next;
345     /** whether to do text matching on our own (true) or the Dataiterator already did it */
346     bool _do_matching;
347
348     /** \name Query Data
349      * Depending on whether regexes are used in the search either \ref _str or
350      * \ref _regex (or either _attrs_str or _attrs_regex respectively) are used.
351      */
352     //@{
353
354     /** string matching option flags */
355     int _flags;
356     /** global query string compiled */
357     std::string _str;
358     /** global query compiled regex */
359     str::regex _regex;
360     /** Attribute to string map holding per-attribute query strings (compiled) */
361     PoolQuery::AttrCompiledStrMap _attrs_str;
362     /** Attribute to regex map holding per-attribute compiled regex */
363     PoolQuery::AttrRegexMap _attrs_regex;
364     /** Set of repository names include in the search. */
365     PoolQuery::StrContainer _repos;
366     /** Set of solvable kinds to include in the search. */
367     PoolQuery::Kinds _kinds;
368     /** Installed status filter flags. \see PoolQuery::StatusFilter */
369     int _status_flags;
370     //@}
371
372     /** used to copy current iterator in order to forward check for next attributes */
373     sat::LookupAttr::iterator _tmpit;
374   };
375   ///////////////////////////////////////////////////////////////////
376
377   ///////////////////////////////////////////////////////////////////
378   } //namespace detail
379   ///////////////////////////////////////////////////////////////////
380
381   /////////////////////////////////////////////////////////////////
382 } // namespace zypp
383 ///////////////////////////////////////////////////////////////////
384
385 #endif // ZYPP_POOLQUERY_H