August 10, 2011 Re: Frontend and backend communication | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pelle | Oh, so structs themselves are only definitions and not global variables, I see. Thanks. |
October 19, 2011 Re: Frontend and backend communication | ||||
---|---|---|---|---|
| ||||
I'm trying to implement the function pointer system right now, and it seems to work on the C side, but not D. I assume I'm missing some kind of syntax here. I have these global variables: struct S_FrontendFunctions { void function() RedrawScreen; void function(const char*, int) PrecacheCard; } shared S_FrontendFunctions FrontendFunctions; And if I try to set the pointers D style, like this: FrontendFunctions.RedrawScreen = function(){}; FrontendFunctions.PrecacheCard = function(const char*, int){}; I get errors: Error: cannot implicitly convert expression (__funcliteral3) of type _error_ function() to shared(void function()) Error: cannot implicitly convert expression (__funcliteral4) of type _error_ function(const const(char*), int) to shared(void function(const const(char*), int)) So how do I define those functions as shared? |
November 29, 2011 Re: Frontend and backend communication | ||||
---|---|---|---|---|
| ||||
I seem to have another problem with the function pointer approach. I am trying to set up a function that would pass the function pointers from C to D, and DMD refuses to compile it: If I have the function pointer struct with D calling convention pointers, like this: struct S_FrontendFunctions { void function(int) SoundPlay; void function() RedrawScreen; } S_FrontendFunctions FrontendFunctions; And I try to set it in D like this: FrontendFunctions.SoundPlay = function(int){}; FrontendFunctions.RedrawScreen = function(){}; And have a function for transferring the pointer from C like this: extern (C): void SetRedrawScreen(void function() RedrawScreen) { FrontendFunctions.RedrawScreen = RedrawScreen; } DMD throws an error in the last function: Error: cannot implicitly convert expression (RedrawScreen) of type extern (C) void function() to void function() Now if I define the two function pointers as extern(C) like this: struct S_FrontendFunctions { extern (C) void function(int) SoundPlay; extern (C) void function() RedrawScreen; } DMD still complains, but this time about when I set the pointers from D directly: Error: cannot implicitly convert expression (__funcliteral3) of type void function() pure nothrow @safe to extern (C) void function() Any ideas about how to make it work from both D and C sides? |
November 29, 2011 Re: Frontend and backend communication | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dainius (GreatEmerald) | On 2011-11-29 12:53, Dainius (GreatEmerald) wrote: > I seem to have another problem with the function pointer approach. I > am trying to set up a function that would pass the function pointers > from C to D, and DMD refuses to compile it: > > If I have the function pointer struct with D calling convention > pointers, like this: > > struct S_FrontendFunctions { > void function(int) SoundPlay; > void function() RedrawScreen; > } > S_FrontendFunctions FrontendFunctions; > > And I try to set it in D like this: > > FrontendFunctions.SoundPlay = function(int){}; > FrontendFunctions.RedrawScreen = function(){}; > > And have a function for transferring the pointer from C like this: > > extern (C): > void SetRedrawScreen(void function() RedrawScreen) > { > FrontendFunctions.RedrawScreen = RedrawScreen; > } > > DMD throws an error in the last function: > > Error: cannot implicitly convert expression (RedrawScreen) of type > extern (C) void function() to void function() > > Now if I define the two function pointers as extern(C) like this: > > struct S_FrontendFunctions { > extern (C) void function(int) SoundPlay; > extern (C) void function() RedrawScreen; > } > > DMD still complains, but this time about when I set the pointers from > D directly: > > Error: cannot implicitly convert expression (__funcliteral3) of > type void function() pure nothrow @safe to extern (C) void function() > > Any ideas about how to make it work from both D and C sides? In stead of doing this: FrontendFunctions.SoundPlay = function(int){}; FrontendFunctions.RedrawScreen = function(){}; Do something like this: extern (C) { void playSound (int) {}; void redrawScreen () {}; } FrontendFunctions.SoundPlay = &playSound; FrontendFunctions.RedrawScreen = &RedrawScreen; -- /Jacob Carlborg |
November 29, 2011 Re: Frontend and backend communication | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | Ah, I see, that makes sense. And it now compiles correctly, thanks. |
Copyright © 1999-2021 by the D Language Foundation