-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayPoolScope.cs
More file actions
64 lines (59 loc) · 2.08 KB
/
ArrayPoolScope.cs
File metadata and controls
64 lines (59 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System;
using System.Buffers;
namespace TestApp
{
/// <summary>
/// <see cref="ArrayPool{T?}"/> オブジェクトからレンタルした配列を
/// <see cref="Dispose"/> メソッドで返却する構造体。
/// </summary>
/// <typeparam name="T">配列要素型。</typeparam>
/// <remarks>
/// この構造体自体をコピーしてはならない。
/// </remarks>
[NonCopyable]
public struct ArrayPoolScope<T> : IDisposable
{
/// <summary>
/// コンストラクタ。
/// </summary>
/// <param name="minimumLength">配列の最小長。</param>
/// <param name="arrayPool">
/// <see cref="ArrayPool{T?}"/> オブジェクト。
/// null ならば <see cref="ArrayPool{T?}.Shared"/> を用いる。
/// </param>
public ArrayPoolScope(int minimumLength, ArrayPool<T?>? arrayPool = null)
{
this.ArrayPool = arrayPool ?? ArrayPool<T?>.Shared;
this.array = this.ArrayPool.Rent(minimumLength);
}
/// <summary>
/// 配列を取得する。
/// </summary>
/// <remarks>
/// デフォルト構築された場合や <see cref="Dispose"/> 呼び出し後は空の配列を返す。
/// </remarks>
public T?[] Array => this.array ?? System.Array.Empty<T>();
/// <summary>
/// レンタルした配列。
/// </summary>
private T?[]? array;
/// <summary>
/// 配列を <see cref="ArrayPool{T?}"/> オブジェクトに返却する。
/// </summary>
public void Dispose()
{
if (this.array is not null)
{
this.ArrayPool?.Return(this.array);
this.array = null;
}
}
/// <summary>
/// <see cref="ArrayPool{T?}"/> オブジェクトを取得する。
/// </summary>
/// <remarks>
/// デフォルト構築された場合は null を返す。
/// </remarks>
private ArrayPool<T?>? ArrayPool { get; }
}
}