Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
April 14, 2019 Transparent cast from class to member pointer? | ||||
---|---|---|---|---|
| ||||
struct IM;
struct C {
IM *impl;
};
int cInit(C* self);
class I {
C handler;
this(){cInit(&handler);}
}
Is there a simple way that I can use handler without the address-of operator and automatically get *impl?
Something like:
class I {
C handler;
this(){cInit(handler);}
}
And later can use I in a way like this without having to define/write explicit casts everywhere?
someFunc(C* self);
I myI;
someFunc(myI);
--
Robert M. Münch
http://www.saphirion.com
smarter | better | faster
|
April 14, 2019 Re: Transparent cast from class to member pointer? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robert M. Münch | Le 14/04/2019 à 20:03, Robert M. Münch via Digitalmars-d-learn a écrit : > struct IM; > struct C { > IM *impl; > }; > > int cInit(C* self); > > class I { > C handler; > > this(){cInit(&handler);} > } > > Is there a simple way that I can use handler without the address-of operator and automatically get *impl? > > Something like: > > class I { > C handler; > > this(){cInit(handler);} > } > > And later can use I in a way like this without having to define/write explicit casts everywhere? > > someFunc(C* self); > > I myI; > someFunc(myI); Do you have a clear and correct view of what you want to express, of the application? How does it look (for us), if you replace IM, C, impl, cInit, I, handler, with meaningful (and correctly chosen) terms? -- diniz {la vita e estranj} |
April 15, 2019 Re: Transparent cast from class to member pointer? | ||||
---|---|---|---|---|
| ||||
Posted in reply to diniz | On 2019-04-14 20:01:27 +0000, diniz said: > Le 14/04/2019 à 20:03, Robert M. Münch via Digitalmars-d-learn a écrit : >> struct IM; >> struct C { >> IM *impl; >> }; >> >> int cInit(C* self); >> >> class I { >> C handler; >> >> this(){cInit(&handler);} >> } >> >> Is there a simple way that I can use handler without the address-of operator and automatically get *impl? >> >> Something like: >> >> class I { >> C handler; >> >> this(){cInit(handler);} >> } >> >> And later can use I in a way like this without having to define/write explicit casts everywhere? >> >> someFunc(C* self); >> >> I myI; >> someFunc(myI); > > Do you have a clear and correct view of what you want to express, of the application? How does it look (for us), if you replace IM, C, impl, cInit, I, handler, with meaningful (and correctly chosen) terms? Well, ok... even it really doesn't matter a lot. IM = Implementaiton Context (C-API) C = Core Context (C-API) I = Implementation D Class The C side requires that *impl is the 1st member in the struct/class whereever it is stored. Hence, the wrapping in a struct and not directly putting it into a D class. -- Robert M. Münch http://www.saphirion.com smarter | better | faster |
April 15, 2019 Re: Transparent cast from class to member pointer? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robert M. Münch | On 04/14/2019 11:03 AM, Robert M. Münch wrote:
> struct IM;
> struct C {
> IM *impl;
> };
>
> int cInit(C* self);
>
> class I {
> C handler;
>
> this(){cInit(&handler);}
> }
>
> Is there a simple way that I can use handler without the address-of operator and automatically get *impl?
>
> Something like:
>
> class I {
> C handler;
>
> this(){cInit(handler);}
> }
>
> And later can use I in a way like this without having to define/write explicit casts everywhere?
>
> someFunc(C* self);
>
> I myI;
> someFunc(myI);
>
>
'alias this' can do that:
struct IM;
struct C {
IM *impl;
};
int cInit(C* self) {
return 0;
}
class I {
C handler;
this(){cInit(&handler);}
C* ptr() { // <== ADDED
return &handler;
}
alias ptr this; // <== ADDED
}
void someFunc(C* self) {
}
void main() {
I myI = new I();
someFunc(myI);
}
Ali
|
April 15, 2019 Re: Transparent cast from class to member pointer? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robert M. Münch | Le 15/04/2019 à 08:30, Robert M. Münch via Digitalmars-d-learn a écrit : > The C side requires that *impl is the 1st member in the struct/class whereever it is stored. Hence, the wrapping in a struct and not directly putting it into a D class. All right! Did not think at this usage case, interfacing with C. -- diniz {la vita e estranj} |
April 15, 2019 Re: Transparent cast from class to member pointer? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On 2019-04-15 08:19:57 +0000, Ali ‡ehreli said: > 'alias this' can do that: Hi, I had the suspicion already... > struct IM; > struct C { > IM *impl; > }; > > int cInit(C* self) { > return 0; > } > > class I { > C handler; > > this(){cInit(&handler);} > > C* ptr() { // <== ADDED > return &handler; > } > > alias ptr this; // <== ADDED > } > > void someFunc(C* self) { > } > > void main() { > I myI = new I(); > someFunc(myI); > } Bingo, I didn't know that I can do an 'alias this' using a function and not only a type... pretty cool. So, with several of these I can setup implicit conversions to different types. Thanks. -- Robert M. Münch http://www.saphirion.com smarter | better | faster |
April 15, 2019 Re: Transparent cast from class to member pointer? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robert M. Münch | On Monday, 15 April 2019 at 15:07:10 UTC, Robert M. Münch wrote: > On 2019-04-15 08:19:57 +0000, Ali ‡ehreli > > Bingo, I didn't know that I can do an 'alias this' using a function and not only a type... pretty cool. So, with several of these I can setup implicit conversions to different types. Thanks. Well, as I know, multiple alias this won't work yet... https://wiki.dlang.org/DIP66 |
Copyright © 1999-2021 by the D Language Foundation