Exemple #1
0
        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);
        }
Exemple #2
0
 public static CppElementMappingRule Discard(this CppElementMappingRule mappingRule)
 {
     mappingRule.CppElementActions.Add((converter, element, context, matches) =>
     {
         converter.Discard(element);
     });
     return(mappingRule);
 }
Exemple #3
0
 public static CppElementMappingRule CSharpAction(this CppElementMappingRule mappingRule, Action <CSharpConverter, CSharpElement> action)
 {
     mappingRule.CSharpElementActions.Add((converter, csElement, match) =>
     {
         action(converter, csElement);
     });
     return(mappingRule);
 }
Exemple #4
0
        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);
        }
Exemple #5
0
        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);
        }
Exemple #6
0
        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);
        }
Exemple #7
0
        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);
        }
Exemple #8
0
 public static CppElementMappingRule Internal(this CppElementMappingRule mappingRule)
 {
     return(Visibility(mappingRule, CSharpVisibility.Internal));
 }
Exemple #9
0
 public static CppElementMappingRule Protected(this CppElementMappingRule mappingRule)
 {
     return(Visibility(mappingRule, CSharpVisibility.Protected));
 }
Exemple #10
0
 public static CppElementMappingRule Private(this CppElementMappingRule mappingRule)
 {
     return(Visibility(mappingRule, CSharpVisibility.Private));
 }
Exemple #11
0
        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);
        }
Exemple #12
0
 public static CppElementMappingRule MarshalAs(this CppElementMappingRule mappingRule, CSharpUnmanagedKind unmanagedKind)
 {
     return(MarshalAs(mappingRule, new CSharpMarshalAttribute(unmanagedKind)));
 }
Exemple #13
0
 public CppElementMatch(CppElementMappingRule rule, List <ICppElementMatch> matches)
 {
     Rule    = rule;
     Matches = matches;
 }