Imported Upstream version 16.3.2
[platform/upstream/libzypp.git] / zypp / IdStringType.h
index dbe2cc7..bb009be 100644 (file)
@@ -83,10 +83,10 @@ namespace zypp
    * \ingroup g_CRTP
    */
   template <class Derived>
-  class IdStringType : protected sat::detail::PoolMember,
-                       private base::SafeBool<Derived>
+  class IdStringType : protected sat::detail::PoolMember
   {
-    typedef typename base::SafeBool<Derived>::bool_type bool_type;
+    public:
+      typedef IdString::IdType IdType;
 
     protected:
       IdStringType() {}
@@ -98,62 +98,66 @@ namespace zypp
       const Derived & self() const { return *static_cast<const Derived*>( this ); }
 
     public:
-      const IdString & idStr()    const { return self()._str; }
+      IdString      idStr()       const { return self()._str; }
 
       bool          empty()       const { return idStr().empty(); }
       unsigned      size()        const { return idStr().size(); }
       const char *  c_str()       const { return idStr().c_str(); }
       std::string   asString()    const { return idStr().asString(); }
 
-      IdString::IdType id()       const { return idStr().id(); }
+      IdType        id()          const { return idStr().id(); }
 
     public:
-#ifndef SWIG // Swig treats it as syntax error
       /** Evaluate in a boolean context <tt>( ! empty() )</tt>. */
-      using base::SafeBool<Derived>::operator bool_type;
-#endif
+      explicit operator bool() const
+      { return ! empty(); }
+
+      /** Explicit conversion to IdString */
+      explicit operator IdString() const
+      { return idStr(); }
+
+      /** Explicit conversion to std::string */
+      explicit operator std::string() const
+      { return asString(); }
+
     public:
-      static int compare( const Derived & lhs,    const Derived & rhs )      { return compare( lhs.idStr(), rhs.idStr() ); }
-      static int compare( const Derived & lhs,    const IdString & rhs )     { return compare( lhs.idStr(), rhs ); }
-      static int compare( const Derived & lhs,    const std::string & rhs )  { return Derived::_doCompare( lhs.c_str(), rhs.c_str() ); }
-      static int compare( const Derived & lhs,    const char * rhs )         { return Derived::_doCompare( lhs.c_str(), rhs );}
+      // - break it down to idString/const char* <=> idString/cont char*
+      // - handle idString(0)/NULL being the least value
+      // - everything else goes to _doCompare (no NULL)
+      static int compare( const Derived & lhs,     const Derived & rhs )     { return compare( lhs.idStr(), rhs.idStr() ); }
+      static int compare( const Derived & lhs,     const IdString & rhs )    { return compare( lhs.idStr(), rhs ); }
+      static int compare( const Derived & lhs,     const std::string & rhs ) { return compare( lhs.idStr(), rhs.c_str() ); }
+      static int compare( const Derived & lhs,     const char * rhs )        { return compare( lhs.idStr(), rhs );}
 
-      static int compare( const IdString & lhs,   const Derived & rhs )      { return compare( lhs, rhs.idStr() ); }
-      static int compare( const IdString & lhs,   const IdString & rhs )     { return lhs.compareEQ( rhs ) ? 0 :
-                                                                                      Derived::_doCompare( lhs.c_str(), rhs.c_str() ); }
-      static int compare( const IdString & lhs,   const std::string & rhs )  { return Derived::_doCompare( lhs.c_str(), rhs.c_str() ); }
-      static int compare( const IdString & lhs,   const char * rhs )         { return Derived::_doCompare( lhs.c_str(), rhs ); }
+      static int compare( const IdString & lhs,    const Derived & rhs )     { return compare( lhs, rhs.idStr() ); }
+      static int compare( const IdString & lhs,    const IdString & rhs )    { return lhs == rhs ? 0 : Derived::_doCompare( (lhs ? lhs.c_str() : (const char *)0 ),
+                                                                                                                           (rhs ? rhs.c_str() : (const char *)0 ) ); }
+      static int compare( const IdString & lhs,    const std::string & rhs ) { return compare( lhs, rhs.c_str() ); }
+      static int compare( const IdString & lhs,    const char * rhs )        { return Derived::_doCompare( (lhs ? lhs.c_str() : (const char *)0 ), rhs ); }
 
-      static int compare( const std::string & lhs, const Derived & rhs )     { return Derived::_doCompare( lhs.c_str(), rhs.c_str() );}
-      static int compare( const std::string & lhs, const IdString & rhs )    { return Derived::_doCompare( lhs.c_str(), rhs.c_str() ); }
-      static int compare( const std::string & lhs, const std::string & rhs ) { return Derived::_doCompare( lhs.c_str(), rhs.c_str() ); }
-      static int compare( const std::string & lhs, const char * rhs )        { return Derived::_doCompare( lhs.c_str(), rhs ); }
+      static int compare( const std::string & lhs, const Derived & rhs )     { return compare( lhs.c_str(), rhs.idStr() ); }
+      static int compare( const std::string & lhs, const IdString & rhs )    { return compare( lhs.c_str(), rhs ); }
+      static int compare( const std::string & lhs, const std::string & rhs ) { return compare( lhs.c_str(), rhs.c_str() ); }
+      static int compare( const std::string & lhs, const char * rhs )        { return compare( lhs.c_str(), rhs ); }
 
-      static int compare( const char * lhs,        const Derived & rhs )     { return Derived::_doCompare( lhs, rhs.c_str() );}
-      static int compare( const char * lhs,        const IdString & rhs )    { return Derived::_doCompare( lhs, rhs.c_str() ); }
-      static int compare( const char * lhs,        const std::string & rhs ) { return Derived::_doCompare( lhs, rhs.c_str() ); }
+      static int compare( const char * lhs,        const Derived & rhs )     { return compare( lhs, rhs.idStr() ); }
+      static int compare( const char * lhs,        const IdString & rhs )    { return Derived::_doCompare( lhs, (rhs ? rhs.c_str() : (const char *)0 ) ); }
+      static int compare( const char * lhs,        const std::string & rhs ) { return compare( lhs, rhs.c_str() ); }
       static int compare( const char * lhs,        const char * rhs )        { return Derived::_doCompare( lhs, rhs ); }
 
     public:
       int compare( const Derived & rhs )      const { return compare( idStr(), rhs.idStr() ); }
       int compare( const IdStringType & rhs ) const { return compare( idStr(), rhs.idStr() ); }
       int compare( const IdString & rhs )     const { return compare( idStr(), rhs ); }
-      int compare( const std::string & rhs )  const { return Derived::_doCompare( c_str(), rhs.c_str() ); }
-      int compare( const char * rhs )         const { return Derived::_doCompare( c_str(), rhs ); }
+      int compare( const std::string & rhs )  const { return compare( idStr(), rhs.c_str() ); }
+      int compare( const char * rhs )         const { return compare( idStr(), rhs ); }
 
     private:
       static int _doCompare( const char * lhs,  const char * rhs )
       {
-        if ( lhs == rhs ) return 0;
-        if ( lhs && rhs ) return ::strcmp( lhs, rhs );
-        return( lhs ? 1 : -1 );
+       if ( ! lhs ) return rhs ? -1 : 0;
+       return rhs ? ::strcmp( lhs, rhs ) : 1;
       }
-
-    private:
-#ifndef SWIG // Swig treats it as syntax error
-      friend base::SafeBool<Derived>::operator bool_type() const;
-#endif
-      bool boolTest() const { return ! empty(); }
   };
   ///////////////////////////////////////////////////////////////////
 
@@ -165,7 +169,7 @@ namespace zypp
   /** \relates IdStringType Equal */
   template <class Derived>
   inline bool operator==( const IdStringType<Derived> & lhs, const IdStringType<Derived> & rhs )
-  { return lhs.idStr().compareEQ( rhs.idStr() ); }
+  { return lhs.compare( rhs ) == 0; }
   /** \overload */
   template <class Derived>
   inline bool operator==( const IdStringType<Derived> & lhs, const IdString & rhs )
@@ -194,7 +198,7 @@ namespace zypp
   /** \relates IdStringType NotEqual */
   template <class Derived>
   inline bool operator!=( const IdStringType<Derived> & lhs, const IdStringType<Derived> & rhs )
-  { return ! lhs.idStr().compareEQ( rhs.idStr() ); }
+  { return lhs.compare( rhs ) != 0; }
   /** \overload */
   template <class Derived>
   inline bool operator!=( const IdStringType<Derived> & lhs, const IdString & rhs )