Skip to content
Merged
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
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup>
<Version>2025.11.25.0</Version>
<Version>2025.12.9.0</Version>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>preview</LangVersion>
Expand Down
1 change: 1 addition & 0 deletions TedToolkit.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
</Folder>
<Folder Name="/src/Scopes/">
<Project Path="src/libraries/TedToolkit.Scopes/TedToolkit.Scopes.csproj" />
<Project Path="tests/TedToolkit.Scopes.Tests/TedToolkit.Scopes.Tests.csproj" />
</Folder>
<Folder Name="/src/ValidResults/">
<Project Path="src/analyzers/TedToolkit.ValidResults.SourceGenerator/TedToolkit.ValidResults.SourceGenerator.csproj" />
Expand Down
13 changes: 13 additions & 0 deletions Tests/TedToolkit.Scopes.Tests/ScopesTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace TedToolkit.Scopes.Tests;

public class ScopesTests
{
[Test]
[MatrixDataSource]
public async Task ScopeCreateTest([MatrixRange<int>(-1000, 1000)]int value)
{
using var scope = new TestScope(value);
await Task.Delay(1000);
await Assert.That(TestScope.Current?.Value).IsEqualTo(value);
}
}
5 changes: 5 additions & 0 deletions Tests/TedToolkit.Scopes.Tests/TedToolkit.Scopes.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\src\libraries\TedToolkit.Scopes\TedToolkit.Scopes.csproj" />
</ItemGroup>
</Project>
6 changes: 6 additions & 0 deletions Tests/TedToolkit.Scopes.Tests/TestScope.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace TedToolkit.Scopes.Tests;

public class TestScope(int value) : ScopeBase<TestScope>
{
public int Value => value;
}
63 changes: 31 additions & 32 deletions src/libraries/TedToolkit.CppInteropGen/NativeFunctionLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,41 @@ namespace TedToolkit.CppInteropGen;

public sealed class NativeFunctionLoader : IDisposable
{
private static readonly ConcurrentDictionary<string, NativeFunctionLoader> Loaders = [];
private readonly ConcurrentDictionary<string, IntPtr> _exportCache = new(StringComparer.Ordinal);
private readonly Lazy<IntPtr> _libHandle;
private bool _disposed;
private static readonly ConcurrentDictionary<string, NativeFunctionLoader> Loaders = [];
private readonly ConcurrentDictionary<string, IntPtr> _exportCache = new(StringComparer.Ordinal);
private readonly IntPtr _libHandle;
private bool _disposed;

private NativeFunctionLoader(string libraryPath)
{
_libHandle = new Lazy<IntPtr>(() =>
{
var handle = NativeLibrary.Load(libraryPath);
if (handle != IntPtr.Zero) return handle;
throw new InvalidOperationException($"Failed to load native library: {libraryPath}");
});
}
private NativeFunctionLoader(string libraryPath)
{
_libHandle = NativeLibrary.Load(libraryPath);
if (_libHandle != IntPtr.Zero) return;
throw new InvalidOperationException($"Failed to load native library: {libraryPath}");
}

public void Dispose()
{
if (_disposed) return;
_disposed = true;
if (_libHandle.IsValueCreated) NativeLibrary.Free(_libHandle.Value);
}
public void Dispose()
{
if (_disposed) return;
_disposed = true;
NativeLibrary.Free(_libHandle);
}

internal static NativeFunctionLoader GetLoader(string libName)
{
if (string.IsNullOrEmpty(libName))
throw new ArgumentException("The libName is null or empty.", nameof(libName));
var loader = Loaders.GetOrAdd(libName, n => new NativeFunctionLoader(n));
if (!loader._disposed) return loader;
return Loaders[libName] = new NativeFunctionLoader(libName);
}
internal static NativeFunctionLoader GetLoader(string libName)
{
if (string.IsNullOrEmpty(libName))
throw new ArgumentException("The libName is null or empty.", nameof(libName));

public IntPtr GetFunctionPointer(string name)
{
if (_disposed) throw new ObjectDisposedException(nameof(NativeFunctionLoader));
return _exportCache.GetOrAdd(name, n => NativeLibrary.GetExport(_libHandle.Value, n));
}
return Loaders.AddOrUpdate(
key: libName,
addValueFactory: n => new(n),
updateValueFactory: (n, existingLoader) => existingLoader._disposed ? new(n) : existingLoader);
}

public IntPtr GetFunctionPointer(string name)
{
if (_disposed) throw new ObjectDisposedException(nameof(NativeFunctionLoader));
return _exportCache.GetOrAdd(name, n => NativeLibrary.GetExport(_libHandle, n));
}
}

#if NETSTANDARD || NETFRAMEWORK
Expand Down