public static CSharpElement ConvertFunction(CSharpConverter converter, CppFunction cppFunction, CSharpElement context)
        {
            // We process only public export functions
            if (!cppFunction.IsPublicExport() || ((cppFunction.Flags & (CppFunctionFlags.Inline | CppFunctionFlags.Method | CppFunctionFlags.Constructor | CppFunctionFlags.Destructor)) != 0 && (cppFunction.Flags & CppFunctionFlags.Virtual) == 0))
            {
                return(null);
            }

            // Register the struct as soon as possible
            var csFunction = new CSharpMethod()
            {
                CppElement = cppFunction
            };

            var container = converter.GetCSharpContainer(cppFunction, context);

            converter.ApplyDefaultVisibility(csFunction, container);
            container.Members.Add(csFunction);

            converter.AddUsing(container, "System.Runtime.InteropServices");

            if ((cppFunction.Flags & CppFunctionFlags.Virtual) == 0)
            {
                csFunction.Modifiers |= CSharpModifiers.Static | CSharpModifiers.Extern;
            }
            else
            {
                csFunction.Visibility = CSharpVisibility.None;
            }
            csFunction.Name       = converter.GetCSharpName(cppFunction, csFunction);
            csFunction.Comment    = converter.GetCSharpComment(cppFunction, csFunction);
            csFunction.ReturnType = converter.GetCSharpType(cppFunction.ReturnType, csFunction);

            return(csFunction);
        }
Ejemplo n.º 2
0
        public static CSharpMethod Wrap(this CSharpMethod csMethod)
        {
            var dllImport = csMethod.Attributes.OfType <CSharpDllImportAttribute>().FirstOrDefault();

            // Create a new method
            var clonedMethod = new CSharpMethod
            {
                Name       = csMethod.Name,
                ReturnType = csMethod.ReturnType,
                Modifiers  = csMethod.Modifiers,
                Comment    = csMethod.Comment,
            };

            // Remove the comment from the private method now
            for (int i = 0; i < csMethod.Parameters.Count; i++)
            {
                var fromParam   = csMethod.Parameters[i];
                var clonedParam = fromParam.Clone();
                clonedParam.Parent = clonedMethod;
                clonedMethod.Parameters.Add(clonedParam);
            }

            // If original function has a DllImport, update its EntryPoint
            // as we are going to change its name after
            if (dllImport != null)
            {
                // Remove extern
                clonedMethod.Modifiers ^= CSharpModifiers.Extern;
                dllImport.EntryPoint    = $"\"{clonedMethod.Name}\"";
            }

            // Remove the comment from the original method
            csMethod.Comment = null;
            // Rename it to avoid naming clash
            csMethod.Name += "__";
            // Make it private
            csMethod.Visibility = CSharpVisibility.Private;

            // Insert the new function right before
            var members = ((ICSharpContainer)csMethod.Parent).Members;
            int index   = members.IndexOf(csMethod);

            members.Insert(index, clonedMethod);


            return(clonedMethod);
        }