deprecate Repository::name() and use alias() to
[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          /** 
60           * Short unique, convenience string to refer to a repo.
61           * ie: openSUSE-10.3
62           *
63           * If you are looking for a label to display
64           * see \ref info() which provides \ref RepoInfo::name()
65           * ie: "openSUSE 10.3 Main repository"
66           *
67           */
68         std::string alias() const;
69
70          /** 
71           * Short unique, convenience string to refer to a repo.
72           * ie: openSUSE-10.3
73           *
74           * The sat solver uses name for what we know as alias
75           * In rpm repositories, name is a label string
76           * ie: "openSUSE 10.3 Main repository"
77           *
78           * We know follow rpm conventions and ignore satsolver
79           * wording for name.
80           *
81           * Use \ref alias() instead
82           */
83         ZYPP_DEPRECATED std::string name() const
84         { return alias(); }
85         
86
87         /** Whether \ref Repository contains solvables. */
88         bool solvablesEmpty() const;
89
90         /** Number of solvables in \ref Repository. */
91         size_type solvablesSize() const;
92
93         /** Iterator to the first \ref Solvable. */
94         SolvableIterator solvablesBegin() const;
95
96         /** Iterator behind the last \ref Solvable. */
97         SolvableIterator solvablesEnd() const;
98
99     public:
100         /** Return any associated \ref RepoInfo. */
101         RepoInfo info() const;
102
103         /** Set \ref RepoInfo for this repository.
104          * \throws Exception if this is \ref noRepository
105          * \throws Exception if the \ref RepoInfo::alias
106          *         does not match the \ref Repository::name.
107          */
108         void setInfo( const RepoInfo & info_r );
109
110         /** Remove any \ref RepoInfo set for this repository. */
111         void clearInfo();
112
113     public:
114         /** Remove this \ref Repository from it's \ref Pool. */
115         void eraseFromPool();
116
117         /** Functor calling \ref eraseFromPool. */
118         struct EraseFromPool;
119
120    public:
121         /** Return next Repository in \ref Pool (or \ref noRepository). */
122         Repository nextInPool() const;
123
124    public:
125         /** \name Repository content manipulating methods.
126          * \todo maybe a separate Repository/Solvable content manip interface
127          * provided by the pool.
128          */
129         //@{
130         /** Load \ref Solvables from a solv-file.
131          * In case of an exception the repository remains in the \ref Pool.
132          * \throws Exception if this is \ref noRepository
133          * \throws Exception if loading the solv-file fails.
134          * \see \ref Pool::addRepoSolv and \ref Repository::EraseFromPool
135          */
136         void addSolv( const Pathname & file_r );
137
138         /** Add \c count_r new empty \ref Solvable to this \ref Repository. */
139         sat::Solvable::IdType addSolvables( unsigned count_r );
140         /** \overload Add only one new \ref Solvable. */
141         sat::Solvable::IdType addSolvable()
142             { return addSolvables( 1 ); }
143         //@}
144
145     public:
146         /** Expert backdoor. */
147         ::_Repo * get() const;
148         /** Expert backdoor. */
149         IdType id() const { return _id; }
150     private:
151         friend base::SafeBool<Repository>::operator bool_type() const;
152         bool boolTest() const { return get(); }
153     private:
154         IdType _id;
155     };
156     ///////////////////////////////////////////////////////////////////
157
158     /** \relates Repository Stream output */
159     std::ostream & operator<<( std::ostream & str, const Repository & obj );
160
161     /** \relates Repository */
162     inline bool operator==( const Repository & lhs, const Repository & rhs )
163     { return lhs.get() == rhs.get(); }
164
165     /** \relates Repository */
166     inline bool operator!=( const Repository & lhs, const Repository & rhs )
167     { return lhs.get() != rhs.get(); }
168
169     /** \relates Repository */
170     inline bool operator<( const Repository & lhs, const Repository & rhs )
171     { return lhs.get() < rhs.get(); }
172
173     ///////////////////////////////////////////////////////////////////
174     //
175     //  CLASS NAME : Repository::EraseFromPool
176     //
177     /** Functor removing \ref Repository from it's \ref Pool.
178      *
179      * E.g. used as dispose function in. \ref AutoDispose
180      * to provide a convenient and exception safe temporary
181      * \ref Repository.
182      * \code
183      *  sat::Pool satpool;
184      *  MIL << "1 " << satpool << endl;
185      *  {
186      *    AutoDispose<Repository> tmprepo( (Repository::EraseFromPool()) );
187      *    *tmprepo = satpool.reposInsert( "A" );
188      *    tmprepo->addSolv( "sl10.1-beta7-packages.solv" );
189      *    DBG << "2 " << satpool << endl;
190      *    // Calling 'tmprepo.resetDispose();' here
191      *    // would keep the Repo.
192      *  }
193      *  MIL << "3 " << satpool << endl;
194      * \endcode
195      * \code
196      * 1 sat::pool(){0repos|2slov}
197      * 2 sat::pool(){1repos|2612slov}
198      * 3 sat::pool(){0repos|2slov}
199      * \endcode
200      * Leaving the block without calling <tt>tmprepo.resetDispose();</tt>
201      * before, will automatically remove the \ref Repo from it's \ref Pool.
202      */
203     struct Repository::EraseFromPool
204     {
205         void operator()( Repository repository_r ) const
206             { repository_r.eraseFromPool(); }
207     };
208     ///////////////////////////////////////////////////////////////////
209     ///////////////////////////////////////////////////////////////////
210     namespace detail
211     { /////////////////////////////////////////////////////////////////
212       ///////////////////////////////////////////////////////////////////
213       //
214       //        CLASS NAME : RepositoryIterator
215       //
216       /** */
217         class RepositoryIterator : public boost::iterator_adaptor<
218             RepositoryIterator                            // Derived
219                            , ::_Repo **                   // Base
220                            , Repository                   // Value
221                            , boost::forward_traversal_tag // CategoryOrTraversal
222                            , Repository                   // Reference
223                              >
224         {
225         public:
226             RepositoryIterator()
227                 : RepositoryIterator::iterator_adaptor_( 0 )
228                 {}
229
230             explicit RepositoryIterator( ::_Repo ** p )
231                 : RepositoryIterator::iterator_adaptor_( p )
232                 {}
233
234         private:
235             friend class boost::iterator_core_access;
236
237             Repository dereference() const
238                 { return Repository( *base() ); }
239         };
240         ///////////////////////////////////////////////////////////////////
241         ///////////////////////////////////////////////////////////////////
242         //
243         //      CLASS NAME : ByRepository
244         //
245         /** Functor filtering \ref Solvable by \ref Repository.*/
246         struct ByRepository
247         {
248         public:
249             ByRepository( const Repository & repository_r ) : _repository( repository_r ) {}
250             ByRepository( sat::detail::RepoIdType id_r ) : _repository( id_r ) {}
251             ByRepository() {}
252
253             bool operator()( const sat::Solvable & slv_r ) const
254                 { return slv_r.repository() == _repository; }
255
256         private:
257             Repository _repository;
258         };
259         ///////////////////////////////////////////////////////////////////
260         /////////////////////////////////////////////////////////////////
261     } // namespace detail
262     ///////////////////////////////////////////////////////////////////
263
264     /////////////////////////////////////////////////////////////////
265 } // namespace zypp
266 ///////////////////////////////////////////////////////////////////
267 #endif // ZYPP_SAT_REPOSITORY_H