00001 /*---------------------------------------------------------------------\ 00002 | | 00003 | __ __ ____ _____ ____ | 00004 | \ \ / /_ _/ ___|_ _|___ \ | 00005 | \ V / _` \___ \ | | __) | | 00006 | | | (_| |___) || | / __/ | 00007 | |_|\__,_|____/ |_| |_____| | 00008 | | 00009 | core system | 00010 | (C) SuSE Linux AG | 00011 \----------------------------------------------------------------------/ 00012 00013 File: _Handle.h 00014 00015 Author: Michael Andres <ma@suse.de> 00016 Maintainer: Michael Andres <ma@suse.de> 00017 00018 Purpose: Handle/Body: Base for Handle classes manipuating a Body 00019 00020 /-*/ 00021 #ifndef _Handle_h 00022 #define _Handle_h 00023 00024 #include <iosfwd> 00025 00026 #include <y2util/_Body.h> 00027 00029 // 00030 // CLASS NAME : _Handle 00068 class _Handle { 00069 00070 private: 00071 00075 _Body * _body; 00076 00077 static void * operator new( size_t ); 00078 00079 public: 00080 00084 _Handle() : _body( 0 ) {} 00088 _Handle( _Body * body_r ) : _body( body_r ) { 00089 _Body::ref( _body ); 00090 } 00094 _Handle( const _Handle & rhs ) : _body( rhs._body ) { 00095 _Body::ref( _body ); 00096 } 00100 _Handle & operator=( _Body * rhs ) { 00101 if ( rhs != _body ) { 00102 _Body::unref( _body ); 00103 _body = rhs; 00104 _Body::ref( _body ); 00105 } 00106 return *this; 00107 } 00111 _Handle & operator=( const _Handle & rhs ) { 00112 return operator=( rhs._body ); 00113 } 00117 virtual ~_Handle() { 00118 _Body::unref( _body ); 00119 } 00120 00121 public: 00122 00126 operator const void *() const { return _body; } 00127 00131 _Body * operator->() { return _body; } 00132 00136 const _Body * operator->() const { return _body; } 00137 00141 _Body & operator*() { if ( !_body ) throw( this ); return *_body; } 00142 00146 const _Body & operator*() const { if ( !_body ) throw( this ); return *_body; } 00147 00148 public: 00149 00155 virtual std::ostream & dumpOn( std::ostream & str ) const; 00156 }; 00157 00161 std::ostream & operator<<( std::ostream & str, const _Handle & obj ); 00162 00164 00166 // 00167 // CLASS NAME : _DHandle<_Tp> 00173 template<typename _Tp> struct _DHandle : public _Handle { 00174 00175 _DHandle() : _Handle() {} 00176 00177 _DHandle( _Body * body_r ) : _Handle( body_r ) {} 00178 00179 _Tp * operator->() { 00180 return static_cast<_Tp *>(_Handle::operator->()); 00181 } 00182 00183 const _Tp * operator->() const { 00184 return static_cast<const _Tp *>(_Handle::operator->()); 00185 } 00186 00187 _Tp & _D() { 00188 return *operator->(); 00189 } 00190 00191 const _Tp & _D() const { 00192 return *operator->(); 00193 } 00194 }; 00195 00197 00198 #endif // _Handle_h
1.4.1