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.

73 lines
3.5 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="GeneratedExtension.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. /// </remarks>
  22. public interface IValueConverter
  23. {
  24. /// <summary>
  25. /// Converts the given object to a <see cref="Value"/>.
  26. /// </summary>
  27. /// <param name="obj">the object to convert</param>
  28. /// <param name="parent">the owning object of <paramref name="obj"/></param>
  29. /// <returns>a representation of <paramref name="obj"/> as a <see cref="Value"/> structure</returns>
  30. Value ToValue(object obj, object parent);
  31. /// <summary>
  32. /// Converts the given <see cref="Value"/> to the object type handled by this converter.
  33. /// </summary>
  34. /// <param name="value">the <see cref="Value"/> to deserialize</param>
  35. /// <param name="parent">the object that will own the result</param>
  36. /// <returns>the deserialized object</returns>
  37. object FromValue(Value value, object parent);
  38. /// <summary>
  39. /// Gets the type that this <see cref="IValueConverter"/> handles.
  40. /// </summary>
  41. Type Type { get; }
  42. }
  43. /// <summary>
  44. /// A strongly-typed <see cref="IValueConverter"/>.
  45. /// </summary>
  46. /// <typeparam name="T">the type of object to handle</typeparam>
  47. /// <seealso cref="IValueConverter"/>
  48. public abstract class ValueConverter<T> : IValueConverter
  49. {
  50. /// <summary>
  51. /// Converts the given object to a <see cref="Value"/>.
  52. /// </summary>
  53. /// <param name="obj">the object to convert</param>
  54. /// <param name="parent">the owning object of <paramref name="obj"/></param>
  55. /// <returns>a representation of <paramref name="obj"/> as a <see cref="Value"/> structure</returns>
  56. /// <seealso cref="IValueConverter.ToValue"/>
  57. public abstract Value ToValue(T obj, object parent);
  58. /// <summary>
  59. /// Converts the given <see cref="Value"/> to the object type handled by this converter.
  60. /// </summary>
  61. /// <param name="value">the <see cref="Value"/> to deserialize</param>
  62. /// <param name="parent">the object that will own the result</param>
  63. /// <returns>the deserialized object</returns>
  64. /// <seealso cref="IValueConverter.FromValue"/>
  65. public abstract T FromValue(Value value, object parent);
  66. Value IValueConverter.ToValue(object obj, object parent) => ToValue((T)obj, parent);
  67. object IValueConverter.FromValue(Value value, object parent) => FromValue(value, parent);
  68. Type IValueConverter.Type => typeof(T);
  69. }
  70. }