www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - query interface

reply Qian Xu <quian.xu stud.tu-ilmenau.de> writes:
Hi All,

can D check, whether a class A an Interface B supports?

like:

  if (supports(class_A, intf_B))
  {
     cast(intf_B) (class_A).hello();
  }

--Qian
Jan 21 2009
next sibling parent Qian Xu <quian.xu stud.tu-ilmenau.de> writes:
Qian Xu wrote:

 Hi All,
 
 can D check, whether a class A an Interface B supports?
 
 like:
 
   if (supports(class_A, intf_B))
   {
      cast(intf_B) (class_A).hello();
   }
 
 --Qian
what I have found is: if (is(class_A == intf_B)) { cast(intf_B) (class_A).hello(); } Is there any better one? --Qian
Jan 21 2009
prev sibling next sibling parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Qian Xu wrote:
 Hi All,
 
 can D check, whether a class A an Interface B supports?
 
 like:
 
   if (supports(class_A, intf_B))
if (is(class_A : intf_B)) tests if 'class_A' is implicitly convertible to 'intf_B'. If the first is a class and the second an interface, that's equivalent to the class implementing the interface.
Jan 21 2009
parent reply Qian Xu <quian.xu stud.tu-ilmenau.de> writes:
Frits van Bommel wrote:

 Qian Xu wrote:
 Hi All,
 
 can D check, whether a class A an Interface B supports?
 
 like:
 
   if (supports(class_A, intf_B))
if (is(class_A : intf_B)) tests if 'class_A' is implicitly convertible to 'intf_B'. If the first is a class and the second an interface, that's equivalent to the class implementing the interface.
Thanks. Could you tell me, how to make a function for this? I do not know how to pass an Interface as parameter. like bool supports(T)(T obj, interface_type t) { return (is(obj : t)); }
Jan 21 2009
next sibling parent Trass3r <mrmocool gmx.de> writes:
Qian Xu schrieb:
 Thanks. Could you tell me, how to make a function for this? I do not know
 how to pass an Interface as parameter.
 
 like
   bool supports(T)(T obj, interface_type t)
   {
     return (is(obj : t));
   }
Guess something like bool supports(T, I) (T obj) { return (is(obj : I)); } should do it.
Jan 21 2009
prev sibling parent BCS <ao pathlink.com> writes:
Reply to Qian,

 Frits van Bommel wrote:
 
 Qian Xu wrote:
 
 Hi All,
 
 can D check, whether a class A an Interface B supports?
 
 like:
 
 if (supports(class_A, intf_B))
 
if (is(class_A : intf_B)) tests if 'class_A' is implicitly convertible to 'intf_B'. If the first is a class and the second an interface, that's equivalent to the class implementing the interface.
Thanks. Could you tell me, how to make a function for this? I do not know how to pass an Interface as parameter. like bool supports(T)(T obj, interface_type t) { return (is(obj : t)); }
Given
 interface I {}
 class C1 {}
 class C2 : C1, I {}
 class C3 : C1 {}
if you want to check if an instance of C1 is a derived class that implements I use this
 null !is cast(I)c1
if you want it in a function
 bool IsA(TI, TC)(TC c)
 {
   return null !is cast(TI)c;
 }
Jan 21 2009
prev sibling parent Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
On Wed, Jan 21, 2009 at 10:30 AM, Qian Xu <quian.xu stud.tu-ilmenau.de> wrote:
 Hi All,

 can D check, whether a class A an Interface B supports?

 like:

  if (supports(class_A, intf_B))
  {
     cast(intf_B) (class_A).hello();
  }
At compile time, like Frits said, you can use is(class_A : intf_B). At runtime, you can use cast(intf_B)someInstance. That returns 'null' if someInstance does _not_ inherit from intf_B. if(auto i = cast(intf_B)instanceOfA) i.hello();
Jan 21 2009