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.

133 lines
5.9 KiB

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace IPA.Config.Data
  8. {
  9. /// <summary>
  10. /// A base value type for config data abstract representations, to be serialized with an
  11. /// <see cref="IConfigProvider"/>. If a <see cref="Value"/> is <see langword="null"/>, then
  12. /// that represents just that: a <c>null</c> in whatever serialization is being used.
  13. /// Also contains factory functions for all derived types.
  14. /// </summary>
  15. public abstract class Value
  16. {
  17. /// <summary>
  18. /// Converts this <see cref="Value"/> into a human-readable format.
  19. /// </summary>
  20. /// <returns>a human-readable string containing the value provided</returns>
  21. public abstract override string ToString();
  22. /// <summary>
  23. /// Creates a Null <see cref="Value"/>.
  24. /// </summary>
  25. /// <returns><see langword="null"/></returns>
  26. public static Value Null() => null;
  27. /// <summary>
  28. /// Creates an empty <see cref="List"/>.
  29. /// </summary>
  30. /// <returns>an empty <see cref="List"/></returns>
  31. /// <seealso cref="From(IEnumerable{Value})"/>
  32. public static List List() => new List();
  33. /// <summary>
  34. /// Creates an empty <see cref="Map"/>.
  35. /// </summary>
  36. /// <returns>an empty <see cref="Map"/></returns>
  37. /// <seealso cref="From(IDictionary{string, Value})"/>
  38. public static Map Map() => new Map();
  39. /// <summary>
  40. /// Creates a new <see cref="Value"/> representing a <see cref="string"/>.
  41. /// </summary>
  42. /// <param name="val">the value to wrap</param>
  43. /// <returns>a <see cref="Data.Text"/> wrapping <paramref name="val"/></returns>
  44. /// <seealso cref="Text(string)"/>
  45. public static Text From(string val) => Text(val);
  46. /// <summary>
  47. /// Creates a new <see cref="Data.Text"/> object wrapping a <see cref="string"/>.
  48. /// </summary>
  49. /// <param name="val">the value to wrap</param>
  50. /// <returns>a <see cref="Data.Text"/> wrapping <paramref name="val"/></returns>
  51. /// <seealso cref="From(string)"/>
  52. public static Text Text(string val) => val == null ? new Text { Value = val } : null;
  53. /// <summary>
  54. /// Creates a new <see cref="Value"/> wrapping a <see cref="long"/>.
  55. /// </summary>
  56. /// <param name="val">the value to wrap</param>
  57. /// <returns>a <see cref="Data.Integer"/> wrapping <paramref name="val"/></returns>
  58. /// <seealso cref="Integer(long)"/>
  59. public static Integer From(long val) => Integer(val);
  60. /// <summary>
  61. /// Creates a new <see cref="Data.Integer"/> wrapping a <see cref="long"/>.
  62. /// </summary>
  63. /// <param name="val">the value to wrap</param>
  64. /// <returns>a <see cref="Data.Integer"/> wrapping <paramref name="val"/></returns>
  65. /// <seealso cref="From(long)"/>
  66. public static Integer Integer(long val) => new Integer { Value = val };
  67. /// <summary>
  68. /// Creates a new <see cref="Value"/> wrapping a <see cref="double"/>.
  69. /// </summary>
  70. /// <param name="val">the value to wrap</param>
  71. /// <returns>a <see cref="FloatingPoint"/> wrapping <paramref name="val"/></returns>
  72. /// <seealso cref="Float(double)"/>
  73. public static FloatingPoint From(double val) => Float(val);
  74. /// <summary>
  75. /// Creates a new <see cref="FloatingPoint"/> wrapping a <see cref="double"/>.
  76. /// </summary>
  77. /// <param name="val">the value to wrap</param>
  78. /// <returns>a <see cref="FloatingPoint"/> wrapping <paramref name="val"/></returns>
  79. /// <seealso cref="From(double)"/>
  80. public static FloatingPoint Float(double val) => new FloatingPoint { Value = val };
  81. /// <summary>
  82. /// Creates a new <see cref="Value"/> wrapping a <see cref="bool"/>.
  83. /// </summary>
  84. /// <param name="val">the value to wrap</param>
  85. /// <returns>a <see cref="Boolean"/> wrapping <paramref name="val"/></returns>
  86. /// <seealso cref="Bool(bool)"/>
  87. public static Boolean From(bool val) => Bool(val);
  88. /// <summary>
  89. /// Creates a new <see cref="Boolean"/> wrapping a <see cref="bool"/>.
  90. /// </summary>
  91. /// <param name="val">the value to wrap</param>
  92. /// <returns>a <see cref="Boolean"/> wrapping <paramref name="val"/></returns>
  93. /// <seealso cref="From(bool)"/>
  94. public static Boolean Bool(bool val) => new Boolean { Value = val };
  95. /// <summary>
  96. /// Creates a new <see cref="Data.List"/> holding the content of an <see cref="IEnumerable{T}"/>
  97. /// of <see cref="Value"/>.
  98. /// </summary>
  99. /// <param name="vals">the <see cref="Value"/>s to initialize the <see cref="Data.List"/> with</param>
  100. /// <returns>a <see cref="Data.List"/> containing the content of <paramref name="vals"/></returns>
  101. /// <seealso cref="List"/>
  102. public static List From(IEnumerable<Value> vals)
  103. {
  104. if (vals == null) return null;
  105. var l = List();
  106. l.AddRange(vals);
  107. return l;
  108. }
  109. /// <summary>
  110. /// Creates a new <see cref="Data.Map"/> holding the content of an <see cref="IDictionary{TKey, TValue}"/>
  111. /// of <see cref="string"/> to <see cref="Value"/>.
  112. /// </summary>
  113. /// <param name="vals">the dictionary of <see cref="Value"/>s to initialize the <see cref="Data.Map"/> wtih</param>
  114. /// <returns>a <see cref="Data.Map"/> containing the content of <paramref name="vals"/></returns>
  115. /// <seealso cref="Map"/>
  116. public static Map From(IDictionary<string, Value> vals)
  117. {
  118. if (vals == null) return null;
  119. var m = Map();
  120. foreach (var v in vals) m.Add(v.Key, v.Value);
  121. return m;
  122. }
  123. }
  124. }