Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,50 @@ public static void Main(string[] args)
var actualResult = transformer.Transform(helloWorldCode);
Assert.Equal(expectedResult, actualResult);
}

[Fact]
public void TemplateLineBreakTest()
{
const string inputCode = @"
struct ISetter<TValue>
{
virtual void Set(TValue value) = 0;

virtual ~ISetter<TValue>() = default;
};";
const string expectedResult = @"
template <typename ...> struct ISetter;
template <typename TValue>
struct ISetter<TValue>
{
virtual void Set(TValue value) = 0;

virtual ~ISetter<TValue>() = default;
};";
var transformer = new CSharpToCppTransformer();
var actualResult = transformer.Transform(inputCode);
Assert.Equal(expectedResult, actualResult);
}

[Fact]
public void InterfaceTemplateLineBreakTest()
{
const string inputCode = @"
interface IFactory<TProduct>
{
TProduct Create();
};";
const string expectedResult = @"
template <typename ...> class IFactory;
template <typename TProduct>
class IFactory<TProduct>
{
public:
TProduct Create();
};";
var transformer = new CSharpToCppTransformer();
var actualResult = transformer.Transform(inputCode);
Assert.Equal(expectedResult, actualResult);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ public class CSharpToCppTransformer : TextTransformer
(new Regex(@"((public|protected|private|internal|abstract|static) )*(?<category>interface|class|struct)"), "${category}", 0),
// class GenericCollectionMethodsBase<TElement> {
// template <typename TElement> class GenericCollectionMethodsBase {
(new Regex(@"(?<before>\r?\n)(?<indent>[ \t]*)(?<type>class|struct) (?<typeName>[a-zA-Z0-9]+)<(?<typeParameters>[a-zA-Z0-9 ,]+)>(?<typeDefinitionEnding>[^{]+){"), "${before}${indent}template <typename ...> ${type} ${typeName};" + Environment.NewLine + "${indent}template <typename ${typeParameters}> ${type} ${typeName}<${typeParameters}>${typeDefinitionEnding}{", 0),
(new Regex(@"(?<before>\r?\n)(?<indent>[ \t]*)(?<type>class|struct) (?<typeName>[a-zA-Z0-9]+)<(?<typeParameters>[a-zA-Z0-9 ,]+)>(?<typeDefinitionEnding>[^{]+){"), "${before}${indent}template <typename ...> ${type} ${typeName};" + Environment.NewLine + "${indent}template <typename ${typeParameters}>" + Environment.NewLine + "${indent}${type} ${typeName}<${typeParameters}>${typeDefinitionEnding}{", 0),
// static void TestMultipleCreationsAndDeletions<TElement>(SizedBinaryTreeMethodsBase<TElement> tree, TElement* root)
// template<typename T> static void TestMultipleCreationsAndDeletions<TElement>(SizedBinaryTreeMethodsBase<TElement> tree, TElement* root)
(new Regex(@"static ([a-zA-Z0-9]+) ([a-zA-Z0-9]+)<([a-zA-Z0-9]+)>\(([^\)\r\n]+)\)"), "template <typename $3> static $1 $2($4)", 0),
// interface IFactory<out TProduct> {
// template <typename...> class IFactory;\ntemplate <typename TProduct> class IFactory<TProduct>
(new Regex(@"(?<before>\r?\n)(?<indent>[ \t]*)interface (?<interface>[a-zA-Z0-9]+)<(?<typeParameters>[a-zA-Z0-9 ,]+)>(?<typeDefinitionEnding>[^{]+){"), "${before}${indent}template <typename ...> class ${interface};" + Environment.NewLine + "${indent}template <typename ${typeParameters}> class ${interface}<${typeParameters}>${typeDefinitionEnding}{" + Environment.NewLine + " public:", 0),
(new Regex(@"(?<before>\r?\n)(?<indent>[ \t]*)interface (?<interface>[a-zA-Z0-9]+)<(?<typeParameters>[a-zA-Z0-9 ,]+)>(?<typeDefinitionEnding>[^{]+){"), "${before}${indent}template <typename ...> class ${interface};" + Environment.NewLine + "${indent}template <typename ${typeParameters}>" + Environment.NewLine + "${indent}class ${interface}<${typeParameters}>${typeDefinitionEnding}{" + Environment.NewLine + " public:", 0),
// template <typename TObject, TProperty, TValue>
// template <typename TObject, typename TProperty, typename TValue>
(new Regex(@"(?<before>template <((, )?typename [a-zA-Z0-9]+)+, )(?<typeParameter>[a-zA-Z0-9]+)(?<after>(,|>))"), "${before}typename ${typeParameter}${after}", 10),
Expand Down
4 changes: 2 additions & 2 deletions python/cs2cpp/cs2cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ def __init__(
SubRule(r"((public|protected|private|internal|abstract|static) )*(?P<category>interface|class|struct)", r"\g<category>", max_repeat=0),
# class GenericCollectionMethodsBase<TElement> {
# template <typename TElement> class GenericCollectionMethodsBase {
SubRule(r"(?P<before>\r?\n)(?P<indent>[ \t]*)(?P<type>class|struct) (?P<typeName>[a-zA-Z0-9]+)<(?P<typeParameters>[a-zA-Z0-9 ,]+)>(?P<typeDefinitionEnding>[^{]+){", r"\g<before>\g<indent>template <typename ...> \g<type> \g<typeName>;\n" + "\g<indent>template <typename \g<typeParameters>> \g<type> \g<typeName><\g<typeParameters>>\g<typeDefinitionEnding>{", max_repeat=0),
SubRule(r"(?P<before>\r?\n)(?P<indent>[ \t]*)(?P<type>class|struct) (?P<typeName>[a-zA-Z0-9]+)<(?P<typeParameters>[a-zA-Z0-9 ,]+)>(?P<typeDefinitionEnding>[^{]+){", r"\g<before>\g<indent>template <typename ...> \g<type> \g<typeName>;\n" + "\g<indent>template <typename \g<typeParameters>>\n" + "\g<indent>\g<type> \g<typeName><\g<typeParameters>>\g<typeDefinitionEnding>{", max_repeat=0),
# static void TestMultipleCreationsAndDeletions<TElement>(SizedBinaryTreeMethodsBase<TElement> tree, TElement* root)
# template<typename T> static void TestMultipleCreationsAndDeletions<TElement>(SizedBinaryTreeMethodsBase<TElement> tree, TElement* root)
SubRule(r"static ([a-zA-Z0-9]+) ([a-zA-Z0-9]+)<([a-zA-Z0-9]+)>\(([^\)\r\n]+)\)", r"template <typename \3> static \1 \2(\4)", max_repeat=0),
# interface IFactory<out TProduct> {
# template <typename...> class IFactory;\ntemplate <typename TProduct> class IFactory<TProduct>
SubRule(r"(?P<before>\r?\n)(?P<indent>[ \t]*)interface (?P<interface>[a-zA-Z0-9]+)<(?P<typeParameters>[a-zA-Z0-9 ,]+)>(?P<typeDefinitionEnding>[^{]+){", r"\g<before>\g<indent>template <typename ...> class \g<interface>;\n" + "\g<indent>template <typename \g<typeParameters>> class \g<interface><\g<typeParameters>>\g<typeDefinitionEnding>{\n" + " public:", max_repeat=0),
SubRule(r"(?P<before>\r?\n)(?P<indent>[ \t]*)interface (?P<interface>[a-zA-Z0-9]+)<(?P<typeParameters>[a-zA-Z0-9 ,]+)>(?P<typeDefinitionEnding>[^{]+){", r"\g<before>\g<indent>template <typename ...> class \g<interface>;\n" + "\g<indent>template <typename \g<typeParameters>>\n" + "\g<indent>class \g<interface><\g<typeParameters>>\g<typeDefinitionEnding>{\n" + " public:", max_repeat=0),
# template <typename TObject, TProperty, TValue>
# template <typename TObject, typename TProperty, typename TValue>
SubRule(r"(?P<before>template <((, )?typename [a-zA-Z0-9]+)+, )(?P<typeParameter>[a-zA-Z0-9]+)(?P<after>(,|>))", r"\g<before>typename \g<typeParameter>\g<after>", max_repeat=10),
Expand Down
Loading