Jump to page: 1 2
Thread overview
converting function pointers
Dec 14, 2004
Tyro
Dec 14, 2004
Russ Lewis
Dec 14, 2004
John Reimer
Dec 14, 2004
John Reimer
Dec 14, 2004
Russ Lewis
Dec 14, 2004
Russ Lewis
Dec 14, 2004
John Reimer
Dec 14, 2004
Tyro
Dec 14, 2004
John Reimer
Dec 14, 2004
Tyro
Dec 14, 2004
John Reimer
Dec 15, 2004
David Medlock
Dec 15, 2004
Russ Lewis
Dec 15, 2004
John Reimer
Dec 15, 2004
Russ Lewis
Dec 15, 2004
Tyro
Dec 15, 2004
David Medlock
Dec 15, 2004
Russ Lewis
Dec 15, 2004
John Reimer
December 14, 2004
How does one convert a C function pointer D?

int   (*u.sfree) ();
void* (*salloc)  ();

int u.sfree(arg1, arg2, arg3)
{ /* do something meaningful */ }

void* salloc(arg1, arg2)
{ /* implementation implementation */ }

==========================
Does this suffice?

int deligate () free;
void* delegate () alloc;

free  = &u.sfree;
alloc = &salloc;

or is there something more that I'm missing?

Thanks,
Andrew


December 14, 2004
Tyro wrote:
> How does one convert a C function pointer D?

I ran a little testcase, and was surprised to find that you can't just take the address of an extern(C) function and save it in a function pointer.  (Is this a calling convention problem, or something?)

However, it's easy enough to define a trivial wrapper which allows you to call the extern(C) function.  Here's example code:

> import std.stdio;
>  extern(C) void foo(int i,char c) { writef("%d, %s\n", i,c); }
>  void main() {
>   void function(int,char) fp = function void(int i,char c) { return foo(i,c); };
>   void delegate(int,char) dg = delegate void(int i,char c) { return foo(i,c); };
>    fp(1,'a');
>   dg(2,'b');
> }

December 14, 2004
Russ Lewis wrote:
> Tyro wrote:
> 
>> How does one convert a C function pointer D?
> 
> 
> I ran a little testcase, and was surprised to find that you can't just take the address of an extern(C) function and save it in a function pointer.  (Is this a calling convention problem, or something?)
> 
> However, it's easy enough to define a trivial wrapper which allows you to call the extern(C) function.  Here's example code:
> 
>> import std.stdio;
>>  extern(C) void foo(int i,char c) { writef("%d, %s\n", i,c); }
>>  void main() {
>>   void function(int,char) fp = function void(int i,char c) { return foo(i,c); };
>>   void delegate(int,char) dg = delegate void(int i,char c) { return foo(i,c); };
>>    fp(1,'a');
>>   dg(2,'b');
>> }
> 
> 


Actually, it's entirely possible to use the function pointers directly.  It probably comes down to appropriate use of the extern(C) for compatible assignment of function pointers to functions.  You can also assign a function to a class function variable.  Here's an example:

# import std.stdio;
#
# extern (C) void foo1(int i, char c)
# {
#    writefln("%d, %s", i,c);
# }
#
# class Class
# {
#    this() {  this.foo = &foo1; }
#	
#    extern (C) void function(int,char) foo;
# }
#
# extern(C) void function(int, char) func;
#
# void main()
# {	
#    Class A = new Class;
#    func = &foo1;
#
#    func(2, 'c');
#    A.foo(2, 'c');
# }
	
Outputs:

2, c
2, c

Hope that helps,

John
December 14, 2004
I should add that the great thing about being able to assign a extern(C) function to a class function variable is that you can directly interface with C functions from toolkits and make them look like class methods. It really beats the idea of a function wrapper, I think.

- John
December 14, 2004
John Reimer wrote:
> Actually, it's entirely possible to use the function pointers directly.  It probably comes down to appropriate use of the extern(C) for compatible assignment of function pointers to functions.  You can also assign a function to a class function variable.

Y'know, that's exactly what I tried at first, but I did it inside a function body (as a local variable).  That didn't work, but doing it as a global did.  (I'm assuming you've tried this already in a class?)

I'll write this up as a bug in the other NG.

December 14, 2004
John Reimer wrote:
> I should add that the great thing about being able to assign a extern(C) function to a class function variable is that you can directly interface with C functions from toolkits and make them look like class methods. It really beats the idea of a function wrapper, I think.

Indeed.

December 14, 2004
Russ Lewis wrote:
> John Reimer wrote:
> 
>> Actually, it's entirely possible to use the function pointers directly.  It probably comes down to appropriate use of the extern(C) for compatible assignment of function pointers to functions.  You can also assign a function to a class function variable.
> 
> 
> Y'know, that's exactly what I tried at first, but I did it inside a function body (as a local variable).  That didn't work, but doing it as a global did.  (I'm assuming you've tried this already in a class?)
> 
> I'll write this up as a bug in the other NG.
> 

Yeah, I did try that too... but hadn't thought there was a solution until you mentioned it; then I got the idea that I gave in answer to your bug forum post.

Yes, declaring extern(C) inside a class works fine.  Although, I found that you should only declare /variables/ extern(C) in a class (not functions).  You can declare functions extern(C) also, but you'll never get your program linked because the name resolution will prepend the class name to the function name to create the fully qualified name for your function... one that doesn't won't be reachable :-(

- John
December 14, 2004
Tyro wrote:
> How does one convert a C function pointer D?
> 
> int   (*u.sfree) ();
> void* (*salloc)  ();
> 
> int u.sfree(arg1, arg2, arg3)
> { /* do something meaningful */ }
> 
> void* salloc(arg1, arg2)
> { /* implementation implementation */ }
> 
> ==========================
> Does this suffice?
> 
> int deligate () free;
> void* delegate () alloc;
> 
> free  = &u.sfree;
> alloc = &salloc;
> 
> or is there something more that I'm missing?
> 
> Thanks,
> Andrew
> 
> 

John & Russ, thank you both very much for your assistance on this.

Andrew
December 14, 2004
Tyro wrote:
> How does one convert a C function pointer D?
> 
> int   (*u.sfree) ();
> void* (*salloc)  ();
> 
> int u.sfree(arg1, arg2, arg3)
> { /* do something meaningful */ }
> 
> void* salloc(arg1, arg2)
> { /* implementation implementation */ }
> 
> ==========================
> Does this suffice?
> 
> int deligate () free;
> void* delegate () alloc;
> 
> free  = &u.sfree;
> alloc = &salloc;
> 
> or is there something more that I'm missing?
> 
> Thanks,
> Andrew
> 
> 

I just realized that you probably weren't talking about extern(C) functions.  Russ and I got off on a tangent on that one.  You are probably just querying about the difference about C function pointers and D function pointers.

I guess the main difference then is that D just uses the "function" keyword (no need for extern(C) except in some specialized situations as Russ and I were talking about) instead of C's (*)() format.

delegates are along the same lines but are used for a slightly different purpose in D.  Their usefulness pertains more to object oriented programming and class methods... although they have similar uses to D's function attribute; in fact they seem to be interchangeable in some respects.  The docs define some of the important differences for where either can be used.  I think Walter has been considering merging function and delegate syntaxes for quite awhile.

Later,

John
December 14, 2004
John Reimer wrote:
> Tyro wrote:
> 
>> How does one convert a C function pointer D?
>>
>> int   (*u.sfree) ();
>> void* (*salloc)  ();
>>
>> int u.sfree(arg1, arg2, arg3)
>> { /* do something meaningful */ }
>>
>> void* salloc(arg1, arg2)
>> { /* implementation implementation */ }
>>
>> ==========================
>> Does this suffice?
>>
>> int deligate () free;
>> void* delegate () alloc;
>>
>> free  = &u.sfree;
>> alloc = &salloc;
>>
>> or is there something more that I'm missing?
>>
>> Thanks,
>> Andrew
>>
>>
> 
> I just realized that you probably weren't talking about extern(C) functions.  Russ and I got off on a tangent on that one.  You are probably just querying about the difference about C function pointers and D function pointers.
> 
> I guess the main difference then is that D just uses the "function" keyword (no need for extern(C) except in some specialized situations as Russ and I were talking about) instead of C's (*)() format.
> 
> delegates are along the same lines but are used for a slightly different purpose in D.  Their usefulness pertains more to object oriented programming and class methods... although they have similar uses to D's function attribute; in fact they seem to be interchangeable in some respects.  The docs define some of the important differences for where either can be used.  I think Walter has been considering merging function and delegate syntaxes for quite awhile.
> 
> Later,
> 
> John

Actually I just came home and didn't get a chance to read your responses as yet. I just did a quick scan to see who had responded to my post and gave thanks where it was due.

I am a purist, so I believe that D programs should be written in D. Maybe at the onset (just to get some functionality that is not yet available in the language) we can wrap up C functions for use in our programs until the "D-way" is devised. Then those functions should be reimplemented in accordance with the way things are done in D.

To this end I was simply wondering how C function pointers differ from D function pointers (as you stated above) and the proper way to "D"-up a function pointer or delegate to accomplish the same thing the C function pointer would.

Cheers,
Andrew
« First   ‹ Prev
1 2