/// <summary> /// Function to perform a comparison of an item name to the name specified, using the filter type specified. /// </summary> /// <param name="name">The name to look up.</param> /// <param name="itemName">The name of the item to evaluate.</param> /// <param name="filterType">The type of filter to apply.</param> /// <param name="comparisonType">The type of string comparer to use.</param> /// <returns><b>true</b> if the resource name matches the filter type, or <b>false</b> if not.</returns> private static bool NameComparison(string name, string itemName, LocateFilterType filterType, StringComparison comparisonType) { if (string.IsNullOrWhiteSpace(itemName)) { return(false); } switch (filterType) { case LocateFilterType.StartsWith: return(itemName.StartsWith(name, comparisonType)); case LocateFilterType.EndsWith: return(itemName.EndsWith(name, comparisonType)); case LocateFilterType.Contains: return(itemName.IndexOf(name, comparisonType) != -1); case LocateFilterType.NotContains: return(itemName.IndexOf(name, comparisonType) == -1); case LocateFilterType.NotStartsWith: return(!itemName.StartsWith(name, comparisonType)); case LocateFilterType.NotEndsWith: return(!itemName.EndsWith(name, comparisonType)); case LocateFilterType.NotEqual: return(!string.Equals(name, itemName, comparisonType)); default: return(string.Equals(name, itemName, comparisonType)); } }
/// <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)); }