Passing myself, a struct, as a C callback context
lobo via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Mar 29 20:23:59 PDT 2015
On Monday, 30 March 2015 at 02:53:36 UTC, Paul O'Neil wrote:
> I'm registering a callback with some C code. The simplified
> story is
> here, but the actual code is on GitHub [1] at the end if you
> care.
>
> The call looks something like this.
>
> void register(void(*fp)(void*), void* context);
>
> I have a class that holds state for the callback and registers
> itself:
>
> final class Klass
> {
> void method()
> {
> register(callback_function, &this);
> }
> }
>
> As of dmd 2.067, doing "&this" is deprecated. Is there an
> idiomatic way
> to do this?
>
> [0] Actual code is at
> https://github.com/todayman/dubik/blob/master/source/vibe/core/drivers/rx.d#L177
> . The msg object eventually gets passed to the registration
> function.
>
> Thanks,
This is only deprecated for class not struct. This code below
works fine:
---
import std.stdio;
extern(C) void f2(void* ins) {
auto s = cast(S*)(ins);
writefln("f2():%s", s);
writefln("f2():%s", *s);
}
void f1(void* ins) {
auto s = cast(S*)(ins);
writefln("f1():%s", s);
writefln("f1():%s", *s);
}
struct S { // <<-- change to "class" to get deprecated message
int value = 10;
void f() {
f1(&this);
f2(&this);
}
}
void main()
{
auto s = S();
s.f();
}
---
bye,
lobo
More information about the Digitalmars-d-learn
mailing list