Skip to content

Commit 8060d2b

Browse files
committed
Add support for IPrefetchableSupport, ITestPlugProvider and ITestPlugProvider2
1 parent 59b7e5b commit 8060d2b

20 files changed

+368
-82
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) Alexandre Mutel. All rights reserved.
2+
// Licensed under the BSD-Clause 2 license.
3+
// See license.txt file in the project root for full license information.
4+
5+
using System;
6+
7+
namespace NPlug;
8+
9+
/// <summary>
10+
/// An <see cref="AudioController"/> plugin class info
11+
/// </summary>
12+
public sealed class AudioControllerClassInfo : AudioPluginClassInfo
13+
{
14+
/// <summary>
15+
/// Creates a new instance of this plugin info.
16+
/// </summary>
17+
/// <param name="classId"></param>
18+
/// <param name="name"></param>
19+
public AudioControllerClassInfo(Guid classId, string name) : base(classId, name, AudioPluginCategory.Controller)
20+
{
21+
}
22+
}

src/NPlug/AudioPluginCategory.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) Alexandre Mutel. All rights reserved.
2+
// Licensed under the BSD-Clause 2 license.
3+
// See license.txt file in the project root for full license information.
4+
5+
namespace NPlug;
6+
7+
/// <summary>
8+
/// Category of an audio processor.
9+
/// </summary>
10+
public enum AudioPluginCategory
11+
{
12+
Processor,
13+
14+
Controller,
15+
16+
TestProvider,
17+
}

src/NPlug/AudioPluginClassInfo.cs

Lines changed: 10 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,34 @@ public abstract class AudioPluginClassInfo
1616
/// <summary>
1717
/// Creates a new instance of this plugin info.
1818
/// </summary>
19-
/// <param name="guid"></param>
19+
/// <param name="classId"></param>
2020
/// <param name="name"></param>
2121
/// <param name="category"></param>
22-
internal AudioPluginClassInfo(Guid guid, string name)
22+
internal AudioPluginClassInfo(Guid classId, string name, AudioPluginCategory pluginCategory)
2323
{
24-
Id = guid;
24+
ClassId = classId;
2525
Name = name;
2626
ClassFlags = AudioPluginFlags.None;
2727
Cardinality = int.MaxValue;
2828
Vendor = string.Empty;
2929
Version = EmptyVersion;
30+
PluginCategory = pluginCategory;
3031
}
3132

3233
/// <summary>
3334
/// see @ref PClassInfo
3435
/// </summary>
35-
public Guid Id { get; }
36+
public Guid ClassId { get; }
3637

3738
/// <summary>
3839
/// Gets or init the category.
3940
/// </summary>
4041
public string Name { get; }
42+
43+
/// <summary>
44+
/// Gets the category of the plugin
45+
/// </summary>
46+
public AudioPluginCategory PluginCategory { get; }
4147

4248
/// <summary>
4349
/// flags used for a specific category, must be defined where category is defined
@@ -58,46 +64,4 @@ internal AudioPluginClassInfo(Guid guid, string name)
5864
/// Version string (e.g. "1.0.0.512" with Major.Minor.Subversion.Build)
5965
/// </summary>
6066
public Version Version { get; init; }
61-
}
62-
63-
/// <summary>
64-
/// An <see cref="AudioProcessor"/> plugin class info
65-
/// </summary>
66-
public sealed class AudioProcessorClassInfo : AudioPluginClassInfo
67-
{
68-
private static readonly Version EmptyVersion = new(0, 0);
69-
70-
/// <summary>
71-
/// Creates a new instance of this plugin info.
72-
/// </summary>
73-
/// <param name="guid"></param>
74-
/// <param name="name"></param>
75-
/// <param name="category"></param>
76-
public AudioProcessorClassInfo(Guid guid, string name, AudioProcessorCategory category) : base(guid, name)
77-
{
78-
Category = category;
79-
}
80-
81-
/// <summary>
82-
/// Gets or init the category.
83-
/// </summary>
84-
public AudioProcessorCategory Category { get; init; }
85-
}
86-
87-
88-
/// <summary>
89-
/// An <see cref="AudioController"/> plugin class info
90-
/// </summary>
91-
public sealed class AudioControllerClassInfo : AudioPluginClassInfo
92-
{
93-
private static readonly Version EmptyVersion = new(0, 0);
94-
95-
/// <summary>
96-
/// Creates a new instance of this plugin info.
97-
/// </summary>
98-
/// <param name="guid"></param>
99-
/// <param name="name"></param>
100-
public AudioControllerClassInfo(Guid guid, string name) : base(guid, name)
101-
{
102-
}
10367
}

src/NPlug/AudioPluginFactory.cs

Lines changed: 72 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,51 @@ namespace NPlug;
99

1010
public sealed class AudioPluginFactory : IAudioPluginFactory
1111
{
12-
private readonly List<AudioPluginClassInfo> _plugins;
13-
private readonly Dictionary<Guid, Func<IAudioPluginComponent>> _mapGuidToComponentFactory;
12+
private readonly List<AudioPluginClassInfo> _pluginClassInfos;
13+
private readonly Dictionary<Guid, Func<IAudioPluginObject>> _mapGuidToComponentFactory;
1414

1515
public AudioPluginFactory(AudioPluginFactoryInfo factoryInfo)
1616
{
1717
FactoryInfo = factoryInfo;
18-
_plugins = new List<AudioPluginClassInfo>();
19-
_mapGuidToComponentFactory = new Dictionary<Guid, Func<IAudioPluginComponent>>();
18+
_pluginClassInfos = new List<AudioPluginClassInfo>();
19+
_mapGuidToComponentFactory = new Dictionary<Guid, Func<IAudioPluginObject>>();
2020
}
2121

2222
public AudioPluginFactoryInfo FactoryInfo { get; }
23-
24-
public void RegisterPlugin<TPlugin>(AudioProcessorClassInfo pluginClassInfo) where TPlugin: class, IAudioProcessor, new()
23+
24+
public IReadOnlyList<AudioPluginClassInfo> PluginClassInfos => _pluginClassInfos;
25+
26+
public IAudioPluginObject? CreateInstance(Guid pluginId)
27+
{
28+
return _mapGuidToComponentFactory.TryGetValue(pluginId, out var factory) ? factory() : null;
29+
}
30+
31+
public void RegisterPlugin<TPlugin>(AudioProcessorClassInfo pluginClassInfo, bool registerTests = false) where TPlugin: class, IAudioProcessor, new()
2532
{
2633
RegisterPlugin(pluginClassInfo, static () => new TPlugin());
34+
35+
// Register test plugin on the fly
36+
if (registerTests)
37+
{
38+
var testPluginClassInfo = new AudioTestProviderClassInfo(Guid.NewGuid(), $"{pluginClassInfo.Name} Test Provider");
39+
int indexOfAudioProcessor = _pluginClassInfos.Count - 1;
40+
RegisterPlugin(testPluginClassInfo, () => new AudioTestProvider(this, indexOfAudioProcessor));
41+
}
2742
}
2843

2944
public void RegisterPlugin<TPlugin>(AudioControllerClassInfo pluginClassInfo) where TPlugin : class, IAudioController, new()
3045
{
3146
RegisterPlugin(pluginClassInfo, static () => new TPlugin());
3247
}
3348

34-
public void RegisterPlugin(AudioPluginClassInfo pluginClassInfo, Func<IAudioPluginComponent> factory)
49+
public void RegisterPlugin(AudioPluginClassInfo pluginClassInfo, Func<IAudioPluginObject> factory)
3550
{
36-
if (_mapGuidToComponentFactory.ContainsKey(pluginClassInfo.Id))
51+
if (_mapGuidToComponentFactory.ContainsKey(pluginClassInfo.ClassId))
3752
{
38-
throw new InvalidOperationException($"The plugin {pluginClassInfo.Name} with id: {pluginClassInfo.Id} has been already registered");
53+
throw new InvalidOperationException($"The plugin {pluginClassInfo.Name} with id: {pluginClassInfo.ClassId} has been already registered");
3954
}
40-
_mapGuidToComponentFactory.Add(pluginClassInfo.Id, factory);
41-
_plugins.Add(pluginClassInfo);
55+
_mapGuidToComponentFactory.Add(pluginClassInfo.ClassId, factory);
56+
_pluginClassInfos.Add(pluginClassInfo);
4257
}
4358

4459
/// <summary>
@@ -47,23 +62,61 @@ public void RegisterPlugin(AudioPluginClassInfo pluginClassInfo, Func<IAudioPlug
4762
/// <param name="factory">The factory to copy registered plugins.</param>
4863
public void CopyFrom(AudioPluginFactory factory)
4964
{
50-
foreach (var pluginInfo in factory._plugins)
65+
foreach (var pluginInfo in factory._pluginClassInfos)
5166
{
52-
var componentFactory = _mapGuidToComponentFactory[pluginInfo.Id];
67+
var componentFactory = _mapGuidToComponentFactory[pluginInfo.ClassId];
5368
RegisterPlugin(pluginInfo, componentFactory);
5469
}
5570
}
5671

57-
int IAudioPluginFactory.PluginClassInfoCount => _plugins.Count;
72+
int IAudioPluginFactory.PluginClassInfoCount => _pluginClassInfos.Count;
5873

5974
AudioPluginClassInfo IAudioPluginFactory.GetPluginClassInfo(int index)
6075
{
61-
if ((uint)index >= (uint)_plugins.Count) throw new ArgumentOutOfRangeException(nameof(index));
62-
return _plugins[index];
76+
if ((uint)index >= (uint)_pluginClassInfos.Count) throw new ArgumentOutOfRangeException(nameof(index));
77+
return _pluginClassInfos[index];
6378
}
64-
65-
IAudioPluginComponent? IAudioPluginFactory.CreateInstance(Guid pluginId)
79+
80+
/// <summary>
81+
/// Base class for <see cref="IAudioTestProvider"/>.
82+
/// </summary>
83+
private class AudioTestProvider : IAudioTestProvider
6684
{
67-
return _mapGuidToComponentFactory.TryGetValue(pluginId, out var factory) ? factory() : null;
85+
private readonly AudioPluginFactory _factory;
86+
private readonly AudioProcessorClassInfo _audioProcessorClassInfo;
87+
private IAudioProcessor? _audioProcessor;
88+
private IAudioController? _audioController;
89+
90+
public AudioTestProvider(AudioPluginFactory factory, int indexToAudioProcessor)
91+
{
92+
_factory = factory;
93+
_audioProcessorClassInfo = (AudioProcessorClassInfo)_factory.PluginClassInfos[indexToAudioProcessor];
94+
}
95+
96+
public IAudioProcessor GetAudioProcessor()
97+
{
98+
if (_audioProcessor != null)
99+
{
100+
return _audioProcessor;
101+
}
102+
_audioProcessor = (IAudioProcessor)_factory.CreateInstance(_audioProcessorClassInfo.ClassId)!;
103+
return _audioProcessor;
104+
}
105+
106+
public IAudioController GetAudioController()
107+
{
108+
if (_audioController != null)
109+
{
110+
return _audioController;
111+
}
112+
_audioController = (IAudioController)_factory.CreateInstance(GetAudioProcessor().ControllerId)!;
113+
return _audioController;
114+
}
115+
116+
public AudioProcessorCategory GetAudioProcessorCategory() => _audioProcessorClassInfo.Category;
117+
118+
public Guid GetAudioProcessorClassId() => _audioProcessorClassInfo.ClassId;
119+
120+
public IAudioPluginFactory GetPluginFactory() => _factory;
68121
}
69122
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (c) Alexandre Mutel. All rights reserved.
2+
// Licensed under the BSD-Clause 2 license.
3+
// See license.txt file in the project root for full license information.
4+
5+
using System;
6+
7+
namespace NPlug;
8+
9+
/// <summary>
10+
/// An <see cref="AudioProcessor"/> plugin class info
11+
/// </summary>
12+
public sealed class AudioProcessorClassInfo : AudioPluginClassInfo
13+
{
14+
/// <summary>
15+
/// Creates a new instance of this plugin info.
16+
/// </summary>
17+
/// <param name="classId"></param>
18+
/// <param name="name"></param>
19+
/// <param name="category"></param>
20+
public AudioProcessorClassInfo(Guid classId, string name, AudioProcessorCategory category) : base(classId, name, AudioPluginCategory.Processor)
21+
{
22+
Category = category;
23+
}
24+
25+
/// <summary>
26+
/// Gets or init the category.
27+
/// </summary>
28+
public AudioProcessorCategory Category { get; init; }
29+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) Alexandre Mutel. All rights reserved.
2+
// Licensed under the BSD-Clause 2 license.
3+
// See license.txt file in the project root for full license information.
4+
5+
namespace NPlug;
6+
7+
/// <summary>
8+
/// Prefetchable Support Enum
9+
/// </summary>
10+
public enum AudioProcessorPrefetchableSupport
11+
{
12+
/// <summary>
13+
/// every instance of the plug does not support prefetch processing
14+
/// </summary>
15+
IsNeverPrefetchable = 0,
16+
17+
/// <summary>
18+
/// in the current state the plug support prefetch processing
19+
/// </summary>
20+
IsYetPrefetchable,
21+
22+
/// <summary>
23+
/// in the current state the plug does not support prefetch processing
24+
/// </summary>
25+
IsNotYetPrefetchable,
26+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) Alexandre Mutel. All rights reserved.
2+
// Licensed under the BSD-Clause 2 license.
3+
// See license.txt file in the project root for full license information.
4+
5+
using System;
6+
7+
namespace NPlug;
8+
9+
/// <summary>
10+
/// An <see cref="AudioController"/> plugin class info
11+
/// </summary>
12+
public sealed class AudioTestProviderClassInfo : AudioPluginClassInfo
13+
{
14+
/// <summary>
15+
/// Creates a new instance of this plugin info.
16+
/// </summary>
17+
/// <param name="classId"></param>
18+
/// <param name="name"></param>
19+
public AudioTestProviderClassInfo(Guid classId, string name) : base(classId, name, AudioPluginCategory.TestProvider)
20+
{
21+
}
22+
}

src/NPlug/IAudioPluginComponent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace NPlug;
1111
/// A list of supported host context interfaces should be included in the documentation
1212
/// of a specific category.
1313
/// </summary>
14-
public interface IAudioPluginComponent
14+
public interface IAudioPluginComponent : IAudioPluginObject
1515
{
1616
/// <summary>
1717
/// The host passes a number of interfaces as context to initialize the plug-in class.

src/NPlug/IAudioPluginFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ public interface IAudioPluginFactory
1414

1515
AudioPluginClassInfo GetPluginClassInfo(int index);
1616

17-
IAudioPluginComponent? CreateInstance(Guid pluginId);
17+
IAudioPluginObject? CreateInstance(Guid pluginId);
1818
}

src/NPlug/IAudioPluginObject.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) Alexandre Mutel. All rights reserved.
2+
// Licensed under the BSD-Clause 2 license.
3+
// See license.txt file in the project root for full license information.
4+
5+
namespace NPlug;
6+
7+
/// <summary>
8+
/// Base interface for instantiable plugin object from the <see cref="IAudioPluginFactory.CreateInstance"/>.
9+
/// </summary>
10+
public interface IAudioPluginObject
11+
{
12+
}

0 commit comments

Comments
 (0)