Browse Source

Extended namespaces tests to consider taking a parameter and returning a value of types declared in other libraries. Implemented QualifiedIdentifierIfNeeded().

pull/408/head
Pyry Kontio 11 years ago
parent
commit
575088aeb6
  1. 45
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  2. 6
      tests/NamespacesBase/NamespacesBase.h
  3. 50
      tests/NamespacesDerived/NamespacesDerived.cpp
  4. 35
      tests/NamespacesDerived/NamespacesDerived.h

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

@ -105,6 +105,43 @@ namespace CppSharp.Generators.CSharp
#region Identifiers #region Identifiers
// Takes a declaration (type, class etc.) that is referenced from a context, and the context.
// If the referenced name needs a qualification in the context, add it. Otherwise, return just the name.
public string QualifiedIdentifierIfNeeded(Declaration context, Declaration reference)
{
var refNames = new Stack<string>();
var ctxNames = new Stack<string>();
var refCtx = reference;
while (refCtx != null)
{
if (!string.IsNullOrWhiteSpace(refCtx.Name))
refNames.Push(refCtx.Name);
refCtx = refCtx.Namespace;
}
var ctxCtx = context;
while (ctxCtx != null)
{
if (!string.IsNullOrWhiteSpace(ctxCtx.Name))
ctxNames.Push(ctxCtx.Name);
ctxCtx = ctxCtx.Namespace;
}
if (context.GenerationKind == GenerationKind.Generate && Options.GenerateLibraryNamespace)
ctxNames.Push(Options.OutputNamespace);
if (reference.GenerationKind == GenerationKind.Generate && Options.GenerateLibraryNamespace)
refNames.Push(Options.OutputNamespace);
while (refNames.Count > 0 && ctxNames.Count > 0 &&refNames.Peek() == ctxNames.Peek())
{
refNames.Pop();
ctxNames.Pop();
}
return string.Join(".", refNames);
}
public string QualifiedIdentifier(Declaration decl) public string QualifiedIdentifier(Declaration decl)
{ {
var names = new List<string> { decl.Name }; var names = new List<string> { decl.Name };
@ -633,7 +670,7 @@ namespace CppSharp.Generators.CSharp
bases.AddRange( bases.AddRange(
from @base in @class.Bases from @base in @class.Bases
where @base.IsClass where @base.IsClass
select QualifiedIdentifier(@base.Class)); select QualifiedIdentifierIfNeeded(@class, @base.Class));
} }
if (@class.IsGenerated) if (@class.IsGenerated)
@ -1781,7 +1818,7 @@ namespace CppSharp.Generators.CSharp
var hasBaseClass = @class.HasBaseClass && @class.BaseClass.IsRefType; var hasBaseClass = @class.HasBaseClass && @class.BaseClass.IsRefType;
if (hasBaseClass) if (hasBaseClass)
WriteLineIndent(": base(({0}.Internal*) native{1})", WriteLineIndent(": base(({0}.Internal*) native{1})",
QualifiedIdentifier(@class.BaseClass), @class.IsAbstractImpl ? ", true" : string.Empty); QualifiedIdentifierIfNeeded(@class, @class.BaseClass), @class.IsAbstractImpl ? ", true" : string.Empty);
WriteStartBraceIndent(); WriteStartBraceIndent();
@ -1828,7 +1865,7 @@ namespace CppSharp.Generators.CSharp
// Allocate memory for a new native object and call the ctor. // Allocate memory for a new native object and call the ctor.
WriteLine("var ret = Marshal.AllocHGlobal({0});", @class.Layout.Size); WriteLine("var ret = Marshal.AllocHGlobal({0});", @class.Layout.Size);
WriteLine("{0}.Internal.{1}(ret, new global::System.IntPtr(&native));", WriteLine("{0}.Internal.{1}(ret, new global::System.IntPtr(&native));",
QualifiedIdentifier(@class), GetFunctionNativeIdentifier(copyCtorMethod)); QualifiedIdentifierIfNeeded(@class, @class), GetFunctionNativeIdentifier(copyCtorMethod));
WriteLine("return ({0}.Internal*) ret;", className); WriteLine("return ({0}.Internal*) ret;", className);
} }
else else
@ -2243,7 +2280,7 @@ namespace CppSharp.Generators.CSharp
if (construct == null) if (construct == null)
{ {
WriteLine("var {0} = new {1}.Internal();", GeneratedIdentifier("ret"), WriteLine("var {0} = new {1}.Internal();", GeneratedIdentifier("ret"),
QualifiedIdentifier(retClass.OriginalClass ?? retClass)); QualifiedIdentifierIfNeeded(function, retClass.OriginalClass ?? retClass));
} }
else else
{ {

6
tests/NamespacesBase/NamespacesBase.h

@ -3,7 +3,7 @@
namespace OverlappingNamespace namespace OverlappingNamespace
{ {
enum Colors { enum ColorsEnum {
white, white,
black, black,
red, red,
@ -21,6 +21,8 @@ namespace OverlappingNamespace
}; };
} }
class DLL_API Base class DLL_API Base
{ {
public: public:
@ -29,4 +31,4 @@ public:
private: private:
int b; int b;
}; };

50
tests/NamespacesDerived/NamespacesDerived.cpp

@ -1,12 +1,60 @@
#include "NamespacesDerived.h" #include "NamespacesDerived.h"
OverlappingNamespace::InDerivedLib::InDerivedLib() : parentNSComponent(), color(black)
{
}
Derived::Derived() : Base(10), baseComponent(5), nestedNSComponent(), color(OverlappingNamespace::blue) Derived::Derived() : Base(10), baseComponent(5), nestedNSComponent(), color(OverlappingNamespace::blue)
{ {
} }
Base Derived::getBase()
{
return baseComponent;
}
OverlappingNamespace::InDerivedLib::InDerivedLib() : parentNSComponent(), color(black) void Derived::setBase(Base b)
{
baseComponent = b;
}
OverlappingNamespace::InBaseLib Derived::getNestedNSComponent()
{
return nestedNSComponent;
}
void Derived::setNestedNSComponent(OverlappingNamespace::InBaseLib c)
{
nestedNSComponent = c;
}
Base2::Base2()
{
}
Derived2::Derived2() : Base2()
{
}
Base2 Derived2::getBase()
{ {
return baseComponent;
}
void Derived2::setBase(Base2 b)
{
baseComponent = b;
}
OverlappingNamespace::InDerivedLib Derived2::getNestedNSComponent()
{
return nestedNSComponent;
}
void Derived2::setNestedNSComponent(OverlappingNamespace::InDerivedLib c)
{
nestedNSComponent = c;
} }

35
tests/NamespacesDerived/NamespacesDerived.h

@ -1,6 +1,8 @@
#include "../Tests.h" #include "../Tests.h"
#include "../NamespacesBase/NamespacesBase.h" #include "../NamespacesBase/NamespacesBase.h"
// Namespace clashes with NamespacesBase.OverlappingNamespace
// Test whether qualified names turn out right.
namespace OverlappingNamespace namespace OverlappingNamespace
{ {
@ -9,21 +11,50 @@ namespace OverlappingNamespace
public: public:
InDerivedLib(); InDerivedLib();
Base parentNSComponent; Base parentNSComponent;
Colors color; ColorsEnum color;
}; };
} }
// Using a type imported from a different library.
class DLL_API Derived : public Base class DLL_API Derived : public Base
{ {
public: public:
Derived(); Derived();
Base baseComponent; Base baseComponent;
Base getBase();
void setBase(Base);
OverlappingNamespace::InBaseLib nestedNSComponent; OverlappingNamespace::InBaseLib nestedNSComponent;
OverlappingNamespace::InBaseLib getNestedNSComponent();
void setNestedNSComponent(OverlappingNamespace::InBaseLib);
OverlappingNamespace::Colors color; OverlappingNamespace::ColorsEnum color;
private: private:
int d; int d;
}; };
// For reference: using a type derived in the same library
class Base2
{
public:
Base2();
};
class Derived2 : public Base2
{
public:
Derived2();
Base2 baseComponent;
Base2 getBase();
void setBase(Base2);
OverlappingNamespace::InDerivedLib nestedNSComponent;
OverlappingNamespace::InDerivedLib getNestedNSComponent();
void setNestedNSComponent(OverlappingNamespace::InDerivedLib);
};

Loading…
Cancel
Save