Thread overview | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
April 19, 2010 metaprogramming question | ||||
---|---|---|---|---|
| ||||
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 |
April 19, 2010 Re: metaprogramming question | ||||
---|---|---|---|---|
| ||||
Posted in reply to Graham Fawcett | 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, > Graham 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); See http://digitalmars.com/d/2.0/template.html#TemplateTupleParameter |
April 19, 2010 Re: metaprogramming question | ||||
---|---|---|---|---|
| ||||
Posted in reply to Graham Fawcett | 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. |
April 19, 2010 Re: metaprogramming question | ||||
---|---|---|---|---|
| ||||
Posted in reply to Justin Spahr-Summers Attachments:
| 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 |
April 19, 2010 Re: metaprogramming question | ||||
---|---|---|---|---|
| ||||
Posted in reply to Justin Spahr-Summers | Hello Justin Spahr-Summers, > 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, >> Graham > 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); IIRC that should be: check!(fooXXX, argType1, ..., argTypeN)(arg1, ..., argN); > See http://digitalmars.com/d/2.0/template.html#TemplateTupleParameter > 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); -- ... <IXOYE>< |
April 19, 2010 Re: metaprogramming question | ||||
---|---|---|---|---|
| ||||
Posted in reply to Philippe Sigaud | 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>< |
April 19, 2010 Re: metaprogramming question | ||||
---|---|---|---|---|
| ||||
Posted in reply to Philippe Sigaud | 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:
>
> > 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);
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.
|
April 19, 2010 Re: metaprogramming question | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS Attachments:
| 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
|
April 19, 2010 Re: metaprogramming question | ||||
---|---|---|---|---|
| ||||
Posted in reply to Justin Spahr-Summers Attachments:
| 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 |
April 19, 2010 Re: metaprogramming question | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | On 10-04-19 01:31 AM, BCS wrote:
> Hello Justin Spahr-Summers,
>
>> 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,
>>> Graham
>> 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);
>
> IIRC that should be:
>
> check!(fooXXX, argType1, ..., argTypeN)(arg1, ..., argN);
>
>> See http://digitalmars.com/d/2.0/template.html#TemplateTupleParameter
>>
>
> 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);
>
Remarkable! Thank you to everyone who responded. I can't wait to give
these a try tonight. :)
Cheers,
Graham
|
Copyright © 1999-2021 by the D Language Foundation