Summary: Compile-time flags generator
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos

--- Comment #0 from 2011-11-13 20:13:10 PST ---
A basic version of a compile-time enum-based bit flags:

import std.traits: isUnsigned;
import std.string: split;
import std.conv: text;
import std.algorithm: canFind;

bool isReservedWord(in string w) {
    string[] reservedWords = "abstract alias align asm
    assert auto body bool break byte case cast catch cdouble cent cfloat
    char class const continue creal dchar debug default delegate delete
    deprecated do double else enum export extern false final finally
    float for foreach foreach_reverse function goto idouble if ifloat
    immutable import in inout int interface invariant ireal is lazy long
    macro mixin module new nothrow null out override package pragma
    private protected public pure real ref return scope shared short
    static struct super switch synchronized template this throw true try
    typedef typeid typeof ubyte ucent uint ulong union unittest ushort
    version void volatile wchar while with __FILE__ __LINE__ __gshared
    __thread __traits".split();
    return canFind(reservedWords, w);

string bitFlags(BaseType)(string enumName, string enumItems)
if (isUnsigned!BaseType) {
    assert (enumItems.split().length < (BaseType.sizeof * 8),
            text("BaseType (", BaseType.stringof,
                 ") doesn't have enough bits to represent all the
    string result = text("enum ", enumName, " : ", BaseType.stringof, " {\n");
    foreach (i, flag; enumItems.split()) {
        assert(!isReservedWord(flag), text("enum '", flag, "' is a D
        result ~= text("  ", flag, " = (1 << ", i, "),\n");
    return result ~ "}";

// example usage:
mixin(bitFlags!size_t("MyEnum", "A B C D"));
//mixin(bitFlags!ulong("MyEnum", "A B C D")); // writeln bug

void main() {
    import std.stdio;

This code lacks many handy features, including safety features when you combine
flags with MyEnum.A|MyEnum.B, but for a basic usage, if you have many flags, it
seems better than not using it.

Note: in this simple implementation the first enum (here MyENum.A) equals to
2^0 = 1, so there is no enum for zero. Change this if you want.

I'd probably like a function like isReservedWord() too in Phobos. Here the
reservedWords array is not static because I need to call isReservedWord() at
compile time too.

Even if flags are sometimes more complex than the ones this function generates,
I think something like this (but improved) is useful in Phobos for the common
basic situations.

Configure issuemail:
------- You are receiving this mail because: -------

Reply via email to