Skip to content

Commit

Permalink
Port to .NET Core 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Latency committed Sep 20, 2017
1 parent 01b3ca8 commit af3fa01
Show file tree
Hide file tree
Showing 30 changed files with 709 additions and 44,385 deletions.
4 changes: 0 additions & 4 deletions BitFields Tests/packages.config

This file was deleted.

184 changes: 184 additions & 0 deletions BitFields-Core/BitField.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
// *****************************************************************************
// File: BitField.cs
// Solution: BitFields
// Date: 10/10/2015
// Author: Latency McLaughlin
// Copywrite: Bio-Hazard Industries - 1997-2015
// *****************************************************************************

using System;

namespace BitFields {
/// <inheritdoc />
/// <summary>
/// The BitField class exposes the following methods:
/// Initialization:
/// ------------------
/// BitField() // Constructor
/// BitField(ulong) // Copy Constructor
/// Mask // Property used to hold the bitmask value.
/// ClearField() // ClearField clears all contents of the Field.
/// FillField() // FillField fills all contents of the Field.
/// SetField() // Setting the specified flag and turning all other flags off.
/// Operations:
/// -----------------------
/// SetOn() // Setting the specified flag and leaving all other flags unchanged.
/// SetOff() // Unsetting the specified flag and leaving all other flags unchanged.
/// Toggle() // Toggling the specified flag and leaving all other bits unchanged.
/// IsSet() // IsSet checks if the specified flag is set/on in the Field.
/// IsEqual() // Compares the bitmask with another and tests if equal.
/// Conversion:
/// -----------------------
/// DecimalToFlag() // Convert a decimal value to a Flag FlagsAttribute value.
/// ToString.Decimal() // Return a string representation of the Field in decimal (base 10) notation.
/// ToString.Hex() // Return a string representation of the Field in hexidecimal (base 16) notation.
/// ToString.Binary() // Return a string representation of the Field in binary (base 2) notation.
/// </summary>
public class BitField : IFluentInterface {
/// <summary>
/// Polymorphic class object
/// </summary>
public new IToString ToString;

/// <summary>
/// Contructor
/// Add all initialization here
/// </summary>
public BitField() {
ClearField();
ToString = new ToStringSwitchBoard(this);
}

/// <summary>
/// Copy Contructor
/// </summary>
public BitField(ulong mask) {
Mask = mask;
}

/// <summary>
/// Public property SET and GET to access the Field
/// </summary>
public ulong Mask { get; set; }

/// <summary>
/// ClearField clears all contents of the Field
/// Set all bits to zero using the clear flag
/// </summary>
public void ClearField() {
SetField(0UL);
}

/// <summary>
/// FillField fills all contents of the Field
/// Set all bits to zero using the negation of clear
/// </summary>
public void FillField() {
SetField(~0UL);
}

/// <summary>
/// Setting the specified flag(s) and turning all other flags off.
/// - Bits that are set to 1 in the flag will be set to one in the Field.
/// - Bits that are set to 0 in the flag will be set to zero in the Field.
/// </summary>
/// <param name="mask">The mask to set in Field</param>
private void SetField(ulong mask) {
Mask = mask;
}

/// <summary>
/// Setting the specified flag(s) and leaving all other flags unchanged.
/// - Bits that are set to 1 in the flag will be set to one in the Field.
/// - Bits that are set to 0 in the flag will be unchanged in the Field.
/// </summary>
/// <example>
/// OR truth table
/// 0 | 0 = 0
/// 1 | 0 = 1
/// 0 | 1 = 1
/// 1 | 1 = 1
/// </example>
/// <param name="mask">The mask to set in Field</param>
public void SetOn(ulong mask) {
Mask |= mask;
}

/// <summary>
/// Unsetting the specified flag(s) and leaving all other flags unchanged.
/// - Bits that are set to 1 in the flag will be set to zero in the Field.
/// - Bits that are set to 0 in the flag will be unchanged in the Field.
/// </summary>
/// <example>
/// AND truth table
/// 0 & 0 = 0
/// 1 & 0 = 0
/// 0 & 1 = 0
/// 1 & 1 = 1
/// </example>
/// <param name="mask">The mask to unset in Field</param>
public void SetOff(ulong mask) {
Mask &= ~mask;
}

/// <summary>
/// Toggling the specified flag(s) and leaving all other bits unchanged.
/// - Bits that are set to 1 in the flag will be toggled in the Field.
/// - Bits that are set to 0 in the flag will be unchanged in the Field.
/// </summary>
/// <example>
/// XOR truth table
/// 0 ^ 0 = 0
/// 1 ^ 0 = 1
/// 0 ^ 1 = 1
/// 1 ^ 1 = 0
/// </example>
/// <param name="mask">The mask to toggle in Field</param>
public void Toggle(ulong mask) {
Mask ^= mask;
}

/// <summary>
/// Checks if any/all of the specified flags are set/on in the Field.
/// </summary>
/// <param name="mask"></param>
/// <param name="compareAll"></param>
/// <returns>
/// true if flag(s) is set in Field
/// false otherwise
/// </returns>
public bool IsSet(ulong mask, bool compareAll = false) {
return !compareAll ? (Mask & mask) != 0 : (Mask & mask) == mask;
}

/// <summary>
/// IsEqual checks if all the specified flags are the same as in the Field.
/// </summary>
/// <param name="mask">The mask to check</param>
/// <returns>
/// true if all flags identical in the Field
/// false otherwise
/// </returns>
public bool IsEqual(ulong mask) {
return Mask == mask;
}

/// <summary>
/// Convert a decimal value to a Flag FlagsAttribute value.
/// Method is thread safe
/// </summary>
/// <param name="dec">Valid input: dec between 0,64 </param>
/// <returns>The bitmask of the decimal value</returns>
public static ulong DecimalToFlag(decimal dec) {
ulong tMsk = 0;
try {
var shift = (byte) dec;
if (shift > 0 && shift <= 64)
tMsk = (ulong) 0x01 << (shift - 1);
} catch (OverflowException e) { //Byte cast operation
Console.WriteLine("Exception caught in ToStringBin: {0}", e);
}
return tMsk;
}
}
}
68 changes: 68 additions & 0 deletions BitFields-Core/BtFields-Core.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<RootNamespace>BitFields</RootNamespace>
<AssemblyName>BitFields</AssemblyName>
<Version>1.0.1</Version>
<Authors>Latency McLaughlin</Authors>
<Company>Bio-Hazard Industries</Company>
<Description>.NET Library For BitField &amp; Enum Extensions</Description>
<Copyright>Copyright © 1998-2017</Copyright>
<PackageLicenseUrl>http://www.gnu.org/licenses/gpl-3.0.en.html</PackageLicenseUrl>
<PackageProjectUrl>http://latency.github.io/BitFields/</PackageProjectUrl>
<RepositoryUrl>https://github.com/Latency/BitFields</RepositoryUrl>
<PackageIconUrl>http://bio-hazard.us/ico/kitty.ico</PackageIconUrl>
<RepositoryType>GIT</RepositoryType>
<PackageTags>BitField BitFields C# Enum Enums Library</PackageTags>
<PackageReleaseNotes>Port to .NET Core 2.0</PackageReleaseNotes>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>snKey.pfx</AssemblyOriginatorKeyFile>
<DelaySign>false</DelaySign>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>

<TargetFrameworkVersionFolder>netcoreapp2.0</TargetFrameworkVersionFolder>
<FrameworkBinPath>$(SolutionDir)NuGet\lib\$(TargetFrameworkVersionFolder)\</FrameworkBinPath>
<NeutralLanguage>en-US</NeutralLanguage>
<AssemblyVersion>1.0.1</AssemblyVersion>
<PackageId>BitFields-Core</PackageId>
<FileVersion>1.0.1</FileVersion>
<Product>BitFields</Product>
</PropertyGroup>

<!-- @@ -->
<Target Name="CreateBinDirectoryIfNotExists" Condition="!Exists('..\Nuget\bin')">
<Message Text="Creating NuGet bin directory -&gt; '..\NuGet\bin\'" Importance="high" />
<MakeDir Directories="..\NuGet\bin" />
</Target>
<!-- @@ -->
<Target Name="CreateLibDirectoryIfNotExists" Condition="!Exists('..\Nuget\lib')">
<Message Text="Creating NuGet lib directory -&gt; '..\NuGet\lib\'" Importance="high" />
<MakeDir Directories="..\NuGet\lib" />
</Target>
<!-- @@ -->
<Target Name="CreateFrameworkDirectoryIfNotExists" Condition="!Exists('$(FrameworkBinPath)')">
<Message Text="Creating output framework directory -&gt; '$(FrameworkBinPath)'" Importance="high" />
<MakeDir Directories="$(FrameworkBinPath)" />
</Target>


<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<CallTarget Targets="CreateBinDirectoryIfNotExists;CreateLibDirectoryIfNotExists;CreateFrameworkDirectoryIfNotExists" />
<Copy SourceFiles="$(OutputPath)$(TargetFileName)" DestinationFolder="$(FrameworkBinPath)" />
<Exec Command="..\NuGet\NuGet.exe pack &quot;..\NuGet\Package.nuspec&quot; -OutputDirectory &quot;..\NuGet\bin&quot; -NoPackageAnalysis -Version &quot;$(Version)&quot; -Build" />
</Target>


<ItemGroup>
<PackageReference Include="IFluentInterface" Version="2.0.3" />
</ItemGroup>


<ItemGroup>
<Compile Update="BitField.cs">
<SubType>Code</SubType>
</Compile>
</ItemGroup>

</Project>
89 changes: 89 additions & 0 deletions BitFields-Core/EnumExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// *****************************************************************************
// File: EnumExtensions.cs
// Solution: BitFields
// Date: 10/10/2015
// Author: Latency McLaughlin
// Copywrite: Bio-Hazard Industries - 1997-2015
// *****************************************************************************

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;

namespace BitFields {
public static class EnumExtensions {
/// <summary>
/// Gets all items for an enum value.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value">The value.</param>
public static IEnumerable<T> GetAllItems<T>(this Enum value) where T : struct, IComparable, IFormattable, IConvertible {
return from object item in Enum.GetValues(typeof (T))
select (T) item;
}

/// <summary>
/// Gets all items for an enum type.
/// </summary>
/// <typeparam name="T"></typeparam>
public static IEnumerable<T> GetAllItems<T>() where T : struct, IComparable, IFormattable, IConvertible {
return Enum.GetValues(typeof (T)).Cast<T>();
}

/// <summary>
/// Gets all combined items from an enum value.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value">The value.</param>
/// <returns>A collection of enums</returns>
public static IEnumerable<T> GetAllSelectedItems<T>(this Enum value) where T : struct, IComparable, IFormattable, IConvertible {
return GetAllSelectedItems<T>(Convert.ToUInt64(value));
}

// Compare All
public static IEnumerable<T> GetAllSelectedItems<T>(ulong mask) where T : struct, IComparable, IFormattable, IConvertible {
var type = typeof (T);
return from field in type.GetFields()
where !field.Name.Equals("value__")
select Convert.ToUInt64(field.GetRawConstantValue())
into value
where value != 0 && Contains(mask, value)
select (T) Enum.ToObject(type, value);
}

/// <summary>
/// Determines whether the enum value contains a specific value.
/// </summary>
/// <param name="mask">The mask</param>
/// <param name="value">The value</param>
/// <returns>
/// <c>true</c> if value contains the specified value; otherwise, <c>false</c>.
/// </returns>
/// <example>
/// <code>
/// EnumExample dummy = EnumExample.Combi;
/// if (dummy.Contains(EnumExample.ValueA))
/// Console.WriteLine("dummy contains EnumExample.ValueA");
/// </code>
/// </example>
public static bool Contains<T>(this Enum mask, T value) where T : struct, IComparable, IFormattable, IConvertible {
return Contains(Convert.ToUInt64(mask), value);
}

// Contains Any
public static bool Contains<T>(ulong mask, T value) where T : struct, IComparable, IFormattable, IConvertible {
return new BitField(mask).IsSet(Convert.ToUInt64(value), true);
}

/// <summary>
/// Gets the enums description attribute name.
/// </summary>
/// <param name="value"></param>
/// <returns>A string of the official spelling of the enum name.</returns>
public static string GetEnumDescription(this Enum value) {
var attributes = (DescriptionAttribute[]) value.GetType().GetField(value.ToString()).GetCustomAttributes(typeof (DescriptionAttribute), false);
return attributes.Length > 0 ? attributes[0].Description : value.ToString();
}
}
}
Loading

0 comments on commit af3fa01

Please sign in to comment.