diff options
author | Mark Page <aptitude@btconnect.com> | 2016-05-18 08:33:33 +0100 |
---|---|---|
committer | Noel Grandin <noelgrandin@gmail.com> | 2016-05-23 11:30:15 +0000 |
commit | d64431ac5a7bede7661c64e0bd6d46805841e704 (patch) | |
tree | 8c2428460af63e2417d36508d8ea5ce3e1a45d96 /vcl | |
parent | cffc2471bc7d08f27a995e4e0f6fcf49d74d0f5a (diff) |
Simplify GfxLink using std::shared_ptr to clarify ownership
The functionality has not changed in this class, however the ABI
has changed (this class is DLL Public)
Change-Id: I11005f03e747d56cb59550e071755429390db7a7
Reviewed-on: https://gerrit.libreoffice.org/25081
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Noel Grandin <noelgrandin@gmail.com>
Diffstat (limited to 'vcl')
-rw-r--r-- | vcl/source/gdi/gfxlink.cxx | 225 |
1 files changed, 65 insertions, 160 deletions
diff --git a/vcl/source/gdi/gfxlink.cxx b/vcl/source/gdi/gfxlink.cxx index 266c8a46681d..5b87fb3b1cac 100644 --- a/vcl/source/gdi/gfxlink.cxx +++ b/vcl/source/gdi/gfxlink.cxx @@ -30,67 +30,29 @@ #include <com/sun/star/ucb/CommandAbortedException.hpp> #include <memory> -GfxLink::GfxLink() : - meType ( GFX_LINK_TYPE_NONE ), - mpBuf ( nullptr ), - mpSwap ( nullptr ), - mnBufSize ( 0 ), - mnUserId ( 0UL ), - mpImpData ( new ImpGfxLink ) +GfxLink::GfxLink() { } -GfxLink::GfxLink( const GfxLink& rGfxLink ) : - mpImpData( new ImpGfxLink ) -{ - ImplCopy( rGfxLink ); -} - -GfxLink::GfxLink( sal_uInt8* pBuf, sal_uInt32 nSize, GfxLinkType nType ) : - mpImpData( new ImpGfxLink ) +GfxLink::GfxLink( sal_uInt8* pBuf, sal_uInt32 nSize, GfxLinkType nType ) { DBG_ASSERT( pBuf != nullptr && nSize, "GfxLink::GfxLink(): empty/NULL buffer given" ); meType = nType; - mnBufSize = nSize; - mpSwap = nullptr; - mnUserId = 0UL; - mpBuf = new ImpBuffer( pBuf ); + mnSwapInDataSize = nSize; + mpSwapInData = std::shared_ptr<sal_uInt8>(pBuf); } GfxLink::~GfxLink() { - if( mpBuf && !( --mpBuf->mnRefCount ) ) - delete mpBuf; - - if( mpSwap && !( --mpSwap->mnRefCount ) ) - delete mpSwap; - - delete mpImpData; -} - -GfxLink& GfxLink::operator=( const GfxLink& rGfxLink ) -{ - if( &rGfxLink != this ) - { - if ( mpBuf && !( --mpBuf->mnRefCount ) ) - delete mpBuf; - - if( mpSwap && !( --mpSwap->mnRefCount ) ) - delete mpSwap; - - ImplCopy( rGfxLink ); - } - - return *this; } bool GfxLink::IsEqual( const GfxLink& rGfxLink ) const { bool bIsEqual = false; - if ( ( mnBufSize == rGfxLink.mnBufSize ) && ( meType == rGfxLink.meType ) ) + if ( ( mnSwapInDataSize == rGfxLink.mnSwapInDataSize ) && ( meType == rGfxLink.meType ) ) { const sal_uInt8* pSource = GetData(); const sal_uInt8* pDest = rGfxLink.GetData(); @@ -106,22 +68,6 @@ bool GfxLink::IsEqual( const GfxLink& rGfxLink ) const return bIsEqual; } -void GfxLink::ImplCopy( const GfxLink& rGfxLink ) -{ - mnBufSize = rGfxLink.mnBufSize; - meType = rGfxLink.meType; - mpBuf = rGfxLink.mpBuf; - mpSwap = rGfxLink.mpSwap; - mnUserId = rGfxLink.mnUserId; - *mpImpData = *rGfxLink.mpImpData; - - if( mpBuf ) - mpBuf->mnRefCount++; - - if( mpSwap ) - mpSwap->mnRefCount++; -} - bool GfxLink::IsNative() const { @@ -134,21 +80,21 @@ const sal_uInt8* GfxLink::GetData() const if( IsSwappedOut() ) const_cast<GfxLink*>(this)->SwapIn(); - return( mpBuf ? mpBuf->mpBuffer : nullptr ); + return( mpSwapInData.get() ); } void GfxLink::SetPrefSize( const Size& rPrefSize ) { - mpImpData->maPrefSize = rPrefSize; - mpImpData->mbPrefSizeValid = true; + maPrefSize = rPrefSize; + mbPrefSizeValid = true; } void GfxLink::SetPrefMapMode( const MapMode& rPrefMapMode ) { - mpImpData->maPrefMapMode = rPrefMapMode; - mpImpData->mbPrefMapModeValid = true; + maPrefMapMode = rPrefMapMode; + mbPrefMapModeValid = true; } @@ -156,7 +102,7 @@ bool GfxLink::LoadNative( Graphic& rGraphic ) { bool bRet = false; - if( IsNative() && mnBufSize ) + if( IsNative() && mnSwapInDataSize ) { const sal_uInt8* pData = GetData(); @@ -165,15 +111,12 @@ bool GfxLink::LoadNative( Graphic& rGraphic ) SvMemoryStream aMemStm; ConvertDataFormat nCvtType; - aMemStm.SetBuffer( const_cast<sal_uInt8*>(pData), mnBufSize, mnBufSize ); + aMemStm.SetBuffer( const_cast<sal_uInt8*>(pData), mnSwapInDataSize, mnSwapInDataSize ); switch( meType ) { case GFX_LINK_TYPE_NATIVE_GIF: nCvtType = ConvertDataFormat::GIF; break; - - // #i15508# added BMP type for better exports (reload when swapped - checked, works) case GFX_LINK_TYPE_NATIVE_BMP: nCvtType = ConvertDataFormat::BMP; break; - case GFX_LINK_TYPE_NATIVE_JPG: nCvtType = ConvertDataFormat::JPG; break; case GFX_LINK_TYPE_NATIVE_PNG: nCvtType = ConvertDataFormat::PNG; break; case GFX_LINK_TYPE_NATIVE_TIF: nCvtType = ConvertDataFormat::TIF; break; @@ -195,21 +138,29 @@ bool GfxLink::LoadNative( Graphic& rGraphic ) void GfxLink::SwapOut() { - if( !IsSwappedOut() && mpBuf ) + if( !IsSwappedOut() && mpSwapInData && mnSwapInDataSize ) { - mpSwap = new ImpSwap( mpBuf->mpBuffer, mnBufSize ); + ::utl::TempFile aTempFile; - if( !mpSwap->IsSwapped() ) - { - delete mpSwap; - mpSwap = nullptr; - } - else + OUString aURL = aTempFile.GetURL(); + + if( !aURL.isEmpty() ) { - if( !( --mpBuf->mnRefCount ) ) - delete mpBuf; + std::shared_ptr<GfxLink::SwapOutData> pSwapOut; + pSwapOut = std::make_shared<SwapOutData>(aURL); // aURL is removed in the destructor + std::unique_ptr<SvStream> xOStm(::utl::UcbStreamHelper::CreateStream( aURL, STREAM_READWRITE | StreamMode::SHARE_DENYWRITE )); + if( xOStm ) + { + xOStm->Write( mpSwapInData.get(), mnSwapInDataSize ); + bool bError = ( ERRCODE_NONE != xOStm->GetError() ); + xOStm.reset(); - mpBuf = nullptr; + if( !bError ) + { + mpSwapOutData = pSwapOut; + mpSwapInData.reset(); + } + } } } } @@ -218,12 +169,12 @@ void GfxLink::SwapIn() { if( IsSwappedOut() ) { - mpBuf = new ImpBuffer( mpSwap->GetData() ); - - if( !( --mpSwap->mnRefCount ) ) - delete mpSwap; - - mpSwap = nullptr; + auto pData = GetSwapInData(); + if (pData) + { + mpSwapInData = pData; + mpSwapOutData.reset(); + } } } @@ -231,10 +182,9 @@ bool GfxLink::ExportNative( SvStream& rOStream ) const { if( GetDataSize() ) { - if( IsSwappedOut() ) - mpSwap->WriteTo( rOStream ); - else if( GetData() ) - rOStream.Write( GetData(), GetDataSize() ); + auto pData = GetSwapInData(); + if (pData) + rOStream.Write( pData.get(), mnSwapInDataSize ); } return ( rOStream.GetError() == ERRCODE_NONE ); @@ -255,10 +205,9 @@ SvStream& WriteGfxLink( SvStream& rOStream, const GfxLink& rGfxLink ) if( rGfxLink.GetDataSize() ) { - if( rGfxLink.IsSwappedOut() ) - rGfxLink.mpSwap->WriteTo( rOStream ); - else if( rGfxLink.GetData() ) - rOStream.Write( rGfxLink.GetData(), rGfxLink.GetDataSize() ); + auto pData = rGfxLink.GetSwapInData(); + if (pData) + rOStream.Write( pData.get(), rGfxLink.mnSwapInDataSize ); } return rOStream; @@ -270,8 +219,7 @@ SvStream& ReadGfxLink( SvStream& rIStream, GfxLink& rGfxLink) MapMode aMapMode; sal_uInt32 nSize; sal_uInt32 nUserId; - sal_uInt16 nType; - sal_uInt8* pBuf; + sal_uInt16 nType; bool bMapAndSizeValid( false ); std::unique_ptr<VersionCompat> pCompat(new VersionCompat( rIStream, StreamMode::READ )); @@ -287,7 +235,7 @@ SvStream& ReadGfxLink( SvStream& rIStream, GfxLink& rGfxLink) pCompat.reset(); // destructor writes stuff into the header - pBuf = new sal_uInt8[ nSize ]; + sal_uInt8* pBuf = new sal_uInt8[ nSize ]; rIStream.Read( pBuf, nSize ); rGfxLink = GfxLink( pBuf, nSize, (GfxLinkType) nType ); @@ -302,83 +250,40 @@ SvStream& ReadGfxLink( SvStream& rIStream, GfxLink& rGfxLink) return rIStream; } -ImpSwap::ImpSwap( sal_uInt8* pData, sal_uLong nDataSize ) : - mnDataSize( nDataSize ), - mnRefCount( 1UL ) +GfxLink::SwapOutData::SwapOutData(const OUString &aURL) : maURL(aURL) { - if( pData && mnDataSize ) - { - ::utl::TempFile aTempFile; - - maURL = aTempFile.GetURL(); - if( !maURL.isEmpty() ) - { - std::unique_ptr<SvStream> xOStm(::utl::UcbStreamHelper::CreateStream( maURL, STREAM_READWRITE | StreamMode::SHARE_DENYWRITE )); - if( xOStm ) - { - xOStm->Write( pData, mnDataSize ); - bool bError = ( ERRCODE_NONE != xOStm->GetError() ); - xOStm.reset(); - - if( bError ) - { - osl_removeFile( maURL.pData ); - maURL.clear(); - } - } - } - } } -ImpSwap::~ImpSwap() +GfxLink::SwapOutData::~SwapOutData() { - if( IsSwapped() ) + if( maURL.getLength() > 0 ) osl_removeFile( maURL.pData ); } -sal_uInt8* ImpSwap::GetData() const +std::shared_ptr<sal_uInt8> GfxLink::GetSwapInData() const { - sal_uInt8* pData; + if( !IsSwappedOut() ) + return mpSwapInData; + + std::shared_ptr<sal_uInt8> pData; - if( IsSwapped() ) + std::unique_ptr<SvStream> xIStm(::utl::UcbStreamHelper::CreateStream( mpSwapOutData->maURL, STREAM_READWRITE )); + if( xIStm ) { - std::unique_ptr<SvStream> xIStm(::utl::UcbStreamHelper::CreateStream( maURL, STREAM_READWRITE )); - if( xIStm ) + pData = std::shared_ptr<sal_uInt8>(new sal_uInt8[ mnSwapInDataSize ]); + xIStm->Read( pData.get(), mnSwapInDataSize ); + bool bError = ( ERRCODE_NONE != xIStm->GetError() ); + sal_Size nActReadSize = xIStm->Tell(); + if (nActReadSize != mnSwapInDataSize) { - pData = new sal_uInt8[ mnDataSize ]; - xIStm->Read( pData, mnDataSize ); - bool bError = ( ERRCODE_NONE != xIStm->GetError() ); - sal_Size nActReadSize = xIStm->Tell(); - if (nActReadSize != mnDataSize) - { - bError = true; - } - xIStm.reset(); - - if( bError ) - { - delete[] pData; - pData = nullptr; - } + bError = true; } - else - pData = nullptr; - } - else - pData = nullptr; + xIStm.reset(); - return pData; -} - -void ImpSwap::WriteTo( SvStream& rOStm ) const -{ - sal_uInt8* pData = GetData(); - - if( pData ) - { - rOStm.Write( pData, mnDataSize ); - delete[] pData; + if( bError ) + pData.reset(); } + return pData; } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |