- Create the cache directly from the schema (installed) file.
[platform/upstream/libzypp.git] / zypp / ResStore.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/ResStore.h
10  *
11 */
12 #ifndef ZYPP_RESSTORE_H
13 #define ZYPP_RESSTORE_H
14
15 #include <iosfwd>
16 #include <set>
17
18 #include "zypp/base/PtrTypes.h"
19 #include "zypp/ResObject.h"
20 #include "zypp/ResFilters.h"
21
22 ///////////////////////////////////////////////////////////////////
23 namespace zypp
24 { /////////////////////////////////////////////////////////////////
25
26   ///////////////////////////////////////////////////////////////////
27   //
28   //    CLASS NAME : ResStore
29   //
30   /**
31   */
32   class ResStore
33   {
34     friend std::ostream & operator<<( std::ostream & str, const ResStore & obj );
35
36   public:
37     /** Implementation */
38     class Impl;
39
40     /** Type of Resolvable provided by ResStore. */
41     typedef ResObject                ResT;
42
43   private:
44     typedef std::set<ResT::Ptr>      StorageT;
45
46   public:
47
48     typedef StorageT::size_type      size_type;
49     typedef StorageT::iterator       iterator;
50     typedef StorageT::const_iterator const_iterator;
51     typedef boost::filter_iterator<resfilter::ResFilter, const_iterator>  resfilter_const_iterator;
52
53   public:
54     /** Default ctor */
55     ResStore();
56     /** Dtor */
57     ~ResStore();
58
59   public:
60     /**  */
61     iterator begin()
62     { return store().begin(); }
63     /**  */
64     iterator end()
65     { return store().end(); }
66     /**  */
67     const_iterator begin() const
68     { return store().begin(); }
69     /**  */
70     const_iterator end() const
71     { return store().end(); }
72
73     /**  */
74     bool empty() const
75     { return store().empty(); }
76     /**  */
77     size_type size() const
78     { return store().size(); }
79
80     // insert/erase
81     /**  */
82     iterator insert( const ResT::Ptr & ptr_r )
83     { return store().insert( ptr_r ).first; }
84     /**  */
85     template <class _InputIterator>
86       void insert( _InputIterator first_r, _InputIterator last_r )
87       { store().insert( first_r, last_r ); }
88     /**  */
89     size_type erase( const ResT::Ptr & ptr_r )
90     { return store().erase( ptr_r ); }
91     /**  */
92     void erase( iterator pos_r )
93     { store().erase( pos_r ); }
94     /**  */
95     void erase( iterator first_r, iterator last_r )
96     { store().erase( first_r, last_r ); }
97     /** Erase by Kind */
98     void erase( const Resolvable::Kind & kind_r )
99     {
100       for ( iterator it = begin(); it != end();  )
101         {
102           if ( (*it)->kind() == kind_r )
103             {
104               store().erase( it++ ); // postfix! Incrementing before erase
105             }
106           else
107             ++it;
108         }
109     }
110     /** Erase by Kind. */
111     template<class _Res>
112       void erase()
113       { erase( ResTraits<_Res>::kind ); }
114     /**  */
115     void clear()
116     { store().clear(); }
117
118     /** Query inerface.
119      * Both, \a filter_r and \a fnc_r are expected to be
120      * functions or functors taking a <tt>ResT::Ptr</tt>
121      * as argument and return a \c bool.
122      *
123      * forEach iterates over all ResTs and invokes \a fnc_r,
124      * iff \a filter_r returned \c true. If \a fnc_r returnes
125      * \c false the loop is aborted.
126      *
127      * forEach returns the number of \a fnc_r invocations. Positive
128      * if the loop succeeded. Negative if some call to \a fnc_r
129      * returned \c false.
130      +
131      * \see \ref RESFILTERS for a collection of predefined filters.
132      */
133     template <class _Function, class _Filter>
134       int forEach( _Filter filter_r, _Function fnc_r ) const
135       {
136         int cnt = 0;
137         for ( ResStore::const_iterator it = _store.begin(); it != _store.end(); ++it )
138           {
139             if ( filter_r( *it ) )
140               {
141                 ++cnt;
142                 if ( ! fnc_r( *it ) )
143                   return -cnt;
144               }
145           }
146         return cnt;
147       }
148
149     template <class _Function>
150       int forEach( _Function fnc_r ) const
151       {
152         int cnt = 0;
153         for ( ResStore::const_iterator it = _store.begin(); it != _store.end(); ++it )
154           {
155             ++cnt;
156             if ( ! fnc_r( *it ) )
157               return -cnt;
158           }
159         return cnt;
160       }
161
162   private:
163     /**  */
164     StorageT _store;
165     /**  */
166     StorageT & store()
167     { return _store; }
168     /**  */
169     const StorageT & store() const
170     { return _store; }
171
172   private:
173     /** Pointer to implementation */
174     RW_pointer<Impl> _pimpl; // currently unsused
175   };
176   ///////////////////////////////////////////////////////////////////
177
178   /** \relates ResStore Stream output */
179   std::ostream & operator<<( std::ostream & str, const ResStore & obj );
180
181   /////////////////////////////////////////////////////////////////
182 } // namespace zypp
183 ///////////////////////////////////////////////////////////////////
184 #endif // ZYPP_RESSTORE_H