1 module jcli.core.utils; 2 3 import std.traits : Unqual; 4 5 string toFlagsString(Flags)(Flags flags) 6 { 7 import std.array; 8 import std.bitmanip; 9 10 alias ReadableFlags = Unqual!Flags; 11 12 auto result = appender!string; 13 bool isFirst = true; 14 foreach (bitIndex; flags.bitsSet) 15 { 16 Switch: switch (bitIndex) 17 { 18 default: assert(0); 19 static foreach (memberName; __traits(allMembers, ReadableFlags)) 20 { 21 static if (__traits(getMember, ReadableFlags, memberName).bitsSet.length == 1) 22 { 23 case __traits(getMember, ReadableFlags, memberName).bitsSet.front: 24 { 25 if (!isFirst) 26 result ~= " | "; 27 else 28 isFirst = false; 29 // result ~= ReadableFlags.stringof ~ "." ~ memberName; 30 result ~= memberName; 31 break Switch; 32 } 33 } 34 } 35 } 36 } 37 return result[]; 38 } 39 40 template FlagsHelpers(TFlags) 41 { 42 @safe nothrow @nogc pure const 43 { 44 bool doesNotHave(TFlags a, TFlags b) 45 { 46 return (a & b) != b; 47 } 48 bool has(TFlags a, TFlags b) 49 { 50 return (a & b) == b; 51 } 52 bool hasEither(TFlags a, TFlags b) 53 { 54 return (a & b) != 0; 55 } 56 bool doesNotHaveEither(TFlags a, TFlags b) 57 { 58 return (a & b) == 0; 59 } 60 } 61 }