コード例 #1
0
        private void GenerateClassDestructor(Class @class)
        {
            PushBlock(BlockKind.Destructor);

            WriteLine("{0}::~{1}()", QualifiedIdentifier(@class), @class.Name);
            WriteStartBraceIndent();

            if (CLIGenerator.ShouldGenerateClassNativeField(@class))
            {
                WriteLine("delete NativePtr;");
            }
            else if (@class.HasNonTrivialDestructor)
            {
                WriteLine("if (NativePtr)");
                WriteStartBraceIndent();
                WriteLine("auto __nativePtr = NativePtr;");
                WriteLine("NativePtr = 0;");
                WriteLine("delete (::{0}*) __nativePtr;", @class.QualifiedOriginalName);
                WriteCloseBraceIndent();
            }

            WriteCloseBraceIndent();

            PopBlock(NewLineKind.BeforeNextBlock);
        }
コード例 #2
0
ファイル: CLISources.cs プロジェクト: tornado12345/CppSharp
        private void GenerateClassDestructor(Class @class)
        {
            PushBlock(BlockKind.Destructor);

            WriteLine("{0}::~{1}()", QualifiedIdentifier(@class), @class.Name);
            WriteOpenBraceAndIndent();

            PushBlock(BlockKind.DestructorBody, @class);

            if (CLIGenerator.ShouldGenerateClassNativeField(@class))
            {
                WriteLine("delete NativePtr;");
            }
            else if (@class.HasNonTrivialDestructor)
            {
                WriteLine("if (NativePtr)");
                WriteOpenBraceAndIndent();
                WriteLine("auto __nativePtr = NativePtr;");
                WriteLine("NativePtr = 0;");
                WriteLine($"delete ({typePrinter.PrintTag(@class)}::{@class.QualifiedOriginalName}*) __nativePtr;", @class.QualifiedOriginalName);
                UnindentAndWriteCloseBrace();
            }

            PopBlock();

            UnindentAndWriteCloseBrace();

            PopBlock(NewLineKind.BeforeNextBlock);
        }
コード例 #3
0
        private void GenerateClassConstructor(Class @class, bool withOwnNativeInstanceParam = false)
        {
            string qualifiedIdentifier = QualifiedIdentifier(@class);

            Write("{0}::{1}(", qualifiedIdentifier, @class.Name);

            var nativeType = string.Format("::{0}*", @class.QualifiedOriginalName);

            WriteLine(!withOwnNativeInstanceParam ? "{0} native)" : "{0} native, const bool ownNativeInstance)", nativeType);

            var hasBase = GenerateClassConstructorBase(@class, null, withOwnNativeInstanceParam);

            if (CLIGenerator.ShouldGenerateClassNativeField(@class))
            {
                Indent();
                Write(hasBase ? "," : ":");
                Unindent();

                WriteLine(!withOwnNativeInstanceParam ? " {0}(false)" : " {0}(ownNativeInstance)", Helpers.OwnsNativeInstanceIdentifier);
            }

            WriteOpenBraceAndIndent();

            PushBlock(BlockKind.ConstructorBody, @class);

            const string nativePtr = "native";

            if (@class.IsRefType)
            {
                if (!hasBase)
                {
                    WriteLine("NativePtr = {0};", nativePtr);
                }
            }
            else
            {
                GenerateStructMarshaling(@class, nativePtr + "->");
            }

            PopBlock();

            UnindentAndWriteCloseBrace();

            if (!withOwnNativeInstanceParam)
            {
                NewLine();
                WriteLine("{0}^ {0}::{1}(::System::IntPtr native)", qualifiedIdentifier, Helpers.CreateInstanceIdentifier);

                WriteOpenBraceAndIndent();

                WriteLine("return gcnew ::{0}(({1}) native.ToPointer());", qualifiedIdentifier, nativeType);

                UnindentAndWriteCloseBrace();
                NewLine();

                GenerateClassConstructor(@class, true);
            }
        }
コード例 #4
0
ファイル: CLISources.cs プロジェクト: tornado12345/CppSharp
        private void GenerateClassConstructor(Class @class, bool withOwnNativeInstanceParam = false)
        {
            string qualifiedIdentifier = QualifiedIdentifier(@class);

            Write("{0}::{1}(", qualifiedIdentifier, @class.Name);

            string nativeType = $"{typePrinter.PrintTag(@class)}::{@class.QualifiedOriginalName}*";

            WriteLine(!withOwnNativeInstanceParam ? "{0} native)" : "{0} native, bool ownNativeInstance)", nativeType);

            var hasBase = GenerateClassConstructorBase(@class, null, withOwnNativeInstanceParam);

            if (CLIGenerator.ShouldGenerateClassNativeField(@class))
            {
                Indent();
                Write(hasBase ? "," : ":");
                Unindent();

                WriteLine(!withOwnNativeInstanceParam ? " {0}(false)" : " {0}(ownNativeInstance)", Helpers.OwnsNativeInstanceIdentifier);
            }

            WriteOpenBraceAndIndent();

            PushBlock(BlockKind.ConstructorBody, @class);

            const string nativePtr = "native";

            if (@class.IsRefType)
            {
                if (!hasBase)
                {
                    WriteLine("NativePtr = {0};", nativePtr);
                }
            }
            else
            {
                GenerateStructMarshaling(@class, nativePtr + "->");
            }

            PopBlock();

            UnindentAndWriteCloseBrace();

            string createInstanceParams       = withOwnNativeInstanceParam ? $"::System::IntPtr native, bool {Helpers.OwnsNativeInstanceIdentifier}" : "::System::IntPtr native";
            string createInstanceParamsValues = withOwnNativeInstanceParam ? $"({nativeType}) native.ToPointer(), {Helpers.OwnsNativeInstanceIdentifier}" : $"({nativeType}) native.ToPointer()";

            NewLine();
            WriteLine($"{qualifiedIdentifier}^ {qualifiedIdentifier}::{Helpers.CreateInstanceIdentifier}({createInstanceParams})");

            WriteOpenBraceAndIndent();

            WriteLine($"return gcnew ::{qualifiedIdentifier}({createInstanceParamsValues});");

            UnindentAndWriteCloseBrace();
            NewLine();
        }
コード例 #5
0
        public void GenerateClass(Class @class)
        {
            if ([email protected] || @class.IsIncomplete)
            {
                return;
            }

            GenerateDeclarationCommon(@class);

            if (GenerateClassProlog(@class))
            {
                return;
            }

            // Process the nested types.
            PushIndent();
            GenerateDeclContext(@class);
            PopIndent();

            var nativeType = string.Format("::{0}*", @class.QualifiedOriginalName);

            if (CLIGenerator.ShouldGenerateClassNativeField(@class))
            {
                GenerateClassNativeField(@class, nativeType);
            }

            GenerateClassConstructors(@class, nativeType);

            GenerateClassProperties(@class);

            GenerateClassEvents(@class);
            GenerateClassMethods(@class.Methods);

            if (Options.GenerateFunctionTemplates)
            {
                GenerateClassGenericMethods(@class);
            }

            GenerateClassVariables(@class);

            PushBlock(CLIBlockKind.AccessSpecifier);
            WriteLine("private:");
            var accBlock = PopBlock(NewLineKind.IfNotEmpty);

            PushBlock(CLIBlockKind.Fields);
            GenerateClassFields(@class);
            var fieldsBlock = PopBlock();

            accBlock.CheckGenerate = () => !fieldsBlock.IsEmpty;

            WriteLine("};");
        }
コード例 #6
0
        private void GenerateClassConstructor(Class @class)
        {
            string qualifiedIdentifier = QualifiedIdentifier(@class);

            Write("{0}::{1}(", qualifiedIdentifier, @class.Name);

            var nativeType = string.Format("::{0}*", @class.QualifiedOriginalName);

            WriteLine("{0} native)", nativeType);

            var hasBase = GenerateClassConstructorBase(@class);

            if (CLIGenerator.ShouldGenerateClassNativeField(@class))
            {
                PushIndent();
                Write(hasBase ? "," : ":");
                PopIndent();

                WriteLine(" {0}(false)", Helpers.OwnsNativeInstanceIdentifier);
            }

            WriteStartBraceIndent();

            const string nativePtr = "native";

            if (@class.IsRefType)
            {
                if (!hasBase)
                {
                    WriteLine("NativePtr = {0};", nativePtr);
                }
            }
            else
            {
                GenerateStructMarshaling(@class, nativePtr + "->");
            }

            WriteCloseBraceIndent();
            NewLine();
            WriteLine("{0}^ {0}::{1}(::System::IntPtr native)", qualifiedIdentifier, Helpers.CreateInstanceIdentifier);

            WriteStartBraceIndent();

            WriteLine("return gcnew ::{0}(({1}) native.ToPointer());", qualifiedIdentifier, nativeType);

            WriteCloseBraceIndent();
            NewLine();
        }
コード例 #7
0
        private void GenerateClassFinalizer(Class @class)
        {
            PushBlock(BlockKind.Finalizer);

            WriteLine("{0}::!{1}()", QualifiedIdentifier(@class), @class.Name);
            WriteStartBraceIndent();

            if (CLIGenerator.ShouldGenerateClassNativeField(@class))
            {
                WriteLine("delete NativePtr;");
            }

            WriteCloseBraceIndent();

            PopBlock(NewLineKind.BeforeNextBlock);
        }
コード例 #8
0
        private void GenerateClassDestructor(Class @class)
        {
            PushBlock(CLIBlockKind.Destructor);

            WriteLine("{0}::~{1}()", QualifiedIdentifier(@class), @class.Name);
            WriteStartBraceIndent();

            if (CLIGenerator.ShouldGenerateClassNativeField(@class))
            {
                WriteLine("if ({0})", Helpers.OwnsNativeInstanceIdentifier);
                WriteLineIndent("delete NativePtr;");
            }

            WriteCloseBraceIndent();

            PopBlock(NewLineKind.BeforeNextBlock);
        }
コード例 #9
0
ファイル: CLISources.cs プロジェクト: tornado12345/CppSharp
        private void GeneratePropertyGetter <T>(T decl, Class @class, string name, Type type)
            where T : Declaration, ITypedDecl
        {
            if (decl == null)
            {
                return;
            }

            var method    = decl as Method;
            var isIndexer = method != null &&
                            method.OperatorKind == CXXOperatorKind.Subscript;

            var args = new List <string>();

            if (isIndexer)
            {
                var indexParameter = method.Parameters[0];
                args.Add(string.Format("{0} {1}", indexParameter.Type, indexParameter.Name));
            }

            WriteLine("{0} {1}::{2}::get({3})", type, QualifiedIdentifier(@class),
                      name, string.Join(", ", args));

            WriteOpenBraceAndIndent();

            if (decl is Function)
            {
                var func = decl as Function;
                if (isIndexer && func.Type.IsAddress())
                {
                    GenerateFunctionCall(func, @class, type);
                }
                else
                {
                    GenerateFunctionCall(func, @class);
                }
            }
            else
            {
                if (@class.IsValueType && decl is Field)
                {
                    WriteLine($"return {decl.Name};");
                    UnindentAndWriteCloseBrace();
                    NewLine();
                    return;
                }

                string variable;
                if (decl is Variable)
                {
                    variable = $"::{@class.QualifiedOriginalName}::{decl.OriginalName}";
                }
                else if (CLIGenerator.ShouldGenerateClassNativeField(@class))
                {
                    variable = $"NativePtr->{decl.OriginalName}";
                }
                else
                {
                    variable = $"(({typePrinter.PrintTag(@class)}::{@class.QualifiedOriginalName}*)NativePtr)->{decl.OriginalName}";
                }

                var ctx = new MarshalContext(Context, CurrentIndentation)
                {
                    ArgName       = decl.Name,
                    ReturnVarName = variable,
                    ReturnType    = decl.QualifiedType
                };
                ctx.PushMarshalKind(MarshalKind.NativeField);

                var marshal = new CLIMarshalNativeToManagedPrinter(ctx);
                decl.Visit(marshal);

                if (!string.IsNullOrWhiteSpace(marshal.Context.Before))
                {
                    Write(marshal.Context.Before);
                }

                WriteLine($"return {marshal.Context.Return};");
            }


            UnindentAndWriteCloseBrace();
            NewLine();
        }
コード例 #10
0
ファイル: CLISources.cs プロジェクト: tornado12345/CppSharp
        public void GenerateClass(Class @class)
        {
            PushBlock(BlockKind.Class);

            GenerateDeclContext(@class);

            GenerateClassConstructors(@class);

            GenerateClassMethods(@class, @class);

            if (CLIGenerator.ShouldGenerateClassNativeField(@class))
            {
                var qualifiedIdentifier = QualifiedIdentifier(@class);

                PushBlock(BlockKind.Method);
                WriteLine("::System::IntPtr {0}::{1}::get()",
                          qualifiedIdentifier, Helpers.InstanceIdentifier);
                WriteOpenBraceAndIndent();
                WriteLine("return ::System::IntPtr(NativePtr);");
                UnindentAndWriteCloseBrace();
                PopBlock(NewLineKind.BeforeNextBlock);

                PushBlock(BlockKind.Method);
                WriteLine("void {0}::{1}::set(::System::IntPtr object)",
                          qualifiedIdentifier, Helpers.InstanceIdentifier);
                WriteOpenBraceAndIndent();
                var nativeType = $"{typePrinter.PrintTag(@class)}::{@class.QualifiedOriginalName}*";
                WriteLine("NativePtr = ({0})object.ToPointer();", nativeType);
                UnindentAndWriteCloseBrace();
                PopBlock(NewLineKind.BeforeNextBlock);
            }

            GenerateClassProperties(@class, @class);

            foreach (var @event in @class.Events)
            {
                if ([email protected])
                {
                    continue;
                }

                GenerateDeclarationCommon(@event);
                GenerateEvent(@event, @class);
            }

            foreach (var variable in @class.Variables)
            {
                if (!variable.IsGenerated)
                {
                    continue;
                }

                if (variable.Access != AccessSpecifier.Public)
                {
                    continue;
                }

                GenerateDeclarationCommon(variable);
                GenerateVariable(variable, @class);
            }

            PopBlock();
        }
コード例 #11
0
ファイル: CLIHeaders.cs プロジェクト: galek/CppSharp
        public void GenerateClass(Class @class)
        {
            if ([email protected] || @class.IsIncomplete)
            {
                return;
            }

            GenerateDeclarationCommon(@class);

            GenerateClassSpecifier(@class);

            if (@class.IsOpaque)
            {
                WriteLine(";");
                return;
            }

            NewLine();
            WriteLine("{");
            WriteLine("public:");
            NewLine();

            // Process the nested types.
            Indent();
            GenerateDeclContext(@class);
            Unindent();

            string nativeType = $"{typePrinter.PrintTag(@class)}::{@class.QualifiedOriginalName}*";

            if (CLIGenerator.ShouldGenerateClassNativeField(@class))
            {
                GenerateClassNativeField(nativeType);
            }

            GenerateClassConstructors(@class, nativeType);

            GenerateClassProperties(@class);

            GenerateClassEvents(@class);
            GenerateClassMethods(@class.Methods);

            if (Options.GenerateFunctionTemplates)
            {
                GenerateClassGenericMethods(@class);
            }

            GenerateClassVariables(@class);

            if (CLIGenerator.ShouldGenerateClassNativeField(@class))
            {
                PushBlock(BlockKind.AccessSpecifier);
                WriteLine("protected:");
                PopBlock(NewLineKind.IfNotEmpty);

                PushBlock(BlockKind.Fields);
                WriteLineIndent("bool {0};", Helpers.OwnsNativeInstanceIdentifier);
                PopBlock();
            }

            PushBlock(BlockKind.AccessSpecifier);
            WriteLine("private:");
            var accBlock = PopBlock(NewLineKind.IfNotEmpty);

            PushBlock(BlockKind.Fields);
            GenerateClassFields(@class);
            var fieldsBlock = PopBlock();

            accBlock.CheckGenerate = () => !fieldsBlock.IsEmpty;

            WriteLine("};");
        }