Skip to content

Commit 5407178

Browse files
Add TryGetExtension methods to AL & ALContext (#598)
* Added TryGetExtension() to AL class * Added a draft TryGetExtension to ALContext * Added Device* to TryGetExtension * Added Obsolete attributes * Updated XML * Apply suggestions from code review Co-authored-by: Dylan Perks <11160611+Perksey@users.noreply.github.com> Co-authored-by: Dylan Perks <11160611+Perksey@users.noreply.github.com>
1 parent ecedff1 commit 5407178

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

src/OpenAL/Silk.NET.OpenAL/AL/AL.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System;
55
using System.Numerics;
6-
using Microsoft.Extensions.DependencyModel;
76
using Silk.NET.Core.Attributes;
87
using Silk.NET.Core.Contexts;
98
using Silk.NET.Core.Loader;
@@ -350,11 +349,31 @@ public static AL GetApi(bool soft = false)
350349
return ret;
351350
}
352351

352+
/// <summary>
353+
/// Attempts to load a native OpenAL extension of type <typeparamref name="T" />.
354+
/// </summary>
355+
/// <param name="ext">The loaded extension.</param>
356+
/// <typeparam name="T">Type of <see cref="NativeExtension{T}" /> to load.</typeparam>
357+
/// <returns><c>true</c> if the extension was loaded, otherwise <c>false</c>.</returns>
358+
public bool TryGetExtension<T>(out T ext)
359+
where T : NativeExtension<AL>
360+
{
361+
ext = IsExtensionPresent(ExtensionAttribute.GetExtensionAttribute(typeof(T)).Name)
362+
? (T) Activator.CreateInstance(typeof(T), Context)
363+
: null;
364+
return ext is not null;
365+
}
366+
353367
/// <summary>
354368
/// Gets an instance of the API of an extension to the API.
355369
/// </summary>
356370
/// <typeparam name="TExtension">The extension type.</typeparam>
357371
/// <returns>The extension.</returns>
372+
[Obsolete
373+
(
374+
"This method has been deprecated and will be removed in Silk.NET 3.0. " +
375+
"Please use TryGetExtension instead."
376+
)]
358377
public TExtension GetExtension<TExtension>()
359378
where TExtension : NativeExtension<AL>
360379
{

src/OpenAL/Silk.NET.OpenAL/ALC/ALContext.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using Silk.NET.Core.Attributes;
56
using Silk.NET.Core.Contexts;
67
using Silk.NET.Core.Loader;
78
using Silk.NET.Core.Native;
@@ -110,12 +111,29 @@ public static unsafe ALContext GetApi(bool soft = false)
110111
return ret;
111112
}
112113

114+
/// <summary>
115+
/// Attempts to load the given extension.
116+
/// </summary>
117+
/// <typeparam name="T">The extension type.</typeparam>
118+
/// <param name="device">The device the context is on.</param>
119+
/// <param name="ext">The extension to check for.</param>
120+
/// <returns>Whether the extension is available.</returns>
121+
public unsafe bool TryGetExtension<T>(Device* device, out T ext) where T : NativeExtension<ALContext>
122+
=> !((ext = IsExtensionPresent(device, ExtensionAttribute.GetExtensionAttribute(typeof(T)).Name)
123+
? (T) Activator.CreateInstance(typeof(T), Context)
124+
: null) is null);
125+
113126
/// <summary>
114127
/// Gets an instance of the API of an extension to the API.
115128
/// </summary>
116129
/// <typeparam name="TContextExtension">The extension type.</typeparam>
117130
/// <param name="device">The device the context is on.</param>
118131
/// <returns>The extension.</returns>
132+
[Obsolete
133+
(
134+
"This method has been deprecated and will be removed in Silk.NET 3.0. " +
135+
"Please use TryGetExtension instead."
136+
)]
119137
public unsafe TContextExtension GetExtension<TContextExtension>(Device* device)
120138
where TContextExtension : ContextExtensionBase
121139
{

src/OpenAL/Silk.NET.OpenAL/Extensions/ALExtensionLoader.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ namespace Silk.NET.OpenAL.Extensions
1111
/// <summary>
1212
/// A loader for OpenAL extensions.
1313
/// </summary>
14+
[Obsolete
15+
(
16+
"This class is deprecated and will be removed in Silk.NET 3.0. "+
17+
"Please use the TryGetExtension method on ALContext."
18+
)]
1419
public static class ALExtensionLoader
1520
{
1621
/// <summary>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy