You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

66 lines
2.8 KiB

using IPA.Config.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IPA.Config.Stores
{
/// <summary>
/// The base interface for a value converter for use by objects generated by
/// <see cref="GeneratedExtension.Generated{T}(Config, bool)"/>.
/// </summary>
/// <remarks>
/// The object returned from <see cref="FromValue(Value)"/>, if fed into <see cref="ToValue(object)"/>,
/// should return equivalent <see cref="Value"/> structures. Similarly, if the result of <see cref="ToValue(object)"/>
/// is fed into <see cref="FromValue(Value)"/>, the resulting object should be equivalent to the one passed to
/// <see cref="ToValue(object)"/>.
/// </remarks>
public interface IValueConverter
{
/// <summary>
/// Converts the given object to a <see cref="Value"/>.
/// </summary>
/// <param name="obj">the object to convert</param>
/// <returns>a representation of <paramref name="obj"/> as a <see cref="Value"/> structure</returns>
Value ToValue(object obj);
/// <summary>
/// Converts the given <see cref="Value"/> to the object type handled by this converter.
/// </summary>
/// <param name="value">the <see cref="Value"/> to deserialize</param>
/// <returns>the deserialized object</returns>
object FromValue(Value value);
/// <summary>
/// Gets the type that this <see cref="IValueConverter"/> handles.
/// </summary>
Type Type { get; }
}
/// <summary>
/// A strongly-typed <see cref="IValueConverter"/>.
/// </summary>
/// <typeparam name="T">the type of object to handle</typeparam>
/// <seealso cref="IValueConverter"/>
public abstract class ValueConverter<T> : IValueConverter
{
/// <summary>
/// Converts the given object to a <see cref="Value"/>.
/// </summary>
/// <param name="obj">the object to convert</param>
/// <returns>a representation of <paramref name="obj"/> as a <see cref="Value"/> structure</returns>
/// <seealso cref="IValueConverter.ToValue(object)"/>
public abstract Value ToValue(T obj);
/// <summary>
/// Converts the given <see cref="Value"/> to the object type handled by this converter.
/// </summary>
/// <param name="value">the <see cref="Value"/> to deserialize</param>
/// <returns>the deserialized object</returns>
/// <seealso cref="IValueConverter.FromValue(Value)"/>
public abstract T FromValue(Value value);
Value IValueConverter.ToValue(object obj) => ToValue((T)obj);
object IValueConverter.FromValue(Value value) => FromValue(value);
Type IValueConverter.Type => typeof(T);
}
}