public void TransformName() { var @namespace = new NamespaceDefinition(); var @enum = new EnumDefinition("CppEnum"); @enum.AddChild(new EnumeratorDefinition("COMMON_PREFIX_NONE", "0")); @enum.AddChild(new EnumeratorDefinition("COMMON_PREFIX_VALUE1", "1")); @enum.AddChild(new EnumeratorDefinition("COMMON_PREFIX_VALUE2", "COMMON_PREFIX_VALUE1*2")); @namespace.AddChild(@enum); new EnumTransformer().Transform(@namespace, null); Assert.AreEqual(3, @enum.Children.Count()); Assert.AreEqual(3, @enum.Enumerators.Count()); EnumeratorDefinition enumerator0 = (EnumeratorDefinition)@enum.Children[0]; Assert.AreEqual("None", enumerator0.Name); Assert.AreEqual("0", enumerator0.Value); EnumeratorDefinition enumerator1 = (EnumeratorDefinition)@enum.Children[1]; Assert.AreEqual("Value1", enumerator1.Name); Assert.AreEqual("1", enumerator1.Value); EnumeratorDefinition enumerator2 = (EnumeratorDefinition)@enum.Children[2]; Assert.AreEqual("Value2", enumerator2.Name); Assert.AreEqual("Value1*2", enumerator2.Value); }
private static bool IsFlag(EnumeratorDefinition enumerator) { if (ParseValue(enumerator, out int intValue)) { return(intValue == 0 || IsPowerOfTwo(intValue)); } return(false); }
private static void EnsureNoneValue(EnumDefinition @enum) { if (@enum.IsFlags() && HasZeroValue(@enum)) { var enumerator = new EnumeratorDefinition("None", "0"); @enum.AddChild(enumerator); @enum.Children.Remove(enumerator); @enum.Children.Insert(0, enumerator); } }
private static void ReplaceNameReferencesInValues(EnumeratorDefinition[] enumerators, string[] newNames) { foreach (EnumeratorDefinition enumerator in enumerators.Where(e => e.Value != null)) { for (int i = 0; i < enumerators.Length; i++) { EnumeratorDefinition oldEnumerator = enumerators[i]; string oldName = oldEnumerator.Name; if (enumerator.Value.Contains(oldName)) { enumerator.Value = enumerator.Value.Replace(oldName, newNames[i]); } } } }
public void AddNoneValueToEnum() { var @namespace = new NamespaceDefinition(); var @enum = new EnumDefinition("CppEnum"); @enum.AddChild(new EnumeratorDefinition("VALUE1", "1")); @enum.AddChild(new EnumeratorDefinition("VALUE2", "2")); @enum.AddChild(new EnumeratorDefinition("VALUE3", "4")); @namespace.AddChild(@enum); new EnumTransformer().Transform(@namespace, null); Assert.AreEqual(4, @enum.Enumerators.Count()); EnumeratorDefinition enumerator0 = (EnumeratorDefinition)@enum.Children[0]; Assert.AreEqual("None", enumerator0.Name); Assert.AreEqual("0", enumerator0.Value); }
public static bool ParseValue(this EnumeratorDefinition enumerator, out int result) { string value = enumerator.Value; if (string.IsNullOrEmpty(value)) { result = 0; return(false); } if (value.StartsWith("0x", StringComparison.OrdinalIgnoreCase)) { return(int.TryParse(value.Substring(2), NumberStyles.HexNumber, CultureInfo.CurrentCulture, out result)); } return(int.TryParse(value, out result)); }