/// <summary> /// Function to locate a graphics resource by its name. /// </summary> /// <typeparam name="T">The type of graphics resource to look up. Must inherit from <see cref="GorgonGraphicsResource"/>.</typeparam> /// <param name="graphics">The graphics instance that was used to create the resource.</param> /// <param name="name">The name of the resource to find.</param> /// <param name="filterType">[Optional] The type of filter to apply.</param> /// <param name="comparisonType">[Optional] The type of string comparison to use for name comparison.</param> /// <returns>An enumerable containing the resources with names that match the filter type.</returns> /// <exception cref="ArgumentNullException">Thrown when the <paramref name="graphics"/> parameter is <b>null</b>.</exception> /// <remarks> /// <para> /// Each instance of the <seealso cref="GorgonGraphics"/> keeps a weak registration of each objected inheriting from <see cref="GorgonGraphicsResource"/> that was created during the lifetime of the /// application. Using this registration an application can look up any previously created resource (assuming it's not been collected, or disposed) should it be necessary. /// </para> /// <para> /// <note type="important"> /// Resource names are not required to be unique. Therefore, searching for the name may result in multiple items being returned in the enumerable. /// </note> /// </para> /// </remarks> /// <seealso cref="GorgonGraphicsResource"/> public static IEnumerable <T> LocateResourcesByName <T>(this GorgonGraphics graphics, string name, LocateFilterType filterType = LocateFilterType.Equal, StringComparison comparisonType = StringComparison.CurrentCultureIgnoreCase) where T : GorgonGraphicsResource { if (graphics == null) { throw new ArgumentNullException(nameof(graphics)); } if (string.IsNullOrWhiteSpace(name)) { return(Enumerable.Empty <T>()); } return(graphics.GetDisposables() .Select(item => { // If the object has been collected/disposed, then do nothing. if ((!item.TryGetTarget(out IDisposable disposable)) || (!(disposable is T resource)) || (resource.IsDisposed)) { return null; } return !NameComparison(name, resource.Name, filterType, comparisonType) ? null : resource; }) .Where(item => item != null)); }
/// <summary> /// Function to locate a graphics resource by its type. /// </summary> /// <typeparam name="T">The type of graphics resource to look up. Must inherit from <see cref="GorgonGraphicsResource"/>.</typeparam> /// <param name="graphics">The graphics instance that was used to create the resource.</param> /// <returns>An enumerable containing the resources with names that match the filter type.</returns> /// <exception cref="ArgumentNullException">Thrown when the <paramref name="graphics"/> parameter is <b>null</b>.</exception> /// <remarks> /// <para> /// Each instance of the <seealso cref="GorgonGraphics"/> keeps a weak registration of each objected inheriting from <see cref="GorgonGraphicsResource"/> that was created during the lifetime of the /// application. Using this registration an application can look up any previously created resource (assuming it's not been collected, or disposed) should it be necessary. /// </para> /// </remarks> /// <seealso cref="GorgonGraphicsResource"/> public static IEnumerable <T> LocateResourcesByType <T>(this GorgonGraphics graphics) where T : GorgonGraphicsResource { if (graphics == null) { throw new ArgumentNullException(nameof(graphics)); } return(graphics.GetDisposables() .Select(item => { // If the object has been collected/disposed, then do nothing. return ((!item.TryGetTarget(out IDisposable disposable)) || (!(disposable is T resource)) || (resource.IsDisposed)) ? null : resource; }) .Where(item => item != null)); }