views:

2534

answers:

8

Hello folk,

I need to detect if a flag is set within an enum value, which type is marked with the Flag attribute.

Usually it is made like that:

(value & flag) == flag

But since I need to do this by generic (sometimes at runtime I event have only an "Enum" reference. I can not find an easy way to use the & operator. At the moment I make it like this:

    public static bool IsSet<T>(this T value, T flags) where T : Enum
    { 
        Type numberType = Enum.GetUnderlyingType(typeof(T));

        if (numberType.Equals(typeof(int)))
        {
            return BoxUnbox<int>(value, flags, (a, b) => (a & b) == b);
        }
        else if (numberType.Equals(typeof(sbyte)))
        {
            return BoxUnbox<sbyte>(value, flags, (a, b) => (a & b) == b);
        }
        else if (numberType.Equals(typeof(byte)))
        {
            return BoxUnbox<byte>(value, flags, (a, b) => (a & b) == b);
        }
        else if (numberType.Equals(typeof(short)))
        {
            return BoxUnbox<short>(value, flags, (a, b) => (a & b) == b);
        }
        else if (numberType.Equals(typeof(ushort)))
        {
            return BoxUnbox<ushort>(value, flags, (a, b) => (a & b) == b);
        }
        else if (numberType.Equals(typeof(uint)))
        {
            return BoxUnbox<uint>(value, flags, (a, b) => (a & b) == b);
        }
        else if (numberType.Equals(typeof(long)))
        {
            return BoxUnbox<long>(value, flags, (a, b) => (a & b) == b);
        }
        else if (numberType.Equals(typeof(ulong)))
        {
            return BoxUnbox<ulong>(value, flags, (a, b) => (a & b) == b);
        }
        else if (numberType.Equals(typeof(char)))
        {
            return BoxUnbox<char>(value, flags, (a, b) => (a & b) == b);
        }
        else
        {
            throw new ArgumentException("Unknown enum underlying type " + numberType.Name + ".");
        }
    }


    private static bool BoxUnbox<T>(object value, object flags, Func<T, T, bool> op)
    {
        return op((T)value, (T)flags);
    }

But I don't like the never ending if - else blocks, so is there a way to cast these values that I can use the & operator or any other solution to check this?

+1  A: 

Personally, I think that look fine because you've wrapped it into a single purpose function. If you had that code scattered through an entire program I think you would have some problems, but what you've created improves clarity everywhere it is used and the function itself is clear enough what it does.

Just my opinion of course.

You could though, use the is keyword, which might help a little

public static bool IsSet<T>(this T value, T flags) where T : Enum
{ 
    if (value is int)
    {
        return ((int)(object)a & (int)(object)b) == (int)(object)b);
    }
    //etc...
Hugoware
Too many unnecessary castings.
arbiter
FWIW, you can't cast a generic type directly to a value type, thus the additional object casting.
Hugoware
Yep, my fault. I forget that this is why I've used Convert class instead of direct casting :)
arbiter
I keep it like I implemented it first.
Enyra
See Ronnie's answer here: http://stackoverflow.com/questions/7244
Keith
+6  A: 

This should do the job for enum types with any underlying types:

public static bool IsSet<T>(this T value, T flags) where T : struct
{
    return (Convert.ToInt64(value) & Convert.ToInt64(flags)) ==
        Convert.ToInt64(flags);
}

Convert.ToInt64 is used because a 64-bit integer is the "widest" integral type possible, to which all enum values can be cast (even ulong). Note that char is not a valid underlying type. It seems that it is not valid in C#, but it is in general valid in CIL/for the CLR.

Also, you can't enforce a generic type constraint for enums (i.e. where T : struct); the best you can do is use where T : struct to enforce T to be a value type, and then optionally perform a dynamic check to ensure that T is an enum type.

For completeness, here is my very brief test harness:

static class Program
{
    static void Main(string[] args)
    {
        Debug.Assert(Foo.abc.IsSet(Foo.abc));
        Debug.Assert(Bar.def.IsSet(Bar.def));
        Debug.Assert(Baz.ghi.IsSet(Baz.ghi));
    }

    enum Foo : int
    {
        abc = 1,
        def = 10,
        ghi = 100
    }

    enum Bar : sbyte
    {
        abc = 1,
        def = 10,
        ghi = 100
    }

    enum Baz : ulong
    {
        abc = 1,
        def = 10,
        ghi = 100
    }
}
Noldorin
Not correct statement, because flags can have more than one bit set.
arbiter
Char isnt a valid underlying type ... in C#. His question does not state who created the enum, and in what language.
Simon Svensson
I removed my original comment, but I'm still not convinced this is absolutely correct since the right hand side of the test is still an Enum. It needs to be converted as well, I believe.
tvanfosson
@arbiter: It's perfectly correct as long as you only test for one flag. Indeed, checking for "== flags" works in the more general case of *combinations* of flags.
Noldorin
@tvanfosson: You're right. That was me being silly and editing the code directly online instead of in Visual Studio. ;)
Noldorin
@Simon: I wasn't aware of that, but I'll take your word that it's valid as an underlying type in general (i.e. in CIL).
Noldorin
@Simon -- actually the question title specifies C#.
tvanfosson
Indeed, but I edited my description anyway to point out that it is valid CIL, just for completeness.
Noldorin
A: 

I have used this to compare flags

public static bool IsSet<T>(this T input, T match)
{
    return (Convert.ToUInt32(input) & Convert.ToUInt32(match)) != 0;
}

Here you can do the different conversions. From int to short to long.

David Basarab
Not correct statement as well.
arbiter
+9  A: 

For me it looks overcomplicated. How about this (keeping in mind that enum is always mapped to an integer value type):

public static bool IsSet<T>(T value, T flags) where T : struct
{
    // You can add enum type checking to be perfectly sure that T is enum, this have some cost however
    // if (!typeof(T).IsEnum)
    //     throw new ArgumentException();
    long longFlags = Convert.ToInt64(flags);
    return (Convert.ToInt64(value) & longFlags) == longFlags;
}
arbiter
Yeah, so this is basically the same as mine now. Not sure I would bother factoring out the `Convert.ToInt64(flags)` anyway, as the C# compiler would optimise it away anyway.
Noldorin
@Noldorin, of course this is pretty basic, even trivial, that is why so many answers here :)
arbiter
@tvanfosson: It's shorter to leave out the variable declaration, any no less readable, in my opinion. This is however pedantry and semantics...
Noldorin
+3  A: 

I wrote a set of extension methods for enums, in case you need it :

public static class EnumExtensions
{
    private static void CheckEnumWithFlags<T>()
    {
        if (!typeof(T).IsEnum)
            throw new ArgumentException(string.Format("Type '{0}' is not an enum", typeof(T).FullName));
        if (!Attribute.IsDefined(typeof(T), typeof(FlagsAttribute)))
            throw new ArgumentException(string.Format("Type '{0}' doesn't have the 'Flags' attribute", typeof(T).FullName));
    }

    public static bool IsFlagSet<T>(this T value, T flag) where T : struct
    {
        CheckEnumWithFlags<T>();
        long lValue = Convert.ToInt64(value);
        long lFlag = Convert.ToInt64(flag);
        return (lValue & lFlag) != 0;
    }

    public static IEnumerable<T> GetFlags<T>(this T value) where T : struct
    {
        CheckEnumWithFlags<T>();
        foreach (T flag in Enum.GetValues(typeof(T)).Cast<T>())
        {
            if (value.IsFlagSet(flag))
                yield return flag;
        }
    }

    public static T SetFlags<T>(this T value, T flags, bool on) where T : struct
    {
        CheckEnumWithFlags<T>();
        long lValue = Convert.ToInt64(value);
        long lFlag = Convert.ToInt64(flags);
        if (on)
        {
            lValue |= lFlag;
        }
        else
        {
            lValue &= (~lFlag);
        }
        return (T)Enum.ToObject(typeof(T), lValue);
    }

    public static T SetFlags<T>(this T value, T flags) where T : struct
    {
        return value.SetFlags(flags, true);
    }

    public static T ClearFlags<T>(this T value, T flags) where T : struct
    {
        return value.SetFlags(flags, false);
    }

    public static T CombineFlags<T>(this IEnumerable<T> flags) where T : struct
    {
        CheckEnumWithFlags<T>();
        long lValue = 0;
        foreach (T flag in flags)
        {
            long lFlag = Convert.ToInt64(flag);
            lValue |= lFlag;
        }
        return (T)Enum.ToObject(typeof(T), lValue);
    }
}

The main drawback is that you can't specify where T : Enum : it is explicitly forbidden ("Constraint cannot be special class 'System.Enum'"), so the extension methods will appear in intellisense for all structs... I added the CheckEnumWithFlags method to check that the type is actually an enum, and has the Flags attribute.


UPDATE : Jon Skeet recently started an interesting library called UnconstrainedMelody which does exactly the same sort of things, and works around the generic type constraint limitation mentioned above

Thomas Levesque
A: 

I believe the fastest way to do it would be :

public static bool IsSet<T>(this T value, T flags) where T : struct
{ 
    int iValue = value.GetHashCode();
    int iFlags = flags.GetHashCode();

    return (iValue & iFlags) == iFlags;
}

This implementation exploits the fact that 'var.GetHashCode()' always returns 'var' for 8-32 bit integers. However, this doesn't work for Int64/UInt64.

Lotfi
Very, very bad idea. You depend on internals, and internals can be changed in forthcoming version of framework. In this case you application will fail and you will spent enormous amount of time to find the problem!
arbiter
Don't be that sure... This will never change, simply for a matter of speed !
Lotfi
A: 

or... public static bool IsSet(this Enum value, Enum compare) { int baseValue = value.ToInt32(); int compareValue = compare.ToInt32(); if (baseValue == 0) return false; return ((baseValue & compareValue) == compareValue); }

Rik
A: 

Simply use the Enum.HasFlag() Method !

Shazam