Skip to content

Commit 55944f0

Browse files
committed
Switch to Polyfill package and polyfill throw helpers
1 parent bbbec37 commit 55944f0

12 files changed

+42
-196
lines changed

src/Directory.Packages.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<ItemGroup>
1717
<PackageVersion Include="MessagePack" Version="3.1.4" />
1818
<PackageVersion Include="Microsoft.IO.RecyclableMemoryStream" Version="3.0.1" />
19-
<PackageVersion Include="PolySharp" Version="1.15.0" />
19+
<PackageVersion Include="Polyfill" Version="9.0.3" />
2020
</ItemGroup>
2121

2222
<ItemGroup>

src/TinyIpc/DependencyInjection/ServiceCollectionExtensions.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#if NET
21
using System.Runtime.Versioning;
3-
#endif
42
using Microsoft.Extensions.DependencyInjection.Extensions;
53
using TinyIpc;
64
using TinyIpc.IO;
@@ -16,9 +14,7 @@ public static partial class ServiceCollectionExtensions
1614
/// <summary>
1715
/// Add a <see cref="ITinyReadWriteLock"/> with default options.
1816
/// </summary>
19-
#if NET
2017
[SupportedOSPlatform("windows")]
21-
#endif
2218
public IServiceCollection AddTinyReadWriteLock()
2319
{
2420
services.AddTinyReadWriteLock(_ => { });
@@ -29,9 +25,7 @@ public IServiceCollection AddTinyReadWriteLock()
2925
/// <summary>
3026
/// Add a <see cref="ITinyReadWriteLock"/> and configure default options.
3127
/// </summary>
32-
#if NET
3328
[SupportedOSPlatform("windows")]
34-
#endif
3529
public IServiceCollection AddTinyReadWriteLock(Action<TinyIpcOptions> configure)
3630
{
3731
services.AddOptions<TinyIpcOptions>()
@@ -46,9 +40,7 @@ public IServiceCollection AddTinyReadWriteLock(Action<TinyIpcOptions> configure)
4640
/// <para>Add a <see cref="ITinyMemoryMappedFile"/> with default options.</para>
4741
/// <para>This also adds a <see cref="ITinyReadWriteLock"/> which is used by the file.</para>
4842
/// </summary>
49-
#if NET
5043
[SupportedOSPlatform("windows")]
51-
#endif
5244
public IServiceCollection AddTinyMemoryMappedFile()
5345
{
5446
services.AddTinyMemoryMappedFile(_ => { });
@@ -60,9 +52,7 @@ public IServiceCollection AddTinyMemoryMappedFile()
6052
/// <para>Add a <see cref="ITinyMemoryMappedFile"/> and configure default options.</para>
6153
/// <para>This also adds a <see cref="ITinyReadWriteLock"/> which is used by the file.</para>
6254
/// </summary>
63-
#if NET
6455
[SupportedOSPlatform("windows")]
65-
#endif
6656
public IServiceCollection AddTinyMemoryMappedFile(Action<TinyIpcOptions> configure)
6757
{
6858
services.AddTinyReadWriteLock(configure);
@@ -76,9 +66,7 @@ public IServiceCollection AddTinyMemoryMappedFile(Action<TinyIpcOptions> configu
7666
/// <para>Add a <see cref="ITinyMessageBus"/> with default options.</para>
7767
/// <para>This also adds a <see cref="ITinyReadWriteLock"/> and a <see cref="ITinyReadWriteLock"/> which is used by the message bus.</para>
7868
/// </summary>
79-
#if NET
8069
[SupportedOSPlatform("windows")]
81-
#endif
8270
public IServiceCollection AddTinyMessageBus()
8371
{
8472
services.AddTinyMessageBus(_ => { });
@@ -90,9 +78,7 @@ public IServiceCollection AddTinyMessageBus()
9078
/// <para>Add a <see cref="ITinyMessageBus"/> and configure default options.</para>
9179
/// <para>This also adds a <see cref="ITinyReadWriteLock"/> and a <see cref="ITinyReadWriteLock"/> which is used by the message bus.</para>
9280
/// </summary>
93-
#if NET
9481
[SupportedOSPlatform("windows")]
95-
#endif
9682
public IServiceCollection AddTinyMessageBus(Action<TinyIpcOptions> configure)
9783
{
9884
services.AddTinyMemoryMappedFile(configure);

src/TinyIpc/DependencyInjection/ServiceCollectionFactoryExtensions.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#if NET
21
using System.Runtime.Versioning;
3-
#endif
42
using Microsoft.Extensions.DependencyInjection.Extensions;
53
using TinyIpc;
64
using TinyIpc.DependencyInjection;
@@ -17,9 +15,7 @@ public static partial class ServiceCollectionFactoryExtensions
1715
/// <remarks>
1816
/// Use <see cref="ITinyIpcFactory.CreateInstance()"/> to create instances.
1917
/// </remarks>
20-
#if NET
2118
[SupportedOSPlatform("windows")]
22-
#endif
2319
public IServiceCollection AddTinyIpcFactory()
2420
{
2521
return AddTinyIpcFactory(services, _ => { });
@@ -31,9 +27,7 @@ public IServiceCollection AddTinyIpcFactory()
3127
/// <remarks>
3228
/// Use <see cref="ITinyIpcFactory.CreateInstance()"/> to create instances.
3329
/// </remarks>
34-
#if NET
3530
[SupportedOSPlatform("windows")]
36-
#endif
3731
public IServiceCollection AddTinyIpcFactory(Action<TinyIpcOptions> configure)
3832
{
3933
services.AddOptions<TinyIpcOptions>()
@@ -51,9 +45,7 @@ public IServiceCollection AddTinyIpcFactory(Action<TinyIpcOptions> configure)
5145
/// Use <see cref="ITinyIpcFactory.CreateInstance(string)"/> to create instances with the name.
5246
/// </remarks>
5347
/// <param name="name">Name of this configuration.</param>
54-
#if NET
5548
[SupportedOSPlatform("windows")]
56-
#endif
5749
public IServiceCollection AddTinyIpcFactory(string name, Action<TinyIpcOptions> configure)
5850
{
5951
services.AddOptions<TinyIpcOptions>(name)

src/TinyIpc/DependencyInjection/ServiceCollectionKeyedExtensions.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#if NET
21
using System.Runtime.Versioning;
3-
#endif
42
using Microsoft.Extensions.DependencyInjection.Extensions;
53
using TinyIpc;
64
using TinyIpc.IO;
@@ -17,9 +15,7 @@ public static partial class ServiceCollectionKeyedExtensions
1715
/// Add a keyed <see cref="ITinyReadWriteLock"/> with named options.
1816
/// </summary>
1917
/// <param name="serviceKey">Service key, this is alos used as a name for the options.</param>
20-
#if NET
2118
[SupportedOSPlatform("windows")]
22-
#endif
2319
public IServiceCollection AddKeyedTinyReadWriteLock(string serviceKey)
2420
{
2521
services.AddKeyedTinyReadWriteLock(serviceKey, _ => { });
@@ -31,9 +27,7 @@ public IServiceCollection AddKeyedTinyReadWriteLock(string serviceKey)
3127
/// Add a keyed <see cref="ITinyReadWriteLock"/> and configure named options.
3228
/// </summary>
3329
/// <param name="serviceKey">Service key, this is alos used as a name for the options.</param>
34-
#if NET
3530
[SupportedOSPlatform("windows")]
36-
#endif
3731
public IServiceCollection AddKeyedTinyReadWriteLock(string serviceKey, Action<TinyIpcOptions> configure)
3832
{
3933
services.AddOptions<TinyIpcOptions>(serviceKey)
@@ -50,9 +44,7 @@ public IServiceCollection AddKeyedTinyReadWriteLock(string serviceKey, Action<Ti
5044
/// <para>This also adds a <see cref="ITinyReadWriteLock"/> which is used by the file.</para>
5145
/// </summary>
5246
/// <param name="serviceKey">Service key, this is alos used as a name for the options.</param>
53-
#if NET
5447
[SupportedOSPlatform("windows")]
55-
#endif
5648
public IServiceCollection AddKeyedTinyMemoryMappedFile(string serviceKey)
5749
{
5850
services.AddKeyedTinyMemoryMappedFile(serviceKey, _ => { });
@@ -65,9 +57,7 @@ public IServiceCollection AddKeyedTinyMemoryMappedFile(string serviceKey)
6557
/// <para>This also adds a <see cref="ITinyReadWriteLock"/> which is used by the file.</para>
6658
/// </summary>
6759
/// <param name="serviceKey">Service key, this is alos used as a name for the options.</param>
68-
#if NET
6960
[SupportedOSPlatform("windows")]
70-
#endif
7161
public IServiceCollection AddKeyedTinyMemoryMappedFile(string serviceKey, Action<TinyIpcOptions> configure)
7262
{
7363
services.AddKeyedTinyReadWriteLock(serviceKey, configure);
@@ -82,9 +72,7 @@ public IServiceCollection AddKeyedTinyMemoryMappedFile(string serviceKey, Action
8272
/// <para>This also adds a <see cref="ITinyReadWriteLock"/> and a <see cref="ITinyReadWriteLock"/> which is used by the message bus.</para>
8373
/// </summary>
8474
/// <param name="serviceKey">Service key, this is alos used as a name for the options.</param>
85-
#if NET
8675
[SupportedOSPlatform("windows")]
87-
#endif
8876
public IServiceCollection AddKeyedTinyMessageBus(string serviceKey)
8977
{
9078
services.AddKeyedTinyMessageBus(serviceKey, _ => { });
@@ -97,9 +85,7 @@ public IServiceCollection AddKeyedTinyMessageBus(string serviceKey)
9785
/// <para>This also adds a <see cref="ITinyReadWriteLock"/> and a <see cref="ITinyReadWriteLock"/> which is used by the message bus.</para>
9886
/// </summary>
9987
/// <param name="serviceKey">Service key, this is alos used as a name for the options.</param>
100-
#if NET
10188
[SupportedOSPlatform("windows")]
102-
#endif
10389
public IServiceCollection AddKeyedTinyMessageBus(string serviceKey, Action<TinyIpcOptions> configure)
10490
{
10591
services.AddKeyedTinyMemoryMappedFile(serviceKey, configure);

src/TinyIpc/DependencyInjection/TinyIpcFactory.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#if NET
21
using System.Runtime.Versioning;
3-
#endif
42
using Microsoft.Extensions.Logging;
53
using Microsoft.Extensions.Options;
64

@@ -22,9 +20,7 @@ public interface ITinyIpcFactory
2220

2321
public sealed class TinyIpcFactory(IOptionsMonitor<TinyIpcOptions> optionsMonitor, ILoggerFactory loggerFactory) : ITinyIpcFactory
2422
{
25-
#if NET
2623
[SupportedOSPlatform("windows")]
27-
#endif
2824
public ITinyIpcInstance CreateInstance()
2925
{
3026
var instance = new TinyIpcInstance(
@@ -35,9 +31,7 @@ public ITinyIpcInstance CreateInstance()
3531
return instance;
3632
}
3733

38-
#if NET
3934
[SupportedOSPlatform("windows")]
40-
#endif
4135
public ITinyIpcInstance CreateInstance(string name)
4236
{
4337
var options = optionsMonitor.Get(name);

src/TinyIpc/DependencyInjection/TinyIpcInstance.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#if NET
21
using System.Runtime.Versioning;
3-
#endif
42
using Microsoft.Extensions.Logging;
53
using Microsoft.Extensions.Options;
64
using TinyIpc.IO;
@@ -26,9 +24,7 @@ public sealed class TinyIpcInstance : ITinyIpcInstance
2624
public ITinyMemoryMappedFile MemoryMappedFile => memoryMappedFile;
2725
public ITinyMessageBus MessageBus => messageBus;
2826

29-
#if NET
3027
[SupportedOSPlatform("windows")]
31-
#endif
3228
public TinyIpcInstance(IOptions<TinyIpcOptions> options, ILoggerFactory loggerFactory)
3329
{
3430
readWriteLock = new TinyReadWriteLock(options, loggerFactory.CreateLogger<TinyReadWriteLock>());

src/TinyIpc/IO/TinyMemoryMappedFile.cs

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System.Buffers;
22
using System.IO.MemoryMappedFiles;
3-
#if NET
43
using System.Runtime.Versioning;
5-
#endif
64
using Microsoft.Extensions.Logging;
75
using Microsoft.Extensions.Options;
86
using TinyIpc.Synchronization;
@@ -34,9 +32,7 @@ public partial class TinyMemoryMappedFile : ITinyMemoryMappedFile
3432
/// Initializes a new instance of the TinyMemoryMappedFile class.
3533
/// </summary>
3634
/// <param name="options">Options from dependency injection or an OptionsWrapper containing options</param>
37-
#if NET
3835
[SupportedOSPlatform("windows")]
39-
#endif
4036
public TinyMemoryMappedFile(ITinyReadWriteLock readWriteLock, IOptions<TinyIpcOptions> options, ILogger<TinyMemoryMappedFile> logger)
4137
: this((options ?? throw new ArgumentNullException(nameof(options))).Value.Name, options.Value.MaxFileSize, readWriteLock, disposeLock: false, logger)
4238
{
@@ -46,9 +42,7 @@ public TinyMemoryMappedFile(ITinyReadWriteLock readWriteLock, IOptions<TinyIpcOp
4642
/// Initializes a new instance of the TinyMemoryMappedFile class.
4743
/// </summary>
4844
/// <param name="name">A system wide unique name, the name will have a prefix appended before use</param>
49-
#if NET
5045
[SupportedOSPlatform("windows")]
51-
#endif
5246
public TinyMemoryMappedFile(string name, ILogger<TinyMemoryMappedFile>? logger = null)
5347
: this(name, TinyIpcOptions.DefaultMaxFileSize, logger)
5448
{
@@ -59,9 +53,7 @@ public TinyMemoryMappedFile(string name, ILogger<TinyMemoryMappedFile>? logger =
5953
/// </summary>
6054
/// <param name="name">A system wide unique name, the name will have a prefix appended before use</param>
6155
/// <param name="maxFileSize">The maximum amount of data that can be written to the file memory mapped file</param>
62-
#if NET
6356
[SupportedOSPlatform("windows")]
64-
#endif
6557
public TinyMemoryMappedFile(string name, long maxFileSize, ILogger<TinyMemoryMappedFile>? logger = null)
6658
: this(name, maxFileSize, new TinyReadWriteLock(name), disposeLock: true, logger)
6759
{
@@ -74,9 +66,7 @@ public TinyMemoryMappedFile(string name, long maxFileSize, ILogger<TinyMemoryMap
7466
/// <param name="maxFileSize">The maximum amount of data that can be written to the file memory mapped file</param>
7567
/// <param name="readWriteLock">A read/write lock that will be used to control access to the memory mapped file</param>
7668
/// <param name="disposeLock">Set to true if the read/write lock is to be disposed when this instance is disposed</param>
77-
#if NET
7869
[SupportedOSPlatform("windows")]
79-
#endif
8070
public TinyMemoryMappedFile(string name, long maxFileSize, ITinyReadWriteLock readWriteLock, bool disposeLock, ILogger<TinyMemoryMappedFile>? logger = null)
8171
: this(CreateOrOpenMemoryMappedFile(name, maxFileSize), CreateEventWaitHandle(name), maxFileSize, readWriteLock, disposeLock, logger)
8272
{
@@ -151,14 +141,7 @@ protected virtual void Dispose(bool disposing)
151141
/// <returns>File size</returns>
152142
public int GetFileSize(CancellationToken cancellationToken = default)
153143
{
154-
#if NET
155144
ObjectDisposedException.ThrowIf(disposed, this);
156-
#else
157-
if (disposed)
158-
{
159-
throw new ObjectDisposedException(nameof(TinyMemoryMappedFile));
160-
}
161-
#endif
162145

163146
using var readLock = readWriteLock.AcquireReadLock(cancellationToken);
164147
using var accessor = memoryMappedFile.CreateViewAccessor();
@@ -178,23 +161,8 @@ public int GetFileSize(CancellationToken cancellationToken = default)
178161
/// <returns>File content</returns>
179162
public T Read<T>(Func<MemoryStream, T> readData, CancellationToken cancellationToken = default)
180163
{
181-
#if NET
182164
ArgumentNullException.ThrowIfNull(readData);
183-
#else
184-
if (readData is null)
185-
{
186-
throw new ArgumentNullException(nameof(readData));
187-
}
188-
#endif
189-
190-
#if NET
191165
ObjectDisposedException.ThrowIf(disposed, this);
192-
#else
193-
if (disposed)
194-
{
195-
throw new ObjectDisposedException(nameof(TinyMemoryMappedFile));
196-
}
197-
#endif
198166

199167
using var readLock = readWriteLock.AcquireReadLock(cancellationToken);
200168
using var readStream = MemoryStreamPool.Manager.GetStream(nameof(TinyMemoryMappedFile));
@@ -215,14 +183,7 @@ public T Read<T>(Func<MemoryStream, T> readData, CancellationToken cancellationT
215183
/// </summary>
216184
public void Write(MemoryStream data, CancellationToken cancellationToken = default)
217185
{
218-
#if NET
219186
ArgumentNullException.ThrowIfNull(data);
220-
#else
221-
if (data is null)
222-
{
223-
throw new ArgumentNullException(nameof(data));
224-
}
225-
#endif
226187

227188
#if NET
228189
ArgumentOutOfRangeException.ThrowIfGreaterThan(data.Length, MaxFileSize);
@@ -233,14 +194,7 @@ public void Write(MemoryStream data, CancellationToken cancellationToken = defau
233194
}
234195
#endif
235196

236-
#if NET
237197
ObjectDisposedException.ThrowIf(disposed, this);
238-
#else
239-
if (disposed)
240-
{
241-
throw new ObjectDisposedException(nameof(TinyMemoryMappedFile));
242-
}
243-
#endif
244198

245199
// Make sure the file watcher is ready before writing
246200
watcherTaskCompletionSource.Task.GetAwaiter().GetResult();
@@ -268,23 +222,8 @@ public void Write(MemoryStream data, CancellationToken cancellationToken = defau
268222
/// </summary>
269223
public void ReadWrite(Action<MemoryStream, MemoryStream> updateFunc, CancellationToken cancellationToken = default)
270224
{
271-
#if NET
272225
ArgumentNullException.ThrowIfNull(updateFunc);
273-
#else
274-
if (updateFunc is null)
275-
{
276-
throw new ArgumentNullException(nameof(updateFunc));
277-
}
278-
#endif
279-
280-
#if NET
281226
ObjectDisposedException.ThrowIf(disposed, this);
282-
#else
283-
if (disposed)
284-
{
285-
throw new ObjectDisposedException(nameof(TinyMemoryMappedFile));
286-
}
287-
#endif
288227

289228
// Make sure the file watcher is ready before writing
290229
watcherTaskCompletionSource.Task.GetAwaiter().GetResult();
@@ -400,9 +339,7 @@ private void InternalWrite(MemoryStream input)
400339
/// <param name="name">A system wide unique name, the name will have a prefix appended</param>
401340
/// <param name="maxFileSize">The maximum amount of data that can be written to the file memory mapped file</param>
402341
/// <returns>A system wide MemoryMappedFile</returns>
403-
#if NET
404342
[SupportedOSPlatform("windows")]
405-
#endif
406343
public static MemoryMappedFile CreateOrOpenMemoryMappedFile(string name, long maxFileSize)
407344
{
408345
if (string.IsNullOrWhiteSpace(name))

0 commit comments

Comments
 (0)