From 27a70dbbfe867a0cd7b7bf4e0fa4a3f16fd33154 Mon Sep 17 00:00:00 2001 From: triton Date: Wed, 23 Jan 2013 09:56:14 +0000 Subject: [PATCH] We now create an extra public constructor that takes an IntPtr. We do this because by default native types are not exported as public in MSVC C++/CLI and we need to be able to instantiate these objects. --- .../Generators/CLI/CLIHeadersTemplate.cs | 3 +- .../Generators/CLI/CLISourcesTemplate.cs | 48 ++++++++++++------- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/Generator/Generators/CLI/CLIHeadersTemplate.cs b/src/Generator/Generators/CLI/CLIHeadersTemplate.cs index da2b3509..e6b82aa4 100644 --- a/src/Generator/Generators/CLI/CLIHeadersTemplate.cs +++ b/src/Generator/Generators/CLI/CLIHeadersTemplate.cs @@ -199,7 +199,7 @@ namespace Cxxi.Generators.CLI WriteLine("{"); WriteLine("public:"); - var nativeType = string.Format("::{0}*", @class.OriginalName); + var nativeType = string.Format("::{0}*", @class.QualifiedOriginalName); if (@class.IsRefType) { @@ -212,6 +212,7 @@ namespace Cxxi.Generators.CLI // Output a default constructor that takes the native pointer. PushIndent(); WriteLine("{0}({1} native);", SafeIdentifier(@class.Name), nativeType); + WriteLine("{0}({1} native);", SafeIdentifier(@class.Name), "System::IntPtr"); PopIndent(); if (@class.IsValueType) diff --git a/src/Generator/Generators/CLI/CLISourcesTemplate.cs b/src/Generator/Generators/CLI/CLISourcesTemplate.cs index bd44c302..e7634702 100644 --- a/src/Generator/Generators/CLI/CLISourcesTemplate.cs +++ b/src/Generator/Generators/CLI/CLISourcesTemplate.cs @@ -99,22 +99,9 @@ namespace Cxxi.Generators.CLI { GenerateDeclarationCommon(@class); - if (!@class.IsValueType) - { - // Output a default constructor that takes the native pointer. - Write("{0}::{1}(", QualifiedIdentifier(@class), SafeIdentifier(@class.Name)); - var nativeType = string.Format("::{0}*", @class.OriginalName); - WriteLine("{0} native)", nativeType); - WriteLine("{"); - - PushIndent(); - WriteLine("NativePtr = native;"); - PopIndent(); - - WriteLine("}"); - - NewLine(); - } + // Output a default constructor that takes the native pointer. + GenerateClassConstructor(@class, isIntPtr: false); + GenerateClassConstructor(@class, isIntPtr: true); foreach (var method in @class.Methods) { @@ -128,6 +115,35 @@ namespace Cxxi.Generators.CLI } } + private void GenerateClassConstructor(Class @class, bool isIntPtr) + { + Write("{0}::{1}(", QualifiedIdentifier(@class), SafeIdentifier(@class.Name)); + + var nativeType = string.Format("::{0}*", @class.OriginalName); + WriteLine("{0} native)", isIntPtr ? "System::IntPtr" : nativeType); + WriteLine("{"); + PushIndent(); + + if (@class.IsRefType) + { + Write("NativePtr = "); + if (isIntPtr) + Write("({0})", nativeType); + Write("native"); + if (isIntPtr) + Write(".ToPointer()"); + WriteLine(";"); + } + else + { + WriteLine("// TODO: Struct marshaling"); + } + + PopIndent(); + WriteLine("}"); + NewLine(); + } + public void GenerateMethod(Method method, Class @class) { GenerateDeclarationCommon(method);