1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/IdStringType.h
12 #ifndef ZYPP_IDSTRINGTYPE_H
13 #define ZYPP_IDSTRINGTYPE_H
15 #include "zypp/IdString.h"
17 ///////////////////////////////////////////////////////////////////
19 { /////////////////////////////////////////////////////////////////
21 ///////////////////////////////////////////////////////////////////
23 // CLASS NAME : IdStringType<Derived>
25 /** Base class for creating \ref IdString based types.
27 * Just by deriving from \ref IdStringType a class provides all
28 * the operations an \ref IdString does. (incl. conversion to string types,
29 * comparison of string types and stream output).
31 * To disable any comparison, declare (but do not define) \ref _doCompare
34 * class NoCompare : public IdStringType<NoCompare>
37 * static int _doCompare( const char * lhs, const char * rhs );
42 * If you need a different than the default lexicographical
43 * order, write your own \ref _doCompare.
46 * // uses default lexicographical order
47 * class CaseCmp : public IdStringType<CaseCmp>
51 * explicit CaseCmp( const char * cstr_r ) : _str( cstr_r ) {}
53 * friend class IdStringType<CaseCmp>;
57 * // uses case insensitive comparison order
58 * class NoCaseCmp : public IdStringType<NoCaseCmp>
62 * explicit NoCaseCmp( const char * cstr_r ) : _str( cstr_r ) {}
64 * static int _doCompare( const char * lhs, const char * rhs )
66 * if ( lhs == rhs ) return 0;
67 * if ( lhs && rhs ) return ::strcasecmp( lhs, rhs );
68 * return( lhs ? 1 : -1 );
71 * friend class IdStringType<NoCaseCmp>;
76 * NoCaseCmp na( "a" );
77 * DBG << "ca == a ? " << (ca == "a") << endl; // ca == a ? 1
78 * DBG << "ca == A ? " << (ca == "A") << endl; // ca == A ? 0
79 * DBG << "na == a ? " << (na == "a") << endl; // na == a ? 1
80 * DBG << "na == A ? " << (na == "A") << endl; // na == A ? 1
82 * \todo allow redefinition of order vis _doCompare not only for char* but on any level
85 template <class Derived>
86 class IdStringType : protected sat::detail::PoolMember,
87 private base::SafeBool<Derived>
89 typedef typename base::SafeBool<Derived>::bool_type bool_type;
91 typedef IdString::IdType IdType;
95 IdStringType(const IdStringType &) {}
96 void operator=(const IdStringType &) {}
100 const Derived & self() const { return *static_cast<const Derived*>( this ); }
103 const IdString & idStr() const { return self()._str; }
105 bool empty() const { return idStr().empty(); }
106 unsigned size() const { return idStr().size(); }
107 const char * c_str() const { return idStr().c_str(); }
108 std::string asString() const { return idStr().asString(); }
110 IdType id() const { return idStr().id(); }
113 #ifndef SWIG // Swig treats it as syntax error
114 /** Evaluate in a boolean context <tt>( ! empty() )</tt>. */
115 using base::SafeBool<Derived>::operator bool_type;
118 static int compare( const Derived & lhs, const Derived & rhs ) { return compare( lhs.idStr(), rhs.idStr() ); }
119 static int compare( const Derived & lhs, const IdString & rhs ) { return compare( lhs.idStr(), rhs ); }
120 static int compare( const Derived & lhs, const std::string & rhs ) { return Derived::_doCompare( lhs.c_str(), rhs.c_str() ); }
121 static int compare( const Derived & lhs, const char * rhs ) { return Derived::_doCompare( lhs.c_str(), rhs );}
123 static int compare( const IdString & lhs, const Derived & rhs ) { return compare( lhs, rhs.idStr() ); }
124 static int compare( const IdString & lhs, const IdString & rhs ) { return lhs.compareEQ( rhs ) ? 0 :
125 Derived::_doCompare( lhs.c_str(), rhs.c_str() ); }
126 static int compare( const IdString & lhs, const std::string & rhs ) { return Derived::_doCompare( lhs.c_str(), rhs.c_str() ); }
127 static int compare( const IdString & lhs, const char * rhs ) { return Derived::_doCompare( lhs.c_str(), rhs ); }
129 static int compare( const std::string & lhs, const Derived & rhs ) { return Derived::_doCompare( lhs.c_str(), rhs.c_str() );}
130 static int compare( const std::string & lhs, const IdString & rhs ) { return Derived::_doCompare( lhs.c_str(), rhs.c_str() ); }
131 static int compare( const std::string & lhs, const std::string & rhs ) { return Derived::_doCompare( lhs.c_str(), rhs.c_str() ); }
132 static int compare( const std::string & lhs, const char * rhs ) { return Derived::_doCompare( lhs.c_str(), rhs ); }
134 static int compare( const char * lhs, const Derived & rhs ) { return Derived::_doCompare( lhs, rhs.c_str() );}
135 static int compare( const char * lhs, const IdString & rhs ) { return Derived::_doCompare( lhs, rhs.c_str() ); }
136 static int compare( const char * lhs, const std::string & rhs ) { return Derived::_doCompare( lhs, rhs.c_str() ); }
137 static int compare( const char * lhs, const char * rhs ) { return Derived::_doCompare( lhs, rhs ); }
140 int compare( const Derived & rhs ) const { return compare( idStr(), rhs.idStr() ); }
141 int compare( const IdStringType & rhs ) const { return compare( idStr(), rhs.idStr() ); }
142 int compare( const IdString & rhs ) const { return compare( idStr(), rhs ); }
143 int compare( const std::string & rhs ) const { return Derived::_doCompare( c_str(), rhs.c_str() ); }
144 int compare( const char * rhs ) const { return Derived::_doCompare( c_str(), rhs ); }
147 static int _doCompare( const char * lhs, const char * rhs )
149 if ( lhs == rhs ) return 0;
150 if ( lhs && rhs ) return ::strcmp( lhs, rhs );
151 return( lhs ? 1 : -1 );
155 #ifndef SWIG // Swig treats it as syntax error
156 friend base::SafeBool<Derived>::operator bool_type() const;
158 bool boolTest() const { return ! empty(); }
160 ///////////////////////////////////////////////////////////////////
162 /** \relates IdStringType Stream output */
163 template <class Derived>
164 inline std::ostream & operator<<( std::ostream & str, const IdStringType<Derived> & obj )
165 { return str << obj.c_str(); }
167 /** \relates IdStringType Equal */
168 template <class Derived>
169 inline bool operator==( const IdStringType<Derived> & lhs, const IdStringType<Derived> & rhs )
170 { return lhs.idStr().compareEQ( rhs.idStr() ); }
172 template <class Derived>
173 inline bool operator==( const IdStringType<Derived> & lhs, const IdString & rhs )
174 { return lhs.compare( rhs ) == 0; }
176 template <class Derived>
177 inline bool operator==( const IdStringType<Derived> & lhs, const char * rhs )
178 { return lhs.compare( rhs ) == 0; }
180 template <class Derived>
181 inline bool operator==( const IdStringType<Derived> & lhs, const std::string & rhs )
182 { return lhs.compare( rhs ) == 0; }
184 template <class Derived>
185 inline bool operator==( const IdString & lhs, const IdStringType<Derived> & rhs )
186 { return rhs.compare( lhs ) == 0; }
188 template <class Derived>
189 inline bool operator==( const char * lhs, const IdStringType<Derived> & rhs )
190 { return rhs.compare( lhs ) == 0; }
192 template <class Derived>
193 inline bool operator==( const std::string & lhs, const IdStringType<Derived> & rhs )
194 { return rhs.compare( lhs ) == 0; }
196 /** \relates IdStringType NotEqual */
197 template <class Derived>
198 inline bool operator!=( const IdStringType<Derived> & lhs, const IdStringType<Derived> & rhs )
199 { return ! lhs.idStr().compareEQ( rhs.idStr() ); }
201 template <class Derived>
202 inline bool operator!=( const IdStringType<Derived> & lhs, const IdString & rhs )
203 { return lhs.compare( rhs ) != 0; }
205 template <class Derived>
206 inline bool operator!=( const IdStringType<Derived> & lhs, const char * rhs )
207 { return lhs.compare( rhs ) != 0; }
209 template <class Derived>
210 inline bool operator!=( const IdStringType<Derived> & lhs, const std::string & rhs )
211 { return lhs.compare( rhs ) != 0; }
213 template <class Derived>
214 inline bool operator!=( const IdString & lhs, const IdStringType<Derived> & rhs )
215 { return rhs.compare( lhs ) != 0; }
217 template <class Derived>
218 inline bool operator!=( const char * lhs, const IdStringType<Derived> & rhs )
219 { return rhs.compare( lhs ) != 0; }
221 template <class Derived>
222 inline bool operator!=( const std::string & lhs, const IdStringType<Derived> & rhs )
223 { return rhs.compare( lhs ) != 0; }
225 /** \relates IdStringType Less */
226 template <class Derived>
227 inline bool operator<( const IdStringType<Derived> & lhs, const IdStringType<Derived> & rhs )
228 { return lhs.compare( rhs ) < 0; }
230 template <class Derived>
231 inline bool operator<( const IdStringType<Derived> & lhs, const IdString & rhs )
232 { return lhs.compare( rhs ) < 0; }
234 template <class Derived>
235 inline bool operator<( const IdStringType<Derived> & lhs, const char * rhs )
236 { return lhs.compare( rhs ) < 0; }
238 template <class Derived>
239 inline bool operator<( const IdStringType<Derived> & lhs, const std::string & rhs )
240 { return lhs.compare( rhs ) < 0; }
242 template <class Derived>
243 inline bool operator<( const IdString & lhs, const IdStringType<Derived> & rhs )
244 { return rhs.compare( lhs ) >= 0; }
246 template <class Derived>
247 inline bool operator<( const char * lhs, const IdStringType<Derived> & rhs )
248 { return rhs.compare( lhs ) >= 0; }
250 template <class Derived>
251 inline bool operator<( const std::string & lhs, const IdStringType<Derived> & rhs )
252 { return rhs.compare( lhs ) >= 0; }
254 /** \relates IdStringType LessEqual */
255 template <class Derived>
256 inline bool operator<=( const IdStringType<Derived> & lhs, const IdStringType<Derived> & rhs )
257 { return lhs.compare( rhs ) <= 0; }
259 template <class Derived>
260 inline bool operator<=( const IdStringType<Derived> & lhs, const IdString & rhs )
261 { return lhs.compare( rhs ) <= 0; }
263 template <class Derived>
264 inline bool operator<=( const IdStringType<Derived> & lhs, const char * rhs )
265 { return lhs.compare( rhs ) <= 0; }
267 template <class Derived>
268 inline bool operator<=( const IdStringType<Derived> & lhs, const std::string & rhs )
269 { return lhs.compare( rhs ) <= 0; }
271 template <class Derived>
272 inline bool operator<=( const IdString & lhs, const IdStringType<Derived> & rhs )
273 { return rhs.compare( lhs ) > 0; }
275 template <class Derived>
276 inline bool operator<=( const char * lhs, const IdStringType<Derived> & rhs )
277 { return rhs.compare( lhs ) > 0; }
279 template <class Derived>
280 inline bool operator<=( const std::string & lhs, const IdStringType<Derived> & rhs )
281 { return rhs.compare( lhs ) > 0; }
283 /** \relates IdStringType Greater */
284 template <class Derived>
285 inline bool operator>( const IdStringType<Derived> & lhs, const IdStringType<Derived> & rhs )
286 { return lhs.compare( rhs ) > 0; }
288 template <class Derived>
289 inline bool operator>( const IdStringType<Derived> & lhs, const IdString & rhs )
290 { return lhs.compare( rhs ) > 0; }
292 template <class Derived>
293 inline bool operator>( const IdStringType<Derived> & lhs, const char * rhs )
294 { return lhs.compare( rhs ) > 0; }
296 template <class Derived>
297 inline bool operator>( const IdStringType<Derived> & lhs, const std::string & rhs )
298 { return lhs.compare( rhs ) > 0; }
300 template <class Derived>
301 inline bool operator>( const IdString & lhs, const IdStringType<Derived> & rhs )
302 { return rhs.compare( lhs ) <= 0; }
304 template <class Derived>
305 inline bool operator>( const char * lhs, const IdStringType<Derived> & rhs )
306 { return rhs.compare( lhs ) <= 0; }
308 template <class Derived>
309 inline bool operator>( const std::string & lhs, const IdStringType<Derived> & rhs )
310 { return rhs.compare( lhs ) <= 0; }
312 /** \relates IdStringType GreaterEqual */
313 template <class Derived>
314 inline bool operator>=( const IdStringType<Derived> & lhs, const IdStringType<Derived> & rhs )
315 { return lhs.compare( rhs ) >= 0; }
317 template <class Derived>
318 inline bool operator>=( const IdStringType<Derived> & lhs, const IdString & rhs )
319 { return lhs.compare( rhs ) >= 0; }
321 template <class Derived>
322 inline bool operator>=( const IdStringType<Derived> & lhs, const char * rhs )
323 { return lhs.compare( rhs ) >= 0; }
325 template <class Derived>
326 inline bool operator>=( const IdStringType<Derived> & lhs, const std::string & rhs )
327 { return lhs.compare( rhs ) >= 0; }
329 template <class Derived>
330 inline bool operator>=( const IdString & lhs, const IdStringType<Derived> & rhs )
331 { return rhs.compare( lhs ) < 0; }
333 template <class Derived>
334 inline bool operator>=( const char * lhs, const IdStringType<Derived> & rhs )
335 { return rhs.compare( lhs ) < 0; }
337 template <class Derived>
338 inline bool operator>=( const std::string & lhs, const IdStringType<Derived> & rhs )
339 { return rhs.compare( lhs ) < 0; }
341 /////////////////////////////////////////////////////////////////
343 ///////////////////////////////////////////////////////////////////
344 #endif // ZYPP_IDSTRINGTYPE_H