Thread overview
How to convert C macro that calls function returning a struct*?
Jan 30, 2007
Rick Mann
Jan 30, 2007
Lionello Lunesu
January 30, 2007
I've run into a problem creating D bindings for the Mac OS X Carbon API. In particular, I'm trying to find a way to implement the following C macro (context provided):

typedef const struct __CFString * CFStringRef;
#define CFSTR(cStr)  __CFStringMakeConstantString("" cStr "")
CFStringRef  __CFStringMakeConstantString(const char *cStr);
#define kSomeCFStringConstant      CFSTR("myCFStringConstantValue");

I tried doing this:

struct __CFString {};
typedef const __CFString* CFStringRef;
template CFSTR(char[] inS)
{
	const CFStringRef CFSTR = __CFStringMakeConstantString(inS.ptr);
}
extern (C) CFStringRef __CFStringMakeConstantString(char* cStr);

But I get the following errors:

src/d/darbon/examples/GrabBag/GrabBag.d:49: Error: non-constant expression (__CFStringMakeConstantString)("darbon.grabbag.demoview")
../../src/d/macos/corefoundation/CFString.d:152: Error: non-constant expression (__CFStringMakeConstantString)("darbon.grabbag.demoview")


The first occurs on this line:

	const CFStringRef viewID			=	CFSTR!("darbon.grabbag.demoview");

and the second error is referring to the line inside the template definition.

I'm at a loss. I need to be able to define constants that effectively call __CFStringMakeConstantString(), and I need to be able to call it directly. Any suggestions? (GDC 0.27/DMD 1.00).

Thanks!

January 30, 2007
Rick Mann wrote:
> I've run into a problem creating D bindings for the Mac OS X Carbon API. In particular, I'm trying to find a way to implement the following C macro (context provided):
> 
> typedef const struct __CFString * CFStringRef;
> #define CFSTR(cStr)  __CFStringMakeConstantString("" cStr "")
> CFStringRef  __CFStringMakeConstantString(const char *cStr);	
> #define kSomeCFStringConstant      CFSTR("myCFStringConstantValue");

I used this:
struct __CFString { }
typedef __CFString* CFStringRef;
typedef __CFString* CFMutableStringRef;

extern (C) CFStringRef __CFStringMakeConstantString(char *cStr);
alias __CFStringMakeConstantString CFSTR; // only for string literals


Generally speaking C macros map into D functions, but for simple
renames one can make use of "alias" to avoid the extra object code.

It only works for ASCII strings, by the way, for regular D strings
you need to specify using the UTF-8 encoding to Core Foundation...

--anders
January 30, 2007
Rick Mann wrote:
> I've run into a problem creating D bindings for the Mac OS X Carbon API. In particular, I'm trying to find a way to implement the following C macro (context provided):
> 
> typedef const struct __CFString * CFStringRef;
> #define CFSTR(cStr)  __CFStringMakeConstantString("" cStr "")
> CFStringRef  __CFStringMakeConstantString(const char *cStr);	
> #define kSomeCFStringConstant      CFSTR("myCFStringConstantValue");
> 
> I tried doing this:
> 
> struct __CFString {};
> typedef const __CFString* CFStringRef;
> template CFSTR(char[] inS)
> {
> 	const CFStringRef CFSTR = __CFStringMakeConstantString(inS.ptr);
> }
> extern (C) CFStringRef __CFStringMakeConstantString(char* cStr);
> 
> But I get the following errors:
> 
> src/d/darbon/examples/GrabBag/GrabBag.d:49: Error: non-constant expression (__CFStringMakeConstantString)("darbon.grabbag.demoview")
> ../../src/d/macos/corefoundation/CFString.d:152: Error: non-constant expression (__CFStringMakeConstantString)("darbon.grabbag.demoview")
> 
> 
> The first occurs on this line:
> 
> 	const CFStringRef viewID			=	CFSTR!("darbon.grabbag.demoview");
> 
> and the second error is referring to the line inside the template definition.
> 
> I'm at a loss. I need to be able to define constants that effectively call __CFStringMakeConstantString(), and I need to be able to call it directly. Any suggestions? (GDC 0.27/DMD 1.00).
> 
> Thanks!
> 

Try dropping the const from the typedef.

L.