使用 Helper 模板类定义类
XInterface, XTypeProvider 和 XWeak
SDK 提供了一些使开发更加轻松的辅助程序(Helper),其中包括处理 com.sun.star.uno.XInterface、com.sun.star.lang.XTypeProvider 以及 com.sun.star.uno.XWeak 实现的 Helper 模板类。这些类让您可以集中处理要实现的接口。
my_module.MyService2 的实现使用 ::cppu::WeakImplHelper3<> Helper 辅助程序。“3” 代表要实现的接口数。这个模板类接受要实现接口作为模板参数,类的声明即从这个模板类继承得到。
#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 >
{
...
};
}
下一节将关注 com.sun.star.lang.XServiceInfo 和 com.sun.star.lang.XInitialization 的编码以及示例接口 my_module.Xsomething。
cppuhelper 共享库提供附加的实现帮助程序类,例如支持 com.sun.star.lang.XComponent。请参见 SDK 的 C++ 引用中的或 udk.openoffice.org 上的 ::cppu 名称空间。
XServiceInfo
UNO 服务实现支持提供有关其实现名称和支持的服务的 com.sun.star.lang.XServiceInfo。实现名称是引用特定实现的唯一名称,在此示例中,分别是 my_module.my_sc_impl.MyService1 和 my_module.my_sc_impl.MyService2。实现名称以后会在将实现注册到 OpenOffice.org 使用的 simple_component.rdb 注册表时使用。它可以将一个服务名称条目链接到一个实现,因为可能会存在多个实现。同一服务的多个实现可以具有不同的属性,例如运行时行为和内存空间。
我们创建的服务实例必须支持 com.sun.star.lang.XServiceInfo 接口。此接口有三个方法,可以进行如下编码以支持一个服务:
// XServiceInfo implementation
OUString MyService2Impl::getImplementationName()
throw (RuntimeException)
{
// unique implementation name
return OUString( RTL_CONSTASCII_USTRINGPARAM("my_module.my_sc_impl.MyService2") );
}
sal_Bool MyService2Impl::supportsService( OUString const & serviceName )
throw (RuntimeException)
{
// this object only supports one service, so the test is simple
return serviceName.equalsAsciiL(
RTL_CONSTASCII_STRINGPARAM("my_module.MyService2") );
}
Sequence< OUString > MyService2Impl::getSupportedServiceNames()
throw (RuntimeException)
{
return getSupportedServiceNames_MyService2Impl();
}
| Content on this page is licensed under the Public Documentation License (PDL). |