Array initialization with Struct templates

Daniel N via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Aug 31 01:36:41 PDT 2015


On Monday, 31 August 2015 at 05:38:54 UTC, Jonathan M Davis wrote:
> So, you're going to need to pass it a Chameleon!(float, 
> purpose.POSITIONAL) and a Chameleon!(float, purpose.COLOR_ONLY 
> color), not 6 doubles - either that, or you're going to need to 
> declare a constructor for VertexData which takes 6 doubles or 
> floats and converts them to what's require to assign to its 
> member variables.
>
> - Jonathan M Davis

Or turn Chameleon into a mixin template.

enum Purpose { POSITIONAL, COLOR_ONLY, COLOR_AND_ALPHA, 
GENERIC_TRIPLE, GENERIC_QUAD }
Purpose purpose;

mixin template Chameleon(T, Purpose p) // mixin template
{
     static if (p == Purpose.POSITIONAL) {  // <-- NOT is 
expression
         T  x, y, z;
     } else static if (p == Purpose.COLOR_ONLY) {
         T  r, g, b;
     } else static if (p == Purpose.COLOR_AND_ALPHA) {
         T  r, g, b, a;
     }  else static if (p == Purpose.GENERIC_TRIPLE) {
         T  a, b, c;
     } else static if (p == Purpose.GENERIC_QUAD) {
         T  a, b, c, d;
     }
};

struct VertexData
{
   mixin Chameleon!(float, purpose.POSITIONAL) position;
   mixin Chameleon!(float, purpose.COLOR_ONLY) color;
}

alias Vert = VertexData;

VertexData[] vertices =
[
     Vert(1.0f, 1.0f, 1.0f, 0.0, 0.0, 0.0),
];

void main()
{}


More information about the Digitalmars-d-learn mailing list