Questions about opAssign alternate and template quality
nobody
nobody at mailinator.com
Fri Aug 11 10:16:31 PDT 2006
Oskar Linde wrote:
>> My second question is mostly an open invitation to make suggestions
>> for improving my template writing style. In particular I am suspicious
>> that there is a way to take better advantage of templates and not
>> write out all 60 permutations.
>>
>> So here is the actual code. I only included the RGB permutations to
>> keep it as short as possible.
>
> Implicit function template instantiation and some static ifs can help
> you. Unfortunately, IFTI isn't yet implemented for member functions, but
> you can make a free function like:
> void pixAssign(A,B)(out A A, in B b) {
> static if (is(a.a) && is(b.a)) {
> // Alpha
> a.a = b.a;
> } else static if(is(a.a)) {
> a.a = typeof(a.a).max;
> }
> static if (is(a.r) && is(b.r)) {
> // RGB->RGB
> auto r = b.r;
> auto g = b.g;
> auto b = b.b;
> a.r = r;
> a.g = g;
> a.b = b;
> } else static if (is(a.h) && is(b.h)) {
> // HLS -> HLS
> auto h = b.h;
> auto l = b.l;
> auto s = b.s;
> a.h = h;
> a.l = l;
> a.s = s;
> } else static if(is(a.r)) {
> // HLS -> RGB
> assert(0,"not implemented.");
> } else static if(is(b.r)) {
> // RGB -> HLS
> assert(0,"not implemented.");
> }
> }
>
> Which should take care of all 3600 combinations of opAssign. is(x.y) is
> there just to check for the existence of a field y in x.
Thanks for the great post! Your pixAssign function is a great example and helped
me better understand static if, is and auto. It might take awhile before I can
see why there would be trouble with trying to use a mixin to avoid paramatized
calls to opAssign.
More information about the Digitalmars-d-learn
mailing list