Browse Source

Extended the C# end with support for free constants and fixed their printing.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/681/head
Dimitar Dobrev 9 years ago
parent
commit
4b82838b54
  1. 15
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  2. 4
      src/Generator/Generators/CSharp/CSharpTypePrinter.cs
  3. 5
      tests/CSharp/AnotherUnit.cpp
  4. 5
      tests/CSharp/AnotherUnit.h
  5. 1
      tests/CSharp/CSharp.Tests.cs
  6. 4
      tests/CSharp/CSharp.cpp
  7. 1
      tests/CSharp/CSharp.h

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

@ -284,7 +284,8 @@ namespace CppSharp.Generators.CSharp
GenerateClassTemplateSpecializationInternal(@class); GenerateClassTemplateSpecializationInternal(@class);
} }
if (context.HasFunctions) if (context.HasFunctions || (!(context is Class) && context.Variables.Any(
v => v.IsGenerated && v.Access == AccessSpecifier.Public)))
{ {
PushBlock(CSharpBlockKind.Functions); PushBlock(CSharpBlockKind.Functions);
var parentName = Helpers.SafeIdentifier(context.TranslationUnit.FileNameWithoutExtension); var parentName = Helpers.SafeIdentifier(context.TranslationUnit.FileNameWithoutExtension);
@ -313,6 +314,10 @@ namespace CppSharp.Generators.CSharp
GenerateFunction(function, parentName); GenerateFunction(function, parentName);
} }
foreach (var variable in context.Variables.Where(
v => v.IsGenerated && v.Access == AccessSpecifier.Public))
GenerateVariable(null, variable);
WriteCloseBraceIndent(); WriteCloseBraceIndent();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
@ -1250,9 +1255,7 @@ namespace CppSharp.Generators.CSharp
if (variable.Access != AccessSpecifier.Public) if (variable.Access != AccessSpecifier.Public)
continue; continue;
var type = variable.Type; GenerateVariable(@class, variable);
GenerateVariable(@class, type, variable);
} }
} }
@ -1354,12 +1357,12 @@ namespace CppSharp.Generators.CSharp
return string.Format("this[{0}]", FormatMethodParameters(@params)); return string.Format("this[{0}]", FormatMethodParameters(@params));
} }
private void GenerateVariable(Class @class, Type type, Variable variable) private void GenerateVariable(Class @class, Variable variable)
{ {
PushBlock(CSharpBlockKind.Variable); PushBlock(CSharpBlockKind.Variable);
GenerateDeclarationCommon(variable); GenerateDeclarationCommon(variable);
WriteLine("public static {0} {1}", type, variable.Name); WriteLine("public static {0} {1}", variable.Type, variable.Name);
WriteStartBraceIndent(); WriteStartBraceIndent();
GeneratePropertyGetter(variable.QualifiedType, variable, @class); GeneratePropertyGetter(variable.QualifiedType, variable, @class);

4
src/Generator/Generators/CSharp/CSharpTypePrinter.cs

@ -671,6 +671,8 @@ namespace CppSharp.Generators.CSharp
names.Add(decl.Name); names.Add(decl.Name);
ctx = decl.Namespace; ctx = decl.Namespace;
} }
if (decl is Variable && !(decl.Namespace is Class))
names.Add(decl.TranslationUnit.FileNameWithoutExtension);
while (!(ctx is TranslationUnit)) while (!(ctx is TranslationUnit))
{ {
if (!string.IsNullOrWhiteSpace(ctx.Name)) if (!string.IsNullOrWhiteSpace(ctx.Name))
@ -693,7 +695,7 @@ namespace CppSharp.Generators.CSharp
public CSharpTypePrinterResult VisitVariableDecl(Variable variable) public CSharpTypePrinterResult VisitVariableDecl(Variable variable)
{ {
return variable.Name; return GetNestedQualifiedName(variable);
} }
public CSharpTypePrinterResult VisitClassTemplateDecl(ClassTemplate template) public CSharpTypePrinterResult VisitClassTemplateDecl(ClassTemplate template)

5
tests/CSharp/AnotherUnit.cpp

@ -3,3 +3,8 @@
void functionInAnotherUnit() void functionInAnotherUnit()
{ {
} }
namespace HasFreeConstant
{
extern const int DLL_API FREE_CONSTANT_IN_NAMESPACE = 5;
}

5
tests/CSharp/AnotherUnit.h

@ -11,3 +11,8 @@ class TemplateInAnotherUnit
class ForwardInOtherUnitButSameModule class ForwardInOtherUnitButSameModule
{ {
}; };
namespace HasFreeConstant
{
extern const int DLL_API FREE_CONSTANT_IN_NAMESPACE;
}

1
tests/CSharp/CSharp.Tests.cs

@ -215,6 +215,7 @@ public unsafe class CSharpTests : GeneratorTestFixture
methodsWithDefaultValues.DefaultWithDirectIntConstant(); methodsWithDefaultValues.DefaultWithDirectIntConstant();
methodsWithDefaultValues.DefaultWithEnumInLowerCasedNameSpace(); methodsWithDefaultValues.DefaultWithEnumInLowerCasedNameSpace();
methodsWithDefaultValues.DefaultWithCharFromInt(); methodsWithDefaultValues.DefaultWithCharFromInt();
methodsWithDefaultValues.DefaultWithFreeConstantInNameSpace();
} }
} }

4
tests/CSharp/CSharp.cpp

@ -587,6 +587,10 @@ void MethodsWithDefaultValues::defaultWithCharFromInt(char c)
{ {
} }
void MethodsWithDefaultValues::defaultWithFreeConstantInNameSpace(int c)
{
}
int MethodsWithDefaultValues::getA() int MethodsWithDefaultValues::getA()
{ {
return m_foo.A; return m_foo.A;

1
tests/CSharp/CSharp.h

@ -412,6 +412,7 @@ public:
void defaultWithDirectIntConstant(int arg = intConstant); void defaultWithDirectIntConstant(int arg = intConstant);
void defaultWithEnumInLowerCasedNameSpace(lowerCaseNameSpace::Enum e = lowerCaseNameSpace::Enum::Item2); void defaultWithEnumInLowerCasedNameSpace(lowerCaseNameSpace::Enum e = lowerCaseNameSpace::Enum::Item2);
void defaultWithCharFromInt(char c = 32); void defaultWithCharFromInt(char c = 32);
void defaultWithFreeConstantInNameSpace(int c = HasFreeConstant::FREE_CONSTANT_IN_NAMESPACE);
int getA(); int getA();
private: private:
Foo m_foo; Foo m_foo;

Loading…
Cancel
Save