SchemaTypesContext.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. namespace SharpGLTF.SchemaReflection
  7. {
  8. public partial class SchemaType
  9. {
  10. /// <summary>
  11. /// Collection class that contains all the types found in a json schema.
  12. /// </summary>
  13. public sealed class Context
  14. {
  15. #region data
  16. private readonly Dictionary<string, SchemaType> _Types = new Dictionary<string, SchemaType>();
  17. #endregion
  18. #region properties
  19. /// <summary>
  20. /// returns all the <see cref="enum"/> types.
  21. /// </summary>
  22. public IEnumerable<EnumType> Enumerations => _Types.Values.OfType<EnumType>();
  23. /// <summary>
  24. /// returns all the <see cref="class"/> types.
  25. /// </summary>
  26. public IEnumerable<ClassType> Classes => _Types.Values.OfType<ClassType>();
  27. #endregion
  28. #region API
  29. /// <summary>
  30. /// Creates a new type or uses an existing one if it already exists.
  31. /// </summary>
  32. /// <param name="item">a newly created type</param>
  33. /// <returns>A stored type if it already exist, or the newly created type</returns>
  34. private SchemaType _UseOrCreate(SchemaType item)
  35. {
  36. if (_Types.TryGetValue(item.PersistentName, out SchemaType value)) return value;
  37. _Types[item.PersistentName] = item;
  38. return item;
  39. }
  40. public ObjectType UseAnyType() { return (ObjectType)_UseOrCreate( new ObjectType(this) ); }
  41. public StringType UseString() { return (StringType)_UseOrCreate( new StringType(this) ); }
  42. public ArrayType UseArray(SchemaType elementType) { return (ArrayType)_UseOrCreate( new ArrayType(this, elementType) ); }
  43. public ClassType UseClass(string name) { return (ClassType)_UseOrCreate(new ClassType(this, name)); }
  44. public BlittableType UseBlittable(TypeInfo t, bool isNullable = false)
  45. {
  46. if (t == null || !t.IsValueType) throw new ArgumentException(nameof(t));
  47. var item = new BlittableType(this, t, isNullable);
  48. return (BlittableType)_UseOrCreate(item);
  49. }
  50. public EnumType UseEnum(string name, bool isNullable = false)
  51. {
  52. var item = new EnumType(this, name, isNullable);
  53. return (EnumType)_UseOrCreate(item);
  54. }
  55. public EnumType GetEnum(string name)
  56. {
  57. return _Types.TryGetValue(name, out SchemaType etype) ? etype as EnumType : null;
  58. }
  59. public DictionaryType UseDictionary(SchemaType key, SchemaType val)
  60. {
  61. return (DictionaryType)_UseOrCreate( new DictionaryType(this, key, val) );
  62. }
  63. public void Remove(SchemaType type) { _Types.Remove(type.PersistentName); }
  64. public void Remove(string persistentName) { _Types.Remove(persistentName); }
  65. #endregion
  66. }
  67. }
  68. }