www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Convert "C Function Pointer" to D?

reply Robert <robert.muench robertmuench.de> writes:
Hi, I have something like this:

struct ext_api {
	int version;
	void *(*make_block)(u32 size);
	void *(*make_string)(u32 size, int uni);
	u32 *(*map_words)(REBSER *ser);
	u32 (*find_word)(u32 *words, u32 word);
	int (*series_info)(REBSER *ser, REBCNT what);
	int (*get_char)(REBSER *ser, u32 index);
	u32 (*set_char)(REBSER *ser, u32 index, u32 chr);
	int (*get_value)(REBSER *ser, u32 index, RXIARG *val);
	int (*set_value)(REBSER *ser, u32 index, RXIARG val, int type);
	int (*get_string)(REBSER *ser, u32 index, void **str);
}

How do I convert such a struct to D? I will get a ext_api pointer from 
a C compiled program and use this pointer via the struct to call the C 
functions.


-- 
Robert M. Münch
http://www.robertmuench.de
Jun 03 2010
next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Thu, 03 Jun 2010 14:28:21 -0400, Robert <robert.muench robertmuench.de>  
wrote:

 Hi, I have something like this:

 struct ext_api {
 	int version;
 	void *(*make_block)(u32 size);
 	void *(*make_string)(u32 size, int uni);
 	u32 *(*map_words)(REBSER *ser);
 	u32 (*find_word)(u32 *words, u32 word);
 	int (*series_info)(REBSER *ser, REBCNT what);
 	int (*get_char)(REBSER *ser, u32 index);
 	u32 (*set_char)(REBSER *ser, u32 index, u32 chr);
 	int (*get_value)(REBSER *ser, u32 index, RXIARG *val);
 	int (*set_value)(REBSER *ser, u32 index, RXIARG val, int type);
 	int (*get_string)(REBSER *ser, u32 index, void **str);
 }

}
 How do I convert such a struct to D? I will get a ext_api pointer from a  
 C compiled program and use this pointer via the struct to call the C  
 functions.

Like I wrote above. Of course, if you wish to make it more d-like, change void *(*make_block)(u32 size); to void * function(u32 size) make_block; Of course, you still need extern(C) around the whole thing to have C linkage. -Steve
Jun 03 2010
prev sibling next sibling parent Robert Clipsham <robert octarineparrot.com> writes:
On 03/06/10 19:28, Robert wrote:
 Hi, I have something like this:

 struct ext_api {
 int version;
 void *(*make_block)(u32 size);
 void *(*make_string)(u32 size, int uni);
 u32 *(*map_words)(REBSER *ser);
 u32 (*find_word)(u32 *words, u32 word);
 int (*series_info)(REBSER *ser, REBCNT what);
 int (*get_char)(REBSER *ser, u32 index);
 u32 (*set_char)(REBSER *ser, u32 index, u32 chr);
 int (*get_value)(REBSER *ser, u32 index, RXIARG *val);
 int (*set_value)(REBSER *ser, u32 index, RXIARG val, int type);
 int (*get_string)(REBSER *ser, u32 index, void **str);
 }

 How do I convert such a struct to D? I will get a ext_api pointer from a
 C compiled program and use this pointer via the struct to call the C
 functions.

struct ext_api { int version; void* function(u32 size) make_block; void* function(u32, size, int uni) make_string; u32* function(REBSER* ser) map_words; /* etc */ } Make sure you define u32 etc before the struct.
Jun 03 2010
prev sibling parent Eric Poggel <dnewsgroup yage3d.net> writes:
On 6/3/2010 2:28 PM, Robert wrote:
 Hi, I have something like this:

 struct ext_api {
 int version;
 void *(*make_block)(u32 size);
 void *(*make_string)(u32 size, int uni);
 u32 *(*map_words)(REBSER *ser);
 u32 (*find_word)(u32 *words, u32 word);
 int (*series_info)(REBSER *ser, REBCNT what);
 int (*get_char)(REBSER *ser, u32 index);
 u32 (*set_char)(REBSER *ser, u32 index, u32 chr);
 int (*get_value)(REBSER *ser, u32 index, RXIARG *val);
 int (*set_value)(REBSER *ser, u32 index, RXIARG val, int type);
 int (*get_string)(REBSER *ser, u32 index, void **str);
 }

 How do I convert such a struct to D? I will get a ext_api pointer from a
 C compiled program and use this pointer via the struct to call the C
 functions.

You might take a look at the Derelict project (dsource.org/projects/derelict) to see a large amount of C code converted to D. I think http://dsource.org/projects/derelict/browser/trunk/DerelictVorbis/derel ct/ogg/vorbisfile.d had something very similar to this, line 52.
Jun 03 2010