using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; namespace SharpGLTF.SchemaReflection { public partial class SchemaType { /// /// Collection class that contains all the types found in a json schema. /// public sealed class Context { #region data private readonly Dictionary _Types = new Dictionary(); #endregion #region properties /// /// returns all the types. /// public IEnumerable Enumerations => _Types.Values.OfType(); /// /// returns all the types. /// public IEnumerable Classes => _Types.Values.OfType(); #endregion #region API /// /// Creates a new type or uses an existing one if it already exists. /// /// a newly created type /// A stored type if it already exist, or the newly created type private SchemaType _UseOrCreate(SchemaType item) { if (_Types.TryGetValue(item.PersistentName, out SchemaType value)) return value; _Types[item.PersistentName] = item; return item; } public ObjectType UseAnyType() { return (ObjectType)_UseOrCreate( new ObjectType(this) ); } public StringType UseString() { return (StringType)_UseOrCreate( new StringType(this) ); } public ArrayType UseArray(SchemaType elementType) { return (ArrayType)_UseOrCreate( new ArrayType(this, elementType) ); } public ClassType UseClass(string name) { return (ClassType)_UseOrCreate(new ClassType(this, name)); } public BlittableType UseBlittable(TypeInfo t, bool isNullable = false) { if (t == null || !t.IsValueType) throw new ArgumentException(nameof(t)); var item = new BlittableType(this, t, isNullable); return (BlittableType)_UseOrCreate(item); } public EnumType UseEnum(string name, bool isNullable = false) { var item = new EnumType(this, name, isNullable); return (EnumType)_UseOrCreate(item); } public EnumType GetEnum(string name) { return _Types.TryGetValue(name, out SchemaType etype) ? etype as EnumType : null; } public DictionaryType UseDictionary(SchemaType key, SchemaType val) { return (DictionaryType)_UseOrCreate( new DictionaryType(this, key, val) ); } public void Remove(SchemaType type) { _Types.Remove(type.PersistentName); } public void Remove(string persistentName) { _Types.Remove(persistentName); } #endregion } } }