Browse Source

Merge pull request #135 from ddobrev/master

Fixed the returning of empty values when the returned type is a structure
pull/136/head
João Matos 12 years ago
parent
commit
1a20f9ace4
  1. 6
      src/AST/Type.cs
  2. 15
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  3. 5
      tests/Basic/Basic.cpp
  4. 2
      tests/Basic/Basic.h

6
src/AST/Type.cs

@ -13,6 +13,12 @@ namespace CppSharp.AST
public bool IsDependent { get; set; } public bool IsDependent { get; set; }
public bool IsPrimitiveType()
{
PrimitiveType type;
return IsPrimitiveType(out type);
}
public bool IsPrimitiveType(out PrimitiveType primitive) public bool IsPrimitiveType(out PrimitiveType primitive)
{ {
var builtin = this as BuiltinType; var builtin = this as BuiltinType;

15
src/Generator/Generators/CSharp/CSharpTextTemplate.cs

@ -2068,11 +2068,20 @@ namespace CppSharp.Generators.CSharp
Type pointee; Type pointee;
if (retType.Type.IsPointerTo(out pointee) && isIntPtr) if (retType.Type.IsPointerTo(out pointee) && isIntPtr)
{ {
PrimitiveType primitive; pointee = pointee.Desugar();
string @null = (pointee.Desugar().IsPrimitiveType(out primitive) || string @null;
pointee.Desugar().IsPointer()) && Class @class;
if (pointee.IsTagDecl(out @class) && @class.IsValueType)
{
@null = string.Format("new {0}()", pointee);
}
else
{
@null = (pointee.IsPrimitiveType() ||
pointee.IsPointer()) &&
!CSharpTypePrinter.IsConstCharString(retType) ? !CSharpTypePrinter.IsConstCharString(retType) ?
"IntPtr.Zero" : "null"; "IntPtr.Zero" : "null";
}
WriteLine("if ({0} == global::System.IntPtr.Zero) return {1};", WriteLine("if ({0} == global::System.IntPtr.Zero) return {1};",
Generator.GeneratedIdentifier("ret"), @null); Generator.GeneratedIdentifier("ret"), @null);
} }

5
tests/Basic/Basic.cpp

@ -32,6 +32,11 @@ Bar::Item Bar::RetItem1()
return Bar::Item1; return Bar::Item1;
} }
Bar* Bar::returnPointerToValueType()
{
return this;
}
Bar2::Nested::operator int() const Bar2::Nested::operator int() const
{ {
return 300; return 300;

2
tests/Basic/Basic.h

@ -43,6 +43,8 @@ struct DLL_API Bar
Item RetItem1(); Item RetItem1();
int A; int A;
float B; float B;
Bar* returnPointerToValueType();
}; };
struct DLL_API Bar2 : public Bar struct DLL_API Bar2 : public Bar

Loading…
Cancel
Save