www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - mixin creating an enum

reply Hendrik Renken <funsheep -[no-spam]-gmx.net> writes:
Hi,

i have a struct containing "attributes" identified by int-ids:

struct Foo
{
     char[uint] attributes;
}


I would like to use instead of the int-ids an enum, since the range of 
the ids is strict and cannot change. And i want this struct to be 
generic. So what i need would be a mixin template, which i give the enum 
-members and the template generates the enum.

struct Foo(char[] enummembers)
{
     enum Bar
     {
          mixin enummembers;
     }

     char[][] attributes;
}

Now i would be able to address the attributes by the enum-members. Is 
that possible? Since i couldn't find anythiong close in the docs or in 
the Wiki4D or in the dsource tutorials
May 23 2008
parent Robert Fraser <fraserofthenight gmail.com> writes:
Hendrik Renken wrote:
 Hi,
 
 i have a struct containing "attributes" identified by int-ids:
 
 struct Foo
 {
     char[uint] attributes;
 }
 
 
 I would like to use instead of the int-ids an enum, since the range of 
 the ids is strict and cannot change. And i want this struct to be 
 generic. So what i need would be a mixin template, which i give the enum 
 -members and the template generates the enum.
 
 struct Foo(char[] enummembers)
 {
     enum Bar
     {
          mixin enummembers;
     }
 
     char[][] attributes;
 }
 
 Now i would be able to address the attributes by the enum-members. Is 
 that possible? Since i couldn't find anythiong close in the docs or in 
 the Wiki4D or in the dsource tutorials
struct Foo(char[] enumMembers) { mixin("enum Bar {" ~ enumMembers ~ "}"); char[][Bar] attributes; } ...? If enumMembers isn't in the exact correct form, you'll probably need a string function to get it there.
May 23 2008