Various imm stuff

This commit is contained in:
Marc Hernandez 2023-12-02 14:30:52 -08:00
parent c8b29e0c0c
commit d6a9cb42cf
2 changed files with 30 additions and 27 deletions

View File

@ -5,21 +5,21 @@ using System.Linq;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
// A spot for immutable helpers // A spot for immutable helpers
namespace imm namespace imm
{ {
public record class Versioned<T> public record class Versioned<T>
where T: Versioned<T> where T : Versioned<T>
{ {
public uint Version { get; protected set; } = 0; public uint Version { get; protected set; } = 0;
public string Reason { get; protected set; } = ""; public string Reason { get; protected set; } = "";
public T Process(Func<T, T> fn, string reason = "") public T Process( Func<T, T> fn, string reason = "" )
{ {
var newT = fn((T)this); var newT = fn( ( T )this );
return newT with return newT with
{ {
@ -38,34 +38,34 @@ namespace imm
public string FilePath { get; protected set; } = ""; public string FilePath { get; protected set; } = "";
public int LineNumber { get; protected set; } = -1; public int LineNumber { get; protected set; } = -1;
public T Record(string reason = "", public T Record( string reason = "",
[CallerMemberName] string memberName = "", [CallerMemberName] string memberName = "",
[CallerFilePath] string filePath = "", [CallerFilePath] string filePath = "",
[CallerLineNumber] int lineNumber = 0) [CallerLineNumber] int lineNumber = 0 )
{ {
var orig = (T)this; var orig = ( T )this;
var newT = base.Process((old) => old with var newT = base.Process( ( old ) => old with
{ {
Old = orig, Old = orig,
MemberName = memberName, MemberName = memberName,
FilePath = filePath, FilePath = filePath,
LineNumber = lineNumber, LineNumber = lineNumber,
}, reason); }, reason );
return newT; return newT;
} }
public T Process(Func<T, T> fn, string reason = "", public T Process( Func<T, T> fn, string reason = "",
[CallerMemberName] string memberName = "", [CallerMemberName] string memberName = "",
[CallerFilePath] string filePath = "", [CallerFilePath] string filePath = "",
[CallerLineNumber] int lineNumber = 0, [CallerLineNumber] int lineNumber = 0,
[CallerArgumentExpression("fn")] [CallerArgumentExpression("fn")]
string expression = default) string expression = default )
{ {
var orig = (T)this; var orig = ( T )this;
return (T)this with return ( T )this with
{ {
//Do the Versioned code here //Do the Versioned code here
Version = orig.Version + 1, Version = orig.Version + 1,
@ -87,16 +87,16 @@ namespace imm
public readonly DateTime CreatedAt = DateTime.Now; public readonly DateTime CreatedAt = DateTime.Now;
public DateTime TouchedAt { get; set; } = DateTime.Now; public DateTime TouchedAt { get; set; } = DateTime.Now;
public T Process(Func<T, T> fn, string reason = "", public T Process( Func<T, T> fn, string reason = "",
[CallerMemberName] string memberName = "", [CallerMemberName] string memberName = "",
[CallerFilePath] string filePath = "", [CallerFilePath] string filePath = "",
[CallerLineNumber] int lineNumber = 0, [CallerLineNumber] int lineNumber = 0,
[CallerArgumentExpression("fn")] [CallerArgumentExpression("fn")]
string expression = default) string expression = default )
{ {
var orig = (T)this; var orig = ( T )this;
return (T)this with return ( T )this with
{ {
//Versioned //Versioned
Version = orig.Version + 1, Version = orig.Version + 1,

View File

@ -8,7 +8,8 @@ namespace net;
public class Context{ public class Context
{
} }
@ -16,15 +17,16 @@ public class State<T, CTX>
where T : State<T, CTX> where T : State<T, CTX>
where CTX : Context where CTX : Context
{ {
virtual public void onEnter(CTX ctx, State<T, CTX> oldState) virtual public void onEnter( CTX ctx, State<T, CTX> oldState )
{ {
} }
virtual public void onExit(CTX ctx, State<T, CTX> newState) virtual public void onExit( CTX ctx, State<T, CTX> newState )
{ {
} }
} }
/*
public class PotentialState<T, CTX> : State<T, CTX> public class PotentialState<T, CTX> : State<T, CTX>
where T : State<T, CTX> where T : State<T, CTX>
where CTX : Context where CTX : Context
@ -37,6 +39,7 @@ public class PotentialState<T, CTX> : State<T, CTX>
{ {
} }
} }
*/
public class FSM<T, CTX, ST> public class FSM<T, CTX, ST>
@ -47,13 +50,13 @@ public class FSM<T, CTX, ST>
public CTX Context { get; private set; } public CTX Context { get; private set; }
public ST State { get; private set; } public ST State { get; private set; }
public FSM(CTX context, ST state) public FSM( CTX context, ST state )
{ {
Context = context; Context = context;
State = state; State = state;
} }
public void Transition(ST newState) public void Transition( ST newState )
{ {
} }