Browse Source

Added the ability to only enable a given type map for a selected back-end.

Signed-off-by: Dimitar Dobrev <dpldobrev@yahoo.com>
pull/344/head
Dimitar Dobrev 11 years ago
parent
commit
9e5be9e92c
  1. 2
      src/Generator/Driver.cs
  2. 4
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  3. 4
      src/Generator/Types/Std/Stdlib.cs
  4. 20
      src/Generator/Types/TypeMap.cs
  5. 5
      tests/Basic/Basic.cpp
  6. 9
      tests/Basic/Basic.h
  7. 7
      tests/CLITemp/CLITemp.Tests.cs
  8. 5
      tests/CLITemp/CLITemp.cpp
  9. 2
      tests/CLITemp/CLITemp.h

2
src/Generator/Driver.cs

@ -73,7 +73,7 @@ namespace CppSharp
{ {
ValidateOptions(Options); ValidateOptions(Options);
TypeDatabase.SetupTypeMaps(); TypeDatabase.SetupTypeMaps(Options.GeneratorKind);
Generator = CreateGeneratorFromKind(Options.GeneratorKind); Generator = CreateGeneratorFromKind(Options.GeneratorKind);
} }

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

@ -790,8 +790,8 @@ namespace CppSharp.Generators.CSharp
// we do not support dependent fields yet, see https://github.com/mono/CppSharp/issues/197 // we do not support dependent fields yet, see https://github.com/mono/CppSharp/issues/197
Class @class; Class @class;
field.Type.TryGetClass(out @class); field.Type.TryGetClass(out @class);
if (field.Type.IsDependent && !field.Type.IsPointer() && if ((field.Type.IsDependent && !field.Type.IsPointer() &&
!(@class != null && @class.IsUnion)) !(@class != null && @class.IsUnion)) || (@class != null && @class.TranslationUnit.IsSystemHeader))
return; return;
var safeIdentifier = Helpers.SafeIdentifier(field.OriginalName); var safeIdentifier = Helpers.SafeIdentifier(field.OriginalName);

4
src/Generator/Types/Std/Stdlib.cs

@ -26,7 +26,7 @@ namespace CppSharp.Types.Std
} }
} }
[TypeMap("std::string")] [TypeMap("std::string", GeneratorKind.CLI)]
public class String : TypeMap public class String : TypeMap
{ {
public override string CLISignature(CLITypePrinterContext ctx) public override string CLISignature(CLITypePrinterContext ctx)
@ -305,7 +305,7 @@ namespace CppSharp.Types.Std
} }
} }
[TypeMap("std::ostream")] [TypeMap("std::ostream", GeneratorKind.CLI)]
public class OStream : TypeMap public class OStream : TypeMap
{ {
public override string CLISignature(CLITypePrinterContext ctx) public override string CLISignature(CLITypePrinterContext ctx)

20
src/Generator/Types/TypeMap.cs

@ -15,11 +15,18 @@ namespace CppSharp.Types
public class TypeMapAttribute : Attribute public class TypeMapAttribute : Attribute
{ {
public string Type { get; private set; } public string Type { get; private set; }
public GeneratorKind? GeneratorKind { get; set; }
public TypeMapAttribute(string type) public TypeMapAttribute(string type)
{ {
Type = type; Type = type;
} }
public TypeMapAttribute(string type, GeneratorKind generatorKind)
{
Type = type;
GeneratorKind = generatorKind;
}
} }
/// <summary> /// <summary>
@ -117,27 +124,28 @@ namespace CppSharp.Types
TypeMaps = new Dictionary<string, System.Type>(); TypeMaps = new Dictionary<string, System.Type>();
} }
public void SetupTypeMaps() public void SetupTypeMaps(GeneratorKind generatorKind)
{ {
var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies(); var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (var assembly in loadedAssemblies) foreach (var assembly in loadedAssemblies)
{ {
var types = assembly.FindDerivedTypes(typeof(TypeMap)); var types = assembly.FindDerivedTypes(typeof(TypeMap));
SetupTypeMaps(types); SetupTypeMaps(types, generatorKind);
} }
} }
private void SetupTypeMaps(IEnumerable<System.Type> types) private void SetupTypeMaps(IEnumerable<System.Type> types, GeneratorKind generatorKind)
{ {
foreach (var typeMap in types) foreach (var typeMap in types)
{ {
var attrs = typeMap.GetCustomAttributes(typeof(TypeMapAttribute), true); var attrs = typeMap.GetCustomAttributes(typeof(TypeMapAttribute), true);
if (attrs == null) continue;
foreach (TypeMapAttribute attr in attrs) foreach (TypeMapAttribute attr in attrs)
{ {
TypeMaps[attr.Type] = typeMap; if (attr.GeneratorKind == null || attr.GeneratorKind == generatorKind)
{
TypeMaps[attr.Type] = typeMap;
}
} }
} }
} }

5
tests/Basic/Basic.cpp

@ -308,3 +308,8 @@ void va_listFunction(va_list v)
void TestDelegates::MarshalUnattributedDelegate(DelegateInGlobalNamespace del) void TestDelegates::MarshalUnattributedDelegate(DelegateInGlobalNamespace del)
{ {
} }
std::string HasStdString::testStdString(std::string s)
{
return s + "_test";
}

9
tests/Basic/Basic.h

@ -1,6 +1,7 @@
#include "../Tests.h" #include "../Tests.h"
#include <vadefs.h> #include <vadefs.h>
#include <string>
class DLL_API IgnoredType class DLL_API IgnoredType
{ {
@ -571,3 +572,11 @@ struct DLL_API TestEmptyName
}; };
}; };
}; };
class DLL_API HasStdString
{
// test if these are ignored with the C# back-end
public:
std::string testStdString(std::string s);
std::string s;
};

7
tests/CLITemp/CLITemp.Tests.cs

@ -1,7 +1,6 @@
using CppSharp.Utils; using CppSharp.Utils;
using NUnit.Framework; using NUnit.Framework;
using CLITemp; using CLITemp;
using System;
public class CLITests : GeneratorTestFixture public class CLITests : GeneratorTestFixture
{ {
@ -20,4 +19,10 @@ public class CLITests : GeneratorTestFixture
var s = date.ToString(); var s = date.ToString();
Assert.AreEqual("24/12/1924", s); Assert.AreEqual("24/12/1924", s);
} }
[Test]
public void TestStdString()
{
Assert.AreEqual("test_test", CLITemp.CLITemp.testStdString("test"));
}
} }

5
tests/CLITemp/CLITemp.cpp

@ -4,3 +4,8 @@ int Types::AttributedSum(int A, int B)
{ {
return A + B; return A + B;
} }
std::string testStdString(std::string s)
{
return s + "_test";
}

2
tests/CLITemp/CLITemp.h

@ -45,3 +45,5 @@ std::ostream& operator<<(std::ostream& os, const Date& dt)
os << dt.mo << '/' << dt.da << '/' << dt.yr; os << dt.mo << '/' << dt.da << '/' << dt.yr;
return os; return os;
} }
DLL_API std::string testStdString(std::string s);

Loading…
Cancel
Save