Wrapping an arbitrary class/struct API

Paolo Invernizzi paolo.invernizzi at gmail.com
Sat Aug 24 05:30:34 PDT 2013


On Saturday, 24 August 2013 at 12:12:29 UTC, Joseph Rushton 
Wakeling wrote:

Hi Joseph,

I'm not really a D guru like others, but...

> ... an I want to wrap it in another struct, B.  If I do this 
> manually it would be something like,
>
>     struct B
>     {
>         private A a;
>         void foo(int n) { return a.foo(n); }
>         void foo(Range)(Range r) { return a.foo(r); }
>         // ... etc ...
>     }
>
> But suppose that I don't a priori know the list of functions 
> (and function arguments) that need to be wrapped.  How could I 
> go about working this out, with a generic programming approach?

For this case I would use alias this, if you want to wrap every 
method...

     struct B
     {
         private A a;
         alias a this;
     }

> More specifically, how could I work this out limited to a 
> specific function of A (say, foo) ... ?

I think that you have to explicitly wrap them, maybe with a 
template to alleviate the copy/past...
But maybe someone else can suggest a better solution!

- Paolo Invernizzi


More information about the Digitalmars-d-learn mailing list