8214210bfc2537f08b7b527363aa587cb62f93a3
[platform/upstream/libzypp.git] / zypp / Repository.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/Repository.h
10  *
11 */
12 #ifndef ZYPP_SAT_REPOSITORY_H
13 #define ZYPP_SAT_REPOSITORY_H
14
15 #include <iosfwd>
16 #include "zypp/base/SafeBool.h"
17 #include "zypp/Pathname.h"
18 #include "zypp/sat/detail/PoolMember.h"
19 #include "zypp/sat/Solvable.h"
20 #include "zypp/RepoInfo.h"
21
22 ///////////////////////////////////////////////////////////////////
23 namespace zypp
24 { /////////////////////////////////////////////////////////////////
25
26     ///////////////////////////////////////////////////////////////////
27     //
28     //  CLASS NAME : Repository
29     //
30     /** */
31     class Repository : protected sat::detail::PoolMember,
32                        private base::SafeBool<Repository>
33     {
34     public:
35         typedef filter_iterator<detail::ByRepository, sat::detail::SolvableIterator> SolvableIterator;
36         typedef sat::detail::size_type size_type;
37         typedef sat::detail::RepoIdType IdType;
38
39     public:
40         /** Default ctor creates \ref noRepository.*/
41         Repository()
42         : _id( sat::detail::noRepoId ) {}
43
44         /** \ref PoolImpl ctor. */
45         explicit Repository( IdType id_r )
46         : _id( id_r ) {}
47
48     public:
49         /** Represents no \ref Repository. */
50         static const Repository noRepository;
51
52         /** Evaluate \ref Repository in a boolean context (\c != \c noRepository). */
53         using base::SafeBool<Repository>::operator bool_type;
54
55         /** Return whether this is the system repository. */
56         bool isSystemRepo() const;
57
58     public:
59         /** The repos name (alias).
60          * \todo FIXME why is this not called alias if it <b>is</b> alias?
61          */
62         std::string name() const;
63
64         /** Whether \ref Repository contains solvables. */
65         bool solvablesEmpty() const;
66
67         /** Number of solvables in \ref Repository. */
68         size_type solvablesSize() const;
69
70         /** Iterator to the first \ref Solvable. */
71         SolvableIterator solvablesBegin() const;
72
73         /** Iterator behind the last \ref Solvable. */
74         SolvableIterator solvablesEnd() const;
75
76     public:
77         /** Return any associated \ref RepoInfo. */
78         RepoInfo info() const;
79
80         /** Set \ref RepoInfo for this repository.
81          * \throws Exception if this is \ref noRepository
82          * \throws Exception if the \ref RepoInfo::alias
83          *         does not match the \ref Repository::name.
84          */
85         void setInfo( const RepoInfo & info_r );
86
87         /** Remove any \ref RepoInfo set for this repository. */
88         void clearInfo();
89
90     public:
91         /** Remove this \ref Repository from it's \ref Pool. */
92         void eraseFromPool();
93
94         /** Functor calling \ref eraseFromPool. */
95         struct EraseFromPool;
96
97    public:
98         /** Return next Repository in \ref Pool (or \ref noRepository). */
99         Repository nextInPool() const;
100
101    public:
102         /** \name Repository content manipulating methods.
103          * \todo maybe a separate Repository/Solvable content manip interface
104          * provided by the pool.
105          */
106         //@{
107         /** Load \ref Solvables from a solv-file.
108          * In case of an exception the repository remains in the \ref Pool.
109          * \throws Exception if this is \ref noRepository
110          * \throws Exception if loading the solv-file fails.
111          * \see \ref Pool::addRepoSolv and \ref Repository::EraseFromPool
112          */
113         void addSolv( const Pathname & file_r );
114
115         /** Add \c count_r new empty \ref Solvable to this \ref Repository. */
116         sat::Solvable::IdType addSolvables( unsigned count_r );
117         /** \overload Add only one new \ref Solvable. */
118         sat::Solvable::IdType addSolvable()
119             { return addSolvables( 1 ); }
120         //@}
121
122     public:
123         /** Expert backdoor. */
124         ::_Repo * get() const;
125         /** Expert backdoor. */
126         IdType id() const { return _id; }
127     private:
128         friend base::SafeBool<Repository>::operator bool_type() const;
129         bool boolTest() const { return get(); }
130     private:
131         IdType _id;
132     };
133     ///////////////////////////////////////////////////////////////////
134
135     /** \relates Repository Stream output */
136     std::ostream & operator<<( std::ostream & str, const Repository & obj );
137
138     /** \relates Repository */
139     inline bool operator==( const Repository & lhs, const Repository & rhs )
140     { return lhs.get() == rhs.get(); }
141
142     /** \relates Repository */
143     inline bool operator!=( const Repository & lhs, const Repository & rhs )
144     { return lhs.get() != rhs.get(); }
145
146     /** \relates Repository */
147     inline bool operator<( const Repository & lhs, const Repository & rhs )
148     { return lhs.get() < rhs.get(); }
149
150     ///////////////////////////////////////////////////////////////////
151     //
152     //  CLASS NAME : Repository::EraseFromPool
153     //
154     /** Functor removing \ref Repository from it's \ref Pool.
155      *
156      * E.g. used as dispose function in. \ref AutoDispose
157      * to provide a convenient and exception safe temporary
158      * \ref Repository.
159      * \code
160      *  sat::Pool satpool;
161      *  MIL << "1 " << satpool << endl;
162      *  {
163      *    AutoDispose<Repository> tmprepo( (Repository::EraseFromPool()) );
164      *    *tmprepo = satpool.reposInsert( "A" );
165      *    tmprepo->addSolv( "sl10.1-beta7-packages.solv" );
166      *    DBG << "2 " << satpool << endl;
167      *    // Calling 'tmprepo.resetDispose();' here
168      *    // would keep the Repo.
169      *  }
170      *  MIL << "3 " << satpool << endl;
171      * \endcode
172      * \code
173      * 1 sat::pool(){0repos|2slov}
174      * 2 sat::pool(){1repos|2612slov}
175      * 3 sat::pool(){0repos|2slov}
176      * \endcode
177      * Leaving the block without calling <tt>tmprepo.resetDispose();</tt>
178      * before, will automatically remove the \ref Repo from it's \ref Pool.
179      */
180     struct Repository::EraseFromPool
181     {
182         void operator()( Repository repository_r ) const
183             { repository_r.eraseFromPool(); }
184     };
185     ///////////////////////////////////////////////////////////////////
186     ///////////////////////////////////////////////////////////////////
187     namespace detail
188     { /////////////////////////////////////////////////////////////////
189       ///////////////////////////////////////////////////////////////////
190       //
191       //        CLASS NAME : RepositoryIterator
192       //
193       /** */
194         class RepositoryIterator : public boost::iterator_adaptor<
195             RepositoryIterator                            // Derived
196                            , ::_Repo **                   // Base
197                            , Repository                   // Value
198                            , boost::forward_traversal_tag // CategoryOrTraversal
199                            , Repository                   // Reference
200                              >
201         {
202         public:
203             RepositoryIterator()
204                 : RepositoryIterator::iterator_adaptor_( 0 )
205                 {}
206
207             explicit RepositoryIterator( ::_Repo ** p )
208                 : RepositoryIterator::iterator_adaptor_( p )
209                 {}
210
211         private:
212             friend class boost::iterator_core_access;
213
214             Repository dereference() const
215                 { return Repository( *base() ); }
216         };
217         ///////////////////////////////////////////////////////////////////
218         ///////////////////////////////////////////////////////////////////
219         //
220         //      CLASS NAME : ByRepository
221         //
222         /** Functor filtering \ref Solvable by \ref Repository.*/
223         struct ByRepository
224         {
225         public:
226             ByRepository( const Repository & repository_r ) : _repository( repository_r ) {}
227             ByRepository( sat::detail::RepoIdType id_r ) : _repository( id_r ) {}
228             ByRepository() {}
229
230             bool operator()( const sat::Solvable & slv_r ) const
231                 { return slv_r.repository() == _repository; }
232
233         private:
234             Repository _repository;
235         };
236         ///////////////////////////////////////////////////////////////////
237         /////////////////////////////////////////////////////////////////
238     } // namespace detail
239     ///////////////////////////////////////////////////////////////////
240
241     /////////////////////////////////////////////////////////////////
242 } // namespace zypp
243 ///////////////////////////////////////////////////////////////////
244 #endif // ZYPP_SAT_REPOSITORY_H