服务的映射
From Apache OpenOffice Wiki
新式服务被映射成同名的 C++ 类。该类有一个或多个公共静态成员函数,这些函数与服务的显式或隐式构造函数相对应。
对于具有给定接口类型 XIfc
的新式服务,以下形式的显式构造函数
name([in] Type1 arg1, [in] Type2 arg2) raises (Exception1, ..., ExceptionN);
通过如下的 C++ 成员函数表示
public: static com::sun::star::uno::Reference< XIfc > name( com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext > const & context, Type1 arg1, Type2 arg2) throw (Exception1, ..., ExceptionN, com::sun::star::uno::RuntimeException) { ... }
如果服务构造函数有一个 rest 参数 (any...
),则在 C++ 中它会被映射成类型 com::sun::star::uno::Sequence< com::sun::star::uno::Any > const &
的参数。
如果新式服务有隐式构造函数,则对应的 C++ 成员函数的形式为
public: static com::sun::star::uno::Reference< XIfc > create( com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext > const & context) throw (com::sun::star::uno::RuntimeException) { ... }
C++ 中显式和隐式服务构造函数的语义如下。它们与 Java 中的语义相同:
- 服务构造函数的第一个参数始终为 com.sun.star.uno.XComponentContext,且必须为非空引用。其他所有参数都用于初始化创建的服务(见下文)。
- 服务构造函数首先使用 com.sun.star.uno.XComponentContext:getServiceManager 从给定的组件上下文获取服务管理器 (com.sun.star.lang.XMultiComponentFactory)。
- 然后,服务构造函数使用 com.sun.star.lang.XMultiComponentFactory:createInstanceWithArgumentsAndContext 创建服务实例,并向它传递参数列表,但不包含初始的
XComponentContext
。如果服务构造函数有单个 rest 参数,则可以直接使用 any 值的序列,否则,给定的参数将列入 any 值的序列中。如果是隐式服务构造函数,则不传递参数,而是使用 com.sun.star.lang.XMultiComponentFactory:createInstanceWithContext。
- 如果以上任何步骤因服务构造函数可能抛出(根据其异常规范)的异常而失败,则服务构造函数也会抛出该异常并以失败告终。否则,如果以上任何步骤因不可能由服务构造函数抛出的异常而失败,则服务构造函数会抛出 com.sun.star.uno.DeploymentException 并以失败告终。最后,如果没有创建任何服务实例(由于给定的组件上下文无服务管理器,或者由于服务管理器不支持请求的服务),则服务构造函数会抛出 com.sun.star.uno.DeploymentException 并以失败告终。实际结果是服务构造函数或者返回所请求服务的非空实例,或者抛出异常;服务构造函数决不会返回空实例。
没有将旧式服务映射至 C++ 语言绑定。
Content on this page is licensed under the Public Documentation License (PDL). |