digitalmars.D.learn - metaprogramming question
- Graham Fawcett (19/19) Apr 18 2010 Hi folks,
- Justin Spahr-Summers (15/44) Apr 18 2010 You can use some expression tuple magic to accomplish something like
- Philippe Sigaud (24/35) Apr 18 2010 But in this case, you need to know the ELs at compile-time. You can make
- BCS (5/7) Apr 18 2010 The call inside check will coever that for you as long as you don't mind...
- Justin Spahr-Summers (5/39) Apr 18 2010 Yes, sorry. That's what I was trying to do originally, but I couldn't
- Philippe Sigaud (23/26) Apr 18 2010 Sorry to comment on it, then. I'm discovering all this by myself, so I d...
- BCS (17/61) Apr 18 2010 IIRC that should be:
- Philippe Sigaud (22/34) Apr 18 2010 (I guess you meant bool check(EL...)(EL el) in the second stage.)
- fawcett uwindsor.ca (5/64) Apr 19 2010 Remarkable! Thank you to everyone who responded. I can't wait to give
- Daniel Keep (3/3) Apr 18 2010 http://while-nan.blogspot.com/2007/06/wrapping-functions-for-fun-and-pro...
Hi folks, I'd like to wrap a family of C functions that look like this: gboolean fooXXX(arg1, ..., argN, GError** err) Their signatures and arities vary, but they all have a GError** as their last argument. If a function returns false, then 'err' will be set to a relevant Error struct. I would like to write a wrapper that would let me call these functions sort of like this: check!(fooXXX(arg1, ..., argN)) where the 'check' expression would be equivalent to: GError* err; bool ok = fooXXX(arg1, ..., argN, &err); if (!ok) throw new Exception((*err).message); Does D (2.0) offer a metaprogramming technique I could use to accomplish this -- particularly, to inject that 'err' argument into the tail of the fooXXX call? Thanks, Graham
Apr 18 2010
On Mon, 19 Apr 2010 00:12:28 +0000 (UTC), Graham Fawcett <fawcett uwindsor.ca> wrote:Hi folks, I'd like to wrap a family of C functions that look like this: gboolean fooXXX(arg1, ..., argN, GError** err) Their signatures and arities vary, but they all have a GError** as their last argument. If a function returns false, then 'err' will be set to a relevant Error struct. I would like to write a wrapper that would let me call these functions sort of like this: check!(fooXXX(arg1, ..., argN)) where the 'check' expression would be equivalent to: GError* err; bool ok = fooXXX(arg1, ..., argN, &err); if (!ok) throw new Exception((*err).message); Does D (2.0) offer a metaprogramming technique I could use to accomplish this -- particularly, to inject that 'err' argument into the tail of the fooXXX call? Thanks, GrahamYou can use some expression tuple magic to accomplish something like that: bool check(alias func, EL ...)() { GError* err; bool ok = func(EL, &err); if (!ok) throw new Exception((*err).message); return ok; } // used like: check!(fooXXX, arg1, ..., argN); See http://digitalmars.com/d/2.0/template.html#TemplateTupleParameter
Apr 18 2010
On Mon, Apr 19, 2010 at 05:21, Justin Spahr-Summers < Justin.SpahrSummers gmail.com> wrote:You can use some expression tuple magic to accomplish something like that: bool check(alias func, EL ...)() { GError* err; bool ok = func(EL, &err); if (!ok) throw new Exception((*err).message); return ok; } // used like: check!(fooXXX, arg1, ..., argN);But in this case, you need to know the ELs at compile-time. You can make them run-time values that way: bool check(alias func, EL ...)(EL el) { GError* err; bool ok = func(el, &err); if (!ok) throw new Exception((*err).message); return ok; } // used like: check!fooXXX(arg1, ..., argN); Of course, it'd be nice to check the EL at CT to see if they correspond to func parameters types. I don't know if you can do this with C functions, but for D funcs you can add: import std.traits; bool check(alias func, EL ...)(EL el) if (is(ParameterTypeTuple!func[0..$-1] == TypeTuple!(EL, GError*)) { ... It's a bit strict, as it doesn't deal with implicit conversions. Cheers, Philippe
Apr 18 2010
Hello Philippe,Of course, it'd be nice to check the EL at CT to see if they correspond to func parameters types.The call inside check will coever that for you as long as you don't mind getting the error in an odd place. -- ... <IXOYE><
Apr 18 2010
On Mon, 19 Apr 2010 07:28:09 +0200, Philippe Sigaud <philippe.sigaud gmail.com> wrote:On Mon, Apr 19, 2010 at 05:21, Justin Spahr-Summers < Justin.SpahrSummers gmail.com> wrote:Yes, sorry. That's what I was trying to do originally, but I couldn't quite spit it out. Indeed, templating the actual arguments is a horrible idea.You can use some expression tuple magic to accomplish something like that: bool check(alias func, EL ...)() { GError* err; bool ok = func(EL, &err); if (!ok) throw new Exception((*err).message); return ok; } // used like: check!(fooXXX, arg1, ..., argN);But in this case, you need to know the ELs at compile-time. You can make them run-time values that way: bool check(alias func, EL ...)(EL el) { GError* err; bool ok = func(el, &err); if (!ok) throw new Exception((*err).message); return ok; } // used like: check!fooXXX(arg1, ..., argN);
Apr 18 2010
On Mon, Apr 19, 2010 at 07:43, Justin Spahr-Summers < Justin.SpahrSummers gmail.com> wrote:Yes, sorry. That's what I was trying to do originally, but I couldn't quite spit it out. Indeed, templating the actual arguments is a horrible idea.Sorry to comment on it, then. I'm discovering all this by myself, so I don't know if what I generally post is a well-known established practice or a nice trick few people use... (or a brillant idea only a genius could utter, but hey...) Just to complete it, and as it's on my mind: I found one case, yesterday, when you have to pass the args as CT args: filtering on a tuple. Strange idea maybe, but fun to code. That is: auto t = tuple(1, 'a', "abc", 3.14159); auto f = filterTuple!(polymorphicPredicate)(t); // should return a smaller tuple, with 1, 'a',... filtered according to polymPredicate This does not work, because I have to determine the return type of filterTuple as CT, obviously. And this return type in turn depends on the _values_ stored inside the tuple. At CT, I can check the types, but not the values, unless I use them as CT args: auto f = filterTuple!(polymorphicPredicate, t)(); But then, I'm limited by what you can use as a template parameter (not all possible D types are allowed, I think). Maybe as an alias... I'll stop, now, before derailing the thread. I'm trying to use tuples as a sort-of cousin of ranges, by mapping, chaining or truncating them... Philippe
Apr 18 2010
Hello Justin Spahr-Summers,On Mon, 19 Apr 2010 00:12:28 +0000 (UTC), Graham Fawcett <fawcett uwindsor.ca> wrote:IIRC that should be: check!(fooXXX, argType1, ..., argTypeN)(arg1, ..., argN);Hi folks, I'd like to wrap a family of C functions that look like this: gboolean fooXXX(arg1, ..., argN, GError** err) Their signatures and arities vary, but they all have a GError** as their last argument. If a function returns false, then 'err' will be set to a relevant Error struct. I would like to write a wrapper that would let me call these functions sort of like this: check!(fooXXX(arg1, ..., argN)) where the 'check' expression would be equivalent to: GError* err; bool ok = fooXXX(arg1, ..., argN, &err); if (!ok) throw new Exception((*err).message); Does D (2.0) offer a metaprogramming technique I could use to accomplish this -- particularly, to inject that 'err' argument into the tail of the fooXXX call? Thanks, GrahamYou can use some expression tuple magic to accomplish something like that: bool check(alias func, EL ...)() { GError* err; bool ok = func(EL, &err); if (!ok) throw new Exception((*err).message); return ok; } // used like: check!(fooXXX, arg1, ..., argN);See http://digitalmars.com/d/2.0/template.html#TemplateTupleParameterOr you can mangel that a bit: template check(alias func) { bool check(EL ...)() { GError* err; bool ok = func(EL, &err); if (!ok) throw new Exception((*err).message); return ok; } } that should allow you to call it like: check!(fooXXX)(arg1, ..., argN); -- ... <IXOYE><
Apr 18 2010
On Mon, Apr 19, 2010 at 07:31, BCS <none anon.com> wrote:Or you can mangel that a bit: template check(alias func) { bool check(EL ...)() { GError* err; bool ok = func(EL, &err); if (!ok) throw new Exception((*err).message); return ok; } } that should allow you to call it like: check!(fooXXX)(arg1, ..., argN);(I guess you meant bool check(EL...)(EL el) in the second stage.) Yes, this example is even more powerful. The version I posted already allows you to write check!fun(params); // no need to give explictly the EL, the compiler wil deduce them for you but yours allow you to 'store' the checking of fun and then to use it for any parameter typetuple. Maybe not useful for the OP, but mighty handy in some other cases. I discovered this trick a few weeks ago and, though a bitch to code sometimes, it's very nice to have. Usage: alias check!foo fooCheck; // checkFoo is a templated function, so it's _not_ a function. You cannot assign it to a variable. // use alias instead. // some other code // auto check1 = fooCheck(1,2,3); // auto check2 = fooCheck("a","b", 2.34); It's particularly useful when foo is polymorphic. Again, not interesting for the OP, but very fun. Philippe
Apr 18 2010
On 10-04-19 01:31 AM, BCS wrote:Hello Justin Spahr-Summers,Remarkable! Thank you to everyone who responded. I can't wait to give these a try tonight. :) Cheers, GrahamOn Mon, 19 Apr 2010 00:12:28 +0000 (UTC), Graham Fawcett <fawcett uwindsor.ca> wrote:IIRC that should be: check!(fooXXX, argType1, ..., argTypeN)(arg1, ..., argN);Hi folks, I'd like to wrap a family of C functions that look like this: gboolean fooXXX(arg1, ..., argN, GError** err) Their signatures and arities vary, but they all have a GError** as their last argument. If a function returns false, then 'err' will be set to a relevant Error struct. I would like to write a wrapper that would let me call these functions sort of like this: check!(fooXXX(arg1, ..., argN)) where the 'check' expression would be equivalent to: GError* err; bool ok = fooXXX(arg1, ..., argN, &err); if (!ok) throw new Exception((*err).message); Does D (2.0) offer a metaprogramming technique I could use to accomplish this -- particularly, to inject that 'err' argument into the tail of the fooXXX call? Thanks, GrahamYou can use some expression tuple magic to accomplish something like that: bool check(alias func, EL ...)() { GError* err; bool ok = func(EL, &err); if (!ok) throw new Exception((*err).message); return ok; } // used like: check!(fooXXX, arg1, ..., argN);See http://digitalmars.com/d/2.0/template.html#TemplateTupleParameterOr you can mangel that a bit: template check(alias func) { bool check(EL ...)() { GError* err; bool ok = func(EL, &err); if (!ok) throw new Exception((*err).message); return ok; } } that should allow you to call it like: check!(fooXXX)(arg1, ..., argN);
Apr 19 2010
http://while-nan.blogspot.com/2007/06/wrapping-functions-for-fun-and-profit.html It shouldn't be too difficult to inject an extra parameter; just add it to the end of the call to the wrapped function after args.
Apr 18 2010