backup
[platform/upstream/libzypp.git] / zypp / sat / Solvable.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/sat/Solvable.h
10  *
11 */
12 #ifndef ZYPP_SAT_SOLVABLE_H
13 #define ZYPP_SAT_SOLVABLE_H
14
15 #include <iosfwd>
16
17 #include "zypp/base/SafeBool.h"
18
19 #include "zypp/sat/detail/PoolMember.h"
20
21 ///////////////////////////////////////////////////////////////////
22 namespace zypp
23 { /////////////////////////////////////////////////////////////////
24   ///////////////////////////////////////////////////////////////////
25   namespace sat
26   { /////////////////////////////////////////////////////////////////
27
28     class PoolId : private base::SafeBool<PoolId>
29     {
30       public:
31         PoolId() : _id( 0 ) {}
32         PoolId( int id_r ) : _id( id_r ) {}
33         static const PoolId noid;
34         /** Evaluate \ref PoolId in a boolean context (\c != \c 0). */
35         using base::SafeBool<PoolId>::operator bool_type;
36         int get() const { return _id; }
37       private:
38         friend base::SafeBool<PoolId>::operator bool_type() const;
39         bool boolTest() const { return _id; }
40       private:
41         int _id;
42     };
43
44     /** \relates PoolId Stream output */
45     std::ostream & operator<<( std::ostream & str, const PoolId & obj );
46
47     /** \relates PoolId */
48     inline bool operator==( const PoolId & lhs, const PoolId & rhs )
49     { return lhs.get() == rhs.get(); }
50
51     /** \relates PoolId */
52     inline bool operator!=( const PoolId & lhs, const PoolId & rhs )
53     { return lhs.get() != rhs.get(); }
54
55     typedef PoolId NameId;
56     typedef PoolId EvrId;
57     typedef PoolId ArchId;
58     typedef PoolId VendorId;
59
60     ///////////////////////////////////////////////////////////////////
61     //
62     //  CLASS NAME : Solvable
63     //
64     /** */
65     class Solvable : protected detail::PoolMember,
66                      private base::SafeBool<Solvable>
67     {
68       public:
69         /** Default ctor creates \ref nosolvable.*/
70         Solvable()
71         : _id( detail::noSolvableId ) {}
72
73         /** \ref PoolImpl ctor. */
74         explicit Solvable( detail::SolvableIdType id_r )
75         : _id( id_r ) {}
76
77       public:
78         /** Represents no \ref Solvable. */
79         static const Solvable nosolvable;
80
81         /** Evaluate \ref Solvable in a boolean context (\c != \c nosolvable). */
82         using base::SafeBool<Solvable>::operator bool_type;
83
84       public:
85         NameId   name() const;
86         EvrId    evr() const;
87         ArchId   arch() const;
88         VendorId vendor() const;
89
90         Repo repo() const;
91
92       public:
93         const char * string( const PoolId & id_r ) const;
94
95         const char * nameStr() const { return string( name() ); }
96         const char * evrStr() const { return string( evr() ); }
97         const char * archStr() const { return string( arch() ); }
98         const char * vendorStr() const { return string( vendor() ); }
99
100       public:
101         /** Return next Solvable in \ref Pool (or \ref nosolvable). */
102         Solvable nextInPool() const;
103         /** Return next Solvable in \ref Repo (or \ref nosolvable). */
104         Solvable nextInRepo() const;
105       public:
106         /** Expert backdoor. */
107         ::_Solvable * get() const;
108         /** Expert backdoor. */
109         detail::SolvableIdType id() const { return _id; }
110       private:
111         friend base::SafeBool<Solvable>::operator bool_type() const;
112         bool boolTest() const { return get(); }
113       private:
114         detail::SolvableIdType _id;
115     };
116     ///////////////////////////////////////////////////////////////////
117
118     /** \relates Solvable Stream output */
119     std::ostream & operator<<( std::ostream & str, const Solvable & obj );
120
121     /** \relates Solvable */
122     inline bool operator==( const Solvable & lhs, const Solvable & rhs )
123     { return lhs.get() == rhs.get(); }
124
125     /** \relates Solvable */
126     inline bool operator!=( const Solvable & lhs, const Solvable & rhs )
127     { return lhs.get() != rhs.get(); }
128
129     ///////////////////////////////////////////////////////////////////
130     namespace detail
131     { /////////////////////////////////////////////////////////////////
132       ///////////////////////////////////////////////////////////////////
133       //
134       //        CLASS NAME : SolvableIterator
135       //
136       /** */
137       class SolvableIterator : public boost::iterator_adaptor<
138           SolvableIterator                   // Derived
139           , ::_Solvable*                     // Base
140           , Solvable                         // Value
141           , boost::single_pass_traversal_tag // CategoryOrTraversal
142           , Solvable                         // Reference
143           >
144       {
145         public:
146           SolvableIterator()
147           : SolvableIterator::iterator_adaptor_( 0 )
148           {}
149
150           explicit SolvableIterator( const Solvable & val_r )
151           : SolvableIterator::iterator_adaptor_( 0 )
152           { assignVal( val_r ); }
153
154           explicit SolvableIterator( SolvableIdType id_r )
155           : SolvableIterator::iterator_adaptor_( 0 )
156           { assignVal( Solvable( id_r ) ); }
157
158         private:
159           friend class boost::iterator_core_access;
160
161           void increment()
162           { assignVal( _val.nextInPool() ); }
163
164           Solvable dereference() const
165           { return _val; }
166
167           void assignVal( const Solvable & val_r )
168           { _val = val_r; base_reference() = _val.get(); }
169
170           Solvable _val;
171       };
172       ///////////////////////////////////////////////////////////////////
173       /////////////////////////////////////////////////////////////////
174     } // namespace detail
175     ///////////////////////////////////////////////////////////////////
176
177    /////////////////////////////////////////////////////////////////
178   } // namespace sat
179   ///////////////////////////////////////////////////////////////////
180   /////////////////////////////////////////////////////////////////
181 } // namespace zypp
182 ///////////////////////////////////////////////////////////////////
183 #endif // ZYPP_SAT_SOLVABLE_H