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.

76 lines
3.7 KiB

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