1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/RelCompare.h
12 #ifndef ZYPP_RELCOMPARE_H
13 #define ZYPP_RELCOMPARE_H
19 ///////////////////////////////////////////////////////////////////
21 { /////////////////////////////////////////////////////////////////
23 /** \defgroup RelCompare Comparison using relational operator zypp::Rel.
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).
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.
34 * All the class has to do, is providing a general comparison
35 * method (preferably static)
37 * // Compare two elements returning -1, 0, 1
38 * // if the elemants compare <,==,>.
39 * static int compare( const Tp & lhs, const Tp & rhs );
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.
46 * <tt>compareByRel</tt> then compares two elements using a certain
47 * operator and general comparison method.
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
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\>).
59 * // Editions sets use lexicographical order per default:
62 * // An Edition set using Edition::compare as order:
63 * std::set<Edition,CompareByLT<Edition> >;
65 * // Edition::match is not transitive, thus not an appropriate
66 * // order relation for std::set or std::map.
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).
74 * Again: Everything a class has to provide is the general
75 * comparison method. Comparison functors and ranges are then
76 * immediately available.
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
86 struct Compare : public std::binary_function<Tp,Tp,int>
88 int operator()( const Tp & lhs, const Tp & rhs ) const
89 { return Tp::compare( lhs, rhs ); }
92 ///////////////////////////////////////////////////////////////////
94 /** Comparison of two elements using relational operator \a op.
95 * Expects \a TCompare to be a binary operator returning
96 * <tt>-1, 0, 1</tt> if the elemants compare <tt>\<,==,\></tt>.
98 * // Signature of compare function or functor:
99 * int compare( const Tp & lhs, const Tp & rhs );
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.
105 * \ingroup RelCompare
107 template<class Tp, class TCompare>
108 inline bool compareByRel( Rel op, const Tp & lhs, const Tp & rhs, TCompare compare )
110 switch ( op.inSwitch() )
113 return compare( lhs, rhs ) == 0;
116 return compare( lhs, rhs ) != 0;
119 return compare( lhs, rhs ) < 0;
122 return compare( lhs, rhs ) <= 0;
125 return compare( lhs, rhs ) > 0;
128 return compare( lhs, rhs ) >= 0;
140 /** \ref compareByRel convenience using Compare<Tp> as general compare
144 inline bool compareByRel( Rel op, const Tp & lhs, const Tp & rhs )
145 { return compareByRel( op, lhs, rhs, Compare<Tp>() ); }
147 ///////////////////////////////////////////////////////////////////
149 ///////////////////////////////////////////////////////////////////
151 /** Functor to compare two elements by \ref Rel based on
152 * a general \a TCompare functor.
154 * Expects \a TCompare to be suitable for use in \ref compareByRel.
155 * Defaults to Compare\<Tp\>.
157 template<class Tp, class TCompare = Compare<Tp> >
158 struct CompareBy : public std::binary_function<Tp,Tp,bool>
160 CompareBy( Rel op_r )
164 bool operator()( const Tp & lhs, const Tp & rhs ) const
165 { return compareByRel( _op, lhs, rhs, TCompare() ); }
170 template<class Tp, class TCompare = Compare<Tp> >
171 struct CompareByEQ : public std::binary_function<Tp,Tp,bool>
173 bool operator()( const Tp & lhs, const Tp & rhs ) const
174 { return compareByRel( Rel::EQ, lhs, rhs, TCompare() ); }
177 template<class Tp, class TCompare = Compare<Tp> >
178 struct CompareByNE : public std::binary_function<Tp,Tp,bool>
180 bool operator()( const Tp & lhs, const Tp & rhs ) const
181 { return compareByRel( Rel::NE, lhs, rhs, TCompare() ); }
184 template<class Tp, class TCompare = Compare<Tp> >
185 struct CompareByLT : public std::binary_function<Tp,Tp,bool>
187 bool operator()( const Tp & lhs, const Tp & rhs ) const
188 { return compareByRel( Rel::LT, lhs, rhs, TCompare() ); }
191 template<class Tp, class TCompare = Compare<Tp> >
192 struct CompareByLE : public std::binary_function<Tp,Tp,bool>
194 bool operator()( const Tp & lhs, const Tp & rhs ) const
195 { return compareByRel( Rel::LE, lhs, rhs, TCompare() ); }
198 template<class Tp, class TCompare = Compare<Tp> >
199 struct CompareByGT : public std::binary_function<Tp,Tp,bool>
201 bool operator()( const Tp & lhs, const Tp & rhs ) const
202 { return compareByRel( Rel::GT, lhs, rhs, TCompare() ); }
205 template<class Tp, class TCompare = Compare<Tp> >
206 struct CompareByGE : public std::binary_function<Tp,Tp,bool>
208 bool operator()( const Tp & lhs, const Tp & rhs ) const
209 { return compareByRel( Rel::GE, lhs, rhs, TCompare() ); }
212 template<class Tp, class TCompare = Compare<Tp> >
213 struct CompareByANY : public std::binary_function<Tp,Tp,bool>
215 bool operator()( const Tp & lhs, const Tp & rhs ) const
216 { return compareByRel( Rel::ANY, lhs, rhs, TCompare() ); }
219 template<class Tp, class TCompare = Compare<Tp> >
220 struct CompareByNONE : public std::binary_function<Tp,Tp,bool>
222 bool operator()( const Tp & lhs, const Tp & rhs ) const
223 { return compareByRel( Rel::NONE, lhs, rhs, TCompare() ); }
226 ///////////////////////////////////////////////////////////////////
229 /////////////////////////////////////////////////////////////////
231 ///////////////////////////////////////////////////////////////////
232 #endif // ZYPP_RELCOMPARE_H