private void GenerateEnumItemCode(CodeEnumItem itemDecl) { foreach (CodeComment commentDecl in itemDecl.Comments) { GenerateCommentCode(commentDecl); } string value = itemDecl.Value; if (value.StartsWith("0x") && uint.TryParse(value.Substring(2), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out uint num) && num > int.MaxValue) { value = "unchecked((int)" + value + ")"; } WriteIndent(); Output.Write(itemDecl.Name); Output.Write(" = "); Output.Write(value); Output.WriteLine(","); }
protected override void BuildEnum(CodeNamespace ns, CppEnum @enum) { var decl = new CodeEnum(GetClassName(@enum.Name)); decl.Type = @enum.IntegerType.GetDisplayName(); if (decl.Type != "int" && decl.Type != "unsigned int") { Console.WriteLine(decl.Type); throw new NotImplementedException(); } decl.Attributes = CodeAttributes.Public; decl.Comments.AddVSDocComment(@enum.Comment, "summary"); //bool first = true; //string pattern = @enum.Items.FirstOrDefault()?.Name ?? string.Empty; //foreach (CppEnumItem item in @enum.Items) //{ // while (!item.Name.StartsWith(pattern) && pattern.Length > 0) // { // pattern = pattern.Remove(pattern.Length - 1); // } // if (!first && char.IsDigit(item.Name[pattern.Length])) // { // pattern = null; // break; // } // first = false; //} bool first = true; string pattern = @enum.Items.FirstOrDefault()?.Name ?? string.Empty; foreach (CppEnumItem item in @enum.Items) { while (pattern != null && !item.Name.StartsWith(pattern) && pattern.Length > 0) { //pattern = pattern.Remove(pattern.Length - 1); int split = pattern.TrimEnd('_').LastIndexOf('_') + 1; if (split == 0) { pattern = null; break; } pattern = pattern.Remove(split); } if (pattern != null && !first && char.IsDigit(item.Name[pattern.Length])) { pattern = null; break; } first = false; } foreach (CppEnumItem item in @enum.Items) { string name = pattern != null?item.Name.Substring(pattern.Length).ToLowerInvariant().ToUpperCamel() : item.Name; string value = item.ValueExpression?.ToString(); value = (string.IsNullOrEmpty(value) || value.StartsWith("(")) ? item.Value.ToString() : value; if (pattern != null) { string[] combined = value.Split(' ', StringSplitOptions.RemoveEmptyEntries); if (combined.Length > 1) { for (int i = 0; i < combined.Length; i++) { string subVal = combined[i]; if (subVal.StartsWith(pattern)) { combined[i] = subVal.Substring(pattern.Length).ToLowerInvariant().ToUpperCamel(); } } value = string.Join(' ', combined); } else if (value.StartsWith(pattern)) { value = value.Substring(pattern.Length).ToLowerInvariant().ToUpperCamel(); } } if (Regex.IsMatch(value, @"\d<<\d")) { value = value.Replace("<<", " << "); } var itemDecl = new CodeEnumItem(name, value); itemDecl.Comments.AddVSDocComment(item.Comment, "summary"); decl.Members.Add(itemDecl); } ns.Types.Add(decl); }