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.

77 lines
3.7 KiB

  1. #nullable enable
  2. using IPA.Config.Data;
  3. using System;
  4. namespace IPA.Config.Stores
  5. {
  6. /// <summary>
  7. /// The base interface for a value converter for use by objects generated by
  8. /// <see cref="GeneratedStore.Generated{T}(Config, bool)"/>.
  9. /// </summary>
  10. /// <remarks>
  11. /// <para>
  12. /// The object returned from <see cref="FromValue(Value, object)"/>, if fed into <see cref="ToValue(object, object)"/>,
  13. /// should return equivalent <see cref="Value"/> structures. Similarly, if the result of <see cref="ToValue(object, object)"/>
  14. /// is fed into <see cref="FromValue(Value, object)"/>, the resulting object should be equivalent to the one passed to
  15. /// <see cref="ToValue(object, object)"/>.
  16. /// </para>
  17. /// <para>
  18. /// The <c>parent</c> parameter to <see cref="ToValue(object, object)"/> and <see cref="FromValue(Value, object)"/> should
  19. /// be (ideally) the the top of the serialization tree, or some other generated object in that tree, rather than some arbitrary
  20. /// object in the middle that is not managed by the generatd config system.
  21. /// </para>
  22. /// <para>
  23. /// Converters do <i>not</i> need to perform null checks, as the serializer and deserializer will do that automatically.
  24. /// </para>
  25. /// </remarks>
  26. public interface IValueConverter
  27. {
  28. /// <summary>
  29. /// Converts the given object to a <see cref="Value"/>.
  30. /// </summary>
  31. /// <param name="obj">the object to convert</param>
  32. /// <param name="parent">the owning object of <paramref name="obj"/></param>
  33. /// <returns>a representation of <paramref name="obj"/> as a <see cref="Value"/> structure</returns>
  34. Value? ToValue(object? obj, object parent);
  35. /// <summary>
  36. /// Converts the given <see cref="Value"/> to the object type handled by this converter.
  37. /// </summary>
  38. /// <param name="value">the <see cref="Value"/> to deserialize</param>
  39. /// <param name="parent">the object that will own the result</param>
  40. /// <returns>the deserialized object</returns>
  41. object? FromValue(Value? value, object parent);
  42. /// <summary>
  43. /// Gets the type that this <see cref="IValueConverter"/> handles.
  44. /// </summary>
  45. Type Type { get; }
  46. }
  47. /// <summary>
  48. /// A strongly-typed <see cref="IValueConverter"/>.
  49. /// </summary>
  50. /// <typeparam name="T">the type of object to handle</typeparam>
  51. /// <seealso cref="IValueConverter"/>
  52. public abstract class ValueConverter<T> : IValueConverter
  53. {
  54. /// <summary>
  55. /// Converts the given object to a <see cref="Value"/>.
  56. /// </summary>
  57. /// <param name="obj">the object to convert</param>
  58. /// <param name="parent">the owning object of <paramref name="obj"/></param>
  59. /// <returns>a representation of <paramref name="obj"/> as a <see cref="Value"/> structure</returns>
  60. /// <seealso cref="IValueConverter.ToValue"/>
  61. public abstract Value? ToValue(T? obj, object parent);
  62. /// <summary>
  63. /// Converts the given <see cref="Value"/> to the object type handled by this converter.
  64. /// </summary>
  65. /// <param name="value">the <see cref="Value"/> to deserialize</param>
  66. /// <param name="parent">the object that will own the result</param>
  67. /// <returns>the deserialized object</returns>
  68. /// <seealso cref="IValueConverter.FromValue"/>
  69. public abstract T? FromValue(Value? value, object parent);
  70. Value? IValueConverter.ToValue(object? obj, object parent) => ToValue((T?)obj, parent);
  71. object? IValueConverter.FromValue(Value? value, object parent) => FromValue(value, parent);
  72. Type IValueConverter.Type => typeof(T);
  73. }
  74. }