From f0e019579936c285ae768278f4a25ed4999090fa Mon Sep 17 00:00:00 2001 From: "Stefanos A." Date: Fri, 1 Nov 2013 09:21:50 +0100 Subject: [PATCH] Avoid singletons; improve ToString() This is part of a long-due series of source cleanup patches. ToString() is no longer used for code generation (code generation is handled by an ISpecWriter instance.) Enum is no longer public. EnumCollection now exposes its backing store through an interface, rather than a concrete class (simpler to change backing store in the future.) --- Source/Bind/Structures/Enum.cs | 144 ++++++++++++++++++++++++--------- 1 file changed, 104 insertions(+), 40 deletions(-) diff --git a/Source/Bind/Structures/Enum.cs b/Source/Bind/Structures/Enum.cs index 12cc98f9..c9ced22f 100644 --- a/Source/Bind/Structures/Enum.cs +++ b/Source/Bind/Structures/Enum.cs @@ -15,7 +15,7 @@ namespace Bind.Structures { #region class Enum - public class Enum + class Enum { static StringBuilder translator = new StringBuilder(); string _name, _type; @@ -31,13 +31,13 @@ namespace Bind.Structures get { return _name ?? ""; } set { _name = value; } } - - // Typically 'long' or 'int'. Default is 'int'. - public string Type - { - get { return String.IsNullOrEmpty(_type) ? "int" : _type; } - set { _type = value; } - } + + // Typically 'long' or 'int'. Default is 'int'. + public string Type + { + get { return String.IsNullOrEmpty(_type) ? "int" : _type; } + set { _type = value; } + } SortedDictionary _constant_collection = new SortedDictionary(); @@ -57,37 +57,13 @@ namespace Bind.Structures } } - [Obsolete("This code belongs to the various language-specific ISpecWriter implementations")] + // Use only for debugging, not for code generation. public override string ToString() { - StringBuilder sb = new StringBuilder(); - List constants = new List(ConstantCollection.Values); - constants.Sort(delegate(Constant c1, Constant c2) - { - int ret = String.Compare(c1.Value, c2.Value); - if (ret == 0) - return String.Compare(c1.Name, c2.Name); - return ret; - }); - - if (IsFlagCollection) - sb.AppendLine("[Flags]"); - sb.Append("public enum "); - sb.Append(Name); - sb.Append(" : "); - sb.AppendLine(Type); - sb.AppendLine("{"); - - foreach (Constant c in constants) - { - sb.Append(" "); - sb.Append(c.ToString()); - if (!String.IsNullOrEmpty(c.ToString())) - sb.AppendLine(","); - } - sb.Append("}"); - - return sb.ToString(); + return String.Format("enum {0} : {1} {{ {2} }}", + Name, + Type, + ConstantCollection); } public void Add(Constant constant) @@ -100,8 +76,10 @@ namespace Bind.Structures #region class EnumCollection - public class EnumCollection : SortedDictionary + class EnumCollection : IDictionary { + SortedDictionary Enumerations = new SortedDictionary(); + internal void AddRange(EnumCollection enums) { foreach (Enum e in enums.Values) @@ -138,10 +116,96 @@ namespace Bind.Structures return 0; } - new bool TryGetValue(string key, out Enum value) + #region IDictionary Members + + public void Add(string key, Enum value) { - return base.TryGetValue(key, out value); + Enumerations.Add(key, value); } + + public bool ContainsKey(string key) + { + return Enumerations.ContainsKey(key); + } + + public ICollection Keys + { + get { return Enumerations.Keys; } + } + + public bool Remove(string key) + { + return Enumerations.Remove(key); + } + + public bool TryGetValue(string key, out Enum value) + { + return Enumerations.TryGetValue(key, out value); + } + + public ICollection Values + { + get { return Enumerations.Values; } + } + + public Enum this[string key] + { + get + { + return Enumerations[key]; + } + set + { + Enumerations[key] = value; + } + } + + public void Add(KeyValuePair item) + { + Enumerations.Add(item.Key, item.Value); + } + + public void Clear() + { + Enumerations.Clear(); + } + + public bool Contains(KeyValuePair item) + { + return Enumerations.Contains(item); + } + + public void CopyTo(KeyValuePair[] array, int arrayIndex) + { + Enumerations.CopyTo(array, arrayIndex); + } + + public int Count + { + get { return Enumerations.Count; } + } + + public bool IsReadOnly + { + get { return (Enumerations as IDictionary).IsReadOnly; } + } + + public bool Remove(KeyValuePair item) + { + return Enumerations.Remove(item.Key); + } + + public IEnumerator> GetEnumerator() + { + return Enumerations.GetEnumerator(); + } + + System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() + { + return Enumerations.GetEnumerator(); + } + + #endregion } #endregion