mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-26 06:28:35 +01:00
extract GenericDataHolder behavior into an interface
This commit is contained in:
parent
106102adf6
commit
1f40129ad3
1 changed files with 35 additions and 21 deletions
|
@ -3,21 +3,14 @@ using System.Collections.Generic;
|
|||
using System.Runtime.Serialization;
|
||||
|
||||
namespace MLEM.Misc {
|
||||
/// <summary>
|
||||
/// Represents an object that can hold generic key-value based data.
|
||||
/// A lot of MLEM components extend this class to allow for users to add additional data to them easily.
|
||||
/// </summary>
|
||||
/// <inheritdoc />
|
||||
[DataContract]
|
||||
public class GenericDataHolder {
|
||||
public class GenericDataHolder : IGenericDataHolder {
|
||||
|
||||
[DataMember]
|
||||
[DataMember(EmitDefaultValue = false)]
|
||||
private Dictionary<string, object> data;
|
||||
|
||||
/// <summary>
|
||||
/// Store a piece of generic data on this object.
|
||||
/// </summary>
|
||||
/// <param name="key">The key to store the data by</param>
|
||||
/// <param name="data">The data to store in the object</param>
|
||||
/// <inheritdoc />
|
||||
public void SetData(string key, object data) {
|
||||
if (data == default) {
|
||||
if (this.data != null)
|
||||
|
@ -29,22 +22,14 @@ namespace MLEM.Misc {
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a piece of generic data of the given type on this object.
|
||||
/// </summary>
|
||||
/// <param name="key">The key that the data is stored by</param>
|
||||
/// <typeparam name="T">The type of the data stored</typeparam>
|
||||
/// <returns>The data, or default if it doesn't exist</returns>
|
||||
/// <inheritdoc />
|
||||
public T GetData<T>(string key) {
|
||||
if (this.data != null && this.data.TryGetValue(key, out var val) && val is T t)
|
||||
return t;
|
||||
return default;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns all of the generic data that this object stores.
|
||||
/// </summary>
|
||||
/// <returns>The generic data on this object</returns>
|
||||
/// <inheritdoc />
|
||||
public IReadOnlyCollection<string> GetDataKeys() {
|
||||
if (this.data == null)
|
||||
return Array.Empty<string>();
|
||||
|
@ -52,4 +37,33 @@ namespace MLEM.Misc {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents an object that can hold generic key-value based data.
|
||||
/// A lot of MLEM components extend this class to allow for users to add additional data to them easily.
|
||||
/// </summary>
|
||||
public interface IGenericDataHolder {
|
||||
|
||||
/// <summary>
|
||||
/// Store a piece of generic data on this object.
|
||||
/// </summary>
|
||||
/// <param name="key">The key to store the data by</param>
|
||||
/// <param name="data">The data to store in the object</param>
|
||||
void SetData(string key, object data);
|
||||
|
||||
/// <summary>
|
||||
/// Returns a piece of generic data of the given type on this object.
|
||||
/// </summary>
|
||||
/// <param name="key">The key that the data is stored by</param>
|
||||
/// <typeparam name="T">The type of the data stored</typeparam>
|
||||
/// <returns>The data, or default if it doesn't exist</returns>
|
||||
T GetData<T>(string key);
|
||||
|
||||
/// <summary>
|
||||
/// Returns all of the generic data that this object stores.
|
||||
/// </summary>
|
||||
/// <returns>The generic data on this object</returns>
|
||||
IReadOnlyCollection<string> GetDataKeys();
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue