ac3dc1f9fc9170d2c79f8db8bc46a1a4208cb809
[platform/upstream/libzypp.git] / zypp / RelCompare.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/RelCompare.h
10  *
11 */
12 #ifndef ZYPP_RELCOMPARE_H
13 #define ZYPP_RELCOMPARE_H
14
15 #include <functional>
16
17 #include "zypp/Rel.h"
18
19 ///////////////////////////////////////////////////////////////////
20 namespace zypp
21 { /////////////////////////////////////////////////////////////////
22
23   /** \defgroup RelCompare Comparison using relational operator zypp::Rel.
24    *
25    * . Take a class like zypp::Edition. Editions are comaprable.
26    * You can compare them lexicographical, or according to their
27    * version and release values, or match them (i.e. taking empty
28    * version or release values as wildcard).
29    *
30    * No matter which way is appropriate within a certain context.
31    * You need functions to compare, and may want to use classes like
32    * zypp::Range, based on the desired comparison.
33    *
34    * All the class has to do, is providing a general comparison
35    * method (preferably static)
36    * \code
37    *     // Compare two elements returning -1, 0, 1
38    *     //  if the elemants compare <,==,>.
39    *     static int compare( const _Tp & lhs, const _Tp & rhs );
40    * \endcode
41    *
42    * <tt>Compare\<_Tp\></tt> provides a functor wrapping \c compare.
43    * In case the general comparison method is named differently, the
44    * class, or you, have to provide an approriate functor.
45    *
46    * <tt>compareByRel</tt> then compares two elements using a certain
47    * operator and general comparison method.
48    * \code
49    * compareByRel( Rel::EQ, lhs, rhs, Edition::compare );
50    * compareByRel( Rel::EQ, lhs, rhs, Edition::match );
51    * compareByRel( Rel::EQ, lhs, rhs ); // defaults to Compare\<Edition\>
52    *                                    // thus Edition::compare
53    * \endcode
54    *
55    * Furthermore a bunch of functors using a certain opertator is
56    * defined. All templated by type and general comparison
57    * method (defaults to Compare\<_Tp\>).
58    * \code
59    * // Editions sets use lexicographical order per default:
60    * std::set<Edition>
61    *
62    * // An Edition set using Edition::compare as order:
63    * std::set<Edition,CompareByLT<Edition> >;
64    *
65    * // Edition::match is not transitive, thus not an appropriate
66    * // order relation for std::set or std::map.
67    * \endcode
68    *
69    * Classes like zypp:Range are templated by  by type and general
70    * comparison method as well. Thus you may use Edition ranges based
71    * on Edition::Compare, as well as ranges based on Edition::Match
72    * (Edition provides these two functors).
73    *
74    * Again: Everything a class has to provide is the general
75    * comparison method. Comparison functors and ranges are then
76    * immediately available.
77   */
78   //@{
79
80   /** General compare functor returning <tt>-1, 0, 1</tt>.
81    * Expects _Tp::compare to be a static comaprison method
82    * returning <tt>-1, 0, 1</tt> if the elements compare
83    * <tt>\<,==,\></tt>.
84   */
85   template<class _Tp>
86     struct Compare : public std::binary_function<_Tp,_Tp,int>
87     {
88       int operator()( const _Tp & lhs, const _Tp & rhs ) const
89       { return _Tp::compare( lhs, rhs ); }
90     };
91
92   ///////////////////////////////////////////////////////////////////
93
94   /** Comparison of two elements using relational operator \a op.
95    * Expects \a _Compare to be a binary operator returning
96    * <tt>-1, 0, 1</tt> if the elemants compare <tt>\<,==,\></tt>.
97    * \code
98    *     // Signature of compare function or functor:
99    *     int compare( const _Tp & lhs, const _Tp & rhs );
100    * \endcode
101    * \li If \a op is Rel::ANY, the expression is always \c true.
102    * \li If \a op is Rel::NONE, the expression is always \c false.
103    * \li Otherwise the expression is evaluated using \a compare.
104    *
105    * \ingroup RelCompare
106   */
107   template<class _Tp, class _Compare>
108     inline bool compareByRel( Rel op, const _Tp & lhs, const _Tp & rhs,
109                               _Compare compare )
110     {
111       switch ( op.inSwitch() )
112       {
113       case Rel::EQ_e:
114         return compare( lhs, rhs ) == 0;
115         break;
116       case Rel::NE_e:
117         return compare( lhs, rhs ) != 0;
118         break;
119       case Rel::LT_e:
120         return compare( lhs, rhs ) < 0;
121         break;
122       case Rel::LE_e:
123         return compare( lhs, rhs ) <= 0;
124         break;
125       case Rel::GT_e:
126         return compare( lhs, rhs ) > 0;
127         break;
128       case Rel::GE_e:
129         return compare( lhs, rhs ) >= 0;
130         break;
131       case Rel::ANY_e:
132         return true;
133         break;
134       case Rel::NONE_e:
135         return false;
136         break;
137       }
138       return false;
139     }
140
141   /** \ref compareByRel convenience using Compare<_Tp> as general compare
142    *  functor.
143   */
144   template<class _Tp>
145     inline bool compareByRel( Rel op, const _Tp & lhs, const _Tp & rhs )
146     { return compareByRel( op, lhs, rhs, Compare<_Tp>() ); }
147
148   ///////////////////////////////////////////////////////////////////
149
150   ///////////////////////////////////////////////////////////////////
151
152   /** Functor to compare two elements by \ref Rel based on
153    * a general \a _Compare functor.
154    *
155    * Expects \a _Compare to be suitable for use in \ref compareByRel.
156    * Defaults to Compare\<_Tp\>.
157   */
158   template<class _Tp, class _Compare = Compare<_Tp> >
159     struct CompareBy : public std::binary_function<_Tp,_Tp,bool>
160     {
161       CompareBy( Rel op_r )
162       : _op( op_r )
163       {}
164
165       bool operator()( const _Tp & lhs, const _Tp & rhs ) const
166       { return compareByRel( _op, lhs, rhs, _Compare() ); }
167
168       Rel _op;
169     };
170
171   template<class _Tp, class _Compare = Compare<_Tp> >
172     struct CompareByEQ : public std::binary_function<_Tp,_Tp,bool>
173     {
174       bool operator()( const _Tp & lhs, const _Tp & rhs ) const
175       { return compareByRel( Rel::EQ, lhs, rhs, _Compare() ); }
176     };
177
178   template<class _Tp, class _Compare = Compare<_Tp> >
179     struct CompareByNE : public std::binary_function<_Tp,_Tp,bool>
180     {
181       bool operator()( const _Tp & lhs, const _Tp & rhs ) const
182       { return compareByRel( Rel::NE, lhs, rhs, _Compare() ); }
183     };
184
185   template<class _Tp, class _Compare = Compare<_Tp> >
186     struct CompareByLT : public std::binary_function<_Tp,_Tp,bool>
187     {
188       bool operator()( const _Tp & lhs, const _Tp & rhs ) const
189       { return compareByRel( Rel::LT, lhs, rhs, _Compare() ); }
190     };
191
192   template<class _Tp, class _Compare = Compare<_Tp> >
193     struct CompareByLE : public std::binary_function<_Tp,_Tp,bool>
194     {
195       bool operator()( const _Tp & lhs, const _Tp & rhs ) const
196       { return compareByRel( Rel::LE, lhs, rhs, _Compare() ); }
197     };
198
199   template<class _Tp, class _Compare = Compare<_Tp> >
200     struct CompareByGT : public std::binary_function<_Tp,_Tp,bool>
201     {
202       bool operator()( const _Tp & lhs, const _Tp & rhs ) const
203       { return compareByRel( Rel::GT, lhs, rhs, _Compare() ); }
204     };
205
206   template<class _Tp, class _Compare = Compare<_Tp> >
207     struct CompareByGE : public std::binary_function<_Tp,_Tp,bool>
208     {
209       bool operator()( const _Tp & lhs, const _Tp & rhs ) const
210       { return compareByRel( Rel::GE, lhs, rhs, _Compare() ); }
211     };
212
213   template<class _Tp, class _Compare = Compare<_Tp> >
214     struct CompareByANY : public std::binary_function<_Tp,_Tp,bool>
215     {
216       bool operator()( const _Tp & lhs, const _Tp & rhs ) const
217       { return compareByRel( Rel::ANY, lhs, rhs, _Compare() ); }
218     };
219
220   template<class _Tp, class _Compare = Compare<_Tp> >
221     struct CompareByNONE : public std::binary_function<_Tp,_Tp,bool>
222     {
223       bool operator()( const _Tp & lhs, const _Tp & rhs ) const
224       { return compareByRel( Rel::NONE, lhs, rhs, _Compare() ); }
225     };
226
227   ///////////////////////////////////////////////////////////////////
228
229   //@}
230   /////////////////////////////////////////////////////////////////
231 } // namespace zypp
232 ///////////////////////////////////////////////////////////////////
233 #endif // ZYPP_RELCOMPARE_H