// ----------------------------------------------------------------------------- // file="GlRegistryModel" // Project: StgSharp // Copyright (c) Nitload. // SPDX-License-Identifier: MIT // ----------------------------------------------------------------------------- using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; namespace StgSharp.GenerateGL.Registry { /// /// Contains the structural OpenGL registry model without managed type mapping. /// internal sealed class GlRegistryModel { public GlRegistryModel( string sourcePath, string? comment, IEnumerable types, IEnumerable kinds, IEnumerable enumGroups, IEnumerable enumBlocks, IEnumerable commandBlocks, IEnumerable features, IEnumerable extensions, GlRegistryElement element ) { ArgumentException.ThrowIfNullOrWhiteSpace(sourcePath); ArgumentNullException.ThrowIfNull(types); ArgumentNullException.ThrowIfNull(kinds); ArgumentNullException.ThrowIfNull(enumGroups); ArgumentNullException.ThrowIfNull(enumBlocks); ArgumentNullException.ThrowIfNull(commandBlocks); ArgumentNullException.ThrowIfNull(features); ArgumentNullException.ThrowIfNull(extensions); ArgumentNullException.ThrowIfNull(element); SourcePath = sourcePath; Comment = comment; Types = Freeze(types); Kinds = Freeze(kinds); EnumGroups = Freeze(enumGroups); EnumBlocks = Freeze(enumBlocks); CommandBlocks = Freeze(commandBlocks); Commands = Freeze(CommandBlocks.SelectMany(value => value.Commands)); Features = Freeze(features); Extensions = Freeze(extensions); Element = element; TypesByName = CreateIndex(Types, value => value.Name); KindsByName = CreateIndex(Kinds, value => value.Name); EnumGroupsByName = CreateIndex(EnumGroups, value => value.Name); EnumsByName = CreateIndex( EnumBlocks.SelectMany(value => value.Enums), value => value.Name); CommandsByName = CreateIndex(Commands, value => value.Name); FeaturesByName = CreateIndex(Features, value => value.Name); ExtensionsByName = CreateIndex(Extensions, value => value.Name); } public string SourcePath { get; } public string? Comment { get; } public IReadOnlyList Types { get; } public IReadOnlyList Kinds { get; } public IReadOnlyList EnumGroups { get; } public IReadOnlyList EnumBlocks { get; } public IReadOnlyList CommandBlocks { get; } public IReadOnlyList Commands { get; } public IReadOnlyList Features { get; } public IReadOnlyList Extensions { get; } public GlRegistryElement Element { get; } public IReadOnlyDictionary> TypesByName { get; } public IReadOnlyDictionary> KindsByName { get; } public IReadOnlyDictionary> EnumGroupsByName { get; } public IReadOnlyDictionary> EnumsByName { get; } public IReadOnlyDictionary> CommandsByName { get; } public IReadOnlyDictionary> FeaturesByName { get; } public IReadOnlyDictionary> ExtensionsByName { get; } private static ReadOnlyDictionary> CreateIndex( IEnumerable values, Func readName ) { Dictionary> index = values.GroupBy( readName, StringComparer.Ordinal) .ToDictionary( value => value.Key, value => (IReadOnlyList)Freeze(value), StringComparer.Ordinal); return new ReadOnlyDictionary>(index); } private static ReadOnlyCollection Freeze( IEnumerable values ) { return Array.AsReadOnly(values.ToArray()); } } }