74 lines
2.3 KiB
C#
74 lines
2.3 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using PkmnLibSharp.Utils;
|
|
|
|
namespace PkmnLibSharp.StaticData.Libraries
|
|
{
|
|
/// <summary>
|
|
/// The base backing class for all data libraries.
|
|
/// </summary>
|
|
/// <typeparam name="T">
|
|
/// The type that this data library stores.
|
|
/// </typeparam>
|
|
public abstract class DataLibrary<T> : HandleType, IReadOnlyDictionary<string, T> where T : HandleType
|
|
{
|
|
private Dictionary<string, T> _backingDictionary = new(StringComparer.InvariantCultureIgnoreCase);
|
|
|
|
/// <inheritdoc cref="DataLibrary{T}"/>
|
|
protected DataLibrary(FFIHandle handle) : base(handle)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reserves the cache capacity for this data library.
|
|
/// </summary>
|
|
private protected void ReserveCapacity(ulong capacity)
|
|
{
|
|
_backingDictionary = new Dictionary<string, T>((int)capacity, StringComparer.InvariantCultureIgnoreCase);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public IEnumerator<KeyValuePair<string, T>> GetEnumerator()
|
|
{
|
|
return _backingDictionary.GetEnumerator();
|
|
}
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
{
|
|
return GetEnumerator();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Calls the native add function for this data library.
|
|
/// </summary>
|
|
protected abstract void AddNative(string key, T value);
|
|
|
|
/// <summary>
|
|
/// Adds a new value to the library.
|
|
/// </summary>
|
|
public void Add(string key, T value)
|
|
{
|
|
AddNative(key, value);
|
|
_backingDictionary.Add(key, value);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public int Count => _backingDictionary.Count;
|
|
|
|
/// <inheritdoc />
|
|
public bool ContainsKey(string key) => _backingDictionary.ContainsKey(key);
|
|
|
|
/// <inheritdoc />
|
|
public bool TryGetValue(string key, out T value) => _backingDictionary.TryGetValue(key, out value);
|
|
|
|
/// <inheritdoc />
|
|
public T this[string key] => _backingDictionary[key];
|
|
|
|
/// <inheritdoc />
|
|
public IEnumerable<string> Keys => _backingDictionary.Keys;
|
|
|
|
/// <inheritdoc />
|
|
public IEnumerable<T> Values => _backingDictionary.Values;
|
|
}
|
|
} |