新增 FlowScope Git 包雏形
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
using System;
|
||||
|
||||
namespace FlowScope.Resources
|
||||
{
|
||||
public sealed class ResourceHandle<T> : IResourceHandle<T> where T : class
|
||||
{
|
||||
private readonly ResourceEntry _entry;
|
||||
private readonly Action<string, object> _release;
|
||||
private T _asset;
|
||||
|
||||
internal ResourceHandle(ResourceEntry entry)
|
||||
{
|
||||
_entry = entry ?? throw new ArgumentNullException(nameof(entry));
|
||||
Key = entry.Key;
|
||||
_asset = (T)entry.Asset;
|
||||
}
|
||||
|
||||
public ResourceHandle(string key, T asset, Action<string, object> release)
|
||||
{
|
||||
Key = string.IsNullOrWhiteSpace(key)
|
||||
? throw new ArgumentException("Resource key is required.", nameof(key))
|
||||
: key;
|
||||
_asset = asset ?? throw new ArgumentNullException(nameof(asset));
|
||||
_release = release ?? throw new ArgumentNullException(nameof(release));
|
||||
}
|
||||
|
||||
public string Key { get; }
|
||||
public T Asset => IsDisposed ? null : _asset;
|
||||
public bool IsDisposed { get; private set; }
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (IsDisposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
IsDisposed = true;
|
||||
var asset = _asset;
|
||||
_asset = null;
|
||||
|
||||
if (_entry != null)
|
||||
{
|
||||
_entry.ReleaseHandle();
|
||||
return;
|
||||
}
|
||||
|
||||
_release(Key, asset);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user