comp.lang.ada
 help / color / mirror / Atom feed
From: Matthew Heaney <matthewjheaney@earthlink.net>
Subject: Re: Interfaces
Date: Fri, 20 May 2005 05:31:28 GMT
Date: 2005-05-20T05:31:28+00:00	[thread overview]
Message-ID: <uu0kyzcnj.fsf@earthlink.net> (raw)
In-Reply-To: 8764xj9wzf.fsf@deneb.enyo.de

Florian Weimer <fw@deneb.enyo.de> writes:

> Suppose there are two interface types, J1 and J2, and the following
> dispatching subprogramms:
> 
>   procedure Foo (Obj : J1);
>   procedure Foo (Obj : J2);

This is no different from COM, for example:

   interface I1 : IUnknown { /* ... */ };
   interface I2 : IUnknown { /* ... */ };

Now both I1 and I2 have QueryInterface, AddRef, etc.


> T implements both J1 and J2, and provides a body for
> 
>   procedure Foo (Obj : T);

This is no different from a class that inherits from both I1 and I2:

  class C : public I1, public I2 
  {
  public:
     HRESULT QueryInterface(...);
     //...
  };

The operation QueryInterface is inherited from both I1 and I2.

 
> As far as I understand the Ada 200x spec, this subprogram declaration
> overrides both versions of Foo, such that
> 
>   Foo ((J1 (T_Obj));
>   Foo ((J2 (T_Obj));
> 
> invoke the same subprogram.  Is this correct?

This is no different from saying:

   void f(C& obj)
   {
      const HRESULT hr = obj.QueryInterface(...);
   }


> I don't think this is a desirable approach because it makes interfaces
> a strictly non-modular concept, and might force library designers to
> add unique prefixes to interface subprogram names.

In the example above, both I1::QueryInterface and I2::QueryInterface do
exactly the same thing, so the same subprogram should be called.

If you have a true name clash (meaning that two separate interfaces have
the same name for an operation that does completly different things),
then all you have to do is declare one of the interfaces as a nested
class (in Ada-speak, that would be a "multiple views idiom"):

  class C : public I1 
  {
  public:
     void foo();  //I1::foo()

     class C2 : public I2 
     {
        void foo();
     };

     C2 m_obj2;
  };

Now you can say:

   void f(C& obj)
   {
      obj.foo();  //I1
      obj.m_obj2.foo();  //I2
   }

You don't have to have unique prefixes or anything for your interface
types, since the outer class serves in effect as the prefix.

Note that COM hides the nesting behind QueryInterface.  That's more or
less how the multiple views idiom works in Ada:

   type T is limited private;

   function Get_I1 (O : access T) return I1_Access;
   function Get_I2 (O : access T) return I2_Access;

So instead of:

  Foo ((J1 (T_Obj));
  Foo ((J2 (T_Obj));

You would say:

   Foo (Get_I1 (T_Obj'Access).all);
   Foo (Get_I2 (T_Obj'Access).all);

Being able to inherit from multiple interface types is mostly a
syntactic convenience.  Nested classes work in COM, and the multiple
views idiom works in Ada95.

Calling Foo as in your example is a statement that I1::Foo and I2::Foo
do the same thing.  (Like QueryInterface, or AddRef, etc.)  If you want
to overload the name Foo across interface types, then fine, just revert
to the multiple views idiom for one or both of the interface types.

-Matt



      parent reply	other threads:[~2005-05-20  5:31 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-05-16 12:27 Interfaces Florian Weimer
2005-05-16 16:13 ` Interfaces Georg Bauhaus
2005-05-16 16:12   ` Interfaces Florian Weimer
2005-05-16 18:59 ` Interfaces Robert A Duff
2005-05-16 19:27   ` Interfaces Florian Weimer
2005-05-16 20:09     ` Interfaces Robert A Duff
2005-05-16 20:44       ` Interfaces Florian Weimer
2005-05-17  6:14     ` Interfaces Vinzent 'Gadget' Hoefler
2005-05-17 10:53       ` Interfaces Florian Weimer
2005-05-17 13:34         ` Interfaces Vinzent 'Gadget' Hoefler
     [not found]         ` <4289f382$0$10805$afc38c87@>
2005-05-17 16:51           ` Interfaces Pascal Obry
     [not found]             ` <428a2832$0$10805$afc38c87@>
2005-05-17 18:15               ` Interfaces Pascal Obry
2005-05-17 21:11                 ` Interfaces Florian Weimer
2005-05-18 10:36                   ` Interfaces Georg Bauhaus
2005-05-17  7:36     ` Interfaces Dmitry A. Kazakov
2005-05-17  9:06       ` Interfaces Florian Weimer
2005-05-17 10:19         ` Interfaces Dmitry A. Kazakov
2005-05-17 10:57           ` Interfaces Florian Weimer
2005-05-17 11:13             ` Interfaces Dmitry A. Kazakov
2005-05-17 15:35               ` Interfaces Robert A Duff
2005-05-17 21:53                 ` Interfaces Florian Weimer
2005-05-17 23:21                   ` Interfaces Randy Brukardt
2005-05-17 23:44                     ` Interfaces Marius Amado Alves
2005-05-18  7:53                     ` Interfaces Dmitry A. Kazakov
2005-05-18 16:57 ` Interfaces adam
2005-05-20  5:31 ` Matthew Heaney [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox