diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2016-06-02 15:06:06 +0200 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2016-06-02 15:33:59 +0200 |
commit | 0d7c5823124696f80583ac2a5f0e28f329f6f786 (patch) | |
tree | c62e4a5490e941f39d775477f1529e9c869fa273 /xmloff/source/draw | |
parent | e5d8dc12fcf64fbbefadefbe863c772dc9134d38 (diff) |
New o3tl::try/doGet to obtain value from Any
...in an attempt to reduce usage of type-unsafe
void const * css::uno::Any::getValue()
These new functions are often more convenient to use than the existing ">>=" and
Any::get<T>. Note how they are careful to provide a pointer directly into the
given Any, instead of creating temporaries.
As an example, replaced most calls of getValue across xmloff:
* Cases that first check for a specific type (via getValueType etc.) and then
call getValue can instead call tryGet. (But beware that tryGet supports some
conversions, which a check for a specific type may have missed---either
intentionally or by accident. Also beware the somewhat common idiom of
checking for TypeClass_ENUM and then using getValue to obtain a sal_Int32;
this cannot be replaced with a call to tryGet.)
* Cases that seem confident that the Any is of the correct type when calling
getValue (but apparently are confident due to some higher-layer protocol, as
the surrounding code does not do any checking via getValueType or similar) can
instead call doGet. It throws an exception if it turns out the confidence
wasn't warranted. (Many of the existing calls that directly dereferenced the
return value of getValue as sal_Bool look suspicious, in that the author might
have thought the given code would also cover a VOID Any---which technically it
even would have happened to do. If any RuntimeExceptions thrown from these
doGet calls start to crop up, these changes need to be revisited. Some may
even be rewritten as uses of ">>=". But at least "make check" did not show
any such problems. Also note that casting the value obtained from getValue to
any css::uno::Reference<X> with X being anything but the base
css::uno::XInterface was always prone to producing a bad pointer, in case the
interface actually stored in the Any derived from X via multiple inheritance.)
* Should there ever be cases where an Any is known to be of the requested type,
some additional forceGet could be introduced (which would assert instead of
throwing an exception).
Change-Id: I2d8739e86314eff73abfcafe01d806f5bc5c34db
Diffstat (limited to 'xmloff/source/draw')
-rw-r--r-- | xmloff/source/draw/XMLImageMapExport.cxx | 3 | ||||
-rw-r--r-- | xmloff/source/draw/animationexport.cxx | 66 | ||||
-rw-r--r-- | xmloff/source/draw/shapeexport.cxx | 13 | ||||
-rw-r--r-- | xmloff/source/draw/ximpcustomshape.cxx | 21 |
4 files changed, 48 insertions, 55 deletions
diff --git a/xmloff/source/draw/XMLImageMapExport.cxx b/xmloff/source/draw/XMLImageMapExport.cxx index 9ea9b5863e88..43ba08306b38 100644 --- a/xmloff/source/draw/XMLImageMapExport.cxx +++ b/xmloff/source/draw/XMLImageMapExport.cxx @@ -18,6 +18,7 @@ */ #include "XMLImageMapExport.hxx" +#include <o3tl/any.hxx> #include <rtl/ustring.hxx> #include <rtl/ustrbuf.hxx> #include <tools/debug.hxx> @@ -199,7 +200,7 @@ void XMLImageMapExport::ExportMapEntry( // is-active aAny = rPropertySet->getPropertyValue(msIsActive); - if (! *static_cast<sal_Bool const *>(aAny.getValue())) + if (! *o3tl::doGet<bool>(aAny)) { mrExport.AddAttribute(XML_NAMESPACE_DRAW, XML_NOHREF, XML_NOHREF); } diff --git a/xmloff/source/draw/animationexport.cxx b/xmloff/source/draw/animationexport.cxx index 97ae81c7b97a..b9180575fe73 100644 --- a/xmloff/source/draw/animationexport.cxx +++ b/xmloff/source/draw/animationexport.cxx @@ -49,7 +49,7 @@ #include <com/sun/star/presentation/ShapeAnimationSubType.hpp> #include <com/sun/star/presentation/EffectCommands.hpp> #include <com/sun/star/drawing/XShape.hpp> - +#include <o3tl/any.hxx> #include <sax/tools/converter.hxx> #include <tools/debug.hxx> @@ -513,7 +513,7 @@ public: void exportAudio( const Reference< XAudio >& xAudio ); void exportCommand( const Reference< XCommand >& xCommand ); - static Reference< XInterface > getParagraphTarget( const ParagraphTarget* pTarget ); + static Reference< XInterface > getParagraphTarget( const ParagraphTarget& pTarget ); static void convertPath( OUStringBuffer& sTmp, const Any& rPath ); void convertValue( XMLTokenEnum eAttributeName, OUStringBuffer& sTmp, const Any& rValue ) const; @@ -1412,14 +1412,14 @@ void AnimationsExporterImpl::exportCommand( const Reference< XCommand >& xComman } } -Reference< XInterface > AnimationsExporterImpl::getParagraphTarget( const ParagraphTarget* pTarget ) +Reference< XInterface > AnimationsExporterImpl::getParagraphTarget( const ParagraphTarget& pTarget ) { - if( pTarget ) try + try { - Reference< XEnumerationAccess > xParaEnumAccess( pTarget->Shape, UNO_QUERY_THROW ); + Reference< XEnumerationAccess > xParaEnumAccess( pTarget.Shape, UNO_QUERY_THROW ); Reference< XEnumeration > xEnumeration( xParaEnumAccess->createEnumeration(), UNO_QUERY_THROW ); - sal_Int32 nParagraph = pTarget->Paragraph; + sal_Int32 nParagraph = pTarget.Paragraph; while( xEnumeration->hasMoreElements() ) { @@ -1450,18 +1450,16 @@ void AnimationsExporterImpl::convertValue( XMLTokenEnum eAttributeName, OUString if( !rValue.hasValue() ) return; - if( rValue.getValueType() == cppu::UnoType<ValuePair>::get() ) + if( auto pValuePair = o3tl::tryGet<ValuePair>(rValue) ) { - const ValuePair* pValuePair = static_cast< const ValuePair* >( rValue.getValue() ); OUStringBuffer sTmp2; convertValue( eAttributeName, sTmp, pValuePair->First ); sTmp.append( ',' ); convertValue( eAttributeName, sTmp2, pValuePair->Second ); sTmp.append( sTmp2.makeStringAndClear() ); } - else if( rValue.getValueType() == cppu::UnoType< Sequence<Any> >::get() ) + else if( auto pSequence = o3tl::tryGet<Sequence<Any>>(rValue) ) { - const Sequence<Any>* pSequence = static_cast< const Sequence<Any>* >( rValue.getValue() ); const sal_Int32 nLength = pSequence->getLength(); sal_Int32 nElement; const Any* pAny = pSequence->getConstArray(); @@ -1478,7 +1476,6 @@ void AnimationsExporterImpl::convertValue( XMLTokenEnum eAttributeName, OUString } else { - OUString aString; sal_Int32 nType; switch( eAttributeName ) @@ -1490,13 +1487,13 @@ void AnimationsExporterImpl::convertValue( XMLTokenEnum eAttributeName, OUString case XML_ANIMATETRANSFORM: case XML_ANIMATEMOTION: { - if( rValue >>= aString ) + if( auto aString = o3tl::tryGet<OUString>(rValue) ) { - sTmp.append( aString ); + sTmp.append( *aString ); } - else if( rValue.getValueType() == cppu::UnoType<double>::get() ) + else if( auto x = o3tl::tryGet<double>(rValue) ) { - sTmp.append( *(static_cast< const double* >( rValue.getValue() )) ); + sTmp.append( *x ); } else { @@ -1530,6 +1527,7 @@ void AnimationsExporterImpl::convertValue( XMLTokenEnum eAttributeName, OUString const XMLPropertyHandler* pHandler = mpSdPropHdlFactory->GetPropertyHandler( nType ); if( pHandler ) { + OUString aString; pHandler->exportXML( aString, rValue, mrExport.GetMM100UnitConverter() ); sTmp.append( aString ); } @@ -1541,9 +1539,8 @@ void AnimationsExporterImpl::convertTiming( OUStringBuffer& sTmp, const Any& rVa if( !rValue.hasValue() ) return; - if( rValue.getValueType() == cppu::UnoType< Sequence<Any> >::get() ) + if( auto pSequence = o3tl::tryGet<Sequence<Any>>(rValue) ) { - const Sequence<Any>* pSequence = static_cast< const Sequence<Any>* >( rValue.getValue() ); const sal_Int32 nLength = pSequence->getLength(); sal_Int32 nElement; const Any* pAny = pSequence->getConstArray(); @@ -1558,22 +1555,19 @@ void AnimationsExporterImpl::convertTiming( OUStringBuffer& sTmp, const Any& rVa sTmp.append( sTmp2.makeStringAndClear() ); } } - else if( rValue.getValueType() == cppu::UnoType<double>::get() ) + else if( auto x = o3tl::tryGet<double>(rValue) ) { - sTmp.append( *(static_cast< const double* >( rValue.getValue() )) ); + sTmp.append( *x ); sTmp.append( 's'); } - else if( rValue.getValueType() == cppu::UnoType<Timing>::get() ) + else if( auto pTiming = o3tl::tryGet<Timing>(rValue) ) { - const Timing* pTiming = static_cast< const Timing* >( rValue.getValue() ); sTmp.append( GetXMLToken( (*pTiming == Timing_MEDIA) ? XML_MEDIA : XML_INDEFINITE ) ); } - else if( rValue.getValueType() == cppu::UnoType<Event>::get() ) + else if( auto pEvent = o3tl::tryGet<Event>(rValue) ) { OUStringBuffer sTmp2; - const Event* pEvent = static_cast< const Event* >( rValue.getValue() ); - if( pEvent->Trigger != EventTrigger::NONE ) { if( pEvent->Source.hasValue() ) @@ -1615,13 +1609,12 @@ void AnimationsExporterImpl::convertTarget( OUStringBuffer& sTmp, const Any& rTa Reference< XInterface > xRef; - if( rTarget.getValueTypeClass() == css::uno::TypeClass_INTERFACE ) + if( !(rTarget >>= xRef) ) { - rTarget >>= xRef; - } - else if( rTarget.getValueType() == cppu::UnoType<ParagraphTarget>::get() ) - { - xRef = getParagraphTarget( static_cast< const ParagraphTarget* >( rTarget.getValue() ) ); + if( auto pt = o3tl::tryGet<ParagraphTarget>(rTarget) ) + { + xRef = getParagraphTarget( *pt ); + } } DBG_ASSERT( xRef.is(), "xmloff::AnimationsExporterImpl::convertTarget(), invalid target type!" ); @@ -1638,15 +1631,13 @@ void AnimationsExporterImpl::prepareValue( const Any& rValue ) if( !rValue.hasValue() ) return; - if( rValue.getValueType() == cppu::UnoType<ValuePair>::get() ) + if( auto pValuePair = o3tl::tryGet<ValuePair>(rValue) ) { - const ValuePair* pValuePair = static_cast< const ValuePair* >( rValue.getValue() ); prepareValue( pValuePair->First ); prepareValue( pValuePair->Second ); } - else if( rValue.getValueType() == cppu::UnoType< Sequence<Any> >::get() ) + else if( auto pSequence = o3tl::tryGet<Sequence<Any>>(rValue) ) { - const Sequence<Any>* pSequence = static_cast< const Sequence<Any>* >( rValue.getValue() ); const sal_Int32 nLength = pSequence->getLength(); sal_Int32 nElement; const Any* pAny = pSequence->getConstArray(); @@ -1660,15 +1651,14 @@ void AnimationsExporterImpl::prepareValue( const Any& rValue ) if( xRef.is() ) mrExport.getInterfaceToIdentifierMapper().registerReference( xRef ); } - else if( rValue.getValueType() == cppu::UnoType<ParagraphTarget>::get() ) + else if( auto pt = o3tl::tryGet<ParagraphTarget>(rValue) ) { - Reference< XInterface> xRef( getParagraphTarget( static_cast< const ParagraphTarget* >( rValue.getValue() ) ) ); + Reference< XInterface> xRef( getParagraphTarget( *pt ) ); if( xRef.is() ) mrExport.getInterfaceToIdentifierMapper().registerReference( xRef ); } - else if( rValue.getValueType() == cppu::UnoType<Event>::get() ) + else if( auto pEvent = o3tl::tryGet<Event>(rValue) ) { - const Event* pEvent = static_cast< const Event* >( rValue.getValue() ); prepareValue( pEvent->Source ); } } diff --git a/xmloff/source/draw/shapeexport.cxx b/xmloff/source/draw/shapeexport.cxx index ed70149d0574..d851046beb9d 100644 --- a/xmloff/source/draw/shapeexport.cxx +++ b/xmloff/source/draw/shapeexport.cxx @@ -85,6 +85,8 @@ #include <comphelper/processfactory.hxx> #include <comphelper/storagehelper.hxx> +#include <o3tl/any.hxx> + #include <rtl/math.hxx> #include <rtl/ustrbuf.hxx> @@ -1995,9 +1997,8 @@ void XMLShapeExport::ImpExportLineShape( // get the two points uno::Any aAny(xPropSet->getPropertyValue("Geometry")); - drawing::PointSequenceSequence const * pSourcePolyPolygon = static_cast<drawing::PointSequenceSequence const *>(aAny.getValue()); - - if(pSourcePolyPolygon) + if (auto pSourcePolyPolygon + = o3tl::tryGet<drawing::PointSequenceSequence>(aAny)) { drawing::PointSequence* pOuterSequence = const_cast<css::drawing::PointSequenceSequence *>(pSourcePolyPolygon)->getArray(); if(pOuterSequence) @@ -2172,7 +2173,7 @@ void XMLShapeExport::ImpExportPolygonShape( // get PolygonBezier uno::Any aAny( xPropSet->getPropertyValue("Geometry") ); const basegfx::B2DPolyPolygon aPolyPolygon( - basegfx::tools::UnoPolyPolygonBezierCoordsToB2DPolyPolygon(*static_cast<drawing::PolyPolygonBezierCoords const *>(aAny.getValue()))); + basegfx::tools::UnoPolyPolygonBezierCoordsToB2DPolyPolygon(*o3tl::doGet<drawing::PolyPolygonBezierCoords>(aAny))); if(aPolyPolygon.count()) { @@ -2193,7 +2194,7 @@ void XMLShapeExport::ImpExportPolygonShape( // get non-bezier polygon uno::Any aAny( xPropSet->getPropertyValue("Geometry") ); const basegfx::B2DPolyPolygon aPolyPolygon( - basegfx::tools::UnoPointSequenceSequenceToB2DPolyPolygon(*static_cast<drawing::PointSequenceSequence const *>(aAny.getValue()))); + basegfx::tools::UnoPointSequenceSequenceToB2DPolyPolygon(*o3tl::doGet<drawing::PointSequenceSequence>(aAny))); if(!aPolyPolygon.areControlPointsUsed() && 1 == aPolyPolygon.count()) { @@ -2585,7 +2586,7 @@ void XMLShapeExport::ImpExportConnectorShape( if( xProps->getPropertyValue("PolyPolygonBezier") >>= aAny ) { // get PolygonBezier - drawing::PolyPolygonBezierCoords const * pSourcePolyPolygon = static_cast<drawing::PolyPolygonBezierCoords const *>(aAny.getValue()); + auto pSourcePolyPolygon = o3tl::tryGet<drawing::PolyPolygonBezierCoords>(aAny); if(pSourcePolyPolygon && pSourcePolyPolygon->Coordinates.getLength()) { diff --git a/xmloff/source/draw/ximpcustomshape.cxx b/xmloff/source/draw/ximpcustomshape.cxx index 9f482d98092e..bdd752417930 100644 --- a/xmloff/source/draw/ximpcustomshape.cxx +++ b/xmloff/source/draw/ximpcustomshape.cxx @@ -19,6 +19,7 @@ #include "ximpcustomshape.hxx" #include "ximpshap.hxx" +#include <o3tl/any.hxx> #include <rtl/math.hxx> #include <rtl/ustrbuf.hxx> #include <rtl/ustring.hxx> @@ -1219,8 +1220,8 @@ void XMLEnhancedCustomShapeContext::EndElement() case EAS_GluePoints : { uno::Sequence< css::drawing::EnhancedCustomShapeParameterPair > const & rSeq = - *static_cast<uno::Sequence< css::drawing::EnhancedCustomShapeParameterPair > const *>( - aPathIter->Value.getValue()); + *o3tl::doGet<uno::Sequence< css::drawing::EnhancedCustomShapeParameterPair > >( + aPathIter->Value); for ( i = 0; i < rSeq.getLength(); i++ ) { CheckAndResolveEquationParameter( const_cast<css::drawing::EnhancedCustomShapeParameter &>(rSeq[ i ].First), pH ); @@ -1231,8 +1232,8 @@ void XMLEnhancedCustomShapeContext::EndElement() case EAS_TextFrames : { uno::Sequence< css::drawing::EnhancedCustomShapeTextFrame > const & rSeq = - *static_cast<uno::Sequence< css::drawing::EnhancedCustomShapeTextFrame > const *>( - aPathIter->Value.getValue()); + *o3tl::doGet<uno::Sequence< css::drawing::EnhancedCustomShapeTextFrame > >( + aPathIter->Value); for ( i = 0; i < rSeq.getLength(); i++ ) { CheckAndResolveEquationParameter( const_cast<css::drawing::EnhancedCustomShapeParameter &>(rSeq[ i ].TopLeft.First), pH ); @@ -1263,18 +1264,18 @@ void XMLEnhancedCustomShapeContext::EndElement() case EAS_RadiusRangeMinimum : case EAS_RadiusRangeMaximum : { - CheckAndResolveEquationParameter( *const_cast<css::drawing::EnhancedCustomShapeParameter *>(static_cast<css::drawing::EnhancedCustomShapeParameter const *>( - pValues->Value.getValue())), pH ); + CheckAndResolveEquationParameter( const_cast<css::drawing::EnhancedCustomShapeParameter &>(*o3tl::doGet<css::drawing::EnhancedCustomShapeParameter>( + pValues->Value)), pH ); } break; case EAS_Position : case EAS_Polar : { - CheckAndResolveEquationParameter( const_cast<css::drawing::EnhancedCustomShapeParameter &>((*static_cast<css::drawing::EnhancedCustomShapeParameterPair const *>( - pValues->Value.getValue())).First), pH ); - CheckAndResolveEquationParameter( const_cast<css::drawing::EnhancedCustomShapeParameter &>((*static_cast<css::drawing::EnhancedCustomShapeParameterPair const *>( - pValues->Value.getValue())).Second), pH ); + CheckAndResolveEquationParameter( const_cast<css::drawing::EnhancedCustomShapeParameter &>((*o3tl::doGet<css::drawing::EnhancedCustomShapeParameterPair>( + pValues->Value)).First), pH ); + CheckAndResolveEquationParameter( const_cast<css::drawing::EnhancedCustomShapeParameter &>((*o3tl::doGet<css::drawing::EnhancedCustomShapeParameterPair>( + pValues->Value)).Second), pH ); } break; default: |