diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2023-01-12 22:39:58 +0100 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2023-01-13 07:50:57 +0000 |
commit | e001703df46d601a9ba21512a259c1caf266edac (patch) | |
tree | a8e060b009c0cff0692912fbfab777393d1178fe /cppuhelper/source | |
parent | 04fd7d0f53faa08c608034707ca4a459a162712d (diff) |
Merge OSingleFactoryHelper into OFactoryComponentHelper
Change-Id: I7faf7a3ad54c80bd3c53525c92c26c0f980110f9
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/145423
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Diffstat (limited to 'cppuhelper/source')
-rw-r--r-- | cppuhelper/source/factory.cxx | 327 |
1 files changed, 127 insertions, 200 deletions
diff --git a/cppuhelper/source/factory.cxx b/cppuhelper/source/factory.cxx index 37ac44ffa1a5..6d5cdc7b23bc 100644 --- a/cppuhelper/source/factory.cxx +++ b/cppuhelper/source/factory.cxx @@ -58,192 +58,6 @@ namespace cppu namespace { -class OSingleFactoryHelper - : public XServiceInfo - , public XSingleServiceFactory - , public lang::XSingleComponentFactory - , public XUnloadingPreference -{ -public: - OSingleFactoryHelper( - const Reference<XMultiServiceFactory > & rServiceManager, - OUString aImplementationName_, - ComponentInstantiation pCreateFunction_, - ComponentFactoryFunc fptr, - const Sequence< OUString > * pServiceNames_ ) - : xSMgr( rServiceManager ) - , pCreateFunction( pCreateFunction_ ) - , m_fptr( fptr ) - , aImplementationName(std::move( aImplementationName_ )) - { - if( pServiceNames_ ) - aServiceNames = *pServiceNames_; - } - - virtual ~OSingleFactoryHelper(); - - // XInterface - Any SAL_CALL queryInterface( const Type & rType ) override; - - // XSingleServiceFactory - Reference<XInterface > SAL_CALL createInstance() override; - virtual Reference<XInterface > SAL_CALL createInstanceWithArguments(const Sequence<Any>& Arguments) override; - // XSingleComponentFactory - virtual Reference< XInterface > SAL_CALL createInstanceWithContext( - Reference< XComponentContext > const & xContext ) override; - virtual Reference< XInterface > SAL_CALL createInstanceWithArgumentsAndContext( - Sequence< Any > const & rArguments, - Reference< XComponentContext > const & xContext ) override; - - // XServiceInfo - OUString SAL_CALL getImplementationName() override; - sal_Bool SAL_CALL supportsService(const OUString& ServiceName) override; - Sequence< OUString > SAL_CALL getSupportedServiceNames() override; - -protected: - /** - * Create an instance specified by the factory. The one instance logic is implemented - * in the createInstance and createInstanceWithArguments methods. - * @return the newly created instance. Do not return a previous (one instance) instance. - * @throw css::uno::Exception - * @throw css::uno::RuntimeException - */ - virtual Reference<XInterface > createInstanceEveryTime( - Reference< XComponentContext > const & xContext ); - - Reference<XMultiServiceFactory > xSMgr; - ComponentInstantiation pCreateFunction; - ComponentFactoryFunc m_fptr; - Sequence< OUString > aServiceNames; - OUString aImplementationName; - -private: - css::uno::Reference<css::uno::XInterface> createInstanceWithArgumentsEveryTime( - css::uno::Sequence<css::uno::Any> const & rArguments, - css::uno::Reference<css::uno::XComponentContext> const & xContext); -}; - -} - -OSingleFactoryHelper::~OSingleFactoryHelper() -{ -} - - -Any OSingleFactoryHelper::queryInterface( const Type & rType ) -{ - return ::cppu::queryInterface( - rType, - static_cast< XSingleComponentFactory * >( this ), - static_cast< XSingleServiceFactory * >( this ), - static_cast< XServiceInfo * >( this ) , - static_cast< XUnloadingPreference * >( this )); -} - -// OSingleFactoryHelper -Reference<XInterface > OSingleFactoryHelper::createInstanceEveryTime( - Reference< XComponentContext > const & xContext ) -{ - if (m_fptr) - { - return (*m_fptr)( xContext ); - } - if( pCreateFunction ) - { - if (xContext.is()) - { - Reference< lang::XMultiServiceFactory > xContextMgr( - xContext->getServiceManager(), UNO_QUERY ); - if (xContextMgr.is()) - return (*pCreateFunction)( xContextMgr ); - } - return (*pCreateFunction)( xSMgr ); - } - return Reference< XInterface >(); -} - -// XSingleServiceFactory -Reference<XInterface > OSingleFactoryHelper::createInstance() -{ - return createInstanceEveryTime( Reference< XComponentContext >() ); -} - -// XSingleServiceFactory -Reference<XInterface > OSingleFactoryHelper::createInstanceWithArguments( - const Sequence<Any>& Arguments ) -{ - return createInstanceWithArgumentsEveryTime( - Arguments, Reference< XComponentContext >() ); -} - -// XSingleComponentFactory - -Reference< XInterface > OSingleFactoryHelper::createInstanceWithContext( - Reference< XComponentContext > const & xContext ) -{ - return createInstanceEveryTime( xContext ); -} - -Reference< XInterface > OSingleFactoryHelper::createInstanceWithArgumentsAndContext( - Sequence< Any > const & rArguments, - Reference< XComponentContext > const & xContext ) -{ - return createInstanceWithArgumentsEveryTime(rArguments, xContext); -} - -css::uno::Reference<css::uno::XInterface> -OSingleFactoryHelper::createInstanceWithArgumentsEveryTime( - css::uno::Sequence<css::uno::Any> const & rArguments, - css::uno::Reference<css::uno::XComponentContext> const & xContext) -{ - Reference< XInterface > xRet( createInstanceEveryTime( xContext ) ); - - Reference< lang::XInitialization > xInit( xRet, UNO_QUERY ); - // always call initialize, even if there are no arguments. #i63511# - if (xInit.is()) - { - xInit->initialize( rArguments ); - } - else - { - if ( rArguments.hasElements() ) - { - // dispose the here created UNO object before throwing out exception - // to avoid risk of memory leaks #i113722# - Reference<XComponent> xComp( xRet, UNO_QUERY ); - if (xComp.is()) - xComp->dispose(); - - throw lang::IllegalArgumentException( - "cannot pass arguments to component => no XInitialization implemented!", - Reference< XInterface >(), 0 ); - } - } - - return xRet; -} - -// XServiceInfo -OUString OSingleFactoryHelper::getImplementationName() -{ - return aImplementationName; -} - -// XServiceInfo -sal_Bool OSingleFactoryHelper::supportsService( - const OUString& ServiceName ) -{ - return cppu::supportsService(this, ServiceName); -} - -// XServiceInfo -Sequence< OUString > OSingleFactoryHelper::getSupportedServiceNames() -{ - return aServiceNames; -} - -namespace { - struct OFactoryComponentHelper_Mutex { Mutex aMutex; @@ -252,20 +66,28 @@ struct OFactoryComponentHelper_Mutex class OFactoryComponentHelper : public OFactoryComponentHelper_Mutex , public OComponentHelper - , public OSingleFactoryHelper + , public XServiceInfo + , public XSingleServiceFactory + , public lang::XSingleComponentFactory + , public XUnloadingPreference { public: OFactoryComponentHelper( const Reference<XMultiServiceFactory > & rServiceManager, - const OUString & rImplementationName_, + OUString aImplementationName_, ComponentInstantiation pCreateFunction_, ComponentFactoryFunc fptr, const Sequence< OUString > * pServiceNames_, bool bOneInstance_ ) : OComponentHelper( aMutex ) - , OSingleFactoryHelper( rServiceManager, rImplementationName_, pCreateFunction_, fptr, pServiceNames_ ) , bOneInstance( bOneInstance_ ) + , xSMgr( rServiceManager ) + , pCreateFunction( pCreateFunction_ ) + , m_fptr( fptr ) + , aImplementationName(std::move( aImplementationName_ )) { + if( pServiceNames_ ) + aServiceNames = *pServiceNames_; } // XInterface @@ -285,6 +107,11 @@ public: Sequence< Any > const & rArguments, Reference< XComponentContext > const & xContext ) override; + // XServiceInfo + OUString SAL_CALL getImplementationName() override; + sal_Bool SAL_CALL supportsService(const OUString& ServiceName) override; + Sequence< OUString > SAL_CALL getSupportedServiceNames() override; + // XTypeProvider virtual Sequence< Type > SAL_CALL getTypes() override; virtual Sequence< sal_Int8 > SAL_CALL getImplementationId() override; @@ -299,12 +126,32 @@ public: void SAL_CALL dispose() override; private: + css::uno::Reference<css::uno::XInterface> createInstanceWithArgumentsEveryTime( + css::uno::Sequence<css::uno::Any> const & rArguments, + css::uno::Reference<css::uno::XComponentContext> const & xContext); + Reference<XInterface > xTheInstance; bool bOneInstance; protected: // needed for implementing XUnloadingPreference in inheriting classes bool isOneInstance() const {return bOneInstance;} bool isInstance() const {return xTheInstance.is();} + + /** + * Create an instance specified by the factory. The one instance logic is implemented + * in the createInstance and createInstanceWithArguments methods. + * @return the newly created instance. Do not return a previous (one instance) instance. + * @throw css::uno::Exception + * @throw css::uno::RuntimeException + */ + virtual Reference<XInterface > createInstanceEveryTime( + Reference< XComponentContext > const & xContext ); + + Reference<XMultiServiceFactory > xSMgr; + ComponentInstantiation pCreateFunction; + ComponentFactoryFunc m_fptr; + Sequence< OUString > aServiceNames; + OUString aImplementationName; }; } @@ -318,7 +165,14 @@ Any SAL_CALL OFactoryComponentHelper::queryInterface( const Type & rType ) Any OFactoryComponentHelper::queryAggregation( const Type & rType ) { Any aRet( OComponentHelper::queryAggregation( rType ) ); - return (aRet.hasValue() ? aRet : OSingleFactoryHelper::queryInterface( rType )); + return (aRet.hasValue() + ? aRet + : ::cppu::queryInterface( + rType, + static_cast< XSingleComponentFactory * >( this ), + static_cast< XSingleServiceFactory * >( this ), + static_cast< XServiceInfo * >( this ) , + static_cast< XUnloadingPreference * >( this ))); } // XTypeProvider @@ -340,6 +194,28 @@ Sequence< sal_Int8 > OFactoryComponentHelper::getImplementationId() return css::uno::Sequence<sal_Int8>(); } +// OFactoryComponentHelper +Reference<XInterface > OFactoryComponentHelper::createInstanceEveryTime( + Reference< XComponentContext > const & xContext ) +{ + if (m_fptr) + { + return (*m_fptr)( xContext ); + } + if( pCreateFunction ) + { + if (xContext.is()) + { + Reference< lang::XMultiServiceFactory > xContextMgr( + xContext->getServiceManager(), UNO_QUERY ); + if (xContextMgr.is()) + return (*pCreateFunction)( xContextMgr ); + } + return (*pCreateFunction)( xSMgr ); + } + return Reference< XInterface >(); +} + // XSingleServiceFactory Reference<XInterface > OFactoryComponentHelper::createInstance() { @@ -349,11 +225,11 @@ Reference<XInterface > OFactoryComponentHelper::createInstance() { MutexGuard aGuard( aMutex ); if( !xTheInstance.is() ) - xTheInstance = OSingleFactoryHelper::createInstance(); + xTheInstance = createInstanceEveryTime( Reference< XComponentContext >() ); } return xTheInstance; } - return OSingleFactoryHelper::createInstance(); + return createInstanceEveryTime( Reference< XComponentContext >() ); } Reference<XInterface > OFactoryComponentHelper::createInstanceWithArguments( @@ -366,11 +242,12 @@ Reference<XInterface > OFactoryComponentHelper::createInstanceWithArguments( MutexGuard aGuard( aMutex ); // OSL_ENSURE( !xTheInstance.is(), "### arguments will be ignored!" ); if( !xTheInstance.is() ) - xTheInstance = OSingleFactoryHelper::createInstanceWithArguments( Arguments ); + xTheInstance = createInstanceWithArgumentsEveryTime( + Arguments, Reference< XComponentContext >() ); } return xTheInstance; } - return OSingleFactoryHelper::createInstanceWithArguments( Arguments ); + return createInstanceWithArgumentsEveryTime( Arguments, Reference< XComponentContext >() ); } // XSingleComponentFactory @@ -385,11 +262,11 @@ Reference< XInterface > OFactoryComponentHelper::createInstanceWithContext( MutexGuard aGuard( aMutex ); // OSL_ENSURE( !xTheInstance.is(), "### context will be ignored!" ); if( !xTheInstance.is() ) - xTheInstance = OSingleFactoryHelper::createInstanceWithContext( xContext ); + xTheInstance = createInstanceEveryTime( xContext ); } return xTheInstance; } - return OSingleFactoryHelper::createInstanceWithContext( xContext ); + return createInstanceEveryTime( xContext ); } Reference< XInterface > OFactoryComponentHelper::createInstanceWithArgumentsAndContext( @@ -403,11 +280,43 @@ Reference< XInterface > OFactoryComponentHelper::createInstanceWithArgumentsAndC MutexGuard aGuard( aMutex ); // OSL_ENSURE( !xTheInstance.is(), "### context and arguments will be ignored!" ); if( !xTheInstance.is() ) - xTheInstance = OSingleFactoryHelper::createInstanceWithArgumentsAndContext( rArguments, xContext ); + xTheInstance = createInstanceWithArgumentsEveryTime( rArguments, xContext ); } return xTheInstance; } - return OSingleFactoryHelper::createInstanceWithArgumentsAndContext( rArguments, xContext ); + return createInstanceWithArgumentsEveryTime( rArguments, xContext ); +} + +css::uno::Reference<css::uno::XInterface> +OFactoryComponentHelper::createInstanceWithArgumentsEveryTime( + css::uno::Sequence<css::uno::Any> const & rArguments, + css::uno::Reference<css::uno::XComponentContext> const & xContext) +{ + Reference< XInterface > xRet( createInstanceEveryTime( xContext ) ); + + Reference< lang::XInitialization > xInit( xRet, UNO_QUERY ); + // always call initialize, even if there are no arguments. #i63511# + if (xInit.is()) + { + xInit->initialize( rArguments ); + } + else + { + if ( rArguments.hasElements() ) + { + // dispose the here created UNO object before throwing out exception + // to avoid risk of memory leaks #i113722# + Reference<XComponent> xComp( xRet, UNO_QUERY ); + if (xComp.is()) + xComp->dispose(); + + throw lang::IllegalArgumentException( + "cannot pass arguments to component => no XInitialization implemented!", + Reference< XInterface >(), 0 ); + } + } + + return xRet; } @@ -429,6 +338,25 @@ void OFactoryComponentHelper::dispose() xComp->dispose(); } +// XServiceInfo +OUString OFactoryComponentHelper::getImplementationName() +{ + return aImplementationName; +} + +// XServiceInfo +sal_Bool OFactoryComponentHelper::supportsService( + const OUString& ServiceName ) +{ + return cppu::supportsService(this, ServiceName); +} + +// XServiceInfo +Sequence< OUString > OFactoryComponentHelper::getSupportedServiceNames() +{ + return aServiceNames; +} + // XUnloadingPreference // This class is used for single factories, component factories and // one-instance factories. Depending on the usage this function has @@ -481,7 +409,7 @@ public: virtual void SAL_CALL getFastPropertyValue( Any & rValue, sal_Int32 nHandle ) const override; - // OSingleFactoryHelper + // OFactoryComponentHelper Reference<XInterface > createInstanceEveryTime( Reference< XComponentContext > const & xContext ) override; @@ -705,7 +633,6 @@ Reference< XInterface > ORegistryFactoryHelper::createInstanceWithArgumentsAndCo } -// OSingleFactoryHelper Reference< XInterface > ORegistryFactoryHelper::createModuleFactory() { OUString aActivatorUrl; |