ReferenceCounted: Added virtual ostream operator<<.
authorMichael Andres <ma@suse.de>
Mon, 12 Dec 2005 14:44:12 +0000 (14:44 +0000)
committerMichael Andres <ma@suse.de>
Mon, 12 Dec 2005 14:44:12 +0000 (14:44 +0000)
Derived classes may overload 'virtual ReferenceCounted::dumpOn'
to do the stream output.

zypp/base/Exception.cc
zypp/base/Exception.h
zypp/base/Makefile.am
zypp/base/ReferenceCounted.cc [new file with mode: 0644]
zypp/base/ReferenceCounted.h
zypp/source/yum/YUMSource.cc

index 8984360..fa1c2d2 100644 (file)
@@ -39,6 +39,9 @@ namespace zypp
   } // namespace exception_detail
   ///////////////////////////////////////////////////////////////////
 
+  Exception::Exception()
+  {}
+
   Exception::Exception( const std::string & msg_r )
   : _msg( msg_r )
   {}
@@ -46,12 +49,27 @@ namespace zypp
   Exception::~Exception() throw()
   {}
 
+#if 0
   std::string Exception::asString() const
   {
     std::string ret( _where.asString() );
     ret += ": ";
     return ret += _msg;
   }
+#endif
+
+  std::ostream & Exception::dumpOn( std::ostream & str ) const
+  {
+    return str << _msg;
+  }
+
+  std::ostream & Exception::dumpError( std::ostream & str ) const
+  {
+    return dumpOn( str << _where << ": " );
+  }
+
+  std::ostream & operator<<( std::ostream & str, const Exception & obj )
+  { return obj.dumpError( str ); }
 
   std::string Exception::strErrno( int errno_r )
   {
@@ -71,19 +89,6 @@ namespace zypp
     INT << where_r << " " << prefix_r << " " << excpt_r << endl;
   }
 
-  std::ostream & Exception::dumpOn( std::ostream & str ) const
-  {
-    return str << asString(); // fix it!
-  }
-
-  std::ostream & Exception::dumpError( std::ostream & str ) const
-  {
-    return dumpOn( str ); // fix it! prepend location info
-  }
-
-  std::ostream & operator<<( std::ostream & str, const Exception & obj )
-  { return obj.dumpError( str ); }
-
   /////////////////////////////////////////////////////////////////
 } // namespace zypp
 ///////////////////////////////////////////////////////////////////
index 7260e55..29f7eb9 100644 (file)
@@ -73,7 +73,7 @@ namespace zypp
    *  44     {
    *  45       try
    *  46         {
-   *  47           ZYPP_THROW( "Something bad happened." );
+   *  47           ZYPP_THROW( Exception, "Something bad happened." );
    *  48         }
    *  49       catch ( Exception & excpt )
    *  50         {
@@ -107,6 +107,11 @@ namespace zypp
   public:
     typedef exception_detail::CodeLocation CodeLocation;
 
+    /** Default ctor.
+     * Use \ref ZYPP_THROW to throw exceptions.
+    */
+    Exception();
+
     /** Ctor taking a message.
      * Use \ref ZYPP_THROW to throw exceptions.
     */
@@ -123,7 +128,7 @@ namespace zypp
     void relocate( const CodeLocation & where_r ) const
     { _where = where_r; }
 
-    /** Return message string. */
+    /** Return the provided message string. */
     const std::string & msg() const
     { return _msg; }
 
@@ -215,6 +220,10 @@ namespace zypp
 #define ZYPP_THROW(EXCPTTYPE, MSG)\
   ZYPP_DOTHROW( EXCPTTYPE( MSG ) )
 
+  /** Throw Exception built from a message string. */
+#define ZYPP_THROW_MSG(EXCPTTYPE, MSG)\
+  ZYPP_DOTHROW( EXCPTTYPE( MSG ) )
+
   /** Throw Exception built from errno. */
 #define ZYPP_THROW_ERRNO(EXCPTTYPE)\
   ZYPP_DOTHROW( EXCPTTYPE( ::zypp::Exception::strErrno(errno) ) )
index 55ca9ee..8177b74 100644 (file)
@@ -16,9 +16,9 @@ include_HEADERS =     \
        PtrTypes.h      \
        ReferenceCounted.h      \
        String.h        \
-       StringVal.h             \
-                               \
-                               \
+       StringVal.h     \
+               \
+               \
        ExternalDataSource.h
 
 
@@ -31,10 +31,11 @@ lib@PACKAGE@_base_la_SOURCES =      \
        Exception.cc    \
        Fd.cc           \
        IOStream.cc     \
+       ReferenceCounted.cc     \
        String.cc       \
        StringVal.cc    \
-                       \
-                       \
+               \
+               \
        ExternalDataSource.cc
 
 lib@PACKAGE@_base_la_LIBADD = -lboost_regex
diff --git a/zypp/base/ReferenceCounted.cc b/zypp/base/ReferenceCounted.cc
new file mode 100644 (file)
index 0000000..22fbd50
--- /dev/null
@@ -0,0 +1,59 @@
+/*---------------------------------------------------------------------\
+|                          ____ _   __ __ ___                          |
+|                         |__  / \ / / . \ . \                         |
+|                           / / \ V /|  _/  _/                         |
+|                          / /__ | | | | | |                           |
+|                         /_____||_| |_| |_|                           |
+|                                                                      |
+\---------------------------------------------------------------------*/
+/** \file      zypp/base/ReferenceCounted.cc
+ *
+*/
+#include <iostream>
+
+#include "zypp/base/Logger.h"
+#include "zypp/base/Exception.h"
+#include "zypp/base/ReferenceCounted.h"
+
+///////////////////////////////////////////////////////////////////
+namespace zypp
+{ /////////////////////////////////////////////////////////////////
+  ///////////////////////////////////////////////////////////////////
+  namespace base
+  { /////////////////////////////////////////////////////////////////
+
+    ReferenceCounted::ReferenceCounted()
+    : _counter( 0 )
+    {}
+
+    ReferenceCounted::ReferenceCounted( const ReferenceCounted & rhs )
+    : _counter( 0 )
+    {}
+
+    ReferenceCounted::~ReferenceCounted()
+    {
+      if ( _counter )
+        {
+          INT << "~ReferenceCounted: nonzero reference count" << std::endl;
+          throw std::out_of_range( "~ReferenceCounted: nonzero reference count" );
+        }
+    }
+
+    void ReferenceCounted::unrefException() const
+    {
+      INT << "ReferenceCounted::unref: zero reference count" << std::endl;
+      throw std::out_of_range( "ReferenceCounted::unref: zero reference count" );
+    }
+
+    std::ostream & ReferenceCounted::dumpOn( std::ostream & str ) const
+    {
+      return str << "ReferenceCounted(@" << (const void *)this
+                 << "<=" << _counter << ")";
+    }
+
+    /////////////////////////////////////////////////////////////////
+  } // namespace base
+  ///////////////////////////////////////////////////////////////////
+  /////////////////////////////////////////////////////////////////
+} // namespace zypp
+///////////////////////////////////////////////////////////////////
index 8628781..f0972eb 100644 (file)
@@ -14,7 +14,6 @@
 
 #include <iosfwd>
 
-#include "zypp/base/Exception.h"
 #include "zypp/base/PtrTypes.h"
 
 ///////////////////////////////////////////////////////////////////
@@ -29,32 +28,28 @@ namespace zypp
     // CLASS NAME : ReferenceCounted
     //
     /** Base class for reference counted objects.
-     * \todo Define exceptions.
      * \todo Make counter thread safe.
-     * \todo get rid of base namesapace.
     */
     class ReferenceCounted
     {
+      /** Stream output via dumpOn. */
+      friend std::ostream & operator<<( std::ostream & str, const ReferenceCounted & obj );
+
     public:
       /** Default ctor.
        * Initial reference count is zero.
       */
-      ReferenceCounted()
-      : _counter( 0 )
-      {}
+      ReferenceCounted();
 
       /** Copy ctor.
        * Initial reference count is zero.
       */
-      ReferenceCounted( const ReferenceCounted & rhs )
-      : _counter( 0 )
-      {}
+      ReferenceCounted( const ReferenceCounted & rhs );
 
       /** Dtor.
-       * \throw INTERNAL if reference count is not zero.
+       * \throw std::out_of_range if reference count is not zero.
       */
-      virtual ~ReferenceCounted()
-      { if ( _counter ) ZYPP_THROW( Exception, "~ReferenceCounted: nonzero reference count" ); }
+      virtual ~ReferenceCounted();
 
       /** Assignment.
        * Reference count remains untouched.
@@ -73,12 +68,12 @@ namespace zypp
 
       /** Release a reference.
        * Deletes the object if reference count gets zero.
-       * \throw INTERNAL if reference count is zero.
+       * \throw std::out_of_range if reference count is zero.
       */
       void unref() const
       {
         if ( !_counter )
-          ZYPP_THROW( Exception, "ReferenceCounted::unref: zero reference count" );
+          unrefException(); // will throw!
         if ( --_counter == 0 )
           delete this;
       }
@@ -95,18 +90,31 @@ namespace zypp
       static void release( const ReferenceCounted * ptr_r )
       { if( ptr_r ) ptr_r->unref(); }
 
+    protected:
+      /** Overload to realize std::ostream & operator\<\<. */
+      virtual std::ostream & dumpOn( std::ostream & str ) const;
+
     private:
       /** The reference counter. */
       mutable unsigned _counter;
+
+      /** Throws Exception on unref. */
+      void unrefException() const;
     };
     ///////////////////////////////////////////////////////////////////
 
+    /** \relates ReferenceCounted intrusive_ptr hook to add_ref. */
     inline void intrusive_ptr_add_ref( const ReferenceCounted * ptr_r )
     { ReferenceCounted::add_ref( ptr_r ); }
 
+    /** \relates ReferenceCounted intrusive_ptr hook to release. */
     inline void intrusive_ptr_release( const ReferenceCounted * ptr_r )
     { ReferenceCounted::release( ptr_r ); }
 
+    /** \relates ReferenceCounted Stream output. */
+    inline std::ostream & operator<<( std::ostream & str, const ReferenceCounted & obj )
+    { return obj.dumpOn( str ); }
+
     /////////////////////////////////////////////////////////////////
   } // namespace base
   ///////////////////////////////////////////////////////////////////
index 44d1f7e..71d5728 100644 (file)
@@ -19,6 +19,7 @@
 #include "zypp/source/yum/YUMGroupImpl.h"
 
 #include "zypp/base/Logger.h"
+#include "zypp/base/Exception.h"
 #include "zypp/CapFactory.h"
 
 #include "zypp/parser/yum/YUMParser.h"