Skip to content

Commit 1a1308a

Browse files
committed
Fixed the generated C# for public fields with type a dependent function pointer.
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent c264690 commit 1a1308a

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

src/Generator/Generators/CSharp/CSharpMarshal.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -270,13 +270,18 @@ public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals)
270270
var functionType = decl.Type as FunctionType;
271271
if (functionType != null || decl.Type.IsPointerTo(out functionType))
272272
{
273-
var ptrName = Generator.GeneratedIdentifier("ptr") +
274-
Context.ParameterIndex;
275-
276-
Context.Before.WriteLine("var {0} = {1};", ptrName,
277-
Context.ReturnVarName);
278-
279-
var res = $"{ptrName} == IntPtr.Zero? null : ({typedef})Marshal.GetDelegateForFunctionPointer({ptrName}, typeof({typedef}))";
273+
var ptrName = $"{Generator.GeneratedIdentifier("ptr")}{Context.ParameterIndex}";
274+
275+
Context.Before.WriteLine($"var {ptrName} = {Context.ReturnVarName};");
276+
277+
var specialization = decl.Namespace as ClassTemplateSpecialization;
278+
var res = string.Format(
279+
"{0} == IntPtr.Zero? null : {1}({2}) Marshal.GetDelegateForFunctionPointer({0}, typeof({2}))",
280+
ptrName,
281+
specialization == null ? string.Empty :
282+
$@"({specialization.TemplatedDecl.TemplatedClass.Typedefs.First(
283+
t => t.Name == decl.Name).Visit(this.typePrinter)}) (object) ",
284+
typedef);
280285
Context.Return.Write(res);
281286
return true;
282287
}

tests/CSharp/CSharpTemplates.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ class DLL_API DependentValueFields : public Base<T>
137137
T getDependentValue();
138138
void setDependentValue(const T& value);
139139
IndependentFields<Nested> returnNestedInTemplate();
140+
typedef void (*DependentFunctionPointer)(T);
141+
DependentFunctionPointer dependentFunctionPointerField;
140142
private:
141143
T field{};
142144
union {

0 commit comments

Comments
 (0)