Browse Source

Added better support for non-C# supported operator overloads.

pull/1/head
triton 13 years ago
parent
commit
df4e435dd0
  1. 45
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs

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

@ -1095,8 +1095,11 @@ namespace CppSharp.Generators.CSharp
Write("public "); Write("public ");
if (method.IsStatic || var isBuiltinOperator = false;
method.Kind == CXXMethodKind.Operator) if (method.IsOperator)
GetOperatorIdentifier(method.OperatorKind, out isBuiltinOperator);
if (method.IsStatic || (method.IsOperator && isBuiltinOperator))
Write("static "); Write("static ");
var functionName = GetFunctionIdentifier(method); var functionName = GetFunctionIdentifier(method);
@ -1213,15 +1216,33 @@ namespace CppSharp.Generators.CSharp
} }
public void GenerateFunctionCall(string functionName, List<Parameter> parameters, public void GenerateFunctionCall(string functionName, List<Parameter> parameters,
Function function, Class @class = null) Function function)
{ {
var retType = function.ReturnType; var retType = function.ReturnType;
var needsReturn = !retType.Type.IsPrimitiveType(PrimitiveType.Void); var needsReturn = !retType.Type.IsPrimitiveType(PrimitiveType.Void);
var method = function as Method; var method = function as Method;
var needsInstance = method != null && !method.IsStatic && !method.IsOperator;
var isValueType = @class != null && @class.IsValueType; bool isValueType = false;
bool needsInstance = false;
if (method != null)
{
var @class = method.Namespace as Class;
if (@class != null)
isValueType = @class.IsValueType;
needsInstance = !method.IsStatic;
if (method.IsOperator)
{
bool isBuiltin;
GetOperatorIdentifier(method.OperatorKind, out isBuiltin);
needsInstance &= !isBuiltin;
}
}
var needsFixedThis = needsInstance && isValueType; var needsFixedThis = needsInstance && isValueType;
Class retClass = null; Class retClass = null;
@ -1540,10 +1561,12 @@ namespace CppSharp.Generators.CSharp
WriteCloseBraceIndent(); WriteCloseBraceIndent();
} }
public static string GetOperatorIdentifier(CXXOperatorKind kind) public static string GetOperatorIdentifier(CXXOperatorKind kind,
out bool isBuiltin)
{ {
// These follow the order described in MSDN (Overloadable Operators). isBuiltin = true;
// These follow the order described in MSDN (Overloadable Operators).
switch (kind) switch (kind)
{ {
// These unary operators can be overloaded // These unary operators can be overloaded
@ -1602,8 +1625,11 @@ namespace CppSharp.Generators.CSharp
case CXXOperatorKind.Delete: case CXXOperatorKind.Delete:
case CXXOperatorKind.Array_New: case CXXOperatorKind.Array_New:
case CXXOperatorKind.Array_Delete: case CXXOperatorKind.Array_Delete:
default: throw new NotImplementedException(); isBuiltin = false;
return "Operator" + kind.ToString();
} }
throw new NotSupportedException();
} }
public string GetFunctionIdentifier(Function function) public string GetFunctionIdentifier(Function function)
@ -1612,6 +1638,7 @@ namespace CppSharp.Generators.CSharp
var isNativeContext = printer.ContextKind == CSharpTypePrinterContextKind.Native; var isNativeContext = printer.ContextKind == CSharpTypePrinterContextKind.Native;
string identifier; string identifier;
bool isBuiltin;
var method = function as Method; var method = function as Method;
if (method != null && method.IsOperator) if (method != null && method.IsOperator)
@ -1619,7 +1646,7 @@ namespace CppSharp.Generators.CSharp
if (isNativeContext) if (isNativeContext)
identifier = "Operator" + method.OperatorKind.ToString(); identifier = "Operator" + method.OperatorKind.ToString();
else else
identifier = GetOperatorIdentifier(method.OperatorKind); identifier = GetOperatorIdentifier(method.OperatorKind, out isBuiltin);
} }
else else
{ {

Loading…
Cancel
Save