Lifetime Management and Obtaining Interfaces

From Apache OpenOffice Wiki
Jump to: navigation, search



The CLR is similar to the Java runtime in that it keeps track of the object's lifetime rather then leaving the task to the developer. Once an object is no longer referenced (unreachable), the CLR deletes that object. Therefore, reference counting, as used in C++, is not necessary. Hence com.sun.star.uno.XInterface:acquire and com.sun.star.uno.XInterface:release are not needed.

XInterface has a third method, queryInterface, which is used to query an object for a particular interface. This language binding does not use queryInterface. Instead objects can be cast to the desired interface. For example:

 // C#
 try {
   XFoo bar = (XFoo) obj;
 } catch (System.InvalidCastException e) {
     // obj does not support XFoo
 }
 
 // using keywords is and as
 if (obj is XFoo) {
     // obj supports XFoo
 }
 
 XFoo foo = obj as XFoo;
 if (foo != null)
 {
     // obj supports XFoo
 }
 
 // C++ with managed extensions
 XFoo * pFoo = dynamic_cast< XFoo * >( obj );
 if (XFoo != 0)
 {
     // obj supports XFoo
 }  
 
 try {
 XFoo * pFoo = __try_cast< XFoo * >( obj );
 } catch (System::InvalidCastException * e) {
     // obj does not support XFoo
 }
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages