/// <summary> /// Adds a list of constant gathered from macros/guids to a C# type. /// </summary> /// <param name="elementFinder">The C++ module to search.</param> /// <param name="macroRegexp">The macro regexp.</param> /// <param name="fullNameCSharpType">Full type of the name C sharp.</param> /// <param name="type">The type.</param> /// <param name="fieldName">Name of the field.</param> /// <param name="valueMap">The value map.</param> /// <param name="visibility">The visibility.</param> /// <param name="nameSpace">The current namespace.</param> public void AddConstantFromMacroToCSharpType(CppElementFinder elementFinder, string macroRegexp, string fullNameCSharpType, string type, string fieldName, string valueMap, Visibility?visibility, string nameSpace) { var constantDefinitions = elementFinder.Find <CppConstant>(macroRegexp); var regex = new Regex(macroRegexp); // $0: Name of the C++ macro // $1: Value of the C++ macro // $2: Name of the C# // $3: Name of current namespace if (valueMap != null) { valueMap = valueMap.Replace("{", "{{"); valueMap = valueMap.Replace("}", "}}"); valueMap = valueMap.Replace("$0", "{0}"); valueMap = valueMap.Replace("$1", "{1}"); valueMap = valueMap.Replace("$2", "{2}"); valueMap = valueMap.Replace("$3", "{3}"); } foreach (var macroDef in constantDefinitions) { var finalFieldName = fieldName == null ? macroDef.Name : NamingRules.ConvertToPascalCase( regex.Replace(macroDef.Name, fieldName), NamingFlags.Default); var finalValue = valueMap == null ? macroDef.Value : string.Format(valueMap, macroDef.Name, macroDef.Value, finalFieldName, nameSpace); var constant = AddConstantToCSharpType(macroDef, fullNameCSharpType, type, finalFieldName, finalValue); constant.Visibility = visibility ?? Visibility.Public | Visibility.Const; } var guidDefinitions = elementFinder.Find <CppGuid>(macroRegexp); foreach (var guidDef in guidDefinitions) { var finalFieldName = fieldName == null ? guidDef.Name : NamingRules.ConvertToPascalCase( regex.Replace(guidDef.Name, fieldName), NamingFlags.Default); var finalValue = valueMap == null? guidDef.Guid.ToString() : string.Format(valueMap, guidDef.Name, guidDef.Guid.ToString(), finalFieldName, nameSpace); var constant = AddConstantToCSharpType(guidDef, fullNameCSharpType, type, finalFieldName, finalValue); constant.Visibility = visibility ?? Visibility.Public | Visibility.Static | Visibility.Readonly; } }
public static IEnumerable <T> Find <T>(this CppElement element, string path) where T : CppElement { var mapper = new CppElementFinder(element); return(mapper.Find <T>(path)); }
private static bool RemoveElements <T>(CppElementFinder finder, string regex) where T : CppElement { var matchedAny = false; foreach (var item in finder.Find <T>(regex).ToList()) { matchedAny = true; item.Parent.Remove(item); } return(matchedAny); }
private static void FindGuidForInterface(CsInterface interfaceType) { var cppInterface = (CppInterface)interfaceType.CppElement; if (string.IsNullOrEmpty(cppInterface.Guid)) { var finder = new CppElementFinder(cppInterface.ParentInclude); var cppGuid = finder.Find <CppGuid>("IID_" + cppInterface.Name).FirstOrDefault(); if (cppGuid != null) { interfaceType.Guid = cppGuid.Guid.ToString(); } } }
private static string FindGuid(CppInterface cppInterface) { if (!string.IsNullOrEmpty(cppInterface.Guid)) { return(cppInterface.Guid); } // If Guid is null we try to recover it from a declared GUID var finder = new CppElementFinder(cppInterface.ParentInclude); var cppGuid = finder.Find <CppGuid>("IID_" + cppInterface.Name).FirstOrDefault(); return(cppGuid != null ? cppGuid.Guid.ToString() : cppInterface.Guid); }
/// <summary> /// Creates a C++ variable declaration from a macro rule. /// </summary> /// <param name="cstRule">The macro rule.</param> /// <param name="finder">The element finder to find the macro definitions in.</param> /// <returns>A C++ variable declaration string</returns> private string CreateVariableFromMacro(CppElementFinder finder, ConstantRule cstRule) { var builder = new StringBuilder(); builder.AppendLine("// Variable created from: " + cstRule); foreach (CppDefine macroDef in finder.Find <CppDefine>(cstRule.Macro)) { var macroName = macroDef.Name + EndTagCustomVariable; // Only add the macro once (could have multiple identical macro in different includes) if (!_variableMacrosDefined.ContainsKey(macroName)) { builder.AppendFormat("extern \"C\" {0} {1} = {3}{2};\n", cstRule.CppType ?? cstRule.Type, macroName, macroDef.Name, cstRule.CppCast ?? ""); _variableMacrosDefined.Add(macroName, macroDef.Name); } } return(builder.ToString()); }
public static bool ExecuteRule <T>(this CppElementFinder finder, string regex, MappingRule rule) where T : CppElement { var mode = CppElementFinder.SelectionMode.MatchedElement; var matchedAny = false; if (regex.StartsWith("#")) { mode = CppElementFinder.SelectionMode.Parent; regex = regex.Substring(1); } var fullRegex = BuildFullRegex(regex); foreach (var item in finder.Find <T>(fullRegex, mode)) { matchedAny = true; ProcessRule(item, rule, fullRegex); } return(matchedAny); }
/// <summary> /// Creates a C++ enum declaration from a macro rule. /// </summary> /// <param name="createCpp">The macro rule.</param> /// <returns>A C++ enum declaration string</returns> private string CreateEnumFromMacro(CppElementFinder finder, CreateCppExtensionRule createCpp) { var cppEnumText = new StringBuilder(); cppEnumText.AppendLine("// Enum created from: " + createCpp); cppEnumText.AppendLine("enum " + createCpp.Enum + " {"); foreach (CppDefine macroDef in finder.Find <CppDefine>(createCpp.Macro)) { var macroName = macroDef.Name + EndTagCustomEnumItem; // Only add the macro once (could have multiple identical macro in different includes) if (!_variableMacrosDefined.ContainsKey(macroName)) { cppEnumText.AppendFormat("\t {0} = {1},\n", macroName, macroDef.Value); _variableMacrosDefined.Add(macroName, macroDef.Value); } } cppEnumText.AppendLine("};"); return(cppEnumText.ToString()); }
/// <summary> /// Finds the specified elements by regex. /// </summary> /// <typeparam name = "T"></typeparam> /// <param name = "regex">The regex.</param> /// <param name="finder">The C++ element finder instance to use.</param> /// <param name="mode">The selection mode for selecting matched elements.</param> /// <returns></returns> public static IEnumerable <T> Find <T>( this CppElementFinder finder, string regex, CppElementFinder.SelectionMode mode = CppElementFinder.SelectionMode.MatchedElement) where T : CppElement => finder.Find <T>(BuildFindFullRegex(regex), mode);