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
3.1 KiB

using IPA.Config.Data;
using System;
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, object)"/>, if fed into <see cref="ToValue(object, object)"/>,
/// should return equivalent <see cref="Value"/> structures. Similarly, if the result of <see cref="ToValue(object, object)"/>
/// is fed into <see cref="FromValue(Value, object)"/>, the resulting object should be equivalent to the one passed to
/// <see cref="ToValue(object, object)"/>.
/// </remarks>
public interface IValueConverter
{
/// <summary>
/// Converts the given object to a <see cref="Value"/>.
/// </summary>
/// <param name="obj">the object to convert</param>
/// <param name="parent">the owning object of <paramref name="obj"/></param>
/// <returns>a representation of <paramref name="obj"/> as a <see cref="Value"/> structure</returns>
Value ToValue(object obj, object parent);
/// <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>
/// <param name="parent">the object that will own the result</param>
/// <returns>the deserialized object</returns>
object FromValue(Value value, object parent);
/// <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>
/// <param name="parent">the owning object of <paramref name="obj"/></param>
/// <returns>a representation of <paramref name="obj"/> as a <see cref="Value"/> structure</returns>
/// <seealso cref="IValueConverter.ToValue"/>
public abstract Value ToValue(T obj, object parent);
/// <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>
/// <param name="parent">the object that will own the result</param>
/// <returns>the deserialized object</returns>
/// <seealso cref="IValueConverter.FromValue"/>
public abstract T FromValue(Value value, object parent);
Value IValueConverter.ToValue(object obj, object parent) => ToValue((T)obj, parent);
object IValueConverter.FromValue(Value value, object parent) => FromValue(value, parent);
Type IValueConverter.Type => typeof(T);
}
}