starmath: create instances with uno constructors

See tdf#74608 for motivation.

And adjust the sfx2 model factory code to cope with UNO
constructor functions.

Change-Id: I7e57fb3136ad0b3caadd511ea63cf98d3c03ab3d
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/99446
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
This commit is contained in:
Noel Grandin 2020-07-25 18:19:20 +02:00 committed by Noel Grandin
parent 5406f8a3cd
commit 8a05c64a5b
8 changed files with 64 additions and 130 deletions

View File

@ -24,10 +24,12 @@
#include <com/sun/star/uno/Reference.hxx>
#include <o3tl/typed_flags_set.hxx>
#include <functional>
namespace com::sun::star::lang { class XMultiServiceFactory; }
namespace com::sun::star::lang { class XSingleServiceFactory; }
namespace com::sun::star::uno { class XInterface; }
namespace com::sun::star::uno { class XComponentContext; }
namespace com::sun::star::uno { template <class E> class Sequence; }
enum class SfxModelFlags
@ -68,6 +70,16 @@ namespace sfx2
const css::uno::Sequence< OUString >& _rServiceNames
);
/**
* Intended to be called from UNO constructor functions
* This evaluates certain creation arguments (passed to createInstanceWithArguments)
* and passes them to the factory function of the derived class.
*/
css::uno::Reference<css::uno::XInterface>
SFX2_DLLPUBLIC createSfxModelInstance(
const css::uno::Sequence<css::uno::Any> & rxArgs,
std::function<css::uno::Reference<css::uno::XInterface>( SfxModelFlags )> creationFunc
);
} // namespace sfx2

View File

@ -23,6 +23,8 @@
#include <com/sun/star/beans/NamedValue.hpp>
#include <com/sun/star/lang/XInitialization.hpp>
#include <com/sun/star/lang/XSingleServiceFactory.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/uno/XComponentContext.hpp>
#include <comphelper/namedvaluecollection.hxx>
#include <cppuhelper/implbase.hxx>
@ -79,6 +81,10 @@ namespace sfx2
virtual Reference< XInterface > SAL_CALL createInstance( ) override;
virtual Reference< XInterface > SAL_CALL createInstanceWithArguments( const Sequence< Any >& aArguments ) override;
static css::uno::Reference<css::uno::XInterface> create(
const css::uno::Sequence<css::uno::Any> & rxArgs,
std::function<css::uno::Reference<css::uno::XInterface>(SfxModelFlags)> creationFunc);
// XServiceInfo
virtual OUString SAL_CALL getImplementationName( ) override;
virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override;
@ -142,7 +148,18 @@ namespace sfx2
}
Reference< XInterface > SAL_CALL SfxModelFactory::createInstanceWithArguments( const Sequence< Any >& _rArguments )
Reference< XInterface > SAL_CALL SfxModelFactory::createInstanceWithArguments( const Sequence< Any >& _rArguments )
{
return create(_rArguments,
[&](SfxModelFlags _nCreationFlags)
{
return m_pComponentFactoryFunc(m_xServiceFactory, _nCreationFlags).get();
});
}
css::uno::Reference<css::uno::XInterface> SfxModelFactory::create(
const css::uno::Sequence<css::uno::Any> & _rArguments,
std::function<css::uno::Reference<css::uno::XInterface>(SfxModelFlags)> creationFunc)
{
::comphelper::NamedValueCollection aArgs( _rArguments );
const bool bEmbeddedObject = aArgs.getOrDefault( "EmbeddedObject", false );
@ -154,7 +171,7 @@ namespace sfx2
| ( bScriptSupport ? SfxModelFlags::NONE : SfxModelFlags::DISABLE_EMBEDDED_SCRIPTS )
| ( bDocRecoverySupport ? SfxModelFlags::NONE : SfxModelFlags::DISABLE_DOCUMENT_RECOVERY );
Reference< XInterface > xInstance( (*m_pComponentFactoryFunc)( m_xServiceFactory, nCreationFlags ) );
Reference< XInterface > xInstance( creationFunc(nCreationFlags ) );
// to mimic the behaviour of the default factory's createInstanceWithArguments, we initialize
// the object with the given arguments, stripped by the three special ones
@ -194,6 +211,13 @@ namespace sfx2
return m_aServiceNames;
}
css::uno::Reference<css::uno::XInterface> createSfxModelInstance(
const css::uno::Sequence<css::uno::Any> & rxArgs,
std::function<css::uno::Reference<css::uno::XInterface>(SfxModelFlags)> creationFunc)
{
return SfxModelFactory::create(rxArgs, creationFunc);
}
Reference< XSingleServiceFactory > createSfxModelFactory( const Reference< XMultiServiceFactory >& _rxServiceFactory,
const OUString& _rImplementationName, const SfxModelFactoryFunc _pComponentFactoryFunc,
const Sequence< OUString >& _rServiceNames )

View File

@ -21,7 +21,6 @@ import xml.etree.ElementTree as ET
core_factory_list = [
("libi18npoollo.a", "i18npool_component_getFactory"),
("libsmlo.a", "sm_component_getFactory"),
("libucb1.a", "ucb_component_getFactory"),
("libvcllo.a", "vcl_component_getFactory"),
("libsvtlo.a", "svt_component_getFactory"),
@ -280,6 +279,18 @@ core_constructor_list = [
"com_sun_star_comp_uri_UriReferenceFactory_get_implementation",
"com_sun_star_comp_uri_UriSchemeParser_vndDOTsunDOTstarDOTexpand_get_implementation",
"com_sun_star_comp_uri_UriSchemeParser_vndDOTsunDOTstarDOTscript_get_implementation",
# starmath/util/sm.component
"Math_FormulaDocument_get_implementation",
"Math_XMLContentExporter_get_implementation",
"Math_XMLExporter_get_implementation",
"Math_XMLImporter_get_implementation",
"Math_XMLMetaExporter_get_implementation",
"Math_XMLOasisMetaExporter_get_implementation",
"Math_XMLOasisMetaImporter_get_implementation",
"Math_XMLOasisSettingsExporter_get_implementation",
"Math_XMLOasisSettingsImporter_get_implementation",
"Math_XMLSettingsExporter_get_implementation",
"com_sun_star_comp_Math_MathTypeFilter_get_implementation",
# svl/source/fsstor/fsstorage.component
"svl_FSStorageFactory_get_implementation",
# svtools/util/svt.component

View File

@ -84,7 +84,6 @@ $(eval $(call gb_Library_add_exception_objects,sm,\
starmath/source/rtfexport \
starmath/source/parse \
starmath/source/rect \
starmath/source/register \
starmath/source/smdll \
starmath/source/smmod \
starmath/source/symbol \

View File

@ -1,72 +0,0 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <rtl/ustring.hxx>
#include <sfx2/sfxmodelfactory.hxx>
#include <com/sun/star/lang/XSingleServiceFactory.hpp>
#include "register.hxx"
using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::lang;
extern "C" {
SAL_DLLPUBLIC_EXPORT void* sm_component_getFactory( const char* pImplementationName,
void* pServiceManager,
void* /*pRegistryKey*/ )
{
// Set default return value for this operation - if it failed.
void* pReturn = nullptr ;
if (
( pImplementationName != nullptr ) &&
( pServiceManager != nullptr )
)
{
// Define variables which are used in following macros.
Reference< XSingleServiceFactory > xFactory ;
Reference< XMultiServiceFactory > xServiceManager( static_cast< XMultiServiceFactory* >( pServiceManager ) ) ;
if (SmDocument_getImplementationName().equalsAscii(pImplementationName))
{
xFactory = ::sfx2::createSfxModelFactory( xServiceManager,
SmDocument_getImplementationName(),
SmDocument_createInstance,
SmDocument_getSupportedServiceNames() );
}
// Factory is valid - service was found.
if ( xFactory.is() )
{
xFactory->acquire();
pReturn = xFactory.get();
}
}
// Return with result of this operation.
return pReturn ;
}
} // extern "C"
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

View File

@ -1,37 +0,0 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#ifndef INCLUDED_STARMATH_SOURCE_REGISTER_HXX
#define INCLUDED_STARMATH_SOURCE_REGISTER_HXX
#include <sal/config.h>
#include <sfx2/sfxmodelfactory.hxx>
//Math document
css::uno::Sequence< OUString >
SmDocument_getSupportedServiceNames() throw();
OUString
SmDocument_getImplementationName() throw();
/// @throws css::uno::Exception
css::uno::Reference< css::uno::XInterface >
SmDocument_createInstance(const css::uno::Reference< css::lang::XMultiServiceFactory > & rSMgr, SfxModelFlags _nCreationFlags);
#endif
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

View File

@ -19,7 +19,6 @@
#include <sfx2/sfxmodelfactory.hxx>
#include "register.hxx"
#include <smdll.hxx>
#include <document.hxx>
#include <com/sun/star/frame/XModel.hpp>
@ -27,23 +26,20 @@
using namespace ::com::sun::star;
OUString SmDocument_getImplementationName() throw()
{
return "com.sun.star.comp.Math.FormulaDocument";
}
uno::Sequence< OUString > SmDocument_getSupportedServiceNames() throw()
{
return uno::Sequence<OUString>{ "com.sun.star.formula.FormulaProperties" };
}
uno::Reference< uno::XInterface > SmDocument_createInstance(
const uno::Reference< lang::XMultiServiceFactory > & /*rSMgr*/, SfxModelFlags _nCreationFlags )
extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
Math_FormulaDocument_get_implementation(
css::uno::XComponentContext* , css::uno::Sequence<css::uno::Any> const& args)
{
SolarMutexGuard aGuard;
SmGlobals::ensure();
SfxObjectShell* pShell = new SmDocShell( _nCreationFlags );
return uno::Reference< uno::XInterface >( pShell->GetModel() );
css::uno::Reference<css::uno::XInterface> xInterface = sfx2::createSfxModelInstance(args,
[&](SfxModelFlags _nCreationFlags)
{
SfxObjectShell* pShell = new SmDocShell( _nCreationFlags );
return pShell->GetModel();
});
xInterface->acquire();
return xInterface.get();
}

View File

@ -18,8 +18,9 @@
-->
<component loader="com.sun.star.loader.SharedLibrary" environment="@CPPU_ENV@"
prefix="sm" xmlns="http://openoffice.org/2010/uno-components">
<implementation name="com.sun.star.comp.Math.FormulaDocument">
xmlns="http://openoffice.org/2010/uno-components">
<implementation name="com.sun.star.comp.Math.FormulaDocument"
constructor="Math_FormulaDocument_get_implementation">
<service name="com.sun.star.formula.FormulaProperties"/>
</implementation>
<implementation name="com.sun.star.comp.Math.XMLContentExporter"