Remove obsolete ResStatus bits.
[platform/upstream/libzypp.git] / zypp / IdStringType.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/IdStringType.h
10  *
11 */
12 #ifndef ZYPP_IDSTRINGTYPE_H
13 #define ZYPP_IDSTRINGTYPE_H
14
15 #include "zypp/IdString.h"
16
17 ///////////////////////////////////////////////////////////////////
18 namespace zypp
19 { /////////////////////////////////////////////////////////////////
20
21   ///////////////////////////////////////////////////////////////////
22   //
23   //    CLASS NAME : IdStringType<Derived>
24   //
25   /** Base class for creating \ref IdString based types.
26    *
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).
30    *
31    * To disable any comparison, declare (but do not define) \ref _doCompare
32    * in your class.
33    * \code
34    * class NoCompare : public IdStringType<NoCompare>
35    * {
36    *   private:
37    *   static int _doCompare( const char * lhs,  const char * rhs );
38    *
39    * };
40    * \endcode
41    *
42    * If you need a different than the default lexicographical
43    * order, write your own \ref _doCompare.
44    *
45    * \code
46    *    // uses default lexicographical order
47    *    class CaseCmp : public IdStringType<CaseCmp>
48    *    {
49    *      public:
50    *        CaseCmp() {}
51    *        explicit CaseCmp( const char * cstr_r ) : _str( cstr_r )  {}
52    *      private:
53    *        friend class IdStringType<CaseCmp>;
54    *        IdString _str;
55    *    };
56    *
57    *    // uses case insensitive comparison order
58    *    class NoCaseCmp : public IdStringType<NoCaseCmp>
59    *    {
60    *      public:
61    *        NoCaseCmp() {}
62    *        explicit NoCaseCmp( const char * cstr_r ) : _str( cstr_r )  {}
63    *      private:
64    *        static int _doCompare( const char * lhs,  const char * rhs )
65    *        {
66    *          if ( lhs == rhs ) return 0;
67    *          if ( lhs && rhs ) return ::strcasecmp( lhs, rhs );
68    *          return( lhs ? 1 : -1 );
69    *        }
70    *      private:
71    *        friend class IdStringType<NoCaseCmp>;
72    *        IdString _str;
73    *    };
74    *
75    *    CaseCmp   ca( "a" );
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
81    * \endcode
82    * \todo allow redefinition of order vis _doCompare not only for char* but on any level
83    * \ingroup g_CRTP
84    */
85   template <class Derived>
86   class IdStringType : protected sat::detail::PoolMember,
87                        private base::SafeBool<Derived>
88   {
89     typedef typename base::SafeBool<Derived>::bool_type bool_type;
90     public:
91       typedef IdString::IdType IdType;
92
93     protected:
94       IdStringType() {}
95       IdStringType(const IdStringType &) {}
96       void operator=(const IdStringType &) {}
97       ~IdStringType() {}
98
99     private:
100       const Derived & self() const { return *static_cast<const Derived*>( this ); }
101
102     public:
103       const IdString & idStr()    const { return self()._str; }
104
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(); }
109
110       IdType        id()          const { return idStr().id(); }
111
112     public:
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;
116 #endif
117     public:
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 );}
122
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 ); }
128
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 ); }
133
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 ); }
138
139     public:
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 ); }
145
146     private:
147       static int _doCompare( const char * lhs,  const char * rhs )
148       {
149         if ( lhs == rhs ) return 0;
150         if ( lhs && rhs ) return ::strcmp( lhs, rhs );
151         return( lhs ? 1 : -1 );
152       }
153
154     private:
155 #ifndef SWIG // Swig treats it as syntax error
156       friend base::SafeBool<Derived>::operator bool_type() const;
157 #endif
158       bool boolTest() const { return ! empty(); }
159   };
160   ///////////////////////////////////////////////////////////////////
161
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(); }
166
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() ); }
171   /** \overload */
172   template <class Derived>
173   inline bool operator==( const IdStringType<Derived> & lhs, const IdString & rhs )
174   { return lhs.compare( rhs ) == 0; }
175   /** \overload */
176   template <class Derived>
177   inline bool operator==( const IdStringType<Derived> & lhs, const char * rhs )
178   { return lhs.compare( rhs ) == 0; }
179   /** \overload */
180   template <class Derived>
181   inline bool operator==( const IdStringType<Derived> & lhs, const std::string & rhs )
182   { return lhs.compare( rhs ) == 0; }
183   /** \overload */
184   template <class Derived>
185   inline bool operator==( const IdString & lhs, const IdStringType<Derived> & rhs )
186   { return rhs.compare( lhs ) == 0; }
187   /** \overload */
188   template <class Derived>
189   inline bool operator==( const char * lhs, const IdStringType<Derived> & rhs )
190   { return rhs.compare( lhs ) == 0; }
191   /** \overload */
192   template <class Derived>
193   inline bool operator==( const std::string & lhs, const IdStringType<Derived> & rhs )
194   { return rhs.compare( lhs ) == 0; }
195
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() ); }
200   /** \overload */
201   template <class Derived>
202   inline bool operator!=( const IdStringType<Derived> & lhs, const IdString & rhs )
203   { return lhs.compare( rhs ) != 0; }
204   /** \overload */
205   template <class Derived>
206   inline bool operator!=( const IdStringType<Derived> & lhs, const char * rhs )
207   { return lhs.compare( rhs ) != 0; }
208   /** \overload */
209   template <class Derived>
210   inline bool operator!=( const IdStringType<Derived> & lhs, const std::string & rhs )
211   { return lhs.compare( rhs ) != 0; }
212   /** \overload */
213   template <class Derived>
214   inline bool operator!=( const IdString & lhs, const IdStringType<Derived> & rhs )
215   { return rhs.compare( lhs ) != 0; }
216   /** \overload */
217   template <class Derived>
218   inline bool operator!=( const char * lhs, const IdStringType<Derived> & rhs )
219   { return rhs.compare( lhs ) != 0; }
220   /** \overload */
221   template <class Derived>
222   inline bool operator!=( const std::string & lhs, const IdStringType<Derived> & rhs )
223   { return rhs.compare( lhs ) != 0; }
224
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; }
229   /** \overload */
230   template <class Derived>
231   inline bool operator<( const IdStringType<Derived> & lhs, const IdString & rhs )
232   { return lhs.compare( rhs ) < 0; }
233   /** \overload */
234   template <class Derived>
235   inline bool operator<( const IdStringType<Derived> & lhs, const char * rhs )
236   { return lhs.compare( rhs ) < 0; }
237   /** \overload */
238   template <class Derived>
239   inline bool operator<( const IdStringType<Derived> & lhs, const std::string & rhs )
240   { return lhs.compare( rhs ) < 0; }
241   /** \overload */
242   template <class Derived>
243   inline bool operator<( const IdString & lhs, const IdStringType<Derived> & rhs )
244   { return rhs.compare( lhs ) >= 0; }
245   /** \overload */
246   template <class Derived>
247   inline bool operator<( const char * lhs, const IdStringType<Derived> & rhs )
248   { return rhs.compare( lhs ) >= 0; }
249   /** \overload */
250   template <class Derived>
251   inline bool operator<( const std::string & lhs, const IdStringType<Derived> & rhs )
252   { return rhs.compare( lhs ) >= 0; }
253
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; }
258   /** \overload */
259   template <class Derived>
260   inline bool operator<=( const IdStringType<Derived> & lhs, const IdString & rhs )
261   { return lhs.compare( rhs ) <= 0; }
262   /** \overload */
263   template <class Derived>
264   inline bool operator<=( const IdStringType<Derived> & lhs, const char * rhs )
265   { return lhs.compare( rhs ) <= 0; }
266   /** \overload */
267   template <class Derived>
268   inline bool operator<=( const IdStringType<Derived> & lhs, const std::string & rhs )
269   { return lhs.compare( rhs ) <= 0; }
270   /** \overload */
271   template <class Derived>
272   inline bool operator<=( const IdString & lhs, const IdStringType<Derived> & rhs )
273   { return rhs.compare( lhs ) > 0; }
274   /** \overload */
275   template <class Derived>
276   inline bool operator<=( const char * lhs, const IdStringType<Derived> & rhs )
277   { return rhs.compare( lhs ) > 0; }
278   /** \overload */
279   template <class Derived>
280   inline bool operator<=( const std::string & lhs, const IdStringType<Derived> & rhs )
281   { return rhs.compare( lhs ) > 0; }
282
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; }
287   /** \overload */
288   template <class Derived>
289   inline bool operator>( const IdStringType<Derived> & lhs, const IdString & rhs )
290   { return lhs.compare( rhs ) > 0; }
291   /** \overload */
292   template <class Derived>
293   inline bool operator>( const IdStringType<Derived> & lhs, const char * rhs )
294   { return lhs.compare( rhs ) > 0; }
295   /** \overload */
296   template <class Derived>
297   inline bool operator>( const IdStringType<Derived> & lhs, const std::string & rhs )
298   { return lhs.compare( rhs ) > 0; }
299   /** \overload */
300   template <class Derived>
301   inline bool operator>( const IdString & lhs, const IdStringType<Derived> & rhs )
302   { return rhs.compare( lhs ) <= 0; }
303   /** \overload */
304   template <class Derived>
305   inline bool operator>( const char * lhs, const IdStringType<Derived> & rhs )
306   { return rhs.compare( lhs ) <= 0; }
307   /** \overload */
308   template <class Derived>
309   inline bool operator>( const std::string & lhs, const IdStringType<Derived> & rhs )
310   { return rhs.compare( lhs ) <= 0; }
311
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; }
316   /** \overload */
317   template <class Derived>
318   inline bool operator>=( const IdStringType<Derived> & lhs, const IdString & rhs )
319   { return lhs.compare( rhs ) >= 0; }
320   /** \overload */
321   template <class Derived>
322   inline bool operator>=( const IdStringType<Derived> & lhs, const char * rhs )
323   { return lhs.compare( rhs ) >= 0; }
324   /** \overload */
325   template <class Derived>
326   inline bool operator>=( const IdStringType<Derived> & lhs, const std::string & rhs )
327   { return lhs.compare( rhs ) >= 0; }
328   /** \overload */
329   template <class Derived>
330   inline bool operator>=( const IdString & lhs, const IdStringType<Derived> & rhs )
331   { return rhs.compare( lhs ) < 0; }
332   /** \overload */
333   template <class Derived>
334   inline bool operator>=( const char * lhs, const IdStringType<Derived> & rhs )
335   { return rhs.compare( lhs ) < 0; }
336   /** \overload */
337   template <class Derived>
338   inline bool operator>=( const std::string & lhs, const IdStringType<Derived> & rhs )
339   { return rhs.compare( lhs ) < 0; }
340
341   /////////////////////////////////////////////////////////////////
342 } // namespace zypp
343 ///////////////////////////////////////////////////////////////////
344 #endif // ZYPP_IDSTRINGTYPE_H