Skip to content

Commit d256d54

Browse files
author
Vyacheslav
committed
feat: resolve #38
1 parent 4b66de4 commit d256d54

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using Microsoft.CodeAnalysis;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace StackMemoryCollections
6+
{
7+
public partial class Generator
8+
{
9+
private void GenerateCommonHelpers(
10+
in List<INamedTypeSymbol> typeHelpers,
11+
in GeneratorExecutionContext context,
12+
in StringBuilder builder
13+
)
14+
{
15+
if (typeHelpers.Count == 0)
16+
{
17+
return;
18+
}
19+
builder.Clear();
20+
builder.Append($@"
21+
/*
22+
{Resource.License}
23+
*/
24+
25+
using System;
26+
using System.Runtime.CompilerServices;
27+
28+
namespace StackMemoryCollections
29+
{{
30+
public unsafe static class CommonHelper
31+
{{
32+
public static bool IsNull(in void* ptr)
33+
{{
34+
return *((byte*)ptr) == 0;
35+
}}
36+
}}
37+
}}
38+
");
39+
40+
context.AddSource($"CommonHelper.g.cs", builder.ToString());
41+
}
42+
}
43+
}

Src/StackMemoryCollections/GenerateHelpers.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using Microsoft.CodeAnalysis;
22
using System;
33
using System.Collections.Generic;
4-
using System.IO;
54
using System.Linq;
65
using System.Text;
76

@@ -28,7 +27,9 @@ in StringBuilder builder
2827

2928
GenerateStart(in builder, in currentType);
3029
GenerateSize(in builder, in typeInfo);
31-
30+
GenerateIsNullable(in builder, in typeInfo);
31+
32+
3233
for (int j = 0; j < typeInfo.Members.Count; j++)
3334
{
3435
MemberInfo memberInfo = typeInfo.Members[j];
@@ -93,6 +94,20 @@ public static nuint GetSize()
9394
return {typeInfo.Members.Sum(s => s.Size) + (typeInfo.IsValueType ? 0 : 1)};
9495
}}
9596
97+
");
98+
}
99+
100+
private void GenerateIsNullable(
101+
in StringBuilder builder,
102+
in TypeInfo typeInfo
103+
)
104+
{
105+
builder.Append($@"
106+
public static bool IsNullable()
107+
{{
108+
return {(!typeInfo.IsValueType).ToString().ToLowerInvariant()};
109+
}}
110+
96111
");
97112
}
98113

Src/StackMemoryCollections/Generator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ out var containCollections
4343
GenerateMemory(in context, in builder);
4444
}
4545

46+
GenerateCommonHelpers(in typeHelpers, in context, in builder);
4647
GenerateHelpers(in typeHelpers, in context, in infos, in builder);
4748
GenerateWrappers(in typeWrappers, in context, in infos, in builder);
4849
GenerateStack(in typeGeneratedStack, in context, in infos, in builder);

0 commit comments

Comments
 (0)