Fix build_init vcxproj to correctly detect changes in config.h.
[mono.git] / mcs / class / System.IO.Compression / SharpCompress / Common / FlagUtility.cs
1 using System;
2
3 namespace SharpCompress.Common
4 {
5     internal static class FlagUtility
6     {
7         /// <summary>
8         /// Returns true if the flag is set on the specified bit field.
9         /// Currently only works with 32-bit bitfields. 
10         /// </summary>
11         /// <typeparam name="T">Enumeration with Flags attribute</typeparam>
12         /// <param name="bitField">Flagged variable</param>
13         /// <param name="flag">Flag to test</param>
14         /// <returns></returns>
15         public static bool HasFlag<T>(long bitField, T flag)
16             where T : struct
17         {
18             return HasFlag(bitField, flag);
19         }
20
21         /// <summary>
22         /// Returns true if the flag is set on the specified bit field.
23         /// Currently only works with 32-bit bitfields. 
24         /// </summary>
25         /// <typeparam name="T">Enumeration with Flags attribute</typeparam>
26         /// <param name="bitField">Flagged variable</param>
27         /// <param name="flag">Flag to test</param>
28         /// <returns></returns>
29         public static bool HasFlag<T>(ulong bitField, T flag)
30             where T : struct
31         {
32             return HasFlag(bitField, flag);
33         }
34
35         /// <summary>
36         /// Returns true if the flag is set on the specified bit field.
37         /// Currently only works with 32-bit bitfields. 
38         /// </summary>
39         /// <param name="bitField">Flagged variable</param>
40         /// <param name="flag">Flag to test</param>
41         /// <returns></returns>
42         public static bool HasFlag(ulong bitField, ulong flag)
43         {
44             return ((bitField & flag) == flag);
45         }
46
47         public static bool HasFlag(short bitField, short flag)
48         {
49             return ((bitField & flag) == flag);
50         }
51
52 #if PORTABLE
53     /// <summary>
54     /// Generically checks enums in a Windows Phone 7 enivronment
55     /// </summary>
56     /// <param name="enumVal"></param>
57     /// <param name="flag"></param>
58     /// <returns></returns>
59         public static bool HasFlag(this Enum enumVal, Enum flag)
60         {
61             if (enumVal.GetHashCode() > 0) //GetHashCode returns the enum value. But it's something very crazy if not set beforehand
62             {
63                 ulong num = Convert.ToUInt64(flag.GetHashCode());
64                 return ((Convert.ToUInt64(enumVal.GetHashCode()) & num) == num);
65             }
66             else
67             {
68                 return false;
69             }
70         }
71 #endif
72
73         /// <summary>
74         /// Returns true if the flag is set on the specified bit field.
75         /// Currently only works with 32-bit bitfields. 
76         /// </summary>
77         /// <typeparam name="T">Enumeration with Flags attribute</typeparam>
78         /// <param name="bitField">Flagged variable</param>
79         /// <param name="flag">Flag to test</param>
80         /// <returns></returns>
81         public static bool HasFlag<T>(T bitField, T flag)
82             where T : struct
83         {
84             return HasFlag(Convert.ToInt64(bitField), Convert.ToInt64(flag));
85         }
86
87         /// <summary>
88         /// Returns true if the flag is set on the specified bit field.
89         /// Currently only works with 32-bit bitfields. 
90         /// </summary>
91         /// <param name="bitField">Flagged variable</param>
92         /// <param name="flag">Flag to test</param>
93         /// <returns></returns>
94         public static bool HasFlag(long bitField, long flag)
95         {
96             return ((bitField & flag) == flag);
97         }
98
99
100         /// <summary>
101         /// Sets a bit-field to either on or off for the specified flag.
102         /// </summary>
103         /// <param name="bitField">Flagged variable</param>
104         /// <param name="flag">Flag to change</param>
105         /// <param name="on">bool</param>
106         /// <returns>The flagged variable with the flag changed</returns>
107         public static long SetFlag(long bitField, long flag, bool @on)
108         {
109             if (@on)
110             {
111                 return bitField | flag;
112             }
113             return bitField & (~flag);
114         }
115
116         /// <summary>
117         /// Sets a bit-field to either on or off for the specified flag.
118         /// </summary>
119         /// <typeparam name="T">Enumeration with Flags attribute</typeparam>
120         /// <param name="bitField">Flagged variable</param>
121         /// <param name="flag">Flag to change</param>
122         /// <param name="on">bool</param>
123         /// <returns>The flagged variable with the flag changed</returns>
124         public static long SetFlag<T>(T bitField, T flag, bool @on)
125             where T : struct
126         {
127             return SetFlag(Convert.ToInt64(bitField), Convert.ToInt64(flag), @on);
128         }
129     }
130 }