Difference between revisions of "Zh/Documentation/DevGuide/ProUNO/C++/Mapping of Services"

From Apache OpenOffice Wiki
Jump to: navigation, search
(New page: {{Documentation/DevGuide/ProUNOTOC/Zh |ProUNO2c=block |ProUNO2cC++=block |ProUNO2cC++TM=block |ShowPrevNext=block |PrevPage=Zh/Documentation/DevGuide/ProUNO/C++/Mapping of Sequence Types |...)
 
Line 10: Line 10:
 
{{DISPLAYTITLE:服务的映射}}
 
{{DISPLAYTITLE:服务的映射}}
  
A new-style service is mapped to a C++ class with the same name. The class has one or more public static member functions that correspond to the explicit or implicit constructors of the service.
+
新式服务被映射成同名的 C++ 类。该类有一个或多个公共静态成员函数,这些函数与服务的显式或隐式构造函数相对应。
  
For a new-style service with a given interface type <code>XIfc</code>, an explicit constructor of the form
+
 
 +
对于具有给定接口类型 <code>XIfc</code> 的新式服务,以下形式的显式构造函数
  
 
   name([in] Type1 arg1, [in] Type2 arg2) raises (Exception1, ..., ExceptionN);
 
   name([in] Type1 arg1, [in] Type2 arg2) raises (Exception1, ..., ExceptionN);
  
is represented by the C++ member function
+
通过如下的 C++ 成员函数表示
  
 
   public:
 
   public:
Line 24: Line 25:
 
       throw (Exception1, ..., ExceptionN, com::sun::star::uno::RuntimeException) { ... }
 
       throw (Exception1, ..., ExceptionN, com::sun::star::uno::RuntimeException) { ... }
  
If a service constructor has a rest parameter (<code>any...</code>), it is mapped to a parameter of type <code>com::sun::star::uno::Sequence< com::sun::star::uno::Any > const &</code> in C++.
+
如果服务构造函数有一个 rest 参数 (<code>any...</code>),则在 C++ 中它会被映射成类型 <code>com::sun::star::uno::Sequence< com::sun::star::uno::Any > const &</code> 的参数。
  
If a new-style service has an implicit constructor, the corresponding C++ member function is of the form
+
 
 +
如果新式服务有隐式构造函数,则对应的 C++ 成员函数的形式为
  
 
   public:
 
   public:
Line 33: Line 35:
 
       throw (com::sun::star::uno::RuntimeException) { ... }
 
       throw (com::sun::star::uno::RuntimeException) { ... }
  
The semantics of both explicit and implicit service constructors in C++ are as follows. They are the same as for Java:
 
  
* The first argument to a service constructor is always a <idl>com.sun.star.uno.XComponentContext</idl>, which must be a non-null reference. Any further arguments are used to initialize the created service (see below).
+
C++ 中显式和隐式服务构造函数的语义如下。它们与 Java 中的语义相同:
* The service constructor first uses [http://api.openoffice.org/docs/common/ref/com/sun/star/uno/XComponentContext.html#getServiceManager com.sun.star.uno.XComponentContext:getServiceManager] to obtain a service manager (a <idl>com.sun.star.lang.XMultiComponentFactory</idl>) from the given component context.
+
 
* The service constructor then uses [http://api.openoffice.org/docs/common/ref/com/sun/star/lang/XMultiComponentFactory.html#createInstanceWithArgumentsAndContext com.sun.star.lang.XMultiComponentFactory:createInstanceWithArgumentsAndContext] to create a service instance, passing it the list of arguments without the initial <code>XComponentContext</code>. If the service constructor has a single rest parameter, its sequence of any values is used directly, otherwise the given arguments are made into a sequence of any values. In the case of an implicit service constructor, no arguments are passed, and [http://api.openoffice.org/docs/common/ref/com/sun/star/lang/XMultiComponentFactory.html#createInstanceWithContext com.sun.star.lang.XMultiComponentFactory:createInstanceWithContext] is used instead.
+
* 服务构造函数的第一个参数始终为 <idl>com.sun.star.uno.XComponentContext</idl>,且必须为非空引用。其他所有参数都用于初始化创建的服务(见下文)。
* If any of the above steps fails with an exception that the service constructor may throw (according to its exception specification), the service constructor also fails by throwing that exception. Otherwise, if any of the above steps fails with an exception that the service constructor may not throw, the service constructor instead fails by throwing a <idl>com.sun.star.uno.DeploymentException</idl>. Finally, if no service instance could be created (because either the given component context has no service manager, or the service manager does not support the requested service), the service constructor fails by throwing a <idl>com.sun.star.uno.DeploymentException</idl>. The net effect is that a service constructor either returns a non-null instance of the requested service, or throws an exception; a service constructor will never return a null instance.
+
 
 +
* 服务构造函数首先使用 [http://api.openoffice.org/docs/common/ref/com/sun/star/uno/XComponentContext.html#getServiceManager com.sun.star.uno.XComponentContext:getServiceManager] 从给定的组件上下文获取服务管理器 (<idl>com.sun.star.lang.XMultiComponentFactory</idl>)
 +
 
 +
* 然后,服务构造函数使用 [http://api.openoffice.org/docs/common/ref/com/sun/star/lang/XMultiComponentFactory.html#createInstanceWithArgumentsAndContext com.sun.star.lang.XMultiComponentFactory:createInstanceWithArgumentsAndContext] 创建服务实例,并向它传递参数列表,但不包含初始的 <code>XComponentContext</code>。如果服务构造函数有单个 rest 参数,则可以直接使用 any 值的序列,否则,给定的参数将列入 any 值的序列中。如果是隐式服务构造函数,则不传递参数,而是使用 [http://api.openoffice.org/docs/common/ref/com/sun/star/lang/XMultiComponentFactory.html#createInstanceWithContext com.sun.star.lang.XMultiComponentFactory:createInstanceWithContext]
 +
 
 +
* 如果以上任何步骤因服务构造函数可能抛出(根据其异常规范)的异常而失败,则服务构造函数也会抛出该异常并以失败告终。否则,如果以上任何步骤因不可能由服务构造函数抛出的异常而失败,则服务构造函数会抛出 <idl>com.sun.star.uno.DeploymentException</idl> 并以失败告终。最后,如果没有创建任何服务实例(由于给定的组件上下文无服务管理器,或者由于服务管理器不支持请求的服务),则服务构造函数会抛出 <idl>com.sun.star.uno.DeploymentException</idl> 并以失败告终。实际结果是服务构造函数或者返回所请求服务的非空实例,或者抛出异常;服务构造函数决不会返回空实例。
 +
 
 +
 
 +
没有将旧式服务映射至 C++ 语言绑定。
  
Old-style services are not mapped into the C++ language binding.
 
  
 
{{PDL1}}
 
{{PDL1}}
  
[[Category:Documentation/Developer's Guide/Professional UNO]]
+
[[Category:文档/开发者指南/专业 UNO]]

Revision as of 02:56, 7 July 2008


新式服务被映射成同名的 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.DeploymentException 并以失败告终。最后,如果没有创建任何服务实例(由于给定的组件上下文无服务管理器,或者由于服务管理器不支持请求的服务),则服务构造函数会抛出 com.sun.star.uno.DeploymentException 并以失败告终。实际结果是服务构造函数或者返回所请求服务的非空实例,或者抛出异常;服务构造函数决不会返回空实例。


没有将旧式服务映射至 C++ 语言绑定。


Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages