www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Transparent cast from class to member pointer?

reply =?iso-8859-1?Q?Robert_M._M=FCnch?= <robert.muench saphirion.com> writes:
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
Apr 14 2019
next sibling parent reply diniz <diniz posteo.net> writes:
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}
Apr 14 2019
parent reply =?iso-8859-1?Q?Robert_M._M=FCnch?= <robert.muench saphirion.com> writes:
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
Apr 14 2019
parent diniz <diniz posteo.net> writes:
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}
Apr 15 2019
prev sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Apr 15 2019
parent reply =?iso-8859-1?Q?Robert_M._M=FCnch?= <robert.muench saphirion.com> writes:
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
Apr 15 2019
parent Alex <sascha.orlov gmail.com> writes:
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
Apr 15 2019