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

  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. /// The object returned from <see cref="FromValue(Value, object)"/>, if fed into <see cref="ToValue(object, object)"/>,
  11. /// should return equivalent <see cref="Value"/> structures. Similarly, if the result of <see cref="ToValue(object, object)"/>
  12. /// is fed into <see cref="FromValue(Value, object)"/>, the resulting object should be equivalent to the one passed to
  13. /// <see cref="ToValue(object, object)"/>.
  14. /// </remarks>
  15. public interface IValueConverter
  16. {
  17. /// <summary>
  18. /// Converts the given object to a <see cref="Value"/>.
  19. /// </summary>
  20. /// <param name="obj">the object to convert</param>
  21. /// <param name="parent">the owning object of <paramref name="obj"/></param>
  22. /// <returns>a representation of <paramref name="obj"/> as a <see cref="Value"/> structure</returns>
  23. Value ToValue(object obj, object parent);
  24. /// <summary>
  25. /// Converts the given <see cref="Value"/> to the object type handled by this converter.
  26. /// </summary>
  27. /// <param name="value">the <see cref="Value"/> to deserialize</param>
  28. /// <param name="parent">the object that will own the result</param>
  29. /// <returns>the deserialized object</returns>
  30. object FromValue(Value value, object parent);
  31. /// <summary>
  32. /// Gets the type that this <see cref="IValueConverter"/> handles.
  33. /// </summary>
  34. Type Type { get; }
  35. }
  36. /// <summary>
  37. /// A strongly-typed <see cref="IValueConverter"/>.
  38. /// </summary>
  39. /// <typeparam name="T">the type of object to handle</typeparam>
  40. /// <seealso cref="IValueConverter"/>
  41. public abstract class ValueConverter<T> : IValueConverter
  42. {
  43. /// <summary>
  44. /// Converts the given object to a <see cref="Value"/>.
  45. /// </summary>
  46. /// <param name="obj">the object to convert</param>
  47. /// <param name="parent">the owning object of <paramref name="obj"/></param>
  48. /// <returns>a representation of <paramref name="obj"/> as a <see cref="Value"/> structure</returns>
  49. /// <seealso cref="IValueConverter.ToValue"/>
  50. public abstract Value ToValue(T obj, object parent);
  51. /// <summary>
  52. /// Converts the given <see cref="Value"/> to the object type handled by this converter.
  53. /// </summary>
  54. /// <param name="value">the <see cref="Value"/> to deserialize</param>
  55. /// <param name="parent">the object that will own the result</param>
  56. /// <returns>the deserialized object</returns>
  57. /// <seealso cref="IValueConverter.FromValue"/>
  58. public abstract T FromValue(Value value, object parent);
  59. Value IValueConverter.ToValue(object obj, object parent) => ToValue((T)obj, parent);
  60. object IValueConverter.FromValue(Value value, object parent) => FromValue(value, parent);
  61. Type IValueConverter.Type => typeof(T);
  62. }
  63. }