Browse Source

The extension method Type.IsPointerTo<T> will take attributed types into account.

Fixed generation of attributed delegate types in my case.
pull/225/head
Elias Holzer 11 years ago
parent
commit
15f1394aaf
  1. 33
      src/AST/TypeExtensions.cs
  2. 8
      tests/Basic/Basic.Tests.cs
  3. 8
      tests/Basic/Basic.h

33
src/AST/TypeExtensions.cs

@ -90,26 +90,19 @@
if (ptr == null) if (ptr == null)
return false; return false;
return ptr.Pointee.IsPrimitiveType(primitive); return ptr.Pointee.IsPrimitiveType(primitive);
} }
public static bool IsPointerTo<T>(this Type t, out T type) where T : Type public static bool IsPointerTo<T>(this Type t, out T type) where T : Type
{ {
var ptr = t as PointerType; var pointee = t.GetPointee();
type = pointee as T;
if (ptr == null) if (type == null)
{ {
var functionPointer = t as MemberPointerType; var attributedType = pointee as AttributedType;
if (functionPointer != null) if (attributedType != null)
{ type = attributedType.Modified.Type as T;
type = functionPointer.Pointee as T; }
return type != null; return type != null;
}
type = null;
return false;
}
type = ptr.Pointee as T;
return type != null;
} }
public static bool IsTagDecl<T>(this Type t, out T decl) where T : Declaration public static bool IsTagDecl<T>(this Type t, out T decl) where T : Declaration

8
tests/Basic/Basic.Tests.cs

@ -176,6 +176,14 @@ public class BasicTests : GeneratorTestFixture
var delegates = new TestDelegates(); var delegates = new TestDelegates();
var doubleSum = delegates.A(2) + delegates.B(2); var doubleSum = delegates.A(2) + delegates.B(2);
Assert.AreEqual(8, doubleSum); Assert.AreEqual(8, doubleSum);
}
[Test]
public void TestAttributedDelegate()
{
var attributedDelegate = basic.GetAttributedDelegate();
var result = attributedDelegate(2);
Assert.AreEqual(4, result);
} }
[Test] [Test]

8
tests/Basic/Basic.h

@ -259,6 +259,14 @@ struct DLL_API TestDelegates
MemberDelegate C; MemberDelegate C;
}; };
// Tests delegate generation for attributed function types
typedef int(__cdecl *AttributedDelegate)(int n);
DLL_API int __cdecl Double(int n) { return n * 2; }
DLL_API AttributedDelegate GetAttributedDelegate()
{
return Double;
}
// Tests memory leaks in constructors // Tests memory leaks in constructors
// C#: Marshal.FreeHGlobal(arg0); // C#: Marshal.FreeHGlobal(arg0);
struct DLL_API TestMemoryLeaks struct DLL_API TestMemoryLeaks

Loading…
Cancel
Save