Is flags enum needed in Phobos?

Era Scarecrow rtcvb32 at yahoo.com
Sat Oct 6 12:50:51 PDT 2012


On Wednesday, 26 September 2012 at 19:11:37 UTC, bearophile wrote:
> I think it's a good idea to have a well written EnumFlags data 
> structure in Phobos. In C# this feature is even built-in. So 
> issue 6946 is not closing, unless Andrei or Walter decide it's 
> a bad idea to have something like this in Phobos.

  I've written one I believe if sufficient and am using in my 
current project. I'll add more to my branch as time goes on.

https://github.com/rtcvb32/Side-Projects

  example in documentation

[code]
   enum ETEST {none = 0, one = 1, two = 2, three = 3, four = 4}
   handle_flags!(ETEST, int) ftest;

   ftest.set_flag(ETEST.two);	//check flag setting
   assert(ftest.state == 2);	//int and enum compares.
   assert(ftest.state == ETEST.two);

   ftest.set_flag(ETEST.four);	//set second flag
   assert(ftest.state == 6);	//2+4, no 6 enum.

   //Flags can also encompass multiple bits.
   //in this case all bits returned with an
   //AND must match the flag.
   ftest.state = 1;	
   assert(!ftest.checkAll(one, two, three));  //can't be true, 
only 1 is there
   ftest.state = 3;
   assert(ftest.checkAll(one, two, three));   //must be true, 
since 1+2 includes 3.
[/code]

  Feel free to scrutinize and offer suggestions.

  Although it doesn't appear to be mentioned, you can also 
shorthand the flag with your alias. So ftest.one would be the 
same as ETEST.one or ftest.Enum.one. I recommend using with when 
you are using such flags.


More information about the Digitalmars-d mailing list