libzypp  10.5.0
IdStringType.h
Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00012 #ifndef ZYPP_IDSTRINGTYPE_H
00013 #define ZYPP_IDSTRINGTYPE_H
00014 
00015 #include "zypp/IdString.h"
00016 
00018 namespace zypp
00019 { 
00020 
00022   //
00023   //    CLASS NAME : IdStringType<Derived>
00024   //
00085   template <class Derived>
00086   class IdStringType : protected sat::detail::PoolMember,
00087                        private base::SafeBool<Derived>
00088   {
00089     typedef typename base::SafeBool<Derived>::bool_type bool_type;
00090     public:
00091       typedef IdString::IdType IdType;
00092 
00093     protected:
00094       IdStringType() {}
00095       IdStringType(const IdStringType &) {}
00096       void operator=(const IdStringType &) {}
00097       ~IdStringType() {}
00098 
00099     private:
00100       const Derived & self() const { return *static_cast<const Derived*>( this ); }
00101 
00102     public:
00103       const IdString & idStr()    const { return self()._str; }
00104 
00105       bool          empty()       const { return idStr().empty(); }
00106       unsigned      size()        const { return idStr().size(); }
00107       const char *  c_str()       const { return idStr().c_str(); }
00108       std::string   asString()    const { return idStr().asString(); }
00109 
00110       IdType        id()          const { return idStr().id(); }
00111 
00112     public:
00113 #ifndef SWIG // Swig treats it as syntax error
00114 
00115       using base::SafeBool<Derived>::operator bool_type;
00116 #endif
00117     public:
00118       // - break it down to idString/const char* <=> idString/cont char*
00119       // - handle idString(0)/NULL being the least value
00120       // - everything else goes to _doCompare (no NULL)
00121       static int compare( const Derived & lhs,     const Derived & rhs )     { return compare( lhs.idStr(), rhs.idStr() ); }
00122       static int compare( const Derived & lhs,     const IdString & rhs )    { return compare( lhs.idStr(), rhs ); }
00123       static int compare( const Derived & lhs,     const std::string & rhs ) { return compare( lhs.idStr(), rhs.c_str() ); }
00124       static int compare( const Derived & lhs,     const char * rhs )        { return compare( lhs.idStr(), rhs );}
00125 
00126       static int compare( const IdString & lhs,    const Derived & rhs )     { return compare( lhs, rhs.idStr() ); }
00127       static int compare( const IdString & lhs,    const IdString & rhs )    { return lhs == rhs ? 0 : Derived::_doCompare( (lhs ? lhs.c_str() : (const char *)0 ),
00128                                                                                                                             (rhs ? rhs.c_str() : (const char *)0 ) ); }
00129       static int compare( const IdString & lhs,    const std::string & rhs ) { return compare( lhs, rhs.c_str() ); }
00130       static int compare( const IdString & lhs,    const char * rhs )        { return Derived::_doCompare( (lhs ? lhs.c_str() : (const char *)0 ), rhs ); }
00131 
00132       static int compare( const std::string & lhs, const Derived & rhs )     { return compare( lhs.c_str(), rhs.idStr() ); }
00133       static int compare( const std::string & lhs, const IdString & rhs )    { return compare( lhs.c_str(), rhs ); }
00134       static int compare( const std::string & lhs, const std::string & rhs ) { return compare( lhs.c_str(), rhs.c_str() ); }
00135       static int compare( const std::string & lhs, const char * rhs )        { return compare( lhs.c_str(), rhs ); }
00136 
00137       static int compare( const char * lhs,        const Derived & rhs )     { return compare( lhs, rhs.idStr() ); }
00138       static int compare( const char * lhs,        const IdString & rhs )    { return Derived::_doCompare( lhs, (rhs ? rhs.c_str() : (const char *)0 ) ); }
00139       static int compare( const char * lhs,        const std::string & rhs ) { return compare( lhs, rhs.c_str() ); }
00140       static int compare( const char * lhs,        const char * rhs )        { return Derived::_doCompare( lhs, rhs ); }
00141 
00142     public:
00143       int compare( const Derived & rhs )      const { return compare( idStr(), rhs.idStr() ); }
00144       int compare( const IdStringType & rhs ) const { return compare( idStr(), rhs.idStr() ); }
00145       int compare( const IdString & rhs )     const { return compare( idStr(), rhs ); }
00146       int compare( const std::string & rhs )  const { return compare( idStr(), rhs.c_str() ); }
00147       int compare( const char * rhs )         const { return compare( idStr(), rhs ); }
00148 
00149     private:
00150       static int _doCompare( const char * lhs,  const char * rhs )
00151       {
00152         if ( ! lhs ) return rhs ? -1 : 0;
00153         return rhs ? ::strcmp( lhs, rhs ) : 1;
00154       }
00155 
00156     private:
00157 #ifndef SWIG // Swig treats it as syntax error
00158       friend base::SafeBool<Derived>::operator bool_type() const;
00159 #endif
00160       bool boolTest() const { return ! empty(); }
00161   };
00163 
00165   template <class Derived>
00166   inline std::ostream & operator<<( std::ostream & str, const IdStringType<Derived> & obj )
00167   { return str << obj.c_str(); }
00168 
00170   template <class Derived>
00171   inline bool operator==( const IdStringType<Derived> & lhs, const IdStringType<Derived> & rhs )
00172   { return lhs.compare( rhs ) == 0; }
00174   template <class Derived>
00175   inline bool operator==( const IdStringType<Derived> & lhs, const IdString & rhs )
00176   { return lhs.compare( rhs ) == 0; }
00178   template <class Derived>
00179   inline bool operator==( const IdStringType<Derived> & lhs, const char * rhs )
00180   { return lhs.compare( rhs ) == 0; }
00182   template <class Derived>
00183   inline bool operator==( const IdStringType<Derived> & lhs, const std::string & rhs )
00184   { return lhs.compare( rhs ) == 0; }
00186   template <class Derived>
00187   inline bool operator==( const IdString & lhs, const IdStringType<Derived> & rhs )
00188   { return rhs.compare( lhs ) == 0; }
00190   template <class Derived>
00191   inline bool operator==( const char * lhs, const IdStringType<Derived> & rhs )
00192   { return rhs.compare( lhs ) == 0; }
00194   template <class Derived>
00195   inline bool operator==( const std::string & lhs, const IdStringType<Derived> & rhs )
00196   { return rhs.compare( lhs ) == 0; }
00197 
00199   template <class Derived>
00200   inline bool operator!=( const IdStringType<Derived> & lhs, const IdStringType<Derived> & rhs )
00201   { return lhs.compare( rhs ) != 0; }
00203   template <class Derived>
00204   inline bool operator!=( const IdStringType<Derived> & lhs, const IdString & rhs )
00205   { return lhs.compare( rhs ) != 0; }
00207   template <class Derived>
00208   inline bool operator!=( const IdStringType<Derived> & lhs, const char * rhs )
00209   { return lhs.compare( rhs ) != 0; }
00211   template <class Derived>
00212   inline bool operator!=( const IdStringType<Derived> & lhs, const std::string & rhs )
00213   { return lhs.compare( rhs ) != 0; }
00215   template <class Derived>
00216   inline bool operator!=( const IdString & lhs, const IdStringType<Derived> & rhs )
00217   { return rhs.compare( lhs ) != 0; }
00219   template <class Derived>
00220   inline bool operator!=( const char * lhs, const IdStringType<Derived> & rhs )
00221   { return rhs.compare( lhs ) != 0; }
00223   template <class Derived>
00224   inline bool operator!=( const std::string & lhs, const IdStringType<Derived> & rhs )
00225   { return rhs.compare( lhs ) != 0; }
00226 
00228   template <class Derived>
00229   inline bool operator<( const IdStringType<Derived> & lhs, const IdStringType<Derived> & rhs )
00230   { return lhs.compare( rhs ) < 0; }
00232   template <class Derived>
00233   inline bool operator<( const IdStringType<Derived> & lhs, const IdString & rhs )
00234   { return lhs.compare( rhs ) < 0; }
00236   template <class Derived>
00237   inline bool operator<( const IdStringType<Derived> & lhs, const char * rhs )
00238   { return lhs.compare( rhs ) < 0; }
00240   template <class Derived>
00241   inline bool operator<( const IdStringType<Derived> & lhs, const std::string & rhs )
00242   { return lhs.compare( rhs ) < 0; }
00244   template <class Derived>
00245   inline bool operator<( const IdString & lhs, const IdStringType<Derived> & rhs )
00246   { return rhs.compare( lhs ) >= 0; }
00248   template <class Derived>
00249   inline bool operator<( const char * lhs, const IdStringType<Derived> & rhs )
00250   { return rhs.compare( lhs ) >= 0; }
00252   template <class Derived>
00253   inline bool operator<( const std::string & lhs, const IdStringType<Derived> & rhs )
00254   { return rhs.compare( lhs ) >= 0; }
00255 
00257   template <class Derived>
00258   inline bool operator<=( const IdStringType<Derived> & lhs, const IdStringType<Derived> & rhs )
00259   { return lhs.compare( rhs ) <= 0; }
00261   template <class Derived>
00262   inline bool operator<=( const IdStringType<Derived> & lhs, const IdString & rhs )
00263   { return lhs.compare( rhs ) <= 0; }
00265   template <class Derived>
00266   inline bool operator<=( const IdStringType<Derived> & lhs, const char * rhs )
00267   { return lhs.compare( rhs ) <= 0; }
00269   template <class Derived>
00270   inline bool operator<=( const IdStringType<Derived> & lhs, const std::string & rhs )
00271   { return lhs.compare( rhs ) <= 0; }
00273   template <class Derived>
00274   inline bool operator<=( const IdString & lhs, const IdStringType<Derived> & rhs )
00275   { return rhs.compare( lhs ) > 0; }
00277   template <class Derived>
00278   inline bool operator<=( const char * lhs, const IdStringType<Derived> & rhs )
00279   { return rhs.compare( lhs ) > 0; }
00281   template <class Derived>
00282   inline bool operator<=( const std::string & lhs, const IdStringType<Derived> & rhs )
00283   { return rhs.compare( lhs ) > 0; }
00284 
00286   template <class Derived>
00287   inline bool operator>( const IdStringType<Derived> & lhs, const IdStringType<Derived> & rhs )
00288   { return lhs.compare( rhs ) > 0; }
00290   template <class Derived>
00291   inline bool operator>( const IdStringType<Derived> & lhs, const IdString & rhs )
00292   { return lhs.compare( rhs ) > 0; }
00294   template <class Derived>
00295   inline bool operator>( const IdStringType<Derived> & lhs, const char * rhs )
00296   { return lhs.compare( rhs ) > 0; }
00298   template <class Derived>
00299   inline bool operator>( const IdStringType<Derived> & lhs, const std::string & rhs )
00300   { return lhs.compare( rhs ) > 0; }
00302   template <class Derived>
00303   inline bool operator>( const IdString & lhs, const IdStringType<Derived> & rhs )
00304   { return rhs.compare( lhs ) <= 0; }
00306   template <class Derived>
00307   inline bool operator>( const char * lhs, const IdStringType<Derived> & rhs )
00308   { return rhs.compare( lhs ) <= 0; }
00310   template <class Derived>
00311   inline bool operator>( const std::string & lhs, const IdStringType<Derived> & rhs )
00312   { return rhs.compare( lhs ) <= 0; }
00313 
00315   template <class Derived>
00316   inline bool operator>=( const IdStringType<Derived> & lhs, const IdStringType<Derived> & rhs )
00317   { return lhs.compare( rhs ) >= 0; }
00319   template <class Derived>
00320   inline bool operator>=( const IdStringType<Derived> & lhs, const IdString & rhs )
00321   { return lhs.compare( rhs ) >= 0; }
00323   template <class Derived>
00324   inline bool operator>=( const IdStringType<Derived> & lhs, const char * rhs )
00325   { return lhs.compare( rhs ) >= 0; }
00327   template <class Derived>
00328   inline bool operator>=( const IdStringType<Derived> & lhs, const std::string & rhs )
00329   { return lhs.compare( rhs ) >= 0; }
00331   template <class Derived>
00332   inline bool operator>=( const IdString & lhs, const IdStringType<Derived> & rhs )
00333   { return rhs.compare( lhs ) < 0; }
00335   template <class Derived>
00336   inline bool operator>=( const char * lhs, const IdStringType<Derived> & rhs )
00337   { return rhs.compare( lhs ) < 0; }
00339   template <class Derived>
00340   inline bool operator>=( const std::string & lhs, const IdStringType<Derived> & rhs )
00341   { return rhs.compare( lhs ) < 0; }
00342 
00344 } // namespace zypp
00346 #endif // ZYPP_IDSTRINGTYPE_H