} // namespace exception_detail
///////////////////////////////////////////////////////////////////
+ Exception::Exception()
+ {}
+
Exception::Exception( const std::string & msg_r )
: _msg( msg_r )
{}
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 )
{
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
///////////////////////////////////////////////////////////////////
* 44 {
* 45 try
* 46 {
- * 47 ZYPP_THROW( "Something bad happened." );
+ * 47 ZYPP_THROW( Exception, "Something bad happened." );
* 48 }
* 49 catch ( Exception & excpt )
* 50 {
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.
*/
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; }
#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) ) )
PtrTypes.h \
ReferenceCounted.h \
String.h \
- StringVal.h \
- \
- \
+ StringVal.h \
+ \
+ \
ExternalDataSource.h
Exception.cc \
Fd.cc \
IOStream.cc \
+ ReferenceCounted.cc \
String.cc \
StringVal.cc \
- \
- \
+ \
+ \
ExternalDataSource.cc
lib@PACKAGE@_base_la_LIBADD = -lboost_regex
--- /dev/null
+/*---------------------------------------------------------------------\
+| ____ _ __ __ ___ |
+| |__ / \ / / . \ . \ |
+| / / \ 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
+///////////////////////////////////////////////////////////////////
#include <iosfwd>
-#include "zypp/base/Exception.h"
#include "zypp/base/PtrTypes.h"
///////////////////////////////////////////////////////////////////
// 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.
/** 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;
}
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
///////////////////////////////////////////////////////////////////
#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"