digitalmars.D.learn - Wrapping an arbitrary class/struct API
- Joseph Rushton Wakeling (29/29) Aug 24 2013 Hello all,
- John Colvin (5/35) Aug 24 2013 You might want to look at std.typecons.Proxy
- Joseph Rushton Wakeling (2/4) Aug 24 2013 Thanks, I'll take a look :-)
Hello all, (This accidentally got posted as a reply to someone else's thread -- I'm reposting in order to make sure that thread doesn't get diverted.) Suppose that I have a struct like e.g.: struct A { void foo(int n) { ... } void foo(Range)(Range r) { ... } int bar() { ... } int bar(double x) { ... } // ... and others ... } ... 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, i.e. _without_ manually writing the individual cases? More specifically, how could I work this out limited to a specific function of A (say, foo) ... ? Thanks & best wishes, -- Joe
Aug 24 2013
On Saturday, 24 August 2013 at 13:14:30 UTC, Joseph Rushton Wakeling wrote:Hello all, (This accidentally got posted as a reply to someone else's thread -- I'm reposting in order to make sure that thread doesn't get diverted.) Suppose that I have a struct like e.g.: struct A { void foo(int n) { ... } void foo(Range)(Range r) { ... } int bar() { ... } int bar(double x) { ... } // ... and others ... } ... 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, i.e. _without_ manually writing the individual cases? More specifically, how could I work this out limited to a specific function of A (say, foo) ... ? Thanks & best wishes, -- JoeYou might want to look at std.typecons.Proxy also, opDispatch is a very powerful tool for doing this sort of thing.
Aug 24 2013
On 24/08/13 15:31, John Colvin wrote:You might want to look at std.typecons.Proxy also, opDispatch is a very powerful tool for doing this sort of thing.Thanks, I'll take a look :-)
Aug 24 2013