www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to convert C macro that calls function returning a struct*?

reply Rick Mann <rmann-d-lang latencyzero.com> writes:
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!
Jan 29 2007
next sibling parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
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
Jan 30 2007
prev sibling parent Lionello Lunesu <lio lunesu.remove.com> writes:
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.
Jan 30 2007