4 using System.Collections;
5 using System.Collections.Generic;
8 using SiliconStudio.Shaders.Utility;
10 namespace SiliconStudio.Shaders.Ast
18 private OrderedSet<CompositeEnum> values;
20 #region Constructors and Destructors
27 Values =
new OrderedSet<CompositeEnum>();
56 #region Public Properties
64 public bool IsComposition
78 public bool IsFlag {
get;
private set; }
86 public object Key {
get; set; }
95 public OrderedSet<CompositeEnum> Values
110 public virtual string DisplayName
114 return Key != null ? Key.ToString() :
"null";
120 #region Public Methods
133 return enumValue.Values.IsSubsetOf(Values);
147 return enumValues.Any(Contains);
161 return enumValues.All(Contains);
175 if (ReferenceEquals(null, other))
180 if (ReferenceEquals(
this, other))
185 if (other.
IsFlag != IsFlag)
190 if (Values.Count != other.
Values.Count)
195 if (Key != null && other.
Key != null)
197 return Key.Equals(other.Key);
201 if (ReferenceEquals(Values, other.
Values))
206 return Values.SetEquals(other.Values);
212 if (ReferenceEquals(null, obj))
217 if (ReferenceEquals(
this, obj))
227 return Equals((CompositeEnum)obj);
233 int hashCode = IsFlag.GetHashCode() * 397;
236 return hashCode ^ Key.GetHashCode() * 397;
239 return Values.Aggregate(hashCode, (current, value) => current ^ value.GetHashCode() * 397);
242 IEnumerator IEnumerable.GetEnumerator()
244 return GetEnumerator();
249 return Values.GetEnumerator();
255 return ToString<CompositeEnum>(null);
261 var builder =
new StringBuilder();
263 var filteredValues = Values.OfType<T>();
264 if (filterEnum != null)
266 filteredValues = filteredValues.Where(filterEnum);
269 foreach (var value
in filteredValues)
276 if (value.Key != null)
278 if (
string.Empty.Equals(value.Key))
284 builder.Append(value.DisplayName);
290 builder.Append(value.ToString());
295 return builder.ToString();
318 public static T1 OperatorAnd<T1>(T1 left, T1 right)
321 var result =
new T1 { IsFlag = left.IsFlag, Values =
new OrderedSet<CompositeEnum>(left.Values) };
322 result.Values.IntersectWith(right.Values);
341 public static T1 OperatorOr<T1>(T1 left, T1 right)
342 where T1 : CompositeEnum,
new()
344 var result =
new T1 { IsFlag = left.IsFlag, Values =
new OrderedSet<CompositeEnum>(left.Values) };
345 result.Values.UnionWith(right.Values);
364 public static T1 OperatorXor<T1>(T1 left, T1 right)
365 where T1 : CompositeEnum,
new()
367 var result =
new T1 { IsFlag = left.IsFlag, Values =
new OrderedSet<CompositeEnum>(left.Values) };
368 result.Values.SymmetricExceptWith(right.Values);
385 var type = typeof(T);
386 while (type != typeof(CompositeEnum))
388 foreach (var field
in type.GetFields())
390 if (typeof(CompositeEnum).IsAssignableFrom(field.FieldType) && field.IsStatic)
392 var fieldValue = (CompositeEnum)field.GetValue(null);
393 if (fieldValue.Values.Count == 1)
395 var key = fieldValue.Values.FirstOrDefault();
396 if (key != null && key.Key != null && !map.ContainsKey(key.Key))
398 map.Add(key.Key, fieldValue);
404 type = type.BaseType;
413 if (Values.Count == 0)
return ChildrenList;
415 ChildrenList.Clear();
416 foreach (var compositeEnum
in Values)
418 if (!ReferenceEquals(
this, compositeEnum) && !string.Empty.Equals(compositeEnum.Key))
420 ChildrenList.Add(compositeEnum);
439 public static bool operator ==(CompositeEnum left, CompositeEnum right)
441 return Equals(left, right);
452 public static bool operator !=(CompositeEnum left, CompositeEnum right)
454 return !Equals(left, right);
464 #region Public Methods
474 public T ParseEnumFromName<T>(
object key) where T : CompositeEnum,
new()
477 if (!TryGetValue(key, out value))
479 throw new ArgumentException(
string.Format(
"Unable to convert [{0}] to qualifier", key),
"key");
486 if (typeof(T).IsAssignableFrom(value.GetType()))
491 return new T { IsFlag = value.IsFlag, Values = value.Values };
IEnumerator< CompositeEnum > GetEnumerator()
object Key
Gets or sets the key.
override IEnumerable< Node > Childrens()
Gets the child nodes. An enumeration of child nodes
CompositeEnum(object key, bool isFlag)
Initializes a new instance of the CompositeEnum class.
override bool Equals(object obj)
Internal dictionary that provides conversion helper methods.
override string ToString()
bool IsFlag
Gets a value indicating whether this instance is an enum flag.
bool ContainsAll(params CompositeEnum[] enumValues)
Determines whether the specified enum values contains all.
CompositeEnum()
Initializes a new instance of the CompositeEnum class.
override int GetHashCode()
bool Contains(CompositeEnum enumValue)
Determines whether [contains] [the specified enum value].
bool Equals(CompositeEnum other)
Determines whether the specified CompositeEnum is equal to this instance.
OrderedSet< CompositeEnum > Values
Gets or sets the values.
CompositeEnum(bool isFlag)
Initializes a new instance of the CompositeEnum class.
bool Contains(params CompositeEnum[] enumValues)
Determines whether [contains] [the specified enum values].