comphelper/source/misc/documentinfo.cxx | 3 - comphelper/source/misc/namedvaluecollection.cxx | 47 ++++++++++++++++++- connectivity/source/commontools/dbmetadata.cxx | 3 - connectivity/source/drivers/mysql_jdbc/YDriver.cxx | 8 +-- dbaccess/source/core/dataaccess/databasedocument.cxx | 3 - dbaccess/source/core/dataaccess/datasource.cxx | 3 - dbaccess/source/ui/app/AppController.cxx | 9 +-- dbaccess/source/ui/uno/copytablewizard.cxx | 3 - framework/source/loadenv/loadenv.cxx | 3 - include/comphelper/namedvaluecollection.hxx | 27 ++++++++++ reportdesign/source/ui/report/ReportController.cxx | 3 - scripting/source/dlgprov/dlgprov.cxx | 3 - sfx2/source/doc/objmisc.cxx | 15 ++---- sfx2/source/view/frmload.cxx | 3 - sfx2/source/view/sfxbasecontroller.cxx | 4 - ucb/source/ucp/tdoc/tdoc_docmgr.cxx | 4 - xmloff/source/xforms/xformsimport.cxx | 3 - 17 files changed, 97 insertions(+), 47 deletions(-)
New commits: commit d03e7a8fba5494dc10433f33d902683eaae639bb Author: Noel Grandin <noel.gran...@collabora.co.uk> AuthorDate: Thu May 12 11:52:05 2022 +0200 Commit: Noel Grandin <noel.gran...@collabora.co.uk> CommitDate: Thu May 12 18:03:46 2022 +0200 speed up simple use of NamedValueCollection where instantiating and allocating a whole map just to extract a single key is way inefficient Change-Id: I55248bc71a9e8826cab9b76fa6916bfa888efa0b Reviewed-on: https://gerrit.libreoffice.org/c/core/+/134226 Tested-by: Jenkins Reviewed-by: Noel Grandin <noel.gran...@collabora.co.uk> diff --git a/comphelper/source/misc/documentinfo.cxx b/comphelper/source/misc/documentinfo.cxx index 0a1c04347c84..21425524e64e 100644 --- a/comphelper/source/misc/documentinfo.cxx +++ b/comphelper/source/misc/documentinfo.cxx @@ -106,8 +106,7 @@ namespace comphelper { } // 4. try model arguments - NamedValueCollection aModelArgs( _rxDocument->getArgs() ); - sTitle = aModelArgs.getOrDefault( "Title", sTitle ); + sTitle = NamedValueCollection::getOrDefault( _rxDocument->getArgs(), u"Title", sTitle ); if ( !sTitle.isEmpty() ) return sTitle; diff --git a/comphelper/source/misc/namedvaluecollection.cxx b/comphelper/source/misc/namedvaluecollection.cxx index 574f8e0a1898..11ef15b30854 100644 --- a/comphelper/source/misc/namedvaluecollection.cxx +++ b/comphelper/source/misc/namedvaluecollection.cxx @@ -201,9 +201,54 @@ namespace comphelper nullptr, 0 ); } + // static + bool NamedValueCollection::get_ensureType( const css::uno::Sequence<css::beans::PropertyValue>& rPropSeq, + std::u16string_view _rValueName, void* _pValueLocation, const Type& _rExpectedValueType ) + { + for (const css::beans::PropertyValue& rPropVal : rPropSeq) + { + if (rPropVal.Name == _rValueName) + { + if ( uno_type_assignData( + _pValueLocation, _rExpectedValueType.getTypeLibType(), + const_cast< void* >( rPropVal.Value.getValue() ), rPropVal.Value.getValueType().getTypeLibType(), + reinterpret_cast< uno_QueryInterfaceFunc >( cpp_queryInterface ), + reinterpret_cast< uno_AcquireFunc >( cpp_acquire ), + reinterpret_cast< uno_ReleaseFunc >( cpp_release ) + ) ) + // argument exists, and could be extracted + return true; + + // argument exists, but is of wrong type + throw IllegalArgumentException( + OUString::Concat("Invalid value type for '") + _rValueName + + "'.\nExpected: " + _rExpectedValueType.getTypeName() + + "\nFound: " + rPropVal.Value.getValueType().getTypeName(), + nullptr, 0 ); + } + } + // argument does not exist + return false; + } + + // static + const css::uno::Any& NamedValueCollection::get( const css::uno::Sequence<css::beans::PropertyValue>& rPropSeq, + std::u16string_view _rValueName ) + { + static const Any theEmptyDefault; + for (const css::beans::PropertyValue& rPropVal : rPropSeq) + { + if (rPropVal.Name == _rValueName) + { + return rPropVal.Value; + } + } + return theEmptyDefault; + } + const Any& NamedValueCollection::impl_get( const OUString& _rValueName ) const { - static Any theEmptyDefault; + static const Any theEmptyDefault; auto pos = maValues.find( _rValueName ); if ( pos != maValues.end() ) return pos->second; diff --git a/connectivity/source/commontools/dbmetadata.cxx b/connectivity/source/commontools/dbmetadata.cxx index 0a2123aaa782..7eb148735f31 100644 --- a/connectivity/source/commontools/dbmetadata.cxx +++ b/connectivity/source/commontools/dbmetadata.cxx @@ -135,8 +135,7 @@ namespace dbtools else { Reference< XDatabaseMetaData2 > xExtendedMetaData( _metaData.xConnectionMetaData, UNO_QUERY_THROW ); - ::comphelper::NamedValueCollection aSettings( xExtendedMetaData->getConnectionInfo() ); - _out_setting = aSettings.get( _asciiName ); + _out_setting = ::comphelper::NamedValueCollection::get( xExtendedMetaData->getConnectionInfo(), _asciiName ); return _out_setting.hasValue(); } return true; diff --git a/connectivity/source/drivers/mysql_jdbc/YDriver.cxx b/connectivity/source/drivers/mysql_jdbc/YDriver.cxx index ffe006c4b041..0f8357abd112 100644 --- a/connectivity/source/drivers/mysql_jdbc/YDriver.cxx +++ b/connectivity/source/drivers/mysql_jdbc/YDriver.cxx @@ -49,8 +49,8 @@ namespace { OUString getJavaDriverClass(css::uno::Sequence<css::beans::PropertyValue> const& info) { - return comphelper::NamedValueCollection(info).getOrDefault("JavaDriverClass", - OUString("com.mysql.jdbc.Driver")); + return comphelper::NamedValueCollection::getOrDefault(info, u"JavaDriverClass", + OUString("com.mysql.jdbc.Driver")); } } @@ -232,8 +232,8 @@ Reference<XConnection> SAL_CALL ODriverDelegator::connect(const OUString& url, Sequence<PropertyValue> aConvertedProperties = lcl_convertProperties(eType, info, url); if (eType == T_DRIVERTYPE::Jdbc) { - ::comphelper::NamedValueCollection aSettings(info); - OUString sIanaName = aSettings.getOrDefault("CharSet", OUString()); + OUString sIanaName = ::comphelper::NamedValueCollection::getOrDefault( + info, u"CharSet", OUString()); if (!sIanaName.isEmpty()) { ::dbtools::OCharsetMap aLookupIanaName; diff --git a/dbaccess/source/core/dataaccess/databasedocument.cxx b/dbaccess/source/core/dataaccess/databasedocument.cxx index 051421fa578a..eabed85802c6 100644 --- a/dbaccess/source/core/dataaccess/databasedocument.cxx +++ b/dbaccess/source/core/dataaccess/databasedocument.cxx @@ -1921,8 +1921,7 @@ void SAL_CALL ODatabaseDocument::loadFromStorage(const Reference<XStorage>& xSto DocumentGuard aGuard(*this, DocumentGuard::InitMethod); uno::Reference<beans::XPropertySet> xInfoSet(comphelper::GenericPropertySet_CreateInstance(new comphelper::PropertySetInfo(aEmbeddedImportInfoMap))); - comphelper::NamedValueCollection aDescriptor(rMediaDescriptor); - xInfoSet->setPropertyValue("StreamRelPath", uno::Any(aDescriptor.getOrDefault("HierarchicalDocumentName", OUString()))); + xInfoSet->setPropertyValue("StreamRelPath", uno::Any(comphelper::NamedValueCollection::getOrDefault(rMediaDescriptor, u"HierarchicalDocumentName", OUString()))); xInfoSet->setPropertyValue("StreamName", uno::Any(OUString("content.xml"))); xInfoSet->setPropertyValue("SourceStorage", uno::Any(xStorage)); diff --git a/dbaccess/source/core/dataaccess/datasource.cxx b/dbaccess/source/core/dataaccess/datasource.cxx index b5f10f1b6bee..d88b661c976d 100644 --- a/dbaccess/source/core/dataaccess/datasource.cxx +++ b/dbaccess/source/core/dataaccess/datasource.cxx @@ -608,8 +608,7 @@ Reference< XConnection > ODatabaseSource::buildLowLevelConnection(const OUString if ( xModel) { //See ODbTypeWizDialogSetup::SaveDatabaseDocument - ::comphelper::NamedValueCollection aArgs( xModel->getArgs() ); - aArgs.get("IgnoreFirebirdMigration") >>= bIgnoreMigration; + ::comphelper::NamedValueCollection::get(xModel->getArgs(), u"IgnoreFirebirdMigration") >>= bIgnoreMigration; } else { diff --git a/dbaccess/source/ui/app/AppController.cxx b/dbaccess/source/ui/app/AppController.cxx index ec83eff76574..06508d5c2e0e 100644 --- a/dbaccess/source/ui/app/AppController.cxx +++ b/dbaccess/source/ui/app/AppController.cxx @@ -348,8 +348,7 @@ void SAL_CALL OApplicationController::disposing() OUString sUrl = m_xModel->getURL(); if ( !sUrl.isEmpty() ) { - ::comphelper::NamedValueCollection aArgs( m_xModel->getArgs() ); - if ( aArgs.getOrDefault( "PickListEntry", true ) ) + if ( ::comphelper::NamedValueCollection::getOrDefault( m_xModel->getArgs(), u"PickListEntry", true ) ) { OUString aFilter; INetURLObject aURL( m_xModel->getURL() ); @@ -907,8 +906,7 @@ namespace bool bHandled = false; // try handling the error with an interaction handler - ::comphelper::NamedValueCollection aArgs( _rxDocument->getArgs() ); - Reference< XInteractionHandler > xHandler( aArgs.getOrDefault( "InteractionHandler", Reference< XInteractionHandler >() ) ); + Reference< XInteractionHandler > xHandler = ::comphelper::NamedValueCollection::getOrDefault( _rxDocument->getArgs(), u"InteractionHandler", Reference< XInteractionHandler >() ); if ( xHandler.is() ) { rtl::Reference pRequest( new ::comphelper::OInteractionRequest( _rException ) ); @@ -2514,8 +2512,7 @@ void OApplicationController::OnFirstControllerConnected() { // If the migration just happened, but was not successful, the document is reloaded. // In this case, we should not show the warning, again. - ::comphelper::NamedValueCollection aModelArgs( m_xModel->getArgs() ); - if ( aModelArgs.getOrDefault( "SuppressMigrationWarning", false ) ) + if ( ::comphelper::NamedValueCollection::getOrDefault( m_xModel->getArgs(), u"SuppressMigrationWarning", false ) ) return; // also, if the document is read-only, then no migration is possible, and the diff --git a/dbaccess/source/ui/uno/copytablewizard.cxx b/dbaccess/source/ui/uno/copytablewizard.cxx index 5abfd7a6dd01..14b4609476db 100644 --- a/dbaccess/source/ui/uno/copytablewizard.cxx +++ b/dbaccess/source/ui/uno/copytablewizard.cxx @@ -571,8 +571,7 @@ namespace // see whether the document model can provide a handler if ( xDocumentModel.is() ) { - ::comphelper::NamedValueCollection aModelArgs( xDocumentModel->getArgs() ); - xHandler = aModelArgs.getOrDefault( "InteractionHandler", xHandler ); + xHandler = ::comphelper::NamedValueCollection::getOrDefault( xDocumentModel->getArgs(), u"InteractionHandler", xHandler ); } return xHandler; diff --git a/framework/source/loadenv/loadenv.cxx b/framework/source/loadenv/loadenv.cxx index c0cd7666813f..005b1a68f932 100644 --- a/framework/source/loadenv/loadenv.cxx +++ b/framework/source/loadenv/loadenv.cxx @@ -160,9 +160,8 @@ css::uno::Reference< css::lang::XComponent > LoadEnv::loadComponentFromURL(const LoadEnv aEnv(xContext); LoadEnvFeatures loadEnvFeatures = LoadEnvFeatures::WorkWithUI; - comphelper::NamedValueCollection aDescriptor( lArgs ); // tdf#118238 Only disable UI interaction when loading as hidden - if (aDescriptor.get("Hidden") == uno::Any(true) || Application::IsHeadlessModeEnabled()) + if (comphelper::NamedValueCollection::get(lArgs, u"Hidden") == uno::Any(true) || Application::IsHeadlessModeEnabled()) loadEnvFeatures = LoadEnvFeatures::NONE; aEnv.startLoading(sURL, diff --git a/include/comphelper/namedvaluecollection.hxx b/include/comphelper/namedvaluecollection.hxx index 33b786437d15..b92646e40a11 100644 --- a/include/comphelper/namedvaluecollection.hxx +++ b/include/comphelper/namedvaluecollection.hxx @@ -157,6 +157,19 @@ namespace comphelper return retVal; } + /** Retrieves a value with a given name, or defaults it to a given value, if it's not present + in the collection. + For when you only need a single value from a Sequence<PropertyValue>. + */ + template < typename VALUE_TYPE > + static VALUE_TYPE getOrDefault( const css::uno::Sequence<css::beans::PropertyValue> & rPropSeq, + std::u16string_view _rValueName, const VALUE_TYPE& _rDefault ) + { + VALUE_TYPE retVal( _rDefault ); + get_ensureType( rPropSeq, _rValueName, &retVal, ::cppu::UnoType< VALUE_TYPE >::get() ); + return retVal; + } + /** retrieves a (untyped) value with a given name If the collection does not contain a value with the given name, an empty @@ -167,6 +180,13 @@ namespace comphelper return impl_get( _rValueName ); } + /** retrieves a (untyped) value with a given name. For when you only need a single value from a Sequence<PropertyValue>. + + If the collection does not contain a value with the given name, an empty + Any is returned. + */ + static const css::uno::Any& get( const css::uno::Sequence<css::beans::PropertyValue>& rPropSeq, std::u16string_view _rValueName ); + /// determines whether a value with a given name is present in the collection bool has( const OUString& _rValueName ) const { @@ -260,6 +280,13 @@ namespace comphelper const css::uno::Type& _rExpectedValueType ) const; + static bool get_ensureType( + const css::uno::Sequence<css::beans::PropertyValue> & rPropSeq, + std::u16string_view _rValueName, + void* _pValueLocation, + const css::uno::Type& _rExpectedValueType + ); + const css::uno::Any& impl_get( const OUString& _rValueName ) const; diff --git a/reportdesign/source/ui/report/ReportController.cxx b/reportdesign/source/ui/report/ReportController.cxx index 6fad529c3b9a..7f20a0040c6e 100644 --- a/reportdesign/source/ui/report/ReportController.cxx +++ b/reportdesign/source/ui/report/ReportController.cxx @@ -1657,8 +1657,7 @@ void OReportController::impl_initialize( ) clearUndoManager(); UndoSuppressor aSuppressUndo( getUndoManager() ); - ::comphelper::NamedValueCollection aArgs(getModel()->getArgs()); - setMode(aArgs.getOrDefault("Mode", OUString("normal"))); + setMode(::comphelper::NamedValueCollection::getOrDefault(getModel()->getArgs(), u"Mode", OUString("normal"))); listen(true); setEditable( !m_aReportModel->IsReadOnly() ); diff --git a/scripting/source/dlgprov/dlgprov.cxx b/scripting/source/dlgprov/dlgprov.cxx index a40c4f6fb2d2..18815f499e72 100644 --- a/scripting/source/dlgprov/dlgprov.cxx +++ b/scripting/source/dlgprov/dlgprov.cxx @@ -292,8 +292,7 @@ namespace dlgprov OUString sDocURL = xModel->getURL(); if ( sDocURL.isEmpty() ) { - ::comphelper::NamedValueCollection aModelArgs( xModel->getArgs() ); - sDocURL = aModelArgs.getOrDefault( "Title", sDocURL ); + sDocURL = ::comphelper::NamedValueCollection::getOrDefault( xModel->getArgs(), u"Title", sDocURL ); } if ( sLocation != sDocURL ) diff --git a/sfx2/source/doc/objmisc.cxx b/sfx2/source/doc/objmisc.cxx index 9ab91983f2c2..873ab8b9ba26 100644 --- a/sfx2/source/doc/objmisc.cxx +++ b/sfx2/source/doc/objmisc.cxx @@ -1882,8 +1882,7 @@ bool SfxObjectShell::isEditDocLocked() const return false; if (!officecfg::Office::Common::Misc::AllowEditReadonlyDocs::get()) return true; - comphelper::NamedValueCollection aArgs(xModel->getArgs2( { "LockEditDoc" } )); - return aArgs.getOrDefault("LockEditDoc", false); + return comphelper::NamedValueCollection::getOrDefault(xModel->getArgs2( { "LockEditDoc" } ), u"LockEditDoc", false); } bool SfxObjectShell::isContentExtractionLocked() const @@ -1891,8 +1890,7 @@ bool SfxObjectShell::isContentExtractionLocked() const Reference<XModel3> xModel = GetModel(); if (!xModel.is()) return false; - comphelper::NamedValueCollection aArgs(xModel->getArgs2( { "LockContentExtraction" } )); - return aArgs.getOrDefault("LockContentExtraction", false); + return comphelper::NamedValueCollection::getOrDefault(xModel->getArgs2( { "LockContentExtraction" } ), u"LockContentExtraction", false); } bool SfxObjectShell::isExportLocked() const @@ -1900,8 +1898,7 @@ bool SfxObjectShell::isExportLocked() const Reference<XModel3> xModel = GetModel(); if (!xModel.is()) return false; - comphelper::NamedValueCollection aArgs(xModel->getArgs2( { "LockExport" } )); - return aArgs.getOrDefault("LockExport", false); + return comphelper::NamedValueCollection::getOrDefault(xModel->getArgs2( { "LockExport" } ), u"LockExport", false); } bool SfxObjectShell::isPrintLocked() const @@ -1909,8 +1906,7 @@ bool SfxObjectShell::isPrintLocked() const Reference<XModel3> xModel = GetModel(); if (!xModel.is()) return false; - comphelper::NamedValueCollection aArgs(xModel->getArgs2( { "LockPrint" } )); - return aArgs.getOrDefault("LockPrint", false); + return comphelper::NamedValueCollection::getOrDefault(xModel->getArgs2( { "LockPrint" } ), u"LockPrint", false); } bool SfxObjectShell::isSaveLocked() const @@ -1918,8 +1914,7 @@ bool SfxObjectShell::isSaveLocked() const Reference<XModel3> xModel = GetModel(); if (!xModel.is()) return false; - comphelper::NamedValueCollection aArgs(xModel->getArgs2( { "LockSave" } )); - return aArgs.getOrDefault("LockSave", false); + return comphelper::NamedValueCollection::getOrDefault(xModel->getArgs2( { "LockSave" } ), u"LockSave", false); } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sfx2/source/view/frmload.cxx b/sfx2/source/view/frmload.cxx index 7c1db6f8b4ac..b3830914adf2 100644 --- a/sfx2/source/view/frmload.cxx +++ b/sfx2/source/view/frmload.cxx @@ -544,8 +544,7 @@ SfxInterfaceId SfxFrameLoader_Impl::impl_determineEffectiveViewId_nothrow( const if ( !( xViewData->getByIndex( 0 ) >>= aViewData ) ) break; - ::comphelper::NamedValueCollection aNamedViewData( aViewData ); - OUString sViewId = aNamedViewData.getOrDefault( "ViewId", OUString() ); + OUString sViewId = ::comphelper::NamedValueCollection::getOrDefault( aViewData, u"ViewId", OUString() ); if ( sViewId.isEmpty() ) break; diff --git a/sfx2/source/view/sfxbasecontroller.cxx b/sfx2/source/view/sfxbasecontroller.cxx index 63e787be6b4c..e7c098fcda27 100644 --- a/sfx2/source/view/sfxbasecontroller.cxx +++ b/sfx2/source/view/sfxbasecontroller.cxx @@ -1225,9 +1225,7 @@ void SfxBaseController::ConnectSfxFrame_Impl( const ConnectSfxFrame i_eConnect ) if ( i_eConnect == E_CONNECT ) { css::uno::Reference<css::frame::XModel3> xModel(getModel(), css::uno::UNO_QUERY_THROW); - ::comphelper::NamedValueCollection aDocumentArgs( xModel->getArgs2( { "PluginMode" } ) ); - - const sal_Int16 nPluginMode = aDocumentArgs.getOrDefault( "PluginMode", sal_Int16( 0 ) ); + const sal_Int16 nPluginMode = ::comphelper::NamedValueCollection::getOrDefault( xModel->getArgs2( { "PluginMode" } ), u"PluginMode", sal_Int16( 0 ) ); const bool bHasPluginMode = ( nPluginMode != 0 ); SfxFrame& rFrame = pViewFrame->GetFrame(); diff --git a/ucb/source/ucp/tdoc/tdoc_docmgr.cxx b/ucb/source/ucp/tdoc/tdoc_docmgr.cxx index 2f0602d23dc4..1ecc775204ba 100644 --- a/ucb/source/ucp/tdoc/tdoc_docmgr.cxx +++ b/ucb/source/ucp/tdoc/tdoc_docmgr.cxx @@ -535,9 +535,7 @@ bool OfficeDocumentsManager::isDocumentPreview( if ( !xModel.is() ) return false; - ::comphelper::NamedValueCollection aArgs( - xModel->getArgs() ); - bool bIsPreview = aArgs.getOrDefault( "Preview", false ); + bool bIsPreview = ::comphelper::NamedValueCollection::getOrDefault( xModel->getArgs(), u"Preview", false ); return bIsPreview; } diff --git a/xmloff/source/xforms/xformsimport.cxx b/xmloff/source/xforms/xformsimport.cxx index 1e4b05295e71..69daa630b63e 100644 --- a/xmloff/source/xforms/xformsimport.cxx +++ b/xmloff/source/xforms/xformsimport.cxx @@ -136,8 +136,7 @@ void applyXFormsSettings( const Reference< XNameAccess >& _rXForms, const Sequen if ( !_rXForms.is() ) return; - ::comphelper::NamedValueCollection aSettings( _rSettings ); - Reference< XNameAccess > xModelSettings( aSettings.get( "XFormModels" ), UNO_QUERY ); + Reference< XNameAccess > xModelSettings( ::comphelper::NamedValueCollection::get( _rSettings, u"XFormModels" ), UNO_QUERY ); if ( !xModelSettings.is() ) { OSL_FAIL( "applyXFormsSettings: wrong type for the XFormModels settings!" );