Skip to content

Commit

Permalink
Support for .NET Standard 2.1, C# 8.0 and nullable reference types
Browse files Browse the repository at this point in the history
  • Loading branch information
bibigone committed Aug 14, 2020
1 parent 926751b commit dc1bb86
Show file tree
Hide file tree
Showing 46 changed files with 626 additions and 476 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
<Platforms>AnyCPU</Platforms>
<Description>Core .NET sample console application to measure speed of Body Tracking.</Description>
<AssemblyName>K4ABodyTrackingSpeed</AssemblyName>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public override bool NextFrame()
return false;
}

tracker.TryEnqueueCapture(capture, Timeout.Infinite);
tracker.TryEnqueueCapture(capture!, Timeout.Infinite);
}

return true;
Expand Down
13 changes: 7 additions & 6 deletions K4AdotNet.Samples.Core.BodyTrackingSpeed/ProcessingParameters.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;

namespace K4AdotNet.Samples.Core.BodyTrackingSpeed
Expand Down Expand Up @@ -26,7 +27,7 @@ public static bool IsValueLikeToMkvFilePath(string value)
return true;
}

public string MkvPath { get; private set; }
public string? MkvPath { get; private set; }
public bool CpuOnlyMode { get; private set; }
public ProcessingImplementation Implementation { get; private set; }
public TimeSpan? StartTime { get; private set; }
Expand All @@ -36,7 +37,7 @@ public bool IsTimeInStartEndInterval(TimeSpan frameTimestamp)
=> (!StartTime.HasValue || StartTime.Value <= frameTimestamp)
&& (!EndTime.HasValue || EndTime.Value >= frameTimestamp);

public bool TrySetMkvPath(string value, out string message)
public bool TrySetMkvPath(string value, [NotNullWhen(returnValue: false)] out string? message)
{
if (string.IsNullOrWhiteSpace(value))
{
Expand Down Expand Up @@ -70,7 +71,7 @@ public bool TrySetMkvPath(string value, out string message)
return true;
}

public bool TrySetCpuOnlyMode(string value, out string message)
public bool TrySetCpuOnlyMode(string value, [NotNullWhen(returnValue: false)] out string? message)
{
if (string.IsNullOrWhiteSpace(value))
{
Expand All @@ -96,7 +97,7 @@ public bool TrySetCpuOnlyMode(string value, out string message)
return false;
}

public bool TrySetImplementation(string value, out string message)
public bool TrySetImplementation(string value, [NotNullWhen(returnValue: false)] out string? message)
{
if (string.IsNullOrWhiteSpace(value))
{
Expand Down Expand Up @@ -126,7 +127,7 @@ public bool TrySetImplementation(string value, out string message)
return false;
}

public bool TrySetStartTime(string value, out string message)
public bool TrySetStartTime(string value, [NotNullWhen(returnValue: false)] out string? message)
{
if (string.IsNullOrWhiteSpace(value))
{
Expand Down Expand Up @@ -154,7 +155,7 @@ public bool TrySetStartTime(string value, out string message)
return true;
}

public bool TrySetEndTime(string value, out string message)
public bool TrySetEndTime(string value, [NotNullWhen(returnValue: false)] out string? message)
{
if (string.IsNullOrWhiteSpace(value))
{
Expand Down
19 changes: 8 additions & 11 deletions K4AdotNet.Samples.Core.BodyTrackingSpeed/Processor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ namespace K4AdotNet.Samples.Core.BodyTrackingSpeed
{
internal abstract class Processor : IDisposable
{
public static Processor Create(ProcessingParameters processingParameters)
public static Processor Create(ProcessingParameters processingParameters) => processingParameters.Implementation switch
{
switch (processingParameters.Implementation)
{
case ProcessingImplementation.SingleThread: return new SingleThreadProcessor(processingParameters);
case ProcessingImplementation.PopInBackground: return new PopInBackgroundProcessor(processingParameters);
case ProcessingImplementation.EnqueueInBackground: return new EnqueueInBackgroundProcessor(processingParameters);
default: throw new NotSupportedException();
}
}
ProcessingImplementation.SingleThread => new SingleThreadProcessor(processingParameters),
ProcessingImplementation.PopInBackground => new PopInBackgroundProcessor(processingParameters),
ProcessingImplementation.EnqueueInBackground => new EnqueueInBackgroundProcessor(processingParameters),
_ => throw new NotSupportedException(),
};

protected readonly ProcessingParameters processingParameters;
protected readonly Record.Playback playback;
Expand All @@ -24,7 +21,7 @@ public static Processor Create(ProcessingParameters processingParameters)
protected Processor(ProcessingParameters processingParameters)
{
this.processingParameters = processingParameters;
playback = new Record.Playback(processingParameters.MkvPath);
playback = new Record.Playback(processingParameters.MkvPath!);
playback.GetRecordConfiguration(out recordConfig);
RecordLength = playback.RecordLength;
playback.GetCalibration(out calibration);
Expand Down Expand Up @@ -61,7 +58,7 @@ private void Seek(TimeSpan value)
throw new ApplicationException("Cannot seek playback to " + value);
}

protected bool IsCaptureInInterval(Sensor.Capture capture)
protected bool IsCaptureInInterval(Sensor.Capture? capture)
{
if (capture == null)
return false;
Expand Down
9 changes: 5 additions & 4 deletions K4AdotNet.Samples.Core.BodyTrackingSpeed/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using K4AdotNet.Sensor;
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

namespace K4AdotNet.Samples.Core.BodyTrackingSpeed
{
Expand Down Expand Up @@ -51,11 +52,11 @@ private static void PrintProcessingStatus(Processor processor)
Console.Write($"processed: {processor.TotalFrameCount} with body: {processor.FrameWithBodyCount} in buffer: {processor.QueueSize}");
}

private delegate bool ParameterSetter(string value, out string message);
private delegate bool ParameterSetter(string value, [NotNullWhen(returnValue: false)] out string? message);

#region Asking parameters from STDIN

private static ProcessingParameters AskProcessingParameters()
private static ProcessingParameters? AskProcessingParameters()
{
Console.WriteLine("No command line arguments specified.");
Console.WriteLine("Please enter execution parameters:");
Expand Down Expand Up @@ -93,7 +94,7 @@ private static bool AskParameter(string prompt, ParameterSetter setter)

#region Parsing parameters from command-line arguments

private static ProcessingParameters ParseCommandLineArguments(string[] args)
private static ProcessingParameters? ParseCommandLineArguments(string[] args)
{
var parameters = new ProcessingParameters();

Expand Down Expand Up @@ -143,7 +144,7 @@ private static bool ParseArgument(string[] args, ref int argIndex, ParameterSett
var name = args[argIndex];
var value = argIndex + 1 < args.Length ? args[argIndex + 1] : null;

string message = null;
string? message = null;
if (value != null && setter(value, out message))
{
argIndex++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public override bool NextFrame()
return false;
}

tracker.TryEnqueueCapture(capture, Timeout.Infinite);
tracker.TryEnqueueCapture(capture!, Timeout.Infinite);

Pop(wait: false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
<AssemblyName>K4ARecorder</AssemblyName>
</PropertyGroup>

Expand Down
6 changes: 3 additions & 3 deletions K4AdotNet.Samples.Core.Recorder/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ internal sealed class Options
[Option('c', "color-mode", Required = false, Default = "1080p", HelpText =
"Set the color sensor mode.\n" +
"Available options: 3072p, 2160p, 1536p, 1440p, 1080p, 720p, 720p_NV12, 720p_YUY2, OFF")]
public string ColorMode { get; set; }
public string ColorMode { get; set; } = "1080p";

[Option('d', "depth-mode", Required = false, Default = "NFOV_UNBINNED", HelpText =
"Set the depth sensor mode.\n" +
"Available options: NFOV_2X2BINNED, NFOV_UNBINNED, WFOV_2X2BINNED, WFOV_UNBINNED, PASSIVE_IR, OFF")]
public string DepthMode { get; set; }
public string DepthMode { get; set; } = "NFOV_UNBINNED";

[Option("depth-delay", Required = false, Default = 0, HelpText =
"Set the time offset between color and depth frames in microseconds.\n" +
Expand All @@ -39,7 +39,7 @@ internal sealed class Options

[Value(0, Default = null, Required = false, MetaValue = "output.mkv", HelpText =
"Output file name. Default: 'yyyy-MM-dd H_mm_ss.mkv' file in 'My Videos' folder.")]
public string Output { get; set; }
public string? Output { get; set; }

public int GetDeviceIndex()
{
Expand Down
18 changes: 9 additions & 9 deletions K4AdotNet/BodyTracking/BodyFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void Dispose()

/// <summary>Raised on object disposing (only once).</summary>
/// <seealso cref="Dispose"/>
public event EventHandler Disposed;
public event EventHandler? Disposed;

/// <summary>Creates new reference to the same unmanaged body frame object.</summary>
/// <returns>New object that references exactly to the same underlying unmanaged object as original one. Not <see langword="null"/>.</returns>
Expand Down Expand Up @@ -85,7 +85,7 @@ public BodyFrame DuplicateReference()
/// use <see cref="Sensor.Capture.DuplicateReference"/> method.
/// </para></remarks>
/// <exception cref="ObjectDisposedException">This property cannot be called for disposed objects.</exception>
public Sensor.Capture Capture => children.Register(Sensor.Capture.Create(NativeApi.FrameGetCapture(handle.ValueNotDisposed)));
public Sensor.Capture Capture => children.Register(Sensor.Capture.Create(NativeApi.FrameGetCapture(handle.ValueNotDisposed)))!;

/// <summary>Non-a-body value on index body map.</summary>
/// <seealso cref="BodyIndexMap"/>
Expand All @@ -111,7 +111,7 @@ public BodyFrame DuplicateReference()
/// use <see cref="Sensor.Image.DuplicateReference"/> method.
/// </para></remarks>
/// <exception cref="ObjectDisposedException">This property cannot be called for disposed objects.</exception>
public Sensor.Image BodyIndexMap => children.Register(Sensor.Image.Create(NativeApi.FrameGetBodyIndexMap(handle.ValueNotDisposed)));
public Sensor.Image BodyIndexMap => children.Register(Sensor.Image.Create(NativeApi.FrameGetBodyIndexMap(handle.ValueNotDisposed)))!;

/// <summary>Gets the joint information for a particular person index.</summary>
/// <param name="bodyIndex">Zero-based index of a tracked body. Must me positive number. Must be less than <see cref="BodyCount"/>.</param>
Expand Down Expand Up @@ -140,22 +140,22 @@ public BodyId GetBodyId(int bodyIndex)
return NativeApi.FrameGetBodyId(handle.ValueNotDisposed, (uint)bodyIndex);
}

internal static BodyFrame Create(NativeHandles.BodyFrameHandle bodyFrameHandle)
internal static BodyFrame? Create(NativeHandles.BodyFrameHandle? bodyFrameHandle)
=> bodyFrameHandle != null && !bodyFrameHandle.IsInvalid ? new BodyFrame(bodyFrameHandle) : null;

#region Equatable

/// <summary>Two body frames are equal when they reference to one and the same unmanaged object.</summary>
/// <param name="bodyFrame">Another body frame to be compared with this one. Can be <see langword="null"/>.</param>
/// <returns><see langword="true"/> if both body frames reference to one and the same unmanaged object.</returns>
public bool Equals(BodyFrame bodyFrame)
public bool Equals(BodyFrame? bodyFrame)
=> !(bodyFrame is null) && bodyFrame.handle.Equals(handle);

/// <summary>Two body frames are equal when they reference to one and the same unmanaged object.</summary>
/// <param name="obj">Some object to be compared with this one. Can be <see langword="null"/>.</param>
/// <returns><see langword="true"/> if <paramref name="obj"/> is also <see cref="BodyFrame"/> and they both reference to one and the same unmanaged object.</returns>
public override bool Equals(object obj)
=> obj is BodyFrame && Equals((BodyFrame)obj);
public override bool Equals(object? obj)
=> obj is BodyFrame frame && Equals(frame);

/// <summary>Uses underlying handle as hash code.</summary>
/// <returns>Hash code. Consistent with overridden equality.</returns>
Expand All @@ -168,15 +168,15 @@ public override int GetHashCode()
/// <param name="right">Right part of operator. Can be <see langword="null"/>.</param>
/// <returns><see langword="true"/> if <paramref name="left"/> equals to <paramref name="right"/>.</returns>
/// <seealso cref="Equals(BodyFrame)"/>
public static bool operator ==(BodyFrame left, BodyFrame right)
public static bool operator ==(BodyFrame? left, BodyFrame? right)
=> (left is null && right is null) || (!(left is null) && left.Equals(right));

/// <summary>To be consistent with <see cref="Equals(BodyFrame)"/>.</summary>
/// <param name="left">Left part of operator. Can be <see langword="null"/>.</param>
/// <param name="right">Right part of operator. Can be <see langword="null"/>.</param>
/// <returns><see langword="true"/> if <paramref name="left"/> is not equal to <paramref name="right"/>.</returns>
/// <seealso cref="Equals(BodyFrame)"/>
public static bool operator !=(BodyFrame left, BodyFrame right)
public static bool operator !=(BodyFrame? left, BodyFrame? right)
=> !(left == right);

/// <summary>Convenient (for debugging needs, first of all) string representation of object as an address of unmanaged object in memory.</summary>
Expand Down
18 changes: 8 additions & 10 deletions K4AdotNet/BodyTracking/BodyId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,14 @@ public bool Equals(int other)
/// <summary>Overloads <see cref="Object.Equals(object)"/> to be consistent with <see cref="Equals(BodyId)"/>.</summary>
/// <param name="obj">Object to be compared with this instance.</param>
/// <returns><see langword="true"/> if <paramref name="obj"/> can be cast to <see cref="BodyId"/> and result is equal to this one.</returns>
public override bool Equals(object obj)
{
if (obj is null)
return false;
if (obj is BodyId)
return Equals((BodyId)obj);
if (obj is IConvertible)
return Equals(Convert.ToInt32(obj));
return false;
}
public override bool Equals(object? obj)
=> obj switch
{
null => false,
BodyId _ => Equals((BodyId)obj),
IConvertible _ => Equals(Convert.ToInt32(obj)),
_ => false
};

/// <summary>To be consistent with <see cref="Equals(BodyId)"/>.</summary>
/// <param name="left">Left part of operator.</param>
Expand Down
Loading

0 comments on commit dc1bb86

Please sign in to comment.