I need to better understand our css::uno::Reference

Lionel Elie Mamane lionel at mamane.lu
Mon Jun 23 19:23:04 PDT 2014


On Mon, Jun 23, 2014 at 01:07:14PM +0200, Stephan Bergmann wrote:
> On 06/18/2014 10:58 PM, Lionel Elie Mamane wrote:
>> On Wed, Jun 18, 2014 at 05:04:02PM +0200, Stephan Bergmann wrote:

>>> Short answer (from just browsing the top of FOO.patch): Use
>>> css::uno::Reference<X> only for (C++ classes representing) UNO
>>> interfaces, and use rtl::Reference<C> for C++ classes implementing
>>> UNO objects.

>> Aha. This works. If you feel like explaining the C++-technical
>> reason behind why we need different Reference implementations for
>> the two cases... Feel free.

> As C++ UNO for largely historic reasons does not use virtual
> inheritance, (...) css::uno::XInterface is an ambiguous base class
> of css::uri::XVndSunStarExpandUrlReference, so calling
> pInterface->acquire() will not compile.

> The trick is that we know that all compilers lay out the vtables in
> roughly the same way: No matter how many types a given type X
> derives from, a pointer to X points to a vtable containing function
> pointers for X's first base (if any) followed by function pointers
> for X's own members (if any).

Aren't we missing X's other bases here? There needs to be function
pointers for these, too, somewhere in the vtable. (I also understand
that changing the order of derivation changes the ABI.) I "guess" the
vtable is actually a depth-first left-to-right walk of the inheritance
graph.

> So as long as X represents any UNO interface type, the
> css::uno::Reference<X> ctor can do that dirty trick of reinterpret-casting
> pInterface to css::uno::XInterface* (...)

> Now, if you break the assumption that X represents a UNO interface type,
> that reinterpret_cast trick potentially breaks down.

Do I understand right that:

1) UNO interface types are "carefully translated" to C++ so that
   (one of the multiple) css::uno::XInterface is always the first
   "ultimate base", so that its function pointers are always the first
   in the vtable. This is what makes the reinterpret_cast trick work.

2) UNO interface types don't use virtual inheritance, but the C++
   objects that implement them typically do, at least for the class
   that implements XInterface, and this is what ensures that all ways
   to call ->acquire do use the same reference counters.

   Or they carefully inherit only from one such XInterface
   implementation...

3) The UNO interface types are (purely) abstract (all their methods
   are " = 0;" in the class definition), but this has nothing to do
   with the discussed problem. This does not change the vtable
   layout.

-- 
Lionel


More information about the LibreOffice mailing list