Skip to content

Commit

Permalink
Added implementations of the Millistream data types.
Browse files Browse the repository at this point in the history
  • Loading branch information
mgnsm committed May 24, 2020
1 parent 152670b commit b936017
Show file tree
Hide file tree
Showing 43 changed files with 7,086 additions and 3 deletions.
20 changes: 17 additions & 3 deletions Millistream.NET.sln
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27703.2000
# Visual Studio Version 16
VisualStudioVersion = 16.0.29409.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Source", "Source", "{660A05E5-718C-45D0-8705-14C9B5DF6F98}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{A0152BDC-0D9F-4AE2-BF3E-CE24F85ECAF8}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Millistream.Streaming", "Source\Millistream.Streaming\Millistream.Streaming.csproj", "{F5CC00D3-449B-4280-8A01-B649AAE9D2C8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Millistream.Streaming.UnitTests", "Tests\Millistream.Streaming.UnitTests\Millistream.Streaming.UnitTests.csproj", "{F86497BC-8BBD-4BD2-A050-FD0882DE69FF}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Millistream.Streaming.UnitTests", "Tests\Millistream.Streaming.UnitTests\Millistream.Streaming.UnitTests.csproj", "{F86497BC-8BBD-4BD2-A050-FD0882DE69FF}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Millistream.Streaming.IntegrationTests", "Tests\Millistream.Streaming.IntegrationTests\Millistream.Streaming.IntegrationTests.csproj", "{DB6A274F-DB2C-4581-88F9-4A798A9D3DF7}"
EndProject
Expand All @@ -29,6 +29,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp", "Samples\Conso
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "v1.0.0", "v1.0.0", "{250E61F3-D935-4729-8F60-9E7C2E6A6F40}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Millistream.Streaming.DataTypes", "Source\Millistream.Streaming.DataTypes\Millistream.Streaming.DataTypes.csproj", "{F4B36982-5C27-4988-8D96-99858868071C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Millistream.Streaming.DataTypes.UnitTests", "Tests\Millistream.Streaming.DataTypes.UnitTests\Millistream.Streaming.DataTypes.UnitTests.csproj", "{ACD99697-5CB5-41A8-8014-98C963002547}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -55,6 +59,14 @@ Global
{78D0CB1A-31D5-44AD-8D93-76DA8136604B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{78D0CB1A-31D5-44AD-8D93-76DA8136604B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{78D0CB1A-31D5-44AD-8D93-76DA8136604B}.Release|Any CPU.Build.0 = Release|Any CPU
{F4B36982-5C27-4988-8D96-99858868071C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F4B36982-5C27-4988-8D96-99858868071C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F4B36982-5C27-4988-8D96-99858868071C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F4B36982-5C27-4988-8D96-99858868071C}.Release|Any CPU.Build.0 = Release|Any CPU
{ACD99697-5CB5-41A8-8014-98C963002547}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ACD99697-5CB5-41A8-8014-98C963002547}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ACD99697-5CB5-41A8-8014-98C963002547}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ACD99697-5CB5-41A8-8014-98C963002547}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -66,6 +78,8 @@ Global
{04B84E2C-DAFD-4E8B-84A8-0BF3CC7512EE} = {250E61F3-D935-4729-8F60-9E7C2E6A6F40}
{78D0CB1A-31D5-44AD-8D93-76DA8136604B} = {C04FDBC8-EB8C-4D9F-8F3A-B6A2DA42566D}
{250E61F3-D935-4729-8F60-9E7C2E6A6F40} = {C04FDBC8-EB8C-4D9F-8F3A-B6A2DA42566D}
{F4B36982-5C27-4988-8D96-99858868071C} = {660A05E5-718C-45D0-8705-14C9B5DF6F98}
{ACD99697-5CB5-41A8-8014-98C963002547} = {A0152BDC-0D9F-4AE2-BF3E-CE24F85ECAF8}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {13B18705-419F-4BCB-B456-5A1E4198605D}
Expand Down
300 changes: 300 additions & 0 deletions Source/Millistream.Streaming.DataTypes/BitField.cs

Large diffs are not rendered by default.

164 changes: 164 additions & 0 deletions Source/Millistream.Streaming.DataTypes/Bool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
using System;
using System.Text;

namespace Millistream.Streaming.DataTypes
{
/// <summary>
/// Represents a bool (true or false) value.
/// </summary>
public readonly struct Bool : IComparable, IComparable<Bool>, IEquatable<Bool>
{
#region Fields
private readonly bool _bool;
#endregion

#region Constructors
/// <summary>
/// Creates an instance of a <see cref="Bool"/>.
/// </summary>
/// <param name="bool">A value indicating whether the <see cref="Bool"/> is true or false.</param>
public Bool(bool @bool) => _bool = @bool;
#endregion

#region Methods
/// <summary>
/// Converts a memory span that contains the UTF-8 character '1' or '0' to its <see cref="Bool"/> equivalent.
/// </summary>
/// <param name="value">The memory span that contains the UTF-8 character to parse.</param>
/// <returns>A <see cref="Bool"/> object that is equivalent to the value contained in <paramref name="value"/>.</returns>
/// <exception cref="ArgumentException"></exception>
public static Bool Parse(ReadOnlySpan<char> value) =>
TryParse(value, out Bool @bool) ? @bool : throw new ArgumentException(Constants.ParseArgumentExceptionMessage, nameof(value));

/// <summary>
/// Converts a memory span that contains the byte of the UTF-8 character '1' or '0' to its <see cref="Bool"/> equivalent.
/// </summary>
/// <param name="value">The memory span that contains the byte of UTF-8 character to parse.</param>
/// <returns>A <see cref="Bool"/> object that is equivalent to the value contained in <paramref name="value"/>.</returns>
/// <exception cref="ArgumentException"></exception>
public static Bool Parse(ReadOnlySpan<byte> value) =>
TryParse(value, out Bool @bool) ? @bool : throw new ArgumentException(Constants.ParseArgumentExceptionMessage, nameof(value));

/// <summary>
/// Tries to convert a memory span that should contain the UTF-8 character '1' or '0' to its <see cref="Bool"/> equivalent and returns a value that indicates whether the conversion succeeded.
/// </summary>
/// <param name="value">The memory span that contains the UTF-8 character(s) to parse.</param>
/// <param name="bool">The converted <see cref="Bool"/> value or default depending on whether the conversion succeeded or failed.</param>
/// <returns>true if value was converted successfully; otherwise, false.</returns>
public static bool TryParse(ReadOnlySpan<char> value, out Bool @bool)
{
if (value.Length == 1 && int.TryParse(value, out int i))
{
switch (i)
{
case 0:
@bool = new Bool(false);
return true;
case 1:
@bool = new Bool(true);
return true;
}
}
@bool = default;
return false;
}

/// <summary>
/// Tries to convert a memory span that should contain the byte of the UTF-8 character '1' or '0' to its <see cref="Bool"/> equivalent and returns a value that indicates whether the conversion succeeded.
/// </summary>
/// <param name="value">The memory span that contains the byte(s) of the UTF-8 character(s) to parse.</param>
/// <param name="bool">The converted <see cref="Bool"/> value or default depending on whether the conversion succeeded or failed.</param>
/// <returns>true if value was converted successfully; otherwise, false.</returns>
public static bool TryParse(ReadOnlySpan<byte> value, out Bool @bool)
{
if (value.Length == 1)
{
Span<char> chars = stackalloc char[1];
Encoding.UTF8.GetChars(value, chars);
return TryParse(chars, out @bool);
}
@bool = default;
return false;
}

/// <summary>
/// Compares this instance to a specified <see cref="Bool"/> object and returns an integer that indicates their relationship to one another.
/// </summary>
/// <param name="obj">A <see cref="Bool"/> object to compare to this instance.</param>
/// <returns>A signed integer that indicates the relative values of this instance and <paramref name="obj"/>.</returns>
public readonly int CompareTo(object obj)
{
if (obj == null)
return 1;

if (!(obj is Bool @bool))
throw new ArgumentException($"Argument must be of type {nameof(Bool)}.", nameof(obj));

return CompareTo(@bool);
}

/// <summary>
/// Compares this instance to a specified <see cref="Bool"/> object and returns an integer that indicates their relationship to one another.
/// </summary>
/// <param name="other">A <see cref="Bool"/> object to compare to this instance.</param>
/// <returns>A signed integer that indicates the relative values of this instance and <paramref name="other"/>.</returns>
public readonly int CompareTo(Bool other) => _bool.CompareTo(other._bool);

/// <summary>
/// Returns a value indicating whether this instance is equal to a specified <see cref="Bool"/> object.
/// </summary>
/// <param name="other">A <see cref="Bool"/> value to compare to this instance.</param>
/// <returns>true if <paramref name="other"/> has the same value as this instance; otherwise, false.</returns>
public readonly bool Equals(Bool other) => _bool == other._bool;

/// <summary>
/// Returns the hash code for this instance.
/// </summary>
/// <returns>A hash code for the current <see cref="Bool"/>.</returns>
public readonly override int GetHashCode() => _bool.GetHashCode();

/// <summary>
/// Returns a value indicating whether this instance is equal to a specified object.
/// </summary>
/// <param name="obj">An object to compare to this instance.</param>
/// <returns>true if <paramref name="obj"/> is a <see cref="Bool"/> and has the same value as this instance; otherwise, false.</returns>
public readonly override bool Equals(object obj) => obj is Bool @bool && Equals(@bool);

/// <summary>
/// Converts the value of this instance to its equivalent string representation (either "True" or "False").
/// </summary>
/// <returns><see cref="bool.TrueString"/> if the value of this instance is true, or <see cref="bool.FalseString"/> if the value of this instance is false.</returns>
public readonly override string ToString() => _bool.ToString();
#endregion

#region Operators
/// <summary>
/// Indicates whether two <see cref="Bool"/> instances are equal.
/// </summary>
/// <param name="left">The first instance to compare.</param>
/// <param name="right">The second instance to compare.</param>
/// <returns>true if the values of <paramref name="left"/> and <paramref name="right"/> are equal; otherwise, false.</returns>
public static bool operator ==(Bool left, Bool right) => left.Equals(right);

/// <summary>
/// Indicates whether two <see cref="Bool"/> instances are not equal.
/// </summary>
/// <param name="left">The first instance to compare.</param>
/// <param name="right">The second instance to compare.</param>
/// <returns>true if the values of <paramref name="left"/> and <paramref name="right"/> are not equal; otherwise, false.</returns>
public static bool operator !=(Bool left, Bool right) => !left.Equals(right);

/// <summary>
/// Converts a <see cref="Bool"/> to a <see cref="bool"/>.
/// </summary>
/// <param name="bool">A <see cref="Bool"/>.</param>
public static implicit operator bool(Bool @bool) => @bool._bool;

/// <summary>
/// Converts a <see cref="bool"/> to a <see cref="Bool"/>.
/// </summary>
/// <param name="bool">A <see cref="bool"/>.</param>
public static explicit operator Bool(bool @bool) => new Bool(@bool);
#endregion
}
}
7 changes: 7 additions & 0 deletions Source/Millistream.Streaming.DataTypes/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Millistream.Streaming.DataTypes
{
internal static class Constants
{
internal const string ParseArgumentExceptionMessage = "Value was not in a correct format.";
}
}
Loading

0 comments on commit b936017

Please sign in to comment.