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.

145 lines
6.8 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. /// <seealso cref="From(IEnumerable{KeyValuePair{string, Value}})"/>
  39. public static Map Map() => new Map();
  40. /// <summary>
  41. /// Creates a new <see cref="Value"/> representing a <see cref="string"/>.
  42. /// </summary>
  43. /// <param name="val">the value to wrap</param>
  44. /// <returns>a <see cref="Data.Text"/> wrapping <paramref name="val"/></returns>
  45. /// <seealso cref="Text(string)"/>
  46. public static Text From(string val) => Text(val);
  47. /// <summary>
  48. /// Creates a new <see cref="Data.Text"/> object wrapping a <see cref="string"/>.
  49. /// </summary>
  50. /// <param name="val">the value to wrap</param>
  51. /// <returns>a <see cref="Data.Text"/> wrapping <paramref name="val"/></returns>
  52. /// <seealso cref="From(string)"/>
  53. public static Text Text(string val) => val == null ? new Text { Value = val } : null;
  54. /// <summary>
  55. /// Creates a new <see cref="Value"/> wrapping a <see cref="long"/>.
  56. /// </summary>
  57. /// <param name="val">the value to wrap</param>
  58. /// <returns>a <see cref="Data.Integer"/> wrapping <paramref name="val"/></returns>
  59. /// <seealso cref="Integer(long)"/>
  60. public static Integer From(long val) => Integer(val);
  61. /// <summary>
  62. /// Creates a new <see cref="Data.Integer"/> wrapping a <see cref="long"/>.
  63. /// </summary>
  64. /// <param name="val">the value to wrap</param>
  65. /// <returns>a <see cref="Data.Integer"/> wrapping <paramref name="val"/></returns>
  66. /// <seealso cref="From(long)"/>
  67. public static Integer Integer(long val) => new Integer { Value = val };
  68. /// <summary>
  69. /// Creates a new <see cref="Value"/> wrapping a <see cref="double"/>.
  70. /// </summary>
  71. /// <param name="val">the value to wrap</param>
  72. /// <returns>a <see cref="FloatingPoint"/> wrapping <paramref name="val"/></returns>
  73. /// <seealso cref="Float(double)"/>
  74. public static FloatingPoint From(double val) => Float(val);
  75. /// <summary>
  76. /// Creates a new <see cref="FloatingPoint"/> wrapping a <see cref="double"/>.
  77. /// </summary>
  78. /// <param name="val">the value to wrap</param>
  79. /// <returns>a <see cref="FloatingPoint"/> wrapping <paramref name="val"/></returns>
  80. /// <seealso cref="From(double)"/>
  81. public static FloatingPoint Float(double val) => new FloatingPoint { Value = val };
  82. /// <summary>
  83. /// Creates a new <see cref="Value"/> wrapping a <see cref="bool"/>.
  84. /// </summary>
  85. /// <param name="val">the value to wrap</param>
  86. /// <returns>a <see cref="Boolean"/> wrapping <paramref name="val"/></returns>
  87. /// <seealso cref="Bool(bool)"/>
  88. public static Boolean From(bool val) => Bool(val);
  89. /// <summary>
  90. /// Creates a new <see cref="Boolean"/> wrapping a <see cref="bool"/>.
  91. /// </summary>
  92. /// <param name="val">the value to wrap</param>
  93. /// <returns>a <see cref="Boolean"/> wrapping <paramref name="val"/></returns>
  94. /// <seealso cref="From(bool)"/>
  95. public static Boolean Bool(bool val) => new Boolean { Value = val };
  96. /// <summary>
  97. /// Creates a new <see cref="Data.List"/> holding the content of an <see cref="IEnumerable{T}"/>
  98. /// of <see cref="Value"/>.
  99. /// </summary>
  100. /// <param name="vals">the <see cref="Value"/>s to initialize the <see cref="Data.List"/> with</param>
  101. /// <returns>a <see cref="Data.List"/> containing the content of <paramref name="vals"/></returns>
  102. /// <seealso cref="List"/>
  103. public static List From(IEnumerable<Value> vals)
  104. {
  105. if (vals == null) return null;
  106. var l = List();
  107. l.AddRange(vals);
  108. return l;
  109. }
  110. /// <summary>
  111. /// Creates a new <see cref="Data.Map"/> holding the content of an <see cref="IDictionary{TKey, TValue}"/>
  112. /// of <see cref="string"/> to <see cref="Value"/>.
  113. /// </summary>
  114. /// <param name="vals">the dictionary of <see cref="Value"/>s to initialize the <see cref="Data.Map"/> wtih</param>
  115. /// <returns>a <see cref="Data.Map"/> containing the content of <paramref name="vals"/></returns>
  116. /// <seealso cref="Map"/>
  117. /// <seealso cref="From(IEnumerable{KeyValuePair{string, Value}})"/>
  118. public static Map From(IDictionary<string, Value> vals) => From(vals as IEnumerable<KeyValuePair<string, Value>>);
  119. /// <summary>
  120. /// Creates a new <see cref="Data.Map"/> holding the content of an <see cref="IEnumerable{T}"/>
  121. /// of <see cref="KeyValuePair{TKey, TValue}"/> of <see cref="string"/> to <see cref="Value"/>.
  122. /// </summary>
  123. /// <param name="vals">the enumerable of <see cref="KeyValuePair{TKey, TValue}"/> of name to <see cref="Value"/></param>
  124. /// <returns>a <see cref="Data.Map"/> containing the content of <paramref name="vals"/></returns>
  125. /// <seealso cref="Map"/>
  126. /// <seealso cref="From(IDictionary{string, Value})"/>
  127. public static Map From(IEnumerable<KeyValuePair<string, Value>> vals)
  128. {
  129. if (vals == null) return null;
  130. var m = Map();
  131. foreach (var v in vals) m.Add(v.Key, v.Value);
  132. return m;
  133. }
  134. }
  135. }