Introduce explicit operator bool in PtrTypes
authorMichael Andres <ma@suse.de>
Sat, 27 Apr 2013 22:04:24 +0000 (00:04 +0200)
committerMichael Andres <ma@suse.de>
Mon, 29 Apr 2013 08:36:42 +0000 (10:36 +0200)
tests/zypp/PtrTypes_test.cc
zypp/PluginScript.cc
zypp/TmpPath.cc
zypp/base/PtrTypes.h
zypp/base/StrMatcher.cc
zypp/media/MediaHandler.h
zypp/media/MediaManager.cc
zypp/parser/susetags/ContentFileReader.cc

index 3a6d12e..c2a93f6 100644 (file)
@@ -29,14 +29,14 @@ template<class _Trace>
 /** Data class for shared_ptr */
 struct NonIntrusive : private Trace<NonIntrusive>
 {
-  Trace<NonIntrusive>::numericId;
+  using Trace<NonIntrusive>::numericId;
 };
 
 /** Data class for intrusive_ptr */
 struct Intrusive : public ReferenceCounted,
                    private Trace<Intrusive>
 {
-  Trace<Intrusive>::numericId;
+  using Trace<Intrusive>::numericId;
 };
 
 namespace zypp
@@ -54,8 +54,8 @@ namespace zypp
 /******************************************************************
 **
 */
-#define T_NULL       assert( !ptr )
-#define T_NOT_NULL   assert( ptr )
+#define T_NULL       assert( !ptr ); assert( ptr == nullptr )
+#define T_NOT_NULL   assert( ptr ); assert( ptr != nullptr )
 #define T_UNIQUE     assert( ptr.unique() ); assert( ptr.use_count() < 2 )
 #define T_NOT_UNIQUE assert( !ptr.unique() ); assert( ptr.use_count() >= 2 )
 // Also comapre with underlying shared ptr type.
@@ -100,6 +100,18 @@ template<class _RW>
     ptr.reset( 0 );
     T_NULL;
     T_UNIQUE;
+    // nullptr compatible
+    ptr.reset( nullptr );
+    T_NULL;
+    T_UNIQUE;
+    ptr = nullptr;
+    T_NULL;
+    T_UNIQUE;
+    ptr = _RW( nullptr );
+    T_NULL;
+    T_UNIQUE;
+
+
   }
 
 template<class _RW>
index a080c4f..8d9ab1a 100644 (file)
@@ -137,7 +137,7 @@ namespace zypp
       { return _cmd ? _cmd->getpid() : NotConnected; }
 
       bool isOpen() const
-      { return _cmd; }
+      { return _cmd != nullptr; }
 
       int lastReturn() const
       { return _lastReturn; }
index 350382d..861088a 100644 (file)
@@ -106,9 +106,7 @@ namespace zypp {
     // METHOD TYPE : Constructor
     //
     TmpPath::TmpPath()
-    :_impl( 0 ) // empty Pathname
-    {
-    }
+    {}
 
     ///////////////////////////////////////////////////////////////////
     //
@@ -116,9 +114,8 @@ namespace zypp {
     // METHOD TYPE : Constructor
     //
     TmpPath::TmpPath( const Pathname & tmpPath_r )
-    :_impl( tmpPath_r.empty() ? 0 : new Impl( tmpPath_r ) )
-    {
-    }
+    :_impl( tmpPath_r.empty() ? nullptr : new Impl( tmpPath_r ) )
+    {}
 
     ///////////////////////////////////////////////////////////////////
     //
index c145bcc..e448f52 100644 (file)
 #include <boost/weak_ptr.hpp>
 #include <boost/intrusive_ptr.hpp>
 
-// boost-1.53 removes implicit smart pointer conversion to
-// an unspecified_bool_type in favor of an explicit operator
-// bool. Not sure if we like this...
-#define ZYPP_SMARTPTR_HAS_UNSPECIFIED_BOOL_TYPE 0
-
 ///////////////////////////////////////////////////////////////////
 namespace zypp
 { /////////////////////////////////////////////////////////////////
@@ -271,15 +266,15 @@ namespace zypp
       {
         typedef typename _Traits::_Ptr               _Ptr;
         typedef typename _Traits::_constPtr          _constPtr;
-#if ZYPP_SMARTPTR_HAS_UNSPECIFIED_BOOL_TYPE
-        typedef typename _Ptr::unspecified_bool_type unspecified_bool_type;
-#else
-       typedef RW_pointer<_D,_Traits> _ThisType;
-       typedef const _D * (_ThisType::*unspecified_bool_type)() const;
-#endif
+
+        RW_pointer()
+        {}
+
+        RW_pointer( std::nullptr_t )
+        {}
 
         explicit
-        RW_pointer( typename _Ptr::element_type * dptr = 0 )
+        RW_pointer( typename _Ptr::element_type * dptr )
         : _dptr( dptr )
         {}
 
@@ -288,6 +283,9 @@ namespace zypp
         : _dptr( dptr )
         {}
 
+        RW_pointer & operator=( std::nullptr_t )
+       { reset(); return *this; }
+
         void reset()
         { _Ptr().swap( _dptr ); }
 
@@ -300,12 +298,8 @@ namespace zypp
         void swap( _Ptr & rhs )
         { _dptr.swap( rhs ); }
 
-        operator unspecified_bool_type() const
-#if ZYPP_SMARTPTR_HAS_UNSPECIFIED_BOOL_TYPE
-        { return _dptr; }
-#else
-       { return _dptr.get() == 0 ? 0 : (unspecified_bool_type)&_ThisType::get; }
-#endif
+        explicit operator bool() const
+        { return _dptr.get() != nullptr; }
 
         const _D & operator*() const
         { return *_dptr; };
@@ -350,7 +344,7 @@ namespace zypp
      *
      * Print the \c _D object the RW_pointer refers, or \c "NULL"
      * if the pointer is \c NULL.
-    */
+     */
     template<class _D, class _Ptr>
       inline std::ostream & operator<<( std::ostream & str, const RW_pointer<_D, _Ptr> & obj )
       {
@@ -364,21 +358,29 @@ namespace zypp
       inline bool operator==( const RW_pointer<_D, _Ptr> & lhs, const RW_pointer<_D, _Ptr> & rhs )
       { return( lhs.get() == rhs.get() ); }
     /** \relates RW_pointer */
-     template<class _D, class _Ptr>
-       inline bool operator==( const RW_pointer<_D, _Ptr> & lhs, const typename _Ptr::_Ptr & rhs )
-       { return( lhs.get() == rhs.get() ); }
+    template<class _D, class _Ptr>
+      inline bool operator==( const RW_pointer<_D, _Ptr> & lhs, const typename _Ptr::_Ptr & rhs )
+      { return( lhs.get() == rhs.get() ); }
     /** \relates RW_pointer */
-     template<class _D, class _Ptr>
-       inline bool operator==( const typename _Ptr::_Ptr & lhs, const RW_pointer<_D, _Ptr> & rhs )
-       { return( lhs.get() == rhs.get() ); }
+    template<class _D, class _Ptr>
+      inline bool operator==( const typename _Ptr::_Ptr & lhs, const RW_pointer<_D, _Ptr> & rhs )
+      { return( lhs.get() == rhs.get() ); }
     /** \relates RW_pointer */
-     template<class _D, class _Ptr>
-       inline bool operator==( const RW_pointer<_D, _Ptr> & lhs, const typename _Ptr::_constPtr & rhs )
-       { return( lhs.get() == rhs.get() ); }
+    template<class _D, class _Ptr>
+      inline bool operator==( const RW_pointer<_D, _Ptr> & lhs, const typename _Ptr::_constPtr & rhs )
+      { return( lhs.get() == rhs.get() ); }
+    /** \relates RW_pointer */
+    template<class _D, class _Ptr>
+      inline bool operator==( const typename _Ptr::_constPtr & lhs, const RW_pointer<_D, _Ptr> & rhs )
+      { return( lhs.get() == rhs.get() ); }
     /** \relates RW_pointer */
-     template<class _D, class _Ptr>
-       inline bool operator==( const typename _Ptr::_constPtr & lhs, const RW_pointer<_D, _Ptr> & rhs )
-       { return( lhs.get() == rhs.get() ); }
+    template<class _D, class _Ptr>
+      inline bool operator==( const RW_pointer<_D, _Ptr> & lhs, std::nullptr_t )
+      { return( lhs.get() == nullptr ); }
+    /** \relates RW_pointer */
+    template<class _D, class _Ptr>
+      inline bool operator==( std::nullptr_t, const RW_pointer<_D, _Ptr> & rhs )
+      { return( nullptr == rhs.get() ); }
 
 
     /** \relates RW_pointer */
@@ -386,21 +388,29 @@ namespace zypp
       inline bool operator!=( const RW_pointer<_D, _Ptr> & lhs, const RW_pointer<_D, _Ptr> & rhs )
       { return ! ( lhs == rhs ); }
     /** \relates RW_pointer */
-     template<class _D, class _Ptr>
-       inline bool operator!=( const RW_pointer<_D, _Ptr> & lhs, const typename _Ptr::_Ptr & rhs )
-       { return ! ( lhs == rhs ); }
+    template<class _D, class _Ptr>
+      inline bool operator!=( const RW_pointer<_D, _Ptr> & lhs, const typename _Ptr::_Ptr & rhs )
+      { return ! ( lhs == rhs ); }
+    /** \relates RW_pointer */
+    template<class _D, class _Ptr>
+      inline bool operator!=( const typename _Ptr::_Ptr & lhs, const RW_pointer<_D, _Ptr> & rhs )
+      { return ! ( lhs == rhs ); }
+    /** \relates RW_pointer */
+    template<class _D, class _Ptr>
+      inline bool operator!=( const RW_pointer<_D, _Ptr> & lhs, const typename _Ptr::_constPtr & rhs )
+      { return ! ( lhs == rhs ); }
     /** \relates RW_pointer */
-     template<class _D, class _Ptr>
-       inline bool operator!=( const typename _Ptr::_Ptr & lhs, const RW_pointer<_D, _Ptr> & rhs )
-       { return ! ( lhs == rhs ); }
+    template<class _D, class _Ptr>
+      inline bool operator!=( const typename _Ptr::_constPtr & lhs, const RW_pointer<_D, _Ptr> & rhs )
+      { return ! ( lhs == rhs ); }
     /** \relates RW_pointer */
-     template<class _D, class _Ptr>
-       inline bool operator!=( const RW_pointer<_D, _Ptr> & lhs, const typename _Ptr::_constPtr & rhs )
-       { return ! ( lhs == rhs ); }
+    template<class _D, class _Ptr>
+      inline bool operator!=( const RW_pointer<_D, _Ptr> & lhs, std::nullptr_t )
+      { return( lhs.get() != nullptr ); }
     /** \relates RW_pointer */
-     template<class _D, class _Ptr>
-       inline bool operator!=( const typename _Ptr::_constPtr & lhs, const RW_pointer<_D, _Ptr> & rhs )
-       { return ! ( lhs == rhs ); }
+    template<class _D, class _Ptr>
+      inline bool operator!=( std::nullptr_t, const RW_pointer<_D, _Ptr> & rhs )
+      { return( nullptr != rhs.get() ); }
 
     ///////////////////////////////////////////////////////////////////
 
@@ -408,7 +418,7 @@ namespace zypp
      * Calls \a rhs <tt>-\>clone()</tt>. Being defined as a
      * function outside \ref RWCOW_pointer allows to overload
      * it, in case a specific \a _D does not have <tt>clone()</tt>.
-    */
+     */
     template<class _D>
       inline _D * rwcowClone( const _D * rhs )
       { return rhs->clone(); }
@@ -429,15 +439,15 @@ namespace zypp
       {
         typedef typename _Traits::_Ptr               _Ptr;
         typedef typename _Traits::_constPtr          _constPtr;
-#if ZYPP_SMARTPTR_HAS_UNSPECIFIED_BOOL_TYPE
-        typedef typename _Ptr::unspecified_bool_type unspecified_bool_type;
-#else
-       typedef RWCOW_pointer<_D,_Traits> _ThisType;
-       typedef const _D * (_ThisType::*unspecified_bool_type)() const;
-#endif
+
+       RWCOW_pointer()
+       {}
+
+       RWCOW_pointer( std::nullptr_t )
+       {}
 
         explicit
-        RWCOW_pointer( typename _Ptr::element_type * dptr = 0 )
+        RWCOW_pointer( typename _Ptr::element_type * dptr )
         : _dptr( dptr )
         {}
 
@@ -446,6 +456,9 @@ namespace zypp
         : _dptr( dptr )
         {}
 
+        RWCOW_pointer & operator=( std::nullptr_t )
+       { reset(); return *this; }
+
         void reset()
         { _Ptr().swap( _dptr ); }
 
@@ -458,12 +471,8 @@ namespace zypp
         void swap( _Ptr & rhs )
         { _dptr.swap( rhs ); }
 
-        operator unspecified_bool_type() const
-#if ZYPP_SMARTPTR_HAS_UNSPECIFIED_BOOL_TYPE
-        { return _dptr; }
-#else
-       { return _dptr ? 0 : (unspecified_bool_type)&_ThisType::get; }
-#endif
+        explicit operator bool() const
+       { return _dptr.get() != nullptr; }
 
         const _D & operator*() const
         { return *_dptr; };
@@ -516,7 +525,7 @@ namespace zypp
      *
      * Print the \c _D object the RWCOW_pointer refers, or \c "NULL"
      * if the pointer is \c NULL.
-    */
+     */
     template<class _D, class _Ptr>
       inline std::ostream & operator<<( std::ostream & str, const RWCOW_pointer<_D, _Ptr> & obj )
       {
@@ -545,6 +554,14 @@ namespace zypp
     template<class _D, class _Ptr>
       inline bool operator==( const typename _Ptr::_constPtr & lhs, const RWCOW_pointer<_D, _Ptr> & rhs )
       { return( lhs.get() == rhs.get() ); }
+    /** \relates RWCOW_pointer */
+    template<class _D, class _Ptr>
+      inline bool operator==( const RWCOW_pointer<_D, _Ptr> & lhs, std::nullptr_t )
+      { return( lhs.get() == nullptr ); }
+    /** \relates RWCOW_pointer */
+    template<class _D, class _Ptr>
+      inline bool operator==( std::nullptr_t, const RWCOW_pointer<_D, _Ptr> & rhs )
+      { return( nullptr == rhs.get() ); }
 
     /** \relates RWCOW_pointer */
     template<class _D, class _Ptr>
@@ -566,6 +583,14 @@ namespace zypp
     template<class _D, class _Ptr>
       inline bool operator!=( const typename _Ptr::_constPtr & lhs, const RWCOW_pointer<_D, _Ptr> & rhs )
       { return ! ( lhs == rhs ); }
+    /** \relates RWCOW_pointer */
+    template<class _D, class _Ptr>
+      inline bool operator!=( const RWCOW_pointer<_D, _Ptr> & lhs, std::nullptr_t )
+      { return( lhs.get() != nullptr ); }
+    /** \relates RWCOW_pointer */
+    template<class _D, class _Ptr>
+      inline bool operator!=( std::nullptr_t, const RWCOW_pointer<_D, _Ptr> & rhs )
+      { return( nullptr != rhs.get() ); }
 
     ///////////////////////////////////////////////////////////////////
 
index 2c5dfb3..0315364 100644 (file)
@@ -197,7 +197,7 @@ namespace zypp
 
     /** Whether the pattern is already compiled. */
     bool isCompiled() const
-    { return _matcher; }
+    { return _matcher != nullptr; }
 
     /** Return whether string matches. */
     bool doMatch( const char * string_r ) const
index 57d0922..f5b5cde 100644 (file)
@@ -520,7 +520,7 @@ class MediaHandler {
        /**
         * True if media is attached.
         **/
-       virtual bool isAttached() const { return _mediaSource; }
+       virtual bool isAttached() const { return _mediaSource != nullptr; }
 
        /**
         * Return the local directory that corresponds to medias url,
@@ -648,7 +648,7 @@ class MediaHandler {
         * return the deltafile set with setDeltafile()
         */
        Pathname deltafile () const;
-   
+
     public:
 
        /**
index 0144ca1..7295132 100644 (file)
@@ -239,7 +239,7 @@ namespace zypp
 
     //////////////////////////////////////////////////////////////////
     // STATIC
-    zypp::RW_pointer<MediaManager_Impl> MediaManager::m_impl(NULL);
+    zypp::RW_pointer<MediaManager_Impl> MediaManager::m_impl;
 
 
     //////////////////////////////////////////////////////////////////
index 3985bdf..1e403e2 100644 (file)
@@ -55,13 +55,13 @@ namespace zypp
          }
 
          bool hasRepoIndex() const
-         { return _repoindex; }
+         { return _repoindex != nullptr; }
 
          RepoIndex_Ptr handoutRepoIndex()
          {
            RepoIndex_Ptr ret;
            ret.swap( _repoindex );
-           _repoindex = 0;
+           _repoindex = nullptr;
            return ret;
          }