Browse Source

Added start of config abstract representation

4.0.0-beta
Anairkoen Schno 4 years ago
parent
commit
2dad5c08e9
4 changed files with 263 additions and 0 deletions
  1. +101
    -0
      IPA.Loader/Config/Data/List.cs
  2. +104
    -0
      IPA.Loader/Config/Data/Map.cs
  3. +55
    -0
      IPA.Loader/Config/Data/Value.cs
  4. +3
    -0
      IPA.Loader/IPA.Loader.csproj

+ 101
- 0
IPA.Loader/Config/Data/List.cs View File

@ -0,0 +1,101 @@
using System;
using System.Collections;
using System.Collections.Generic;
namespace IPA.Config.Data
{
/// <summary>
/// A list of <see cref="Value"/>s for serialization by an <see cref="IConfigProvider"/>.
/// </summary>
public sealed class List : Value, IList<Value>
{
private readonly List<Value> values = new List<Value>();
/// <summary>
/// Gets the value at the given index in this <see cref="List"/>.
/// </summary>
/// <param name="index">the index to retrieve the <see cref="Value"/> at</param>
/// <returns>the <see cref="Value"/> at <paramref name="index"/></returns>
/// <seealso cref="IList{T}.this[int]"/>
public Value this[int index] { get => values[index]; set => values[index] = value; }
/// <summary>
/// Gets the number of elements in the <see cref="List"/>.
/// </summary>
/// <seealso cref="ICollection{T}.Count"/>
public int Count => values.Count;
bool ICollection<Value>.IsReadOnly => ((IList<Value>)values).IsReadOnly;
/// <summary>
/// Adds a <see cref="Value"/> to the end of this <see cref="List"/>.
/// </summary>
/// <param name="item">the <see cref="Value"/> to add</param>
/// <seealso cref="ICollection{T}.Add(T)"/>
public void Add(Value item) => values.Add(item);
/// <summary>
/// Clears the <see cref="List"/>.
/// </summary>
/// <seealso cref="ICollection{T}.Clear"/>
public void Clear() => values.Clear();
/// <summary>
/// Checks if the <see cref="List"/> contains a certian item.
/// </summary>
/// <param name="item">the <see cref="Value"/> to check for</param>
/// <returns><see langword="true"/> if the item was founc, otherwise <see langword="false"/></returns>
/// <seealso cref="ICollection{T}.Contains(T)"/>
public bool Contains(Value item) => values.Contains(item);
/// <summary>
/// Copies the <see cref="Value"/>s in the <see cref="List"/> to the <see cref="Array"/> in <paramref name="array"/>.
/// </summary>
/// <param name="array">the <see cref="Array"/> to copy to</param>
/// <param name="arrayIndex">the starting index to copy to</param>
/// <seealso cref="ICollection{T}.CopyTo(T[], int)"/>
public void CopyTo(Value[] array, int arrayIndex) => values.CopyTo(array, arrayIndex);
/// <summary>
/// Gets an enumerator to enumerate the <see cref="List"/>.
/// </summary>
/// <returns>an <see cref="IEnumerator{T}"/> for this <see cref="List"/></returns>
/// <seealso cref="IEnumerable{T}.GetEnumerator"/>
public IEnumerator<Value> GetEnumerator() => ((IList<Value>)values).GetEnumerator();
/// <summary>
/// Gets the index that a given <see cref="Value"/> is in the <see cref="List"/>.
/// </summary>
/// <param name="item">the <see cref="Value"/> to search for</param>
/// <returns>the index that the <paramref name="item"/> was at, or -1.</returns>
/// <seealso cref="IList{T}.IndexOf(T)"/>
public int IndexOf(Value item) => values.IndexOf(item);
/// <summary>
/// Inserts a <see cref="Value"/> at an index.
/// </summary>
/// <param name="index">the index to insert at</param>
/// <param name="item">the <see cref="Value"/> to insert</param>
/// <seealso cref="IList{T}.Insert(int, T)"/>
public void Insert(int index, Value item) => values.Insert(index, item);
/// <summary>
/// Removes a <see cref="Value"/> from the <see cref="List"/>.
/// </summary>
/// <param name="item">the <see cref="Value"/> to remove</param>
/// <returns><see langword="true"/> if the item was removed, <see langword="false"/> otherwise</returns>
/// <seealso cref="ICollection{T}.Remove(T)"/>
public bool Remove(Value item) => values.Remove(item);
/// <summary>
/// Removes a <see cref="Value"/> at an index.
/// </summary>
/// <param name="index">the index to remove a <see cref="Value"/> at</param>
/// <seealso cref="IList{T}.RemoveAt(int)"/>
public void RemoveAt(int index) => values.RemoveAt(index);
IEnumerator IEnumerable.GetEnumerator() => ((IList<Value>)values).GetEnumerator();
}
}

+ 104
- 0
IPA.Loader/Config/Data/Map.cs View File

@ -0,0 +1,104 @@
using System.Collections;
using System.Collections.Generic;
namespace IPA.Config.Data
{
/// <summary>
/// A map of <see cref="string"/> to <see cref="Value"/> for serialization by an <see cref="IConfigProvider"/>.
/// </summary>
public sealed class Map : Value, IDictionary<string, Value>
{
private readonly Dictionary<string, Value> values = new Dictionary<string, Value>();
/// <summary>
/// Accesses the <see cref="Value"/> at <paramref name="key"/> in the map.
/// </summary>
/// <param name="key">the key to get the value associated with</param>
/// <returns>the value associated with the <paramref name="key"/></returns>
/// <seealso cref="IDictionary{TKey, TValue}.this[TKey]"/>
public Value this[string key] { get => values[key]; set => values[key] = value; }
/// <summary>
/// Gets a collection of the keys for the <see cref="Map"/>.
/// </summary>
/// <seealso cref="IDictionary{TKey, TValue}.Keys"/>
public ICollection<string> Keys => values.Keys;
/// <summary>
/// Gets a collection of the values in the <see cref="Map"/>.
/// </summary>
/// <seealso cref="IDictionary{TKey, TValue}.Values"/>
public ICollection<Value> Values => values.Values;
/// <summary>
/// Gets the number of key-value pairs in this <see cref="Map"/>.
/// </summary>
/// <seealso cref="ICollection{T}.Count"/>
public int Count => values.Count;
bool ICollection<KeyValuePair<string, Value>>.IsReadOnly => ((IDictionary<string, Value>)values).IsReadOnly;
/// <summary>
/// Adds a new <see cref="Value"/> with a given key.
/// </summary>
/// <param name="key">the key to put the value at</param>
/// <param name="value">the <see cref="Value"/> to add</param>
/// <seealso cref="IDictionary{TKey, TValue}.Add(TKey, TValue)"/>
public void Add(string key, Value value) => values.Add(key, value);
void ICollection<KeyValuePair<string, Value>>.Add(KeyValuePair<string, Value> item)
=> ((IDictionary<string, Value>)values).Add(item);
/// <summary>
/// Clears the <see cref="Map"/> of its key-value pairs.
/// </summary>
/// <seealso cref="ICollection{T}.Clear"/>
public void Clear() => values.Clear();
bool ICollection<KeyValuePair<string, Value>>.Contains(KeyValuePair<string, Value> item)
=> ((IDictionary<string, Value>)values).Contains(item);
/// <summary>
/// Checks if the <see cref="Map"/> contains a given <paramref name="key"/>.
/// </summary>
/// <param name="key">the key to check for</param>
/// <returns><see langword="true"/> if the key exists, otherwise <see langword="false"/></returns>
/// <seealso cref="IDictionary{TKey, TValue}.ContainsKey(TKey)"/>
public bool ContainsKey(string key) => values.ContainsKey(key);
void ICollection<KeyValuePair<string, Value>>.CopyTo(KeyValuePair<string, Value>[] array, int arrayIndex)
=> ((IDictionary<string, Value>)values).CopyTo(array, arrayIndex);
/// <summary>
/// Enumerates the <see cref="Map"/>'s key-value pairs.
/// </summary>
/// <returns>an <see cref="IEnumerator{T}"/> of key-value pairs in this <see cref="Map"/></returns>
/// <seealso cref="IEnumerable{T}.GetEnumerator()"/>
public IEnumerator<KeyValuePair<string, Value>> GetEnumerator() => values.GetEnumerator();
/// <summary>
/// Removes the object associated with a key in this <see cref="Map"/>.
/// </summary>
/// <param name="key">the key to remove</param>
/// <returns><see langword="true"/> if the key existed, <see langword="false"/> otherwise</returns>
/// <seealso cref="IDictionary{TKey, TValue}.Remove(TKey)"/>
public bool Remove(string key) => values.Remove(key);
bool ICollection<KeyValuePair<string, Value>>.Remove(KeyValuePair<string, Value> item)
=> ((IDictionary<string, Value>)values).Remove(item);
/// <summary>
/// Gets the value associated with the specified key.
/// </summary>
/// <param name="key">the key of the value to get</param>
/// <param name="value">the target location of the retrieved object</param>
/// <returns><see langword="true"/> if the key was found and <paramref name="value"/> set, <see langword="false"/> otherwise</returns>
/// <seealso cref="IDictionary{TKey, TValue}.TryGetValue(TKey, out TValue)"/>
public bool TryGetValue(string key, out Value value) => values.TryGetValue(key, out value);
IEnumerator IEnumerable.GetEnumerator() => ((IDictionary<string, Value>)values).GetEnumerator();
}
}

+ 55
- 0
IPA.Loader/Config/Data/Value.cs View File

@ -0,0 +1,55 @@
using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IPA.Config.Data
{
/// <summary>
/// A base value type for config data abstract representations, to be serialized with an
/// <see cref="IConfigProvider"/>.
/// </summary>
public abstract class Value
{
}
// not String to prevent overlap with System.String
/// <summary>
/// A <see cref="Value"/> representing a piece of text. The only reason this is not named
/// String is so that it doesn't conflict with <see cref="string"/>.
/// </summary>
public sealed class Text : Value
{
}
/// <summary>
/// A <see cref="Value"/> representing an integer. This may hold a <see cref="long"/>'s
/// worth of data.
/// </summary>
public sealed class Integer : Value
{
}
/// <summary>
/// A <see cref="Value"/> representing a floating point value. This may hold a
/// <see cref="double"/>'s worth of data.
/// </summary>
public sealed class FloatingPoint : Value
{
}
/// <summary>
/// A <see cref="Value"/> representing a boolean value.
/// </summary>
public sealed class Boolean : Value
{
}
}

+ 3
- 0
IPA.Loader/IPA.Loader.csproj View File

@ -86,6 +86,9 @@
<ItemGroup>
<Compile Include="Config\Config.cs" />
<Compile Include="Config\ConfigProviders\JsonConfigProvider.cs" />
<Compile Include="Config\Data\List.cs" />
<Compile Include="Config\Data\Map.cs" />
<Compile Include="Config\Data\Value.cs" />
<Compile Include="Config\IConfigProvider.cs" />
<Compile Include="Config\IConfigStore.cs" />
<Compile Include="Config\SelfConfig.cs" />


Loading…
Cancel
Save