Skip to content

Commit

Permalink
Merge branch 'master' into bunch-of-qol-fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Julius R Friedman <[email protected]>
  • Loading branch information
juliusfriedman authored Nov 12, 2023
2 parents b59bb8f + c10b6c3 commit 74a8413
Show file tree
Hide file tree
Showing 33 changed files with 265 additions and 250 deletions.
9 changes: 4 additions & 5 deletions Common/Classes/EventArgsEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,17 @@ public static class IEventExtensions
{
public static bool HasSource(IEvent @event)
{
return Object.ReferenceEquals(@event, null).Equals(false) &&
Object.ReferenceEquals(@event.Source, null).Equals(false);
return @event is not null && @event.Source is not null;
}

public static bool IsEnabled(IEvent @event)
{
return Object.ReferenceEquals(@event, null).Equals(false) && @event.Enabled;
return @event is not null && @event.Enabled;
}

public static bool IsNull(IEvent @event)
{
return object.ReferenceEquals(@event, null);
return @event is null;
}
}

Expand All @@ -110,7 +109,7 @@ internal class Eventbase : IEvent
{
public Eventbase(Delegate @event, bool enabled = true)
{
if (object.ReferenceEquals(@event, null)) throw new ArgumentNullException("@event");
if (@event is null) throw new ArgumentNullException("@event");

Source = @event;

Expand Down
4 changes: 2 additions & 2 deletions Common/Classes/MemorySegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ public bool Equals(MemorySegment other)
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public static bool operator ==(MemorySegment a, MemorySegment b)
{
return object.ReferenceEquals(b, null) ? object.ReferenceEquals(a, null) : a.Equals(b);
return b is null ? a is null : a.Equals(b);
}

[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
Expand Down Expand Up @@ -622,7 +622,7 @@ public static int Find(byte[] source, int start, int count, params MemorySegment
if (found >= 0)
{
//If not already set then set it
if(object.ReferenceEquals(first, null)) first = segment;
first ??= segment;

//Subtract from needed and if 0 remains break
if ((needed -= found) == 0) break;
Expand Down
4 changes: 3 additions & 1 deletion Common/Classes/PacketBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,9 @@ bool IPacket.IsComplete
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
get
{
if (object.ReferenceEquals(Completer, null)) return StaticCompleteFrom(null, null).Equals(int.MinValue);
if (Completer is null)
return StaticCompleteFrom(null, null).Equals(int.MinValue);

return Completer(null, null).Equals(int.MinValue);
}
}
Expand Down
9 changes: 7 additions & 2 deletions Common/Classes/TaggedException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ public TaggedException(T tag, string message, System.Exception innerException, p
/// <param name="context"><see cref="System.Runtime.Serialization.StreamingContext"/></param>
public override void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
{
if (object.ReferenceEquals(null, info)) throw new System.ArgumentNullException(nameof(info));
if (info is null) throw new System.ArgumentNullException(nameof(info));

//Add the Tag to the info
info.AddValue(TagPropertyString, Tag);
//Get all other exception data from base.
Expand Down Expand Up @@ -232,7 +233,11 @@ public static class TaggedExceptionExtensions
/// </summary>
/// <typeparam name="T">The type related to the exception.</typeparam>
/// <param name="exception">The <see cref="System.Exception"/> which occured.</param>
public static void Raise<T>(this TaggedException<T> exception) { if (exception is not null) throw exception; }
public static void Raise<T>(this TaggedException<T> exception)
{
if (exception is not null)
throw exception;
}

/// <summary>
/// Tries to <see cref="Raise"/> the given <see cref="TaggedException"/>
Expand Down
2 changes: 1 addition & 1 deletion Common/Classes/Transport/TransportClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public virtual void Disconnect() { }
public TransportClient(System.Net.NetworkInformation.NetworkInterface networkInterface, bool shouldDispose = true)
: base(shouldDispose)
{
if (object.ReferenceEquals(networkInterface, null)) throw new System.ArgumentNullException();
if (networkInterface is null) throw new System.ArgumentNullException(nameof(networkInterface));

NetworkInterface = networkInterface;
}
Expand Down
11 changes: 4 additions & 7 deletions Common/Collections/Generic/ConcurrentThesaurus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ The above copyright notice and this permission notice shall be included in all c

#region Using Statements

using Media.Common.Extensions.Generic.Dictionary;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
Expand Down Expand Up @@ -86,8 +85,6 @@ public class ConcurrentThesaurus<TKey, TValue> : ILookup<TKey, TValue>, ICollect

readonly ConcurrentDictionary<TKey, IList<TValue>> Dictionary;

System.Collections.ICollection Collection { get { return Dictionary; } }

public int Count { get { return Dictionary.Count; } }

public IEnumerable<TKey> Keys { get { return Dictionary.Keys; } }
Expand Down Expand Up @@ -328,7 +325,7 @@ void ICollection<TKey>.CopyTo(TKey[] array, int arrayIndex)

int ICollection<TKey>.Count
{
get { return Collection.Count; }
get { return Dictionary.Count; }
}

bool ICollection<TKey>.IsReadOnly
Expand All @@ -352,17 +349,17 @@ IEnumerator<TKey> IEnumerable<TKey>.GetEnumerator()

public ConcurrentThesaurus(IDictionary<TKey, IList<TValue>> values, IEqualityComparer<TKey> equalityComparer)
{
Dictionary = new ConcurrentDictionary<TKey, IList<TValue>>(values, equalityComparer);
Dictionary = new (values, equalityComparer);
}

public ConcurrentThesaurus(int capacity, IEqualityComparer<TKey> equalityComparer)
{
Dictionary = new ConcurrentDictionary<TKey, IList<TValue>>(Environment.ProcessorCount, capacity, equalityComparer);
Dictionary = new (Environment.ProcessorCount, capacity, equalityComparer);
}

public ConcurrentThesaurus(IEqualityComparer<TKey> equalityComparer)
{
Dictionary = new ConcurrentDictionary<TKey, IList<TValue>>(equalityComparer);
Dictionary = new (equalityComparer);
}

public ConcurrentThesaurus(IDictionary<TKey, IList<TValue>> values) : this(values, EqualityComparer<TKey>.Default) { }
Expand Down
34 changes: 11 additions & 23 deletions Common/Extensions/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ public static class EnumerableExtensions
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public static bool SequenceEquals(this System.Collections.IEnumerable left, System.Collections.IEnumerable right)
{
if (object.ReferenceEquals(left, null)) return object.ReferenceEquals(right, null);
if (left is null) return right is null;

System.Collections.IEnumerator one, two;

System.IDisposable weird = null, strnage = null;

try
Expand All @@ -71,38 +70,27 @@ public static bool SequenceEquals(this System.Collections.IEnumerable left, Syst
}
finally
{
if (object.ReferenceEquals(strnage, null).Equals(false)) strnage.Dispose();

if (object.ReferenceEquals(weird, null).Equals(false)) weird.Dispose();
strnage?.Dispose();
weird?.Dispose();
}
}


[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public static bool SequenceEquals<T>(this System.Collections.Generic.IEnumerable<T> left, System.Collections.Generic.IEnumerable<T> right)
{
if (object.ReferenceEquals(left, null)) return object.ReferenceEquals(right, null);
if (left is null) return right is null;

try
{
using (var weird = left.GetEnumerator())
{
using (var strnage = right.GetEnumerator())
{
while (weird.MoveNext())
{
if (strnage.MoveNext().Equals(false) ||
weird.Current.Equals(strnage.Current).Equals(false)) return false;
}
using var weird = left.GetEnumerator();
using var strnage = right.GetEnumerator();

return true;
}
}
}
catch
while (weird.MoveNext())
{
throw;
if (strnage.MoveNext().Equals(false) ||
weird.Current.Equals(strnage.Current).Equals(false)) return false;
}

return true;
}
}
}
2 changes: 1 addition & 1 deletion Common/Extensions/ExceptionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public class ArgumentNullOrDisposedException : System.ArgumentNullException
public bool HasDisposed
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
get { return object.ReferenceEquals(null, Disposed).Equals(false); }
get { return Disposed is not null; }
}

#if DEBUG
Expand Down
8 changes: 4 additions & 4 deletions Common/Extensions/NetworkInterfaceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,15 @@ public static System.Net.NetworkInformation.NetworkInterface GetNetworkInterface

public static System.Net.NetworkInformation.NetworkInterface GetNetworkInterface(System.Net.IPEndPoint localEndPoint)
{
if (object.ReferenceEquals(localEndPoint, null)) throw new System.ArgumentNullException("localEndPoint");
if (localEndPoint is null) throw new System.ArgumentNullException(nameof(localEndPoint));

return GetNetworkInterface(localEndPoint.Address);
}

public static System.Net.NetworkInformation.NetworkInterface GetNetworkInterface(System.Net.Sockets.Socket socket)
{
if (object.ReferenceEquals(socket, null)) throw new System.ArgumentNullException("socket");
else if (socket.Handle == System.IntPtr.Zero) return null;
if (socket is null) throw new System.ArgumentNullException(nameof(socket));
else if (socket.Handle == nint.Zero) return null;

System.Net.IPEndPoint localEndPoint = socket.IsBound.Equals(false) ? Common.Extensions.IPEndPoint.IPEndPointExtensions.Any : (System.Net.IPEndPoint)socket.LocalEndPoint;

Expand All @@ -148,7 +148,7 @@ public static System.Net.NetworkInformation.NetworkInterface GetNetworkInterface
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public static double GetSpeedInMBytesPerSecond(this System.Net.NetworkInformation.NetworkInterface networkInterface)
{
if (object.ReferenceEquals(networkInterface, null)) return 0;
if (networkInterface is null) return 0;

long speed = networkInterface.Speed;

Expand Down
4 changes: 2 additions & 2 deletions Common/Extensions/SocketExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public static int FindOpenPort(System.Net.Sockets.ProtocolType type, int start =
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.Synchronized)]
public static int ProbeForOpenPort(System.Net.Sockets.ProtocolType type, int start = 30000, bool even = true, System.Net.IPAddress localIp = null)
{
if (object.ReferenceEquals(localIp, null)) localIp = GetFirstUnicastIPAddress(System.Net.Sockets.AddressFamily.InterNetwork); // System.Net.IPAddress.Any should give unused ports across all IP's?
localIp ??= GetFirstUnicastIPAddress(System.Net.Sockets.AddressFamily.InterNetwork); // System.Net.IPAddress.Any should give unused ports across all IP's?

System.Net.Sockets.Socket working = null;

Expand Down Expand Up @@ -283,7 +283,7 @@ public static System.Net.IPAddress GetFirstUnicastIPAddress(System.Net.Sockets.A
System.Net.IPAddress result = Common.Extensions.NetworkInterface.NetworkInterfaceExtensions.GetFirstUnicastIPAddress(foundInterface, addressFamily);

//If the result is not null and the result is not System.Net.IPAddress.None
if (false.Equals(object.ReferenceEquals(result, null)) && false.Equals(Equals(result, System.Net.IPAddress.None)))
if (result is not null && false.Equals(Equals(result, System.Net.IPAddress.None)))
{
networkInterface = foundInterface;

Expand Down
22 changes: 11 additions & 11 deletions Common/Extensions/StreamExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1052,19 +1052,16 @@ protected internal override void Dispose(bool disposing)
/// <param name="delimit">The byte to read for</param>
/// <param name="includeDelimit">An optional value indicating if delimit should be present in the result</param>
/// <returns>The bytes read from the reader</returns>
public static byte[] ReadDelimitedValue(this System.IO.Stream stream, byte delimit = Common.ASCII.LineFeed, bool includeDelimit = false)
public static Common.MemorySegment ReadDelimitedValue(this System.IO.Stream stream, byte delimit = Common.ASCII.LineFeed, bool includeDelimit = false)
{
//The result of reading from the stream
byte[] result = null;

//Declare a value which will end up in a register on the stack
int register = -1;
int register = -1, count = 0;

//Indicate when to terminate reading.
bool terminate = false;

//Use a MemoryStream as to not lock the reader
using (var buffer = new System.IO.MemoryStream())
using (var buffer = new System.IO.MemoryStream(128))
{
//While data can be read from the stream
while (false == terminate)
Expand All @@ -1080,13 +1077,16 @@ public static byte[] ReadDelimitedValue(this System.IO.Stream stream, byte delim

//Write the value read from the reader to the MemoryStream
buffer.WriteByte((byte)register);

//Store count
++count;
}
//If terminating then return the array contained in the MemoryStream.
result = buffer.ToArray();
}
var result = buffer.ToArray();

//Return the bytes read from the stream
return result;
//Return the bytes read from the stream
return new Common.MemorySegment(result, 0, count);
}
}

/// <summary>
Expand All @@ -1095,7 +1095,7 @@ public static byte[] ReadDelimitedValue(this System.IO.Stream stream, byte delim
/// </summary>
/// <param name="stream"></param>
/// <param name="result"></param>
public static void ReadLineFeed(this System.IO.Stream stream, out byte[] result)
public static void ReadLineFeed(this System.IO.Stream stream, out Common.MemorySegment result)
{
//The length of the array allocated is known and should also be returned...
result = Common.Extensions.Stream.StreamExtensions.ReadDelimitedValue(stream, Common.ASCII.LineFeed, true);
Expand Down
21 changes: 10 additions & 11 deletions Common/Extensions/SymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ static Func<T, object, object> MagicMethod<T>(MethodInfo method) where T : class
static SymbolExtensions()
{
//Ensure not already ran.
if (false.Equals(object.ReferenceEquals(TypedConstantExpressionType, null))
| false.Equals(object.ReferenceEquals(InstanceMethodCallExpressionNType, null))
| false.Equals(object.ReferenceEquals(TypedConstantExpressionValueProperty, null))) return;
if (false.Equals(TypedConstantExpressionType is null)
| false.Equals(InstanceMethodCallExpressionNType is null)
| false.Equals(TypedConstantExpressionValueProperty is null)) return;

//For example
//Expression<Action> ConstantExpression = () => Common.Binary.Nihil.Equals(null);
Expand Down Expand Up @@ -112,9 +112,9 @@ static SymbolExtensions()
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public static MethodCallExpression CreateMethodCallExpression(MethodInfo method)
{
if (object.ReferenceEquals(method,null))
if (method is null)
{
throw new ArgumentNullException("method");
throw new ArgumentNullException(nameof(method));
}

//Return the MethodCallExpression (parameters are included)
Expand Down Expand Up @@ -215,9 +215,9 @@ public static MemberInfo GetMemberInfo(LambdaExpression expression)
{
MemberExpression outermostExpression = ((expression.Body as MethodCallExpression).Object as MemberExpression);

if (object.ReferenceEquals(outermostExpression, null)) throw new ArgumentException("Invalid Expression. Should be a MemberExpression");

return outermostExpression.Member;
return outermostExpression is null
? throw new ArgumentException("Invalid Expression. Should be a MemberExpression", nameof(expression))
: outermostExpression.Member;
}

/// <summary>
Expand Down Expand Up @@ -261,9 +261,8 @@ public static MethodInfo GetMethodInfo<T, TResult>(Expression<Func<T, TResult>>
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public static MethodInfo GetMethodInfo(LambdaExpression expression)
{
MethodCallExpression outermostExpression = expression.Body as MethodCallExpression;

if (object.ReferenceEquals(outermostExpression, null)) throw new ArgumentException("Invalid Expression. Expression should consist of a Method call only.");
if (expression.Body is not MethodCallExpression outermostExpression)
throw new ArgumentException("Invalid Expression. Expression should consist of a Method call only.", nameof(expression));

return outermostExpression.Method;
}
Expand Down
11 changes: 5 additions & 6 deletions Common/Extensions/ThreadExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static class ThreadExtensions
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public static bool IsRunning(System.Threading.Thread thread)
{
return false.Equals(object.ReferenceEquals(thread, null)) &&
return thread is not null &&
(thread.ThreadState & (System.Threading.ThreadState.Stopped | System.Threading.ThreadState.Unstarted)) == System.Threading.ThreadState.Running;
}

Expand Down Expand Up @@ -89,7 +89,7 @@ public static void AbortAndFree(ref System.Threading.Thread thread, System.Threa
public static void AbortAndFree(ref System.Threading.Thread thread, System.TimeSpan timeout, System.Threading.ThreadState state = System.Threading.ThreadState.Stopped)
{
//If the worker IsAlive and has doesn't have the requested state.
if (false.Equals(object.ReferenceEquals(thread, null)) &&
if (thread is not null &&
false.Equals(thread.ThreadState.HasFlag(state)))
{
//Attempt to join if not already, todo check flags are compatible in all implementations.
Expand All @@ -110,16 +110,15 @@ public static bool TryAbortAndFree(ref System.Threading.Thread thread, System.Th
try { AbortAndFree(ref thread, state, timeout); }
catch { return false; }

return object.ReferenceEquals(thread, null);
return thread is null;
}

public static bool TryAbortAndFree(ref System.Threading.Thread thread, System.TimeSpan timeout, System.Threading.ThreadState state = System.Threading.ThreadState.Stopped)
{
try { AbortAndFree(ref thread, timeout, state); }
catch { return false; }

return object.ReferenceEquals(thread, null);
}

return thread is null;
}
}
}
Loading

0 comments on commit 74a8413

Please sign in to comment.