Skip to content

Commit 94d9ee7

Browse files
committed
Add support for templates in the C# generator
1 parent 9e4013e commit 94d9ee7

90 files changed

Lines changed: 2372 additions & 260 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Src/Directory.Packages.props

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
1+
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
22

33
<Import Project="../Locals/Directory.Packages.props" Condition="Exists('../Locals/Directory.Packages.props')" />
44
<Import Project="Directory.Packages.Analyzers.props" />
@@ -13,5 +13,6 @@
1313
<PackageVersion Include="JetBrains.Annotations" Version="2025.2.4" />
1414
<!--benchmark packages-->
1515
<PackageVersion Include="BenchmarkDotNet" Version="0.15.8" />
16+
<PackageVersion Include="Mono.TextTemplating" Version="3.0.0" />
1617
</ItemGroup>
1718
</Project>

Src/FastData.Benchmarks/packages.lock.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@
186186
"Genbox.FastData.Generator.CSharp": {
187187
"type": "Project",
188188
"dependencies": {
189-
"Genbox.FastData.Generator": "[1.0.0, )"
189+
"Genbox.FastData.Generator": "[1.0.0, )",
190+
"Mono.TextTemplating": "[3.0.0, )"
190191
}
191192
},
192193
"Genbox.FastData.InternalShared": {
@@ -216,6 +217,15 @@
216217
"Microsoft.Extensions.DependencyInjection.Abstractions": "10.0.1"
217218
}
218219
},
220+
"Mono.TextTemplating": {
221+
"type": "CentralTransitive",
222+
"requested": "[3.0.0, )",
223+
"resolved": "3.0.0",
224+
"contentHash": "YqueG52R/Xej4VVbKuRIodjiAhV0HR/XVbLbNrJhCZnzjnSjgMJ/dCdV0akQQxavX6hp/LC6rqLGLcXeQYU7XA==",
225+
"dependencies": {
226+
"System.CodeDom": "6.0.0"
227+
}
228+
},
219229
"Newtonsoft.Json": {
220230
"type": "CentralTransitive",
221231
"requested": "[13.0.4, )",

Src/FastData.Cli.Tests/packages.lock.json

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@
157157
"resolved": "3.1.2",
158158
"contentHash": "/OoEZQxSW6DeTJ9nfrg8BLCOCWpxBiWHV4NkG3t+Xpe8tvzm7yCwKwxkhpauMl3fg9OjlIjJMKX61H6VavLkrw=="
159159
},
160+
"System.CodeDom": {
161+
"type": "Transitive",
162+
"resolved": "6.0.0",
163+
"contentHash": "CPc6tWO1LAer3IzfZufDBRL+UZQcj5uS207NHALQzP84Vp/z6wF0Aa0YZImOQY8iStY0A2zI/e3ihKNPfUm8XA=="
164+
},
160165
"System.Interactive.Async": {
161166
"type": "Transitive",
162167
"resolved": "7.0.0",
@@ -253,7 +258,8 @@
253258
"Genbox.FastData.Generator.CSharp": {
254259
"type": "Project",
255260
"dependencies": {
256-
"Genbox.FastData.Generator": "[1.0.0, )"
261+
"Genbox.FastData.Generator": "[1.0.0, )",
262+
"Mono.TextTemplating": "[3.0.0, )"
257263
}
258264
},
259265
"Genbox.FastData.Generator.Rust": {
@@ -289,6 +295,15 @@
289295
"Microsoft.Extensions.DependencyInjection.Abstractions": "10.0.1"
290296
}
291297
},
298+
"Mono.TextTemplating": {
299+
"type": "CentralTransitive",
300+
"requested": "[3.0.0, )",
301+
"resolved": "3.0.0",
302+
"contentHash": "YqueG52R/Xej4VVbKuRIodjiAhV0HR/XVbLbNrJhCZnzjnSjgMJ/dCdV0akQQxavX6hp/LC6rqLGLcXeQYU7XA==",
303+
"dependencies": {
304+
"System.CodeDom": "6.0.0"
305+
}
306+
},
292307
"Newtonsoft.Json": {
293308
"type": "CentralTransitive",
294309
"requested": "[13.0.4, )",

Src/FastData.Cli/packages.lock.json

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@
138138
"resolved": "8.0.0",
139139
"contentHash": "dk9JPxTCIevS75HyEQ0E4OVAFhB2N+V9ShCXf8Q6FkUQZDkgLI12y679Nym1YqsiSysuQskT7Z+6nUf3yab6Vw=="
140140
},
141+
"System.CodeDom": {
142+
"type": "Transitive",
143+
"resolved": "6.0.0",
144+
"contentHash": "CPc6tWO1LAer3IzfZufDBRL+UZQcj5uS207NHALQzP84Vp/z6wF0Aa0YZImOQY8iStY0A2zI/e3ihKNPfUm8XA=="
145+
},
141146
"System.Interactive.Async": {
142147
"type": "Transitive",
143148
"resolved": "7.0.0",
@@ -164,7 +169,8 @@
164169
"Genbox.FastData.Generator.CSharp": {
165170
"type": "Project",
166171
"dependencies": {
167-
"Genbox.FastData.Generator": "[1.0.0, )"
172+
"Genbox.FastData.Generator": "[1.0.0, )",
173+
"Mono.TextTemplating": "[3.0.0, )"
168174
}
169175
},
170176
"Genbox.FastData.Generator.Rust": {
@@ -181,6 +187,15 @@
181187
"dependencies": {
182188
"Microsoft.Extensions.DependencyInjection.Abstractions": "10.0.1"
183189
}
190+
},
191+
"Mono.TextTemplating": {
192+
"type": "CentralTransitive",
193+
"requested": "[3.0.0, )",
194+
"resolved": "3.0.0",
195+
"contentHash": "YqueG52R/Xej4VVbKuRIodjiAhV0HR/XVbLbNrJhCZnzjnSjgMJ/dCdV0akQQxavX6hp/LC6rqLGLcXeQYU7XA==",
196+
"dependencies": {
197+
"System.CodeDom": "6.0.0"
198+
}
184199
}
185200
}
186201
}

Src/FastData.Examples/packages.lock.json

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
"resolved": "10.0.1",
88
"contentHash": "oIy8fQxxbUsSrrOvgBqlVgOeCtDmrcynnTG+FQufcUWBrwyPfwlUkCDB2vaiBeYPyT+20u9/HeuHeBf+H4F/8g=="
99
},
10+
"System.CodeDom": {
11+
"type": "Transitive",
12+
"resolved": "6.0.0",
13+
"contentHash": "CPc6tWO1LAer3IzfZufDBRL+UZQcj5uS207NHALQzP84Vp/z6wF0Aa0YZImOQY8iStY0A2zI/e3ihKNPfUm8XA=="
14+
},
1015
"Genbox.FastData": {
1116
"type": "Project",
1217
"dependencies": {
@@ -22,7 +27,8 @@
2227
"Genbox.FastData.Generator.CSharp": {
2328
"type": "Project",
2429
"dependencies": {
25-
"Genbox.FastData.Generator": "[1.0.0, )"
30+
"Genbox.FastData.Generator": "[1.0.0, )",
31+
"Mono.TextTemplating": "[3.0.0, )"
2632
}
2733
},
2834
"Microsoft.Extensions.Logging.Abstractions": {
@@ -33,6 +39,15 @@
3339
"dependencies": {
3440
"Microsoft.Extensions.DependencyInjection.Abstractions": "10.0.1"
3541
}
42+
},
43+
"Mono.TextTemplating": {
44+
"type": "CentralTransitive",
45+
"requested": "[3.0.0, )",
46+
"resolved": "3.0.0",
47+
"contentHash": "YqueG52R/Xej4VVbKuRIodjiAhV0HR/XVbLbNrJhCZnzjnSjgMJ/dCdV0akQQxavX6hp/LC6rqLGLcXeQYU7XA==",
48+
"dependencies": {
49+
"System.CodeDom": "6.0.0"
50+
}
3651
}
3752
}
3853
}

Src/FastData.Generator.CPlusPlus/CPlusPlusCodeGenerator.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,18 @@ public static CPlusPlusCodeGenerator Create(CPlusPlusCodeGeneratorConfig userCfg
2525

2626
public override GeneratorEncoding Encoding => GeneratorEncoding.UTF8;
2727

28-
public override string Generate<TKey, TValue>(GeneratorConfig<TKey> genCfg, IContext<TValue> context)
28+
public override string Generate<TKey, TValue>(GeneratorConfig<TKey> genCfg, IContext context)
2929
{
3030
//C++ generator does not support chars outside ASCII
3131
if (genCfg.KeyType == KeyType.Char && (char)(object)genCfg.Constants.MaxValue > 127)
3232
throw new InvalidOperationException("C++ generator does not support chars outside ASCII. Please use a different data type or reduce the max value to 127 or lower.");
3333

34-
return base.Generate(genCfg, context);
34+
return base.Generate<TKey, TValue>(genCfg, context);
3535
}
3636

37-
protected override void AppendHeader<TKey, TValue>(StringBuilder sb, GeneratorConfig<TKey> genCfg, IContext<TValue> context)
37+
protected override void AppendHeader<TKey, TValue>(StringBuilder sb, GeneratorConfig<TKey> genCfg, IContext context)
3838
{
39-
base.AppendHeader(sb, genCfg, context);
39+
base.AppendHeader<TKey, TValue>(sb, genCfg, context);
4040

4141
sb.AppendLine("""
4242
#pragma once
@@ -49,13 +49,13 @@ protected override void AppendHeader<TKey, TValue>(StringBuilder sb, GeneratorCo
4949
""");
5050
}
5151

52-
protected override void AppendBody<TKey, TValue>(StringBuilder sb, GeneratorConfig<TKey> genCfg, string keyTypeName, string valueTypeName, IContext<TValue> context)
52+
protected override void AppendBody<TKey, TValue>(StringBuilder sb, GeneratorConfig<TKey> genCfg, string keyTypeName, string valueTypeName, IContext context)
5353
{
5454
sb.AppendLine($$"""
5555
class {{_cfg.ClassName}} final {
5656
""");
5757

58-
base.AppendBody(sb, genCfg, keyTypeName, valueTypeName, context);
58+
base.AppendBody<TKey, TValue>(sb, genCfg, keyTypeName, valueTypeName, context);
5959
}
6060

6161
protected override void AppendFooter<T>(StringBuilder sb, GeneratorConfig<T> genCfg, string typeName)
@@ -64,12 +64,12 @@ protected override void AppendFooter<T>(StringBuilder sb, GeneratorConfig<T> gen
6464
sb.Append("};");
6565
}
6666

67-
protected override OutputWriter<TKey>? GetOutputWriter<TKey, TValue>(GeneratorConfig<TKey> genCfg, IContext<TValue> context) => context switch
67+
protected override OutputWriter<TKey>? GetOutputWriter<TKey, TValue>(GeneratorConfig<TKey> genCfg, IContext context) => context switch
6868
{
6969
SingleValueContext<TKey, TValue> x => new SingleValueCode<TKey, TValue>(x, Shared),
70-
RangeContext<TKey, TValue> x => new RangeCode<TKey, TValue>(x),
71-
BitSetContext<TKey, TValue> x => new BitSetCode<TKey, TValue>(x, Shared),
72-
BloomFilterContext<TKey, TValue> x => new BloomFilterCode<TKey, TValue>(x),
70+
RangeContext<TKey> x => new RangeCode<TKey, TValue>(x),
71+
BitSetContext<TValue> x => new BitSetCode<TKey, TValue>(x, Shared),
72+
BloomFilterContext x => new BloomFilterCode<TKey>(x),
7373
ArrayContext<TKey, TValue> x => new ArrayCode<TKey, TValue>(x, Shared),
7474
BinarySearchContext<TKey, TValue> x => new BinarySearchCode<TKey, TValue>(x, Shared),
7575
ConditionalContext<TKey, TValue> x => new ConditionalCode<TKey, TValue>(x, Shared),

Src/FastData.Generator.CPlusPlus/Internal/Generators/BitSetCode.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace Genbox.FastData.Generator.CPlusPlus.Internal.Generators;
77

8-
internal sealed class BitSetCode<TKey, TValue>(BitSetContext<TKey, TValue> ctx, SharedCode shared) : CPlusPlusOutputWriter<TKey>
8+
internal sealed class BitSetCode<TKey, TValue>(BitSetContext<TValue> ctx, SharedCode shared) : CPlusPlusOutputWriter<TKey>
99
{
1010
public override string Generate()
1111
{

Src/FastData.Generator.CPlusPlus/Internal/Generators/BloomFilterCode.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace Genbox.FastData.Generator.CPlusPlus.Internal.Generators;
77

8-
internal sealed class BloomFilterCode<TKey, TValue>(BloomFilterContext<TKey, TValue> ctx) : CPlusPlusOutputWriter<TKey>
8+
internal sealed class BloomFilterCode<TKey>(BloomFilterContext ctx) : CPlusPlusOutputWriter<TKey>
99
{
1010
public override string Generate() =>
1111
$$"""

Src/FastData.Generator.CPlusPlus/Internal/Generators/RangeCode.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace Genbox.FastData.Generator.CPlusPlus.Internal.Generators;
77

88
[SuppressMessage("Major Code Smell", "S2326:Unused type parameters should be removed")]
9-
internal sealed class RangeCode<TKey, TValue>(RangeContext<TKey, TValue> ctx) : CPlusPlusOutputWriter<TKey>
9+
internal sealed class RangeCode<TKey, TValue>(RangeContext<TKey> ctx) : CPlusPlusOutputWriter<TKey>
1010
{
1111
public override string Generate() =>
1212
$$"""

Src/FastData.Generator.CPlusPlus/Internal/Generators/SingleValueCode.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ public override string Generate()
1313

1414
if (!ctx.Values.IsEmpty)
1515
{
16-
ReadOnlySpan<TValue> values = ctx.Values.Span;
17-
sb.Append($" static inline const auto stored_value = {ToValueLabel(values[0])};");
16+
sb.Append($" static inline const auto stored_value = {ToValueLabel(ctx.Values.Span[0])};");
1817
}
1918

2019
sb.Append($$"""
@@ -23,7 +22,7 @@ public override string Generate()
2322
{{GetMethodModifier(true)}}bool contains(const {{KeyTypeName}} {{InputKeyName}}){{PostMethodModifier}} {
2423
{{GetMethodHeader(MethodType.Contains)}}
2524
26-
return {{GetEqualFunction(LookupKeyName, ToValueLabel(ctx.Item))}};
25+
return {{GetEqualFunction(LookupKeyName, ToValueLabel(ctx.Key))}};
2726
}
2827
""");
2928

@@ -38,7 +37,7 @@ public override string Generate()
3837
{{GetMethodModifier(false)}}bool try_lookup(const {{KeyTypeName}} {{InputKeyName}}, const {{ValueTypeName}}*& value){{PostMethodModifier}} {
3938
{{GetMethodHeader(MethodType.TryLookup)}}
4039
41-
if ({{GetEqualFunction(LookupKeyName, ToValueLabel(ctx.Item))}}) {
40+
if ({{GetEqualFunction(LookupKeyName, ToValueLabel(ctx.Key))}}) {
4241
value = {{ptr}}stored_value;
4342
return true;
4443
}

0 commit comments

Comments
 (0)