Advertisement
Guest User

dmdbug

a guest
Jul 9th, 2014
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 2.34 KB | None | 0 0
  1. void main() {
  2.     auto e = Vec4(5, 3, 3, 1);
  3.  
  4.     // This worked with dmd_2.065.0-0_amd64
  5.     // but does not work with dmd_2.066.0~b2-0_amd64
  6.     auto x = e.xyz; // Error: no property 'xyz' for type 'Vec!4'
  7. }
  8.  
  9. alias Vec3 = Vec!3;
  10. alias Vec4 = Vec!4;
  11.  
  12. struct Vec(int dim) if (3 <= dim && dim <= 4) {
  13.  
  14.     union {
  15.         float[dim] comps;
  16.         struct {
  17.             float x;
  18.             float y;
  19.             float z;
  20.             static if (4 <= dim) float w;
  21.         }
  22.     }
  23.  
  24.     alias r = x;
  25.     alias g = y;
  26.     alias b = z;
  27.     static if (4 <= dim) alias a = w;
  28.  
  29.     static if (dim == 3) {
  30.         @safe pure nothrow
  31.         this(float x, float y, float z) {
  32.             this.comps[0] = x;
  33.             this.comps[1] = y;
  34.             this.comps[2] = z;
  35.         }
  36.     }
  37.  
  38.     static if (dim == 4) {
  39.         @safe pure nothrow
  40.         this(float x, float y, float z, float w) {
  41.             this.comps[0] = x;
  42.             this.comps[1] = y;
  43.             this.comps[2] = z;
  44.             this.comps[3] = w;
  45.         }
  46.     }
  47.  
  48.     auto opDispatch(string components)() const {
  49.         enum string comps_str = components[0] == '_' ? components[1..$] : components;
  50.         Vec!(comps_str.length) result = void;
  51.         foreach (i; StaticRange!(0, comps_str.length))
  52.             result.comps[i] = _component!(this, comps_str[i]);
  53.         return result;
  54.     }
  55. }
  56.  
  57. private template _component(alias vec, char c) {
  58.     static      if(c == 'x') alias _component = vec.x;
  59.     else static if(c == 'y') alias _component = vec.y;
  60.     else static if(c == 'z') alias _component = vec.z;
  61.     else static if(c == 'w') alias _component = vec.w;
  62.     else static if(c == 'r') alias _component = vec.r;
  63.     else static if(c == 'g') alias _component = vec.g;
  64.     else static if(c == 'b') alias _component = vec.b;
  65.     else static if(c == 'a') alias _component = vec.a;
  66.     else static if(c == '0') enum typeof(vec.x) _component = 0;
  67.     else static if(c == '1') enum typeof(vec.x) _component = 1;
  68.     else static if(c == '2') enum typeof(vec.x) _component = 2;
  69.     else static assert(0);
  70. }
  71.  
  72. import std.typecons : TypeTuple;
  73. template StaticRange(int from, int to) if (from <= to) {
  74.     static if (from >= to) {
  75.         alias StaticRange = TypeTuple!();
  76.     } else {
  77.         alias StaticRange = TypeTuple!(from, StaticRange!(from + 1, to));
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement