Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
February 06, 2013 best D way to port C style array of void* | ||||
---|---|---|---|---|
| ||||
Hi All, I've some old C code which I'm porting to D. It's a learning exercise so I don't want to just wrap the C lib. I have an array of void* and an array of callbacks that take void* pointers for user data. I'm wondering what is a good way to port this into D and avoid the void*. My C++ version uses std::function<> for the callbacks and functors. I'm using std::vector<boost::any> For the array of void*. Maybe it's not the best approach but it's my best efforts. For the D port I'd like to improve on the C++ approach and I'd love to know a better way to do it. * Could I replace the boost::any with an array of Variant from std.variant? * Can I assign a struct with opCall() to a function pointer, similar to how std::function<> can take a struct with an operator(). * Should I forget functors and use a callback that takes a std.variant (or whatever the boost::any like thing in D is) instead of void*? * What would be the best approach do you think to replace use of void* like this. Thanks, Stewart |
February 06, 2013 Re: best D way to port C style array of void* | ||||
---|---|---|---|---|
| ||||
Posted in reply to estew | On Tue, 05 Feb 2013 22:16:17 -0500, estew <estewh@gmail.com> wrote: > Hi All, > > I've some old C code which I'm porting to D. It's a learning exercise so I don't want to just wrap the C lib. > > I have an array of void* and an array of callbacks that take void* pointers for user data. > > I'm wondering what is a good way to port this into D and avoid the void*. My C++ version uses std::function<> for the callbacks and functors. I'm using std::vector<boost::any> For the array of void*. > > Maybe it's not the best approach but it's my best efforts. For the D port I'd like to improve on the C++ approach and I'd love to know a better way to do it. > > * Could I replace the boost::any with an array of Variant from std.variant? That is probably what I would recommend. Although I would consider altering the design to avoid this. D has a much better type system than C or C++. > * Can I assign a struct with opCall() to a function pointer, similar to how std::function<> can take a struct with an operator(). > > * Should I forget functors and use a callback that takes a std.variant (or whatever the boost::any like thing in D is) instead of void*? Use a delegate. A delegate is a function call with a context pointer. No need to specify the type of the pointer, it's whatever type it needs to be. You can create a delegate just about anywhere. It can be a member function of a class or struct, or an internal function, or a lambda function (D supports closures). e.g.: import std.stdio; void callit(void delegate() dg) { // call delegate with context pointer dg(); } void main() { int x; auto dg = ()=>writeln(++x); // create a delegate using a lambda function callit(dg); callit(dg); callit(dg); callit(dg); } -Steve |
February 06, 2013 Re: best D way to port C style array of void* | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Tue, 05 Feb 2013 23:17:59 -0500, Steven Schveighoffer <schveiguy@yahoo.com> wrote:
> e.g.:
>
> import std.stdio;
>
> void callit(void delegate() dg)
> {
> // call delegate with context pointer
> dg();
> }
>
> void main()
> {
> int x;
> auto dg = ()=>writeln(++x); // create a delegate using a lambda function
> callit(dg);
> callit(dg);
> callit(dg);
> callit(dg);
> }
Forgot to say, the output here would be:
1
2
3
4
-Steve
|
February 06, 2013 Re: best D way to port C style array of void* | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | Thanks for your help, much appreciated. I'm using the delegates now for the callbacks. At your suggestion I'm looking for a good way to change the design. I agree, the D type system is way superior to that of C. Starting to get into mixins too, although I'm a bit wary of them. I know they're not macros, but I once had to debug (and eventually rewrote) ~5000 lines of macros that calculate interpolated XYZ values...still recovering :) Cheers, Stewart |
February 06, 2013 Re: best D way to port C style array of void* | ||||
---|---|---|---|---|
| ||||
Posted in reply to estew | On 2013-02-06 06:49, estew wrote: > Thanks for your help, much appreciated. > > I'm using the delegates now for the callbacks. At your suggestion I'm > looking for a good way to change the design. I agree, the D type system > is way superior to that of C. I would suggest you try to replace the array with something more typed, if possible. -- /Jacob Carlborg |
February 06, 2013 Re: best D way to port C style array of void* | ||||
---|---|---|---|---|
| ||||
Posted in reply to estew | Just a note. Be careful with void* pointers. In D it have a 32bit length on x86 machines and 64bit length on x86_64 machines. Probaly in C with different compilers it's may vary. |
Copyright © 1999-2021 by the D Language Foundation