- Fixed RW_pointer unique() and use_count(). These must be implemented
authorMichael Andres <ma@suse.de>
Tue, 14 Feb 2006 14:08:59 +0000 (14:08 +0000)
committerMichael Andres <ma@suse.de>
Tue, 14 Feb 2006 14:08:59 +0000 (14:08 +0000)
  via RW_pointer traits, because they depend on the type of smart
  pointer wrapped.
- Added class base::ProvideNumericId. Base class for objects
  providing a numeric Id. (see the docs)

zypp/base/Makefile.am
zypp/base/ProvideNumericId.h [new file with mode: 0644]
zypp/base/PtrTypes.h

index 0a6d815..42db4bb 100644 (file)
@@ -23,8 +23,9 @@ baseinclude_HEADERS = \
        Gettext.h       \
        IOStream.h      \
        NonCopyable.h   \
-       PtrTypes.h      \
+       ProvideNumericId.h      \
        ReferenceCounted.h      \
+       PtrTypes.h      \
        String.h        \
        Unit.h          \
                \
diff --git a/zypp/base/ProvideNumericId.h b/zypp/base/ProvideNumericId.h
new file mode 100644 (file)
index 0000000..8edf493
--- /dev/null
@@ -0,0 +1,79 @@
+/*---------------------------------------------------------------------\
+|                          ____ _   __ __ ___                          |
+|                         |__  / \ / / . \ . \                         |
+|                           / / \ V /|  _/  _/                         |
+|                          / /__ | | | | | |                           |
+|                         /_____||_| |_| |_|                           |
+|                                                                      |
+\---------------------------------------------------------------------*/
+/** \file      zypp/base/ProvideNumericId.h
+ *
+*/
+#ifndef ZYPP_BASE_PROVIDENUMERICID_H
+#define ZYPP_BASE_PROVIDENUMERICID_H
+
+///////////////////////////////////////////////////////////////////
+namespace zypp
+{ /////////////////////////////////////////////////////////////////
+  ///////////////////////////////////////////////////////////////////
+  namespace base
+  { /////////////////////////////////////////////////////////////////
+
+    ///////////////////////////////////////////////////////////////////
+    //
+    // CLASS NAME : ProvideNumericId
+    //
+    /** Base class for objects providing a numeric Id.
+     * The ctor creates a NumericId from some static counter.
+     *
+     * \code
+     * struct Foo : public base::ProvideNumericId<Foo>
+     * {};
+     * Foo foo;
+     * foo.numericId(); // returns foo's NumericId.
+     * \endcode
+    */
+    template<class _Derived>
+      struct ProvideNumericId
+      {
+      public:
+        typedef unsigned long NumericId;
+
+        /** \return The objects numeric Id. */
+        NumericId numericId() const
+        { return _numericId; }
+
+      protected:
+        /** Default ctor */
+        ProvideNumericId()
+        : _numericId( nextId() )
+        {}
+        /** Copy ctor */
+        ProvideNumericId( const ProvideNumericId & /*rhs*/ )
+        : _numericId( nextId() )
+        {}
+        /** Assign */
+        ProvideNumericId & operator=( const ProvideNumericId & /*rhs*/ )
+        { return *this; }
+        /** Dtor */
+        ~ProvideNumericId()
+        {}
+      private:
+        /**  */
+        static NumericId nextId()
+        {
+          static NumericId _staticCounter = 0;
+          return ++_staticCounter;
+        }
+        /**  */
+        const NumericId _numericId;
+      };
+    ///////////////////////////////////////////////////////////////////
+
+    /////////////////////////////////////////////////////////////////
+  } // namespace base
+  ///////////////////////////////////////////////////////////////////
+  /////////////////////////////////////////////////////////////////
+} // namespace zypp
+///////////////////////////////////////////////////////////////////
+#endif // ZYPP_BASE_PROVIDENUMERICID_H
index ba6c195..413a52a 100644 (file)
@@ -72,9 +72,12 @@ namespace zypp
         {
           typedef shared_ptr<_D>       _Ptr;
           typedef shared_ptr<const _D> _constPtr;
-          /** Check whether pointer is shared. */
-          bool isShared( const _constPtr & ptr_r )
-          { return ptr_r.use_count() > 1; }
+          /** Check whether pointer is not shared. */
+          bool unique( const _constPtr & ptr_r )
+          { return ptr_r.unique(); }
+          /** Return number of references. */
+          long use_count( const _constPtr & ptr_r ) const
+          { return ptr_r.use_count(); }
         };
 
       template<class _D>
@@ -82,9 +85,12 @@ namespace zypp
         {
           typedef intrusive_ptr<_D>       _Ptr;
           typedef intrusive_ptr<const _D> _constPtr;
-          /** Check whether pointer is shared. */
-          bool isShared( const _constPtr & ptr_r )
-          { return ptr_r && (ptr_r->refCount() > 1); }
+          /** Check whether pointer is not shared. */
+          bool unique( const _constPtr & ptr_r )
+          { return !ptr_r || (ptr_r->refCount() <= 1); }
+          /** Return number of references. */
+          long use_count( const _constPtr & ptr_r ) const
+          { return ptr_r ? ptr_r->refCount() : 0; }
         };
     }
     ///////////////////////////////////////////////////////////////////
@@ -181,11 +187,11 @@ namespace zypp
         { return _dptr.get(); }
 
       public:
-        bool      unique() const
-       { return _dptr.unique(); }
+        bool unique() const
+       { return _Traits().unique( _dptr ); }
 
-       long      use_count() const
-       { return _dptr.use_count(); }
+       long use_count() const
+       { return _Traits().use_count( _dptr ); }
 
         _constPtr getPtr() const
         { return _dptr; }
@@ -276,6 +282,12 @@ namespace zypp
         { assertUnshared(); return _dptr.get(); }
 
       public:
+        bool unique() const
+       { return _Traits().unique( _dptr ); }
+
+       long use_count() const
+       { return _Traits().use_count( _dptr ); }
+
         _constPtr getPtr() const
         { return _dptr; }
 
@@ -286,10 +298,8 @@ namespace zypp
 
         void assertUnshared()
         {
-          if ( _Traits().isShared( _dptr ) )
-            {
-              _dptr.reset( rwcowClone( _dptr.get() ) );
-            }
+          if ( !unique() )
+            _dptr.reset( rwcowClone( _dptr.get() ) );
         }
 
       private: