public static CppElementMappingRule Type(this CppElementMappingRule mappingRule, string type, int?arraySize = null) { if (type == null) { throw new ArgumentNullException(nameof(type)); } mappingRule.TypeRemap = type; mappingRule.TypeRemapArraySize = arraySize; mappingRule.CppElementActions.Add((converter, element, context, matches) => { var remapType = DefaultMappingRulesConverter.GetCppTypeRemap(converter, mappingRule.TypeRemap, mappingRule.TypeRemapArraySize); if (remapType == null) { return; } if (element is CppField cppField) { cppField.Type = remapType; } if (element is CppParameter cppParameter) { cppParameter.Type = remapType; } if (element is CppFunction cppFunction) { cppFunction.ReturnType = remapType; } }); return(mappingRule); }
public static CppElementMappingRule Discard(this CppElementMappingRule mappingRule) { mappingRule.CppElementActions.Add((converter, element, context, matches) => { converter.Discard(element); }); return(mappingRule); }
public static CppElementMappingRule CSharpAction(this CppElementMappingRule mappingRule, Action <CSharpConverter, CSharpElement> action) { mappingRule.CSharpElementActions.Add((converter, csElement, match) => { action(converter, csElement); }); return(mappingRule); }
public static CppElementMappingRule Visibility(this CppElementMappingRule mappingRule, CSharpVisibility visibility) { mappingRule.CSharpElementActions.Add((converter, csElement, match) => { if (!(csElement is ICSharpElementWithVisibility csElementWithVisibility)) { return; } csElementWithVisibility.Visibility = visibility; }); return(mappingRule); }
public static CppElementMappingRule InitValue(this CppElementMappingRule mappingRule, string value) { mappingRule.CSharpElementActions.Add((converter, element, matches) => { if (element is CSharpField csField) { csField.InitValue = value; } if (element is CSharpParameter csParam) { csParam.DefaultValue = value; } }); return(mappingRule); }
public static CppElementMappingRule DllImportLibrary(this CppElementMappingRule mappingRule, string dllImportName, string headerFileName) { bool isHeaderMatch = false; mappingRule.CppElementActions.Add(CppRule); mappingRule.CSharpElementActions.Add(CsRule); void CppRule(CSharpConverter converter, CppElement cppElement, CSharpElement context, List <ICppElementMatch> matches) { if (!(context is CSharpMethod)) { return; } var fileName = Path.GetFileName(cppElement.SourceFile); if (string.IsNullOrWhiteSpace(fileName)) { return; } isHeaderMatch = fileName == headerFileName; } void CsRule(CSharpConverter converter, CSharpElement csElement, List <ICppElementMatch> matches) { if (!(csElement is CSharpMethod csMethod) || !isHeaderMatch) { return; } foreach (var attribute in csMethod.Attributes) { if (attribute is CSharpDllImportAttribute dllImportAttribute) { dllImportAttribute.DllName = dllImportName; break; } } } return(mappingRule); }
public static CppElementMappingRule Name(this CppElementMappingRule mappingRule, string replaceName) { mappingRule.CppElementActions.Add((converter, element, context, matches) => { if (!(element is ICppMember cppMember)) { return; } var match = matches.FindMatch <CppElementRegexMatch>(); if (match?.RegexInput != null) { cppMember.Name = Regex.Replace(match.RegexInput, match.RegexPattern, replaceName); } else { cppMember.Name = replaceName; } }); return(mappingRule); }
public static CppElementMappingRule Internal(this CppElementMappingRule mappingRule) { return(Visibility(mappingRule, CSharpVisibility.Internal)); }
public static CppElementMappingRule Protected(this CppElementMappingRule mappingRule) { return(Visibility(mappingRule, CSharpVisibility.Protected)); }
public static CppElementMappingRule Private(this CppElementMappingRule mappingRule) { return(Visibility(mappingRule, CSharpVisibility.Private)); }
public static CppElementMappingRule MarshalAs(this CppElementMappingRule mappingRule, CSharpMarshalAttribute marshalAttribute, bool cloneAttribute = true) { if (marshalAttribute == null) { throw new ArgumentNullException(nameof(marshalAttribute)); } var clonedAttribute = cloneAttribute ? marshalAttribute.Clone() : marshalAttribute; mappingRule.CSharpElementActions.Add((converter, element, matches) => { var csField = element as CSharpField; var csParam = element as CSharpParameter; var csMethod = element as CSharpMethod; if (csField == null && csParam == null && csMethod == null) { return; } var type = csField?.FieldType ?? csParam?.ParameterType ?? csMethod?.ReturnType; // Should not happen, but in case if (type == null) { return; } if (type is CSharpTypeWithAttributes cppTypeWithAttributes) { for (var i = cppTypeWithAttributes.Attributes.Count - 1; i >= 0; i--) { var attr = cppTypeWithAttributes.Attributes[i]; if (attr is CSharpMarshalAttribute) { cppTypeWithAttributes.Attributes.RemoveAt(i); cppTypeWithAttributes.Attributes.Insert(i, clonedAttribute); return; } } cppTypeWithAttributes.Attributes.Add(clonedAttribute); } else { var typeWithAttributes = new CSharpTypeWithAttributes(type); typeWithAttributes.Attributes.Add(clonedAttribute); if (csField != null) { csField.FieldType = typeWithAttributes; } else if (csParam != null) { csParam.ParameterType = typeWithAttributes; } else if (csMethod != null) { csMethod.ReturnType = typeWithAttributes; } } }); return(mappingRule); }
public static CppElementMappingRule MarshalAs(this CppElementMappingRule mappingRule, CSharpUnmanagedKind unmanagedKind) { return(MarshalAs(mappingRule, new CSharpMarshalAttribute(unmanagedKind))); }
public CppElementMatch(CppElementMappingRule rule, List <ICppElementMatch> matches) { Rule = rule; Matches = matches; }