SchemaTypesContext.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 EnumType FindEnum(string persistentName) { return Enumerations.FirstOrDefault(item => item.PersistentName == persistentName); }
  43. public ArrayType UseArray(SchemaType elementType) { return (ArrayType)_UseOrCreate( new ArrayType(this, elementType) ); }
  44. public ClassType FindClass(string persistentName) { return Classes.FirstOrDefault(item => item.PersistentName == persistentName); }
  45. public ClassType UseClass(string name) { return (ClassType)_UseOrCreate(new ClassType(this, name)); }
  46. public BlittableType UseBlittable(TypeInfo t, bool isNullable = false)
  47. {
  48. if (t == null || !t.IsValueType) throw new ArgumentException(nameof(t));
  49. var item = new BlittableType(this, t, isNullable);
  50. return (BlittableType)_UseOrCreate(item);
  51. }
  52. public EnumType UseEnum(string name, bool isNullable = false)
  53. {
  54. var item = new EnumType(this, name, isNullable);
  55. return (EnumType)_UseOrCreate(item);
  56. }
  57. public EnumType GetEnum(string name)
  58. {
  59. return _Types.TryGetValue(name, out SchemaType etype) ? etype as EnumType : null;
  60. }
  61. public DictionaryType UseDictionary(SchemaType key, SchemaType val)
  62. {
  63. return (DictionaryType)_UseOrCreate( new DictionaryType(this, key, val) );
  64. }
  65. public void Remove(SchemaType type) { _Types.Remove(type.PersistentName); }
  66. public void Remove(string persistentName) { _Types.Remove(persistentName); }
  67. public void IgnoredByCodeEmitter(string persistentName)
  68. {
  69. var ct = Classes.FirstOrDefault(item => item.PersistentName == persistentName);
  70. if (ct != null) ct.IgnoredByEmitter = true;
  71. }
  72. #endregion
  73. }
  74. }
  75. }