接口类型的映射
From Apache OpenOffice Wiki
UNO 接口类型的值(为空引用或对给定接口类型对象实现的引用)被映射成模板类:
template< class t > com::sun::star::uno::Reference< t >
模板用于获取类型安全接口引用,因为只可以将正确键入的接口指针指定到该引用。下面的示例将一个桌面服务实例指定到 rDesktop
引用:
// the xSMgr reference gets constructed somehow { ... // construct a deskop object and acquire it Reference< XInterface > rDesktop = xSMgr->createInstance( OUString::createFromAscii("com.sun.star.frame.Desktop”)); ... // reference goes out of scope now, release is called on the interface }
Reference
的构造函数调用接口的 acquire()
,而析构函数调用接口的 release()
。这些引用通常称为智能指针。请始终一致地使用 Reference
模板,以免出现引用计数错误。
Reference
类使我们可以轻松地调用某个具体类型的 queryInterface()
:
// construct a deskop object and acquire it Reference< XInterface > rDesktop = xSMgr->createInstance( OUString::createFromAscii("com.sun.star.frame.Desktop")); // query it for the XFrameLoader interface Reference< XFrameLoader > rLoader( rDesktop , UNO_QUERY ); // check, if the frameloader interface is supported if( rLoader.is() ) { // now do something with the frame loader ... }
UNO_QUERY
是一个样本参数,它告诉构造函数在第一个构造函数参数中查询 XFrameLoader
接口。如果 queryInterface()
成功返回,则将其指定到 rLoader
引用。您可以通过对新引用调用 is()
来检查查询是否成功。
可以使用运算符 -> 来调用接口中的方法:
xSMgr->createInstance(...);
operator ->() 返回接口指针,而不获取该指针,也就是说,不增加引用计数。
可以通过对引用进行 clear()
调用或指定一个默认构造引用来明确释放接口引用。
使用运算符 ==
可以检查两个接口引用是否属于同一个对象。
Content on this page is licensed under the Public Documentation License (PDL). |