Skip to content

Commit

Permalink
Generate valid C# for implicit conversion to const char*
Browse files Browse the repository at this point in the history
Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
  • Loading branch information
ddobrev committed Jun 26, 2019
1 parent 5a92220 commit 0e1fb0b
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
1 change: 0 additions & 1 deletion src/Generator/Generators/CSharp/CSharpSources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2901,7 +2901,6 @@ public void GenerateFunctionCall(string functionName, List<Parameter> parameters
ArgName = Helpers.ReturnIdentifier,
ReturnVarName = Helpers.ReturnIdentifier,
ReturnType = returnType,
Parameter = operatorParam,
Function = function
};

Expand Down
14 changes: 12 additions & 2 deletions tests/CSharp/CSharp.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public void TestDefaultArguments()
methodsWithDefaultValues.DefaultEmptyEnum();
methodsWithDefaultValues.DefaultRefTypeBeforeOthers();
methodsWithDefaultValues.DefaultRefTypeAfterOthers();
methodsWithDefaultValues.DefaultRefTypeBeforeAndAfterOthers(0, null);
methodsWithDefaultValues.DefaultRefTypeBeforeAndAfterOthers();
methodsWithDefaultValues.DefaultIntAssignedAnEnum();
methodsWithDefaultValues.defaultRefAssignedValue();
methodsWithDefaultValues.DefaultRefAssignedValue();
Expand Down Expand Up @@ -1276,7 +1276,17 @@ public void TestConstCharStarRef()
}

[Test]
public void Test()
public void TestImplicitConversionToString()
{
using (Foo foo = new Foo("name"))
{
string name = foo;
Assert.That(name, Is.EqualTo("name"));
}
}

[Test]
public void TestHasFunctionPointerField()
{
using (var hasFunctionPtrField = new HasFunctionPtrField())
{
Expand Down
39 changes: 39 additions & 0 deletions tests/CSharp/CSharp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,20 @@ Foo::Foo(const QString& name)

Foo::Foo(const char* name) : publicFieldMappedToEnum(TestFlag::Flag2)
{
if (name)
{
_name = name;
}
A = 10;
P = 50;
}

Foo::Foo(const Foo& other) : A(other.A), P(other.P),
templateInAnotherUnit(other.templateInAnotherUnit),
_name(other._name)
{
}

Foo::Foo(int a, int p) : publicFieldMappedToEnum(TestFlag::Flag2)
{
A = a;
Expand All @@ -26,6 +36,10 @@ Foo::Foo(wchar_t ch)
{
}

Foo::~Foo()
{
}

int Foo::method()
{
return 1;
Expand Down Expand Up @@ -104,6 +118,11 @@ int Foo::operator --()
return 4;
}

Foo::operator const char*() const
{
return _name.data();
}

const Foo& Bar::operator[](int i) const
{
return m_foo;
Expand Down Expand Up @@ -215,6 +234,10 @@ Bar::Bar(Items item)
{
}

Bar::~Bar()
{
}

int Bar::method()
{
return 2;
Expand Down Expand Up @@ -263,10 +286,18 @@ ForceCreationOfInterface::ForceCreationOfInterface()
{
}

ForceCreationOfInterface::~ForceCreationOfInterface()
{
}

Baz::Baz(Bar::Items item)
{
}

Baz::~Baz()
{
}

int Baz::takesQux(const Qux& qux)
{
return qux.farAwayFunc();
Expand Down Expand Up @@ -970,10 +1001,18 @@ InheritanceBuffer::InheritanceBuffer()
{
}

InheritanceBuffer::~InheritanceBuffer()
{
}

InheritsProtectedVirtualFromSecondaryBase::InheritsProtectedVirtualFromSecondaryBase()
{
}

InheritsProtectedVirtualFromSecondaryBase::~InheritsProtectedVirtualFromSecondaryBase()
{
}

void InheritsProtectedVirtualFromSecondaryBase::protectedVirtual()
{
}
Expand Down
9 changes: 9 additions & 0 deletions tests/CSharp/CSharp.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ class DLL_API Foo
public:
Foo(const QString& name);
Foo(const char* name = 0);
Foo(const Foo& other);
Foo(int a, int p = 0);
Foo(char16_t ch);
Foo(wchar_t ch);
~Foo();
int method();
int operator[](int i) const;
int operator[](unsigned int i);
Expand All @@ -38,13 +40,15 @@ class DLL_API Foo

int operator ++();
int operator --();
operator const char*() const;

bool btest[5];
QFlags<TestFlag> publicFieldMappedToEnum;

protected:
int P;
TemplateInAnotherUnit<int> templateInAnotherUnit;
std::string _name;
};

class DLL_API Quux
Expand Down Expand Up @@ -93,6 +97,7 @@ class DLL_API Bar : public Qux
Bar();
Bar(Qux qux);
Bar(Items item);
~Bar();
int method();
const Foo& operator[](int i) const;
Foo& operator[](int i);
Expand Down Expand Up @@ -121,6 +126,7 @@ class DLL_API ForceCreationOfInterface : public Foo, public Bar
{
public:
ForceCreationOfInterface();
~ForceCreationOfInterface();
};

class DLL_API Baz : public Foo, public Bar
Expand All @@ -136,6 +142,7 @@ class DLL_API Baz : public Foo, public Bar

Baz();
Baz(Bar::Items item);
~Baz();

int P;

Expand Down Expand Up @@ -728,12 +735,14 @@ class DLL_API InheritanceBuffer : public Foo, public HasProtectedVirtual
{
public:
InheritanceBuffer();
~InheritanceBuffer();
};

class DLL_API InheritsProtectedVirtualFromSecondaryBase : public InheritanceBuffer
{
public:
InheritsProtectedVirtualFromSecondaryBase();
~InheritsProtectedVirtualFromSecondaryBase();
protected:
void protectedVirtual();
};
Expand Down

0 comments on commit 0e1fb0b

Please sign in to comment.