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.

167 lines
5.5 KiB

  1. #nullable enable
  2. using System;
  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 <see cref="Value"/> representing a piece of text. The only reason this is not named
  11. /// String is so that it doesn't conflict with <see cref="string"/>.
  12. /// </summary>
  13. public sealed class Text : Value
  14. {
  15. /// <summary>
  16. /// Constructs an empty <see cref="Text"/> object.
  17. /// </summary>
  18. [Obsolete("Use the String constructor.")]
  19. public Text()
  20. {
  21. Value = null!;
  22. }
  23. /// <summary>
  24. /// Constructs a <see cref="Text"/> object containing the provided value.
  25. /// </summary>
  26. /// <param name="value">The value to construct with.</param>
  27. public Text(string value)
  28. {
  29. Value = value;
  30. }
  31. /// <summary>
  32. /// The actual value of this <see cref="Text"/> object.
  33. /// </summary>
  34. public string Value { get; init; }
  35. /// <summary>
  36. /// Converts this <see cref="Data.Value"/> into a human-readable format.
  37. /// </summary>
  38. /// <returns>a quoted, unescaped string form of <see cref="Value"/></returns>
  39. public override string ToString() => $"\"{Value}\"";
  40. }
  41. /// <summary>
  42. /// A <see cref="Value"/> representing an integer. This may hold a <see cref="long"/>'s
  43. /// worth of data.
  44. /// </summary>
  45. public sealed class Integer : Value
  46. {
  47. /// <summary>
  48. /// Constructs an empty <see cref="Integer"/> object.
  49. /// </summary>
  50. [Obsolete("Use the long constructor.")]
  51. public Integer()
  52. {
  53. Value = 0;
  54. }
  55. /// <summary>
  56. /// Constructs a <see cref="Integer"/> object containing the provided value.
  57. /// </summary>
  58. /// <param name="value">The value to construct with.</param>
  59. public Integer(long value)
  60. {
  61. Value = value;
  62. }
  63. /// <summary>
  64. /// The actual value of the <see cref="Integer"/> object.
  65. /// </summary>
  66. public long Value { get; set; }
  67. /// <summary>
  68. /// Coerces this <see cref="Integer"/> into a <see cref="FloatingPoint"/>.
  69. /// </summary>
  70. /// <returns>a <see cref="FloatingPoint"/> representing the closest approximation of <see cref="Value"/></returns>
  71. public FloatingPoint AsFloat() => Float(Value);
  72. /// <summary>
  73. /// Converts this <see cref="Data.Value"/> into a human-readable format.
  74. /// </summary>
  75. /// <returns>the result of <c>Value.ToString()</c></returns>
  76. public override string ToString() => Value.ToString();
  77. }
  78. /// <summary>
  79. /// A <see cref="Value"/> representing a floating point value. This may hold a
  80. /// <see cref="decimal"/>'s worth of data.
  81. /// </summary>
  82. public sealed class FloatingPoint : Value
  83. {
  84. /// <summary>
  85. /// Constructs an empty <see cref="FloatingPoint"/> object.
  86. /// </summary>
  87. [Obsolete("Use the long constructor.")]
  88. public FloatingPoint()
  89. {
  90. Value = 0;
  91. }
  92. /// <summary>
  93. /// Constructs a <see cref="FloatingPoint"/> object containing the provided value.
  94. /// </summary>
  95. /// <param name="value">The value to construct with.</param>
  96. public FloatingPoint(decimal value)
  97. {
  98. Value = value;
  99. }
  100. /// <summary>
  101. /// The actual value fo this <see cref="FloatingPoint"/> object.
  102. /// </summary>
  103. public decimal Value { get; set; }
  104. /// <summary>
  105. /// Coerces this <see cref="FloatingPoint"/> into an <see cref="Integer"/>.
  106. /// </summary>
  107. /// <returns>a <see cref="Integer"/> representing the closest approximation of <see cref="Value"/></returns>
  108. public Integer AsInteger() => Integer((long)Value);
  109. /// <summary>
  110. /// Converts this <see cref="Data.Value"/> into a human-readable format.
  111. /// </summary>
  112. /// <returns>the result of <c>Value.ToString()</c></returns>
  113. public override string ToString() => Value.ToString();
  114. }
  115. /// <summary>
  116. /// A <see cref="Value"/> representing a boolean value.
  117. /// </summary>
  118. public sealed class Boolean : Value
  119. {
  120. /// <summary>
  121. /// Constructs an empty <see cref="Boolean"/> object.
  122. /// </summary>
  123. [Obsolete("Use the long constructor.")]
  124. public Boolean()
  125. {
  126. Value = false;
  127. }
  128. /// <summary>
  129. /// Constructs a <see cref="Boolean"/> object containing the provided value.
  130. /// </summary>
  131. /// <param name="value">The value to construct with.</param>
  132. public Boolean(bool value)
  133. {
  134. Value = value;
  135. }
  136. /// <summary>
  137. /// The actual value fo this <see cref="Boolean"/> object.
  138. /// </summary>
  139. public bool Value { get; set; }
  140. /// <summary>
  141. /// Converts this <see cref="Data.Value"/> into a human-readable format.
  142. /// </summary>
  143. /// <returns>the result of <c>Value.ToString().ToLower()</c></returns>
  144. [System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1308:Normalize strings to uppercase",
  145. Justification = "ToLower is the desired display value.")]
  146. public override string ToString() => Value.ToString().ToLower(System.Globalization.CultureInfo.InvariantCulture);
  147. }
  148. }