-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(#354) CodeGen: implement generic delegate cache
- Loading branch information
Showing
5 changed files
with
93 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
Cesium.CodeGen/Contexts/Utilities/GenericDelegateTypeCache.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Globalization; | ||
using Mono.Cecil; | ||
|
||
namespace Cesium.CodeGen.Contexts.Utilities; | ||
|
||
internal record GenericDelegateTypeCache( | ||
string Namespace, | ||
string TypeName, | ||
ModuleDefinition TargetModule, | ||
TypeSystem TypeSystem) | ||
{ | ||
private readonly object _delegateCacheLock = new(); | ||
private readonly Dictionary<int, TypeReference> _cache = new(); | ||
public TypeReference GetDelegateType(int typeArgumentCount) | ||
{ | ||
lock (_delegateCacheLock) | ||
{ | ||
if (_cache.GetValueOrDefault(typeArgumentCount) is { } result) return result; | ||
return _cache[typeArgumentCount] = FindDelegate(typeArgumentCount); | ||
} | ||
} | ||
|
||
private TypeReference FindDelegate(int typeArgumentCount) | ||
{ | ||
var realTypeName = $"{TypeName}`{typeArgumentCount.ToString(CultureInfo.InvariantCulture)}"; | ||
var type = new TypeReference( | ||
Namespace, | ||
realTypeName, | ||
null, | ||
TypeSystem.CoreLibrary); | ||
for (var i = 0; i < typeArgumentCount; ++i) | ||
type.GenericParameters.Add(new GenericParameter(type)); | ||
|
||
return TargetModule.ImportReference(type); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters