Difference between revisions of "Zh/Documentation/DevGuide/WritingUNO/C++/Providing a Single Factory Using a Helper Method"
From Apache OpenOffice Wiki
< Zh | Documentation
m (New page: {{Documentation/DevGuide/WritingUNOTOC/Zh |WritingUNO2d=block |ShowPrevNext=block |PrevPage=Zh/Documentation/DevGuide/WritingUNO/C++/Implementing your own Interfaces |NextPage=Zh/Documenta...) |
m |
||
(One intermediate revision by the same user not shown) | |||
Line 7: | Line 7: | ||
{{DISPLAYTITLE:使用 Helper 方法提供单个工厂}} | {{DISPLAYTITLE:使用 Helper 方法提供单个工厂}} | ||
− | C++ | + | C++ 组件库必须导出一个名称为 <code>component_getFactory()</code> 的外部 "C" 函数,这个函数为'''给定实现'''提供一个工厂对象 。使用 <code>::cppu::component_getFactoryHelper()</code> 创建此函数。通过 '''cppuhelper/implementationentry.hxx''' 包含此函数的声明。 |
Latest revision as of 06:13, 30 July 2008
C++ 组件库必须导出一个名称为 component_getFactory()
的外部 "C" 函数,这个函数为给定实现提供一个工厂对象 。使用 ::cppu::component_getFactoryHelper()
创建此函数。通过 cppuhelper/implementationentry.hxx 包含此函数的声明。
component_getFactory()
方法出现在以下列表的末尾。该方法假定组件包含一个静态的 ::cppu::ImplementationEntry
数组 s_component_entries[]
,其中含有许多函数指针。列表显示了如何编写组件,以便多服务组件的所有服务的函数指针都可以正确初始化。
#include <cppuhelper/implbase3.hxx> // "3" implementing three interfaces #include <cppuhelper/factory.hxx> #include <cppuhelper/implementationentry.hxx> #include <com/sun/star/lang/XServiceInfo.hpp> #include <com/sun/star/lang/XInitialization.hpp> #include <com/sun/star/lang/IllegalArgumentException.hpp> #include <my_module/XSomething.hpp> using namespace ::rtl; // for OUString using namespace ::com::sun::star; // for sdk interfaces using namespace ::com::sun::star::uno; // for basic types namespace my_sc_impl { class MyService2Impl : public ::cppu::WeakImplHelper3< ::my_module::XSomething, lang::XServiceInfo, lang::XInitialization > { OUString m_arg; public: // focus on three given interfaces, // no need to implement XInterface, XTypeProvider, XWeak // XInitialization will be called upon createInstanceWithArguments[AndContext]() virtual void SAL_CALL initialize( Sequence< Any > const & args ) throw (Exception); // XSomething virtual OUString SAL_CALL methodOne( OUString const & str ) throw (RuntimeException); // XServiceInfo virtual OUString SAL_CALL getImplementationName() throw (RuntimeException); virtual sal_Bool SAL_CALL supportsService( OUString const & serviceName ) throw (RuntimeException); virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() throw (RuntimeException); }; // Implementation of XSomething, XServiceInfo and XInitilization omitted here: ... // component operations from service1_impl.cxx extern Sequence< OUString > SAL_CALL getSupportedServiceNames_MyService1Impl(); extern OUString SAL_CALL getImplementationName_MyService1Impl(); extern Reference< XInterface > SAL_CALL create_MyService1Impl( Reference< XComponentContext > const & xContext ) SAL_THROW( () ); // component operations for MyService2Impl static Sequence< OUString > getSupportedServiceNames_MyService2Impl() { Sequence<OUString> names(1); names[0] = OUString(RTL_CONSTASCII_USTRINGPARAM("my_module.MyService2")); return names; } static OUString getImplementationName_MyService2Impl() { return OUString( RTL_CONSTASCII_USTRINGPARAM( "my_module.my_sc_implementation.MyService2") ); } Reference< XInterface > SAL_CALL create_MyService2Impl( Reference< XComponentContext > const & xContext ) SAL_THROW( () ) { return static_cast< lang::XTypeProvider * >( new MyService2Impl() ); } /* shared lib exports implemented without helpers in service_impl1.cxx */ namespace my_sc_impl { static struct ::cppu::ImplementationEntry s_component_entries [] = { { create_MyService1Impl, getImplementationName_MyService1Impl, getSupportedServiceNames_MyService1Impl, ::cppu::createSingleComponentFactory, 0, 0 }, { create_MyService2Impl, getImplementationName_MyService2Impl, getSupportedServiceNames_MyService2Impl, ::cppu::createSingleComponentFactory, 0, 0 }, { 0, 0, 0, 0, 0, 0 } }; } extern "C" { void * SAL_CALL component_getFactory( sal_Char const * implName, lang::XMultiServiceFactory * xMgr, registry::XRegistryKey * xRegistry ) { return ::cppu::component_getFactoryHelper( implName, xMgr, xRegistry, ::my_sc_impl::s_component_entries ); } // getImplementationEnvironment and component_writeInfo are described later, we omit them here ... }
静态变量 s_component_entries<code> 定义了一个 null 结尾的数组,其中的条目涉及共享库的服务实现。服务实现条目包含用于以下目的的函数指针:
- 创建对象:<code>create_MyServiceXImpl()
- 实现名称:
getImplementationName_MyServiceXImpl()
- 支持的服务名称:
getSupportedServiceNames_MyServiceXImpl()
- 要使用的工厂帮助程序:
::cppu::createComponentFactory()
最后两个值已预留供将来使用,因此可以是 0。
Content on this page is licensed under the Public Documentation License (PDL). |