-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce factory class to create measures.
- Loading branch information
Showing
8 changed files
with
129 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using Quantities.Dimensions; | ||
using Quantities.Units; | ||
|
||
namespace Quantities.Creation; | ||
|
||
public readonly struct Scalar<TDim> | ||
where TDim : IUnit, IDimension | ||
{ | ||
private readonly Factory factory; | ||
internal Factory Factory => this.factory; | ||
internal Scalar(Factory factory) => this.factory = factory; | ||
public Product<TDim, TRight> Dot<TRight>(in Scalar<TRight> rightTerm) | ||
where TRight : IUnit, IDimension => new(rightTerm.factory.Inject(this.factory.Product)); | ||
public Quotient<TDim, TDenominator> Per<TDenominator>(in Scalar<TDenominator> denominator) | ||
where TDenominator : IUnit, IDimension => new(denominator.factory.Inject(this.factory.Quotient)); | ||
internal Quantity Create(in Double value) => new(in value, this.factory.Create()); | ||
internal Quantity Transform(in Quantity other) => other.Project(this.factory.Create()); | ||
} | ||
|
||
public readonly ref struct Alias<TUnit, TLinear> | ||
where TUnit : IAlias<TLinear>, IUnit, IDimension | ||
where TLinear : IDimension | ||
{ | ||
private readonly Measure measure; | ||
internal Alias(Measure measure) => this.measure = measure; | ||
internal Quantity Create(in Double value) => new(in value, in this.measure); | ||
internal Quantity Transform(in Quantity other) => other.Project(in this.measure); | ||
} | ||
|
||
public readonly struct Product<TLeft, TRight> | ||
where TLeft : IUnit, IDimension | ||
where TRight : IUnit, IDimension | ||
{ | ||
private readonly Factory factory; | ||
internal Product(Factory factory) => this.factory = factory; | ||
internal Quantity Create(in Double value) => new(in value, this.factory.Create()); | ||
internal Quantity Transform(in Quantity other) => other.Project(this.factory.Create()); | ||
} | ||
|
||
public readonly ref struct Quotient<TN, TD> | ||
where TN : IUnit, IDimension | ||
where TD : IUnit, IDimension | ||
{ | ||
private readonly Factory factory; | ||
internal Quotient(Factory factory) => this.factory = factory; | ||
internal Quantity Create(in Double value) => new(in value, this.factory.Create()); | ||
internal Quantity Transform(in Quantity other) => other.Project(this.factory.Create()); | ||
} | ||
|
||
public readonly ref struct Square<TDim> | ||
where TDim : IUnit, IDimension | ||
{ | ||
private readonly Factory factory; | ||
internal Square(Factory factory) => this.factory = factory; | ||
internal Quantity Create(in Double value) => new(in value, this.factory.Square()); | ||
internal Quantity Transform(in Quantity other) => other.Project(this.factory.Square()); | ||
} | ||
|
||
public readonly ref struct Cubic<TDim> | ||
where TDim : IUnit, IDimension | ||
{ | ||
private readonly Factory factory; | ||
internal Cubic(Factory factory) => this.factory = factory; | ||
internal Quantity Create(in Double value) => new(in value, this.factory.Cubic()); | ||
internal Quantity Transform(in Quantity other) => other.Project(this.factory.Cubic()); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
namespace Quantities.Creation; | ||
|
||
internal abstract class Factory : IInjector | ||
{ | ||
public IInject<Factory> Product { get; } | ||
public IInject<Factory> Quotient { get; } | ||
private Factory(IInject<Factory> product, IInject<Factory> quotient) => (Product, Quotient) = (product, quotient); | ||
public abstract Measure Create(); | ||
public abstract Measure Square(); | ||
public abstract Measure Cubic(); | ||
public abstract TResult Inject<TResult>(IInject<TResult> inject); | ||
public static Factory Of<TMeasure>() where TMeasure : IMeasure => AllocationFree<Impl<TMeasure>>.Item; | ||
|
||
private sealed class Impl<TMeasure> : Factory | ||
where TMeasure : IMeasure | ||
{ | ||
private static readonly IInject<Factory> product = new ProductInject<TMeasure>(); | ||
private static readonly IInject<Factory> quotient = new QuotientInject<TMeasure>(); | ||
public Impl() : base(product, quotient) { } | ||
public override Measure Create() => Measure.Of<TMeasure>(); | ||
public override Measure Square() => Measure.Of<Measures.Power<Measures.Square, TMeasure>>(); | ||
public override Measure Cubic() => Measure.Of<Measures.Power<Measures.Cubic, TMeasure>>(); | ||
public override TResult Inject<TResult>(IInject<TResult> inject) => inject.Inject<TMeasure>(); | ||
} | ||
|
||
private sealed class ProductInject<TLeftTerm> : IInject<Factory> | ||
where TLeftTerm : IMeasure | ||
{ | ||
public Factory Inject<TMeasure>() where TMeasure : IMeasure => Of<Measures.Product<TLeftTerm, TMeasure>>(); | ||
} | ||
|
||
private sealed class QuotientInject<TLeftTerm> : IInject<Factory> | ||
where TLeftTerm : IMeasure | ||
{ | ||
public Factory Inject<TMeasure>() where TMeasure : IMeasure => Of<Measures.Quotient<TLeftTerm, TMeasure>>(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters