www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Casting an interface to the type that implements it.

This might be equivalent to 'How to do multiple inheritance in D'.

I have a class ControlSet, that is just a bunch of widgets. I use 
it in various places to represent the contents of dialogs that 
are associated with some class.

All such classes have to do is define themselves using the 
CSTarget interface thus:

class Base: CSTarget ...
class Whatever: Base ...

The ControlSet class has a data member host, of type CSTarget. 
When I propagate a button-click or whatever, I use

host.csNotify(Widget w, int id);  // Bam!!!

This works fine in my app, and the ControlSet has been a 
wonderfully reliable and useful part of my armoury.

However, when I try to dispatch the same notification across the 
boundary between my app, and a dynamically loaded plugin module, 
the app crashes. I can fix the crash by casting host to the base 
type of the class that contains it:

(cast(Base) host).csNotify(Widget w, int id);   // Fine ;=)

I believe I need some sort of compile-time skullduggery to define 
this cast information at the point where my ControlSet instances 
are declared. Either that, or I need to pass a delegate to the 
constructor that I can somehow use to replace the cast.

What is the D way of doing this?

Thanks Steve
Mar 12 2014