Skip to content

Commit

Permalink
Use primary constructors where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
charliefoxtwo committed Jun 8, 2024
1 parent 2be67bf commit 19a7c8b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 44 deletions.
12 changes: 3 additions & 9 deletions DcsBiosCommunicator/DataParsers/DataParser.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
namespace DcsBios.Communicator.DataParsers;

public abstract class DataParser<T>
public abstract class DataParser<T>(in ushort address, in string biosCode)
{
public ushort Address { get; }
public ushort Address { get; } = address;

public string BiosCode { get; }
public string BiosCode { get; } = biosCode;

public T CurrentValue { get; protected set; } = default!;

protected DataParser(in ushort address, in string biosCode)
{
Address = address;
BiosCode = biosCode;
}

public abstract void AddData(in ushort address, in ushort data);
}
13 changes: 4 additions & 9 deletions DcsBiosCommunicator/DataParsers/IntegerParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@

namespace DcsBios.Communicator.DataParsers;

public class IntegerParser : DataParser<int>
public class IntegerParser(in ushort mask, [Range(0, 15)] in byte shift, in string biosCode)
: DataParser<int>(default, biosCode)
{
private readonly ushort _mask;
private readonly byte _shift;

public IntegerParser(in ushort mask, [Range(0, 15)] in byte shift, in string biosCode) : base(default, biosCode)
{
_mask = mask;
_shift = shift;
}
private readonly ushort _mask = mask;
private readonly byte _shift = shift;

public override void AddData(in ushort address, in ushort data)
{
Expand Down
24 changes: 7 additions & 17 deletions DcsBiosCommunicator/DataParsers/StringParser.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
using System;
using System.ComponentModel.DataAnnotations;

namespace DcsBios.Communicator.DataParsers;

public sealed class StringParser : DataParser<string>
public sealed class StringParser(in ushort address, [Range(1, 64)] in byte length, in string biosCode)
: DataParser<string>(address, biosCode)
{
public bool DataReady => _bufferFilledBits == _bufferSizeBits;

public byte Length { get; }
private readonly byte[] _buffer;
private readonly long _bufferSizeBits;
public byte Length { get; } = length;
private readonly byte[] _buffer = new byte[length];
private readonly long _bufferSizeBits = (2L << (length - 1)) - 1;
private long _bufferFilledBits;

private readonly ushort _baseAddress;

public StringParser(in ushort address, in byte length, in string biosCode) : base(address, biosCode)
{
if (length is < 1 or > 64)
throw new ArgumentOutOfRangeException(nameof(length), length,
$"Length of {biosCode} should be between 1 and 64, inclusive.");

Length = length;
_buffer = new byte[length];
_bufferSizeBits = (2L << (length - 1)) - 1;
_baseAddress = address;
}
private readonly ushort _baseAddress = address;

private bool SetCharacter(int index, byte b)
{
Expand Down
12 changes: 3 additions & 9 deletions DcsBiosCommunicator/IntegerHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@

namespace DcsBios.Communicator;

internal class IntegerHandler
internal class IntegerHandler(in ushort address, IEnumerable<IntegerParser> maskShifts)
{
public int Address { get; }
public ushort Address { get; } = address;

public IList<IntegerParser> MaskShifts { get; }

public IntegerHandler(in int address, IEnumerable<IntegerParser> maskShifts)
{
Address = address;
MaskShifts = maskShifts.ToList();
}
public IList<IntegerParser> MaskShifts { get; } = maskShifts.ToList();
}

0 comments on commit 19a7c8b

Please sign in to comment.