www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - enum to flags

reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
so I have a bunch of enums (0 .. n) that i also want to represent 
as flags ( 1 << n foreach n ). Is there anyway to do this other 
than a string mixin?

use like:

enum blah
{
     foo,
     bar,
     baz,
}

alias blahFlags = EnumToFlags!blah;

static assert(blahFlags.baz == 1 << blah.baz)
Sep 28 2015
next sibling parent reply Cauterite <cauterite gmail.com> writes:
On Tuesday, 29 September 2015 at 03:31:44 UTC, Nicholas Wilson 
wrote:
 so I have a bunch of enums (0 .. n) that i also want to 
 represent as flags ( 1 << n foreach n ). Is there anyway to do 
 this other than a string mixin?
You could cheat with operator overloading: enum blah { foo, bar, baz, }; struct EnumToFlags(alias E) { template opDispatch(string Name) { enum opDispatch = 1 << __traits(getMember, E, Name); }; }; alias blahFlags = EnumToFlags!blah; static assert(blahFlags.foo == (1 << blah.foo)); static assert(blahFlags.bar == (1 << blah.bar)); static assert(blahFlags.baz == (1 << blah.baz));
Sep 28 2015
parent Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Tuesday, 29 September 2015 at 06:08:03 UTC, Cauterite wrote:
 On Tuesday, 29 September 2015 at 03:31:44 UTC, Nicholas Wilson 
 wrote:
 so I have a bunch of enums (0 .. n) that i also want to 
 represent as flags ( 1 << n foreach n ). Is there anyway to do 
 this other than a string mixin?
You could cheat with operator overloading: enum blah { foo, bar, baz, }; struct EnumToFlags(alias E) { template opDispatch(string Name) { enum opDispatch = 1 << __traits(getMember, E, Name); }; }; alias blahFlags = EnumToFlags!blah; static assert(blahFlags.foo == (1 << blah.foo)); static assert(blahFlags.bar == (1 << blah.bar)); static assert(blahFlags.baz == (1 << blah.baz));
Cheating is always good. I'l probably add some template constraints. Thanks Nic
Sep 29 2015
prev sibling next sibling parent reply John Colvin <john.loughran.colvin gmail.com> writes:
On Tuesday, 29 September 2015 at 03:31:44 UTC, Nicholas Wilson 
wrote:
 so I have a bunch of enums (0 .. n) that i also want to 
 represent as flags ( 1 << n foreach n ). Is there anyway to do 
 this other than a string mixin?

 use like:

 enum blah
 {
     foo,
     bar,
     baz,
 }

 alias blahFlags = EnumToFlags!blah;

 static assert(blahFlags.baz == 1 << blah.baz)
Answering a slightly different question, I just wanted to be sure you're aware of this old idiom: enum blah { foo = 0b1; bar = 0b10; baz = 0b100; //and so on... } auto fdsa = blah.foo | blah.baz; assert(fdsa & blah.foo); assert(fdsa & blah.baz); assert(!(fdsa & blah.bar));
Sep 29 2015
parent Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Tuesday, 29 September 2015 at 09:18:52 UTC, John Colvin wrote:
 On Tuesday, 29 September 2015 at 03:31:44 UTC, Nicholas Wilson 
 wrote:
 so I have a bunch of enums (0 .. n) that i also want to 
 represent as flags ( 1 << n foreach n ). Is there anyway to do 
 this other than a string mixin?

 use like:

 enum blah
 {
     foo,
     bar,
     baz,
 }

 alias blahFlags = EnumToFlags!blah;

 static assert(blahFlags.baz == 1 << blah.baz)
Answering a slightly different question, I just wanted to be sure you're aware of this old idiom: enum blah { foo = 0b1; bar = 0b10; baz = 0b100; //and so on... } auto fdsa = blah.foo | blah.baz; assert(fdsa & blah.foo); assert(fdsa & blah.baz); assert(!(fdsa & blah.bar));
I am. The reason I wanted was so i could easily reorder them (logical groupings etc. ) . Nic
Sep 29 2015
prev sibling parent Meta <jared771 gmail.com> writes:
On Tuesday, 29 September 2015 at 03:31:44 UTC, Nicholas Wilson 
wrote:
 so I have a bunch of enums (0 .. n) that i also want to 
 represent as flags ( 1 << n foreach n ). Is there anyway to do 
 this other than a string mixin?

 use like:

 enum blah
 {
     foo,
     bar,
     baz,
 }

 alias blahFlags = EnumToFlags!blah;

 static assert(blahFlags.baz == 1 << blah.baz)
Take a look at the BitFlags template as well. It won't create such an enum for you, but will provide a convenient wrapper for using it afterword: http://dlang.org/phobos/std_typecons.html#.BitFlags
Sep 29 2015