Commit ef9c1d32 authored by Ben Lambell's avatar Ben Lambell

Added t4 templates for creating, effectively, new double structs

parent c65138d0
using System.Collections.Generic;
namespace System.CodeDom.Compiler
{
public class CompilerError
{
public string ErrorText { get; set; }
public bool IsWarning { get; set; }
}
public class CompilerErrorCollection : List<CompilerError>
{
}
}
/*
* Copyright (c) 2017 Ben Lambell.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
namespace Recurse.Common.Maths.Specialized
{
public interface IMeasurementUnit
{
double ToDouble();
}
}
/*
* Copyright (c) 2017 Ben Lambell.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
using System;
using System;
namespace Recurse.Common.Maths.Specialized
{
[Serializable]
public struct Measurement<T> : IComparable<Measurement<T>>, IEquatable<Measurement<T>>
where T : IMeasurementUnit
public partial struct Measurement : IComparable<Measurement>, IEquatable<Measurement>
{
private double _value;
public bool IsValid => !double.IsNaN(_value);
private double Value { get; }
public Measurement(T unit)
{
_value = unit.ToDouble();
}
public static Measurement Unit => new Measurement(1);
private Measurement(double value)
{
_value = value;
}
public static Measurement<T> operator +(Measurement<T> a, Measurement<T> b)
{
return new Measurement<T>(a._value + b._value);
if (double.IsNaN(value) || double.IsInfinity(value))
throw new ArgumentException("Value cannot be NaN or Infinity", nameof(value));
Value = value;
}
public static Measurement<T> operator -(Measurement<T> a, Measurement<T> b)
public static Measurement operator +(Measurement a, Measurement b)
{
return new Measurement<T>(a._value - b._value);
return new Measurement(a.Value + b.Value);
}
public static Measurement<T> operator *(double operand, Measurement<T> a)
public static Measurement operator -(Measurement a, Measurement b)
{
return new Measurement<T>(a._value * operand);
return new Measurement(a.Value - b.Value);
}
public static Measurement<T> operator *(Measurement<T> a, double operand)
public static Measurement operator *(double operand, Measurement a)
{
return new Measurement<T>(a._value * operand);
return new Measurement(a.Value * operand);
}
public static Measurement<T> operator /(Measurement<T> a, double operand)
public static Measurement operator *(Measurement a, double operand)
{
return new Measurement<T>(a._value / operand);
return new Measurement(a.Value * operand);
}
public static Measurement<T> operator /(double operand, Measurement<T> a)
public static Measurement operator /(Measurement a, double operand)
{
return new Measurement<T>(operand / a._value);
return new Measurement(a.Value / operand);
}
public static double operator /(Measurement<T> a, Measurement<T> b)
public static double operator /(Measurement a, Measurement b)
{
return a._value / b._value;
return a.Value / b.Value;
}
public static double operator %(Measurement<T> a, Measurement<T> b)
public static double operator %(Measurement a, Measurement b)
{
return a._value % b._value;
return a.Value % b.Value;
}
public static bool operator <(Measurement<T> a, Measurement<T> b)
public static bool operator <(Measurement a, Measurement b)
{
return a._value < b._value;
return a.Value < b.Value;
}
public static bool operator >(Measurement<T> a, Measurement<T> b)
public static bool operator >(Measurement a, Measurement b)
{
return a._value > b._value;
return a.Value > b.Value;
}
public static bool operator <=(Measurement<T> a, Measurement<T> b)
public static bool operator <=(Measurement a, Measurement b)
{
return a._value < b._value;
return a.Value <= b.Value;
}
public static bool operator >=(Measurement<T> a, Measurement<T> b)
public static bool operator >=(Measurement a, Measurement b)
{
return a._value > b._value;
return a.Value >= b.Value;
}
public static bool operator ==(Measurement<T> a, Measurement<T> b)
public static bool operator ==(Measurement a, Measurement b)
{
return a._value == b._value;
return a.Value == b.Value;
}
public static bool operator !=(Measurement<T> a, Measurement<T> b)
public static bool operator !=(Measurement a, Measurement b)
{
return a._value != b._value;
return a.Value != b.Value;
}
public int CompareTo(Measurement<T> other)
public int CompareTo(Measurement other)
{
return _value.CompareTo(other._value);
return Value.CompareTo(other.Value);
}
public bool Equals(Measurement<T> other)
public bool Equals(Measurement other)
{
return _value == other._value;
return Value == other.Value;
}
public override bool Equals(object obj)
{
return obj is Measurement<T> ? Equals((Measurement<T>)obj) : base.Equals(obj);
return obj is Measurement ? Equals((Measurement)obj) : base.Equals(obj);
}
public override int GetHashCode()
{
return _value.GetHashCode();
return Value.GetHashCode();
}
public override string ToString()
{
return _value.ToString();
return Value.ToString();
}
}
}
}
\ No newline at end of file
<#@ template language="C#" #>
<#@ output extension=".cs" #>
<#
string Namespace = "Recurse.Common.Maths.Specialized";
string Name = "Measurement";
#>
<#@ include file="MeasurementTemplate.tt" #>
\ No newline at end of file
This diff is collapsed.
namespace Recurse.Common.Maths.Specialized
{
public partial class MeasurementTemplate
{
public string Namespace { get; }
public string Name { get; }
public MeasurementTemplate(string @namespace, string name)
{
Namespace = @namespace;
Name = name;
}
}
}
<#@ template language="C#" #>
using System;
namespace <#= Namespace #>
{
[Serializable]
public partial struct <#= Name #> : IComparable<<#= Name #>>, IEquatable<<#= Name #>>
{
private double Value { get; }
public static <#= Name #> Unit => new <#= Name #>(1);
private <#= Name #>(double value)
{
if (double.IsNaN(value) || double.IsInfinity(value))
throw new ArgumentException("Value cannot be NaN or Infinity", nameof(value));
Value = value;
}
public static <#= Name #> operator +(<#= Name #> a, <#= Name #> b)
{
return new <#= Name #>(a.Value + b.Value);
}
public static <#= Name #> operator -(<#= Name #> a, <#= Name #> b)
{
return new <#= Name #>(a.Value - b.Value);
}
public static <#= Name #> operator *(double operand, <#= Name #> a)
{
return new <#= Name #>(a.Value * operand);
}
public static <#= Name #> operator *(<#= Name #> a, double operand)
{
return new <#= Name #>(a.Value * operand);
}
public static <#= Name #> operator /(<#= Name #> a, double operand)
{
return new <#= Name #>(a.Value / operand);
}
public static double operator /(<#= Name #> a, <#= Name #> b)
{
return a.Value / b.Value;
}
public static double operator %(<#= Name #> a, <#= Name #> b)
{
return a.Value % b.Value;
}
public static bool operator <(<#= Name #> a, <#= Name #> b)
{
return a.Value < b.Value;
}
public static bool operator >(<#= Name #> a, <#= Name #> b)
{
return a.Value > b.Value;
}
public static bool operator <=(<#= Name #> a, <#= Name #> b)
{
return a.Value <= b.Value;
}
public static bool operator >=(<#= Name #> a, <#= Name #> b)
{
return a.Value >= b.Value;
}
public static bool operator ==(<#= Name #> a, <#= Name #> b)
{
return a.Value == b.Value;
}
public static bool operator !=(<#= Name #> a, <#= Name #> b)
{
return a.Value != b.Value;
}
public int CompareTo(<#= Name #> other)
{
return Value.CompareTo(other.Value);
}
public bool Equals(<#= Name #> other)
{
return Value == other.Value;
}
public override bool Equals(object obj)
{
return obj is <#= Name #> ? Equals((<#= Name #>)obj) : base.Equals(obj);
}
public override int GetHashCode()
{
return Value.GetHashCode();
}
public override string ToString()
{
return Value.ToString();
}
}
}
\ No newline at end of file
using System;
namespace Recurse.Common.Maths.Specialized
{
[Serializable]
public partial struct Offset : IComparable<Offset>, IEquatable<Offset>
{
private Measurement Distance { get; }
private Offset(Measurement distance)
{
Distance = distance;
}
public static Offset operator +(Offset time, Measurement duration)
{
return new Offset(time.Distance + duration);
}
public static Offset operator -(Offset time, Measurement duration)
{
return new Offset(time.Distance - duration);
}
public static Measurement operator -(Offset a, Offset b)
{
return a.Distance - b.Distance;
}
public static bool operator <(Offset a, Offset b)
{
return a.Distance < b.Distance;
}
public static bool operator >(Offset a, Offset b)
{
return a.Distance > b.Distance;
}
public static bool operator <=(Offset a, Offset b)
{
return a.Distance <= b.Distance;
}
public static bool operator >=(Offset a, Offset b)
{
return a.Distance >= b.Distance;
}
public static bool operator ==(Offset a, Offset b)
{
return a.Distance == b.Distance;
}
public static bool operator !=(Offset a, Offset b)
{
return a.Distance != b.Distance;
}
public int CompareTo(Offset other)
{
return Distance.CompareTo(other.Distance);
}
public bool Equals(Offset other)
{
return Distance.Equals(other.Distance);
}
public override bool Equals(object obj)
{
return obj is Offset ? Equals((Offset)obj) : base.Equals(obj);
}
public override int GetHashCode()
{
return Distance.GetHashCode();
}
public override string ToString()
{
return Distance.ToString();
}
}
}
\ No newline at end of file
<#@ template language="C#" #>
<#@ output extension=".cs" #>
<#
string Namespace = "Recurse.Common.Maths.Specialized";
string Name = "Offset";
string Measurement = "Measurement";
#>
<#@ include file="OffsetTemplate.tt" #>
\ No newline at end of file
This diff is collapsed.
namespace Recurse.Common.Maths.Specialized
{
public partial class OffsetTemplate
{
public string Namespace { get; }
public string Name { get; }
public string Measurement { get; }
public OffsetTemplate(string @namespace, string name, string measurement)
{
Namespace = @namespace;
Name = name;
Measurement = measurement;
}
}
}
<#@ template language="C#" #>
using System;
namespace <#= Namespace #>
{
[Serializable]
public partial struct <#= Name #> : IComparable<<#= Name #>>, IEquatable<<#= Name #>>
{
private <#= Measurement #> Distance { get; }
private <#= Name #>(<#= Measurement #> distance)
{
Distance = distance;
}
public static <#= Name #> operator +(<#= Name #> time, <#= Measurement #> duration)
{
return new <#= Name #>(time.Distance + duration);
}
public static <#= Name #> operator -(<#= Name #> time, <#= Measurement #> duration)
{
return new <#= Name #>(time.Distance - duration);
}
public static <#= Measurement #> operator -(<#= Name #> a, <#= Name #> b)
{
return a.Distance - b.Distance;
}
public static bool operator <(<#= Name #> a, <#= Name #> b)
{
return a.Distance < b.Distance;
}
public static bool operator >(<#= Name #> a, <#= Name #> b)
{
return a.Distance > b.Distance;
}
public static bool operator <=(<#= Name #> a, <#= Name #> b)
{
return a.Distance <= b.Distance;
}
public static bool operator >=(<#= Name #> a, <#= Name #> b)
{
return a.Distance >= b.Distance;
}
public static bool operator ==(<#= Name #> a, <#= Name #> b)
{
return a.Distance == b.Distance;
}
public static bool operator !=(<#= Name #> a, <#= Name #> b)
{
return a.Distance != b.Distance;
}
public int CompareTo(<#= Name #> other)
{
return Distance.CompareTo(other.Distance);
}
public bool Equals(<#= Name #> other)
{
return Distance.Equals(other.Distance);
}
public override bool Equals(object obj)
{
return obj is <#= Name #> ? Equals((<#= Name #>)obj) : base.Equals(obj);
}
public override int GetHashCode()
{
return Distance.GetHashCode();
}
public override string ToString()
{
return Distance.ToString();
}
}
}
\ No newline at end of file
......@@ -10,6 +10,8 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>6</LangVersion>
<AssemblyName>Recurse.Common</AssemblyName>
<SignAssembly>false</SignAssembly>
</PropertyGroup>
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
......@@ -22,5 +24,51 @@
<PackageReference Include="System.Reflection.TypeExtensions" Version="4.5.1" />
<PackageReference Include="System.Runtime.Serialization.Formatters" Version="4.3.0" />
</ItemGroup>
<ItemGroup>
<None Update="Maths\Specialized\Offset.tt">
<LastGenOutput>Offset.cs</LastGenOutput>
<Generator>TextTemplatingFileGenerator</Generator>
</None>
<None Update="Maths\Specialized\Measurement.tt">
<LastGenOutput>Measurement.cs</LastGenOutput>
<Generator>TextTemplatingFileGenerator</Generator>
</None>
<None Update="Maths\Specialized\OffsetTemplate.tt">
<LastGenOutput>OffsetTemplate.cs</LastGenOutput>
<Generator>TextTemplatingFilePreprocessor</Generator>
</None>
<None Update="Maths\Specialized\MeasurementTemplate.tt">
<Generator>TextTemplatingFilePreprocessor</Generator>
<LastGenOutput>MeasurementTemplate.cs</LastGenOutput>
</None>
</ItemGroup>
<ItemGroup>
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
</ItemGroup>
<ItemGroup>
<Compile Update="Maths\Specialized\Measurement.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Measurement.tt</DependentUpon>
</Compile>
<Compile Update="Maths\Specialized\MeasurementTemplate.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>MeasurementTemplate.tt</DependentUpon>
</Compile>
<Compile Update="Maths\Specialized\Offset.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Offset.tt</DependentUpon>
</Compile>
<Compile Update="Maths\Specialized\OffsetTemplate.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>OffsetTemplate.tt</DependentUpon>
</Compile>
</ItemGroup>
</Project>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment