From bea7080a5cbfa4e8d751440bbcac7a288a0190fb Mon Sep 17 00:00:00 2001 From: Marc Hernandez Date: Tue, 30 Apr 2024 12:19:56 -0700 Subject: [PATCH] Immutability and strip the / off the path x) Better path substring x) Add some interfaces for immutability --- imm/FSM.cs | 20 ++++++++++++-------- imm/Imm.cs | 35 ++++++++++++++++++++++++++++------- imm/iu.cs | 6 +++--- logging/Log.cs | 17 +++++++++++------ 4 files changed, 54 insertions(+), 24 deletions(-) diff --git a/imm/FSM.cs b/imm/FSM.cs index 4fdfabd..d392630 100644 --- a/imm/FSM.cs +++ b/imm/FSM.cs @@ -10,15 +10,17 @@ namespace imm; -public record class Context : imm.Recorded +public record class Context : imm.Recorded, Imm { - + Meta Imm.Meta => base.Meta; } -public record class State( CTX Context ) : imm.Recorded +public record class State( CTX Context ) : imm.Recorded, Imm where TSUB : State where CTX : Context { + Meta Imm.Meta => base.Meta; + virtual public (CTX, TSUB) onEnter(CTX ctx, State oldState) { return (ctx, (TSUB)this); @@ -32,11 +34,13 @@ public record class State( CTX Context ) : imm.Recorded -public record class FSM : imm.Recorded +public record class FSM : imm.Recorded, Imm where TSUB : FSM where ST : State where CTX : Context { + Meta Imm.Meta => base.Meta; + public CTX Context { get; init; } public ST State { get; init; } @@ -59,7 +63,7 @@ public record class FSM : imm.Recorded var origState = State; var (newCtx, oldState) = State.onExit(Context, newState); - + var (newCTX, storeState) = newState.onEnter(newCtx, oldState); var newFSM = this.Process( (v) => (this as TSUB) with @@ -72,12 +76,12 @@ public record class FSM : imm.Recorded } /* - public TSUB ( Func fn, string reason, + public TSUB ( Func fn, string reason, [CallerMemberName] string member = "", [CallerFilePath] string file = "", [CallerLineNumber] int line = 0, [CallerArgumentExpression("fn")] - string expression = default + string expression = default ) { var newState = fn( State ); @@ -88,7 +92,7 @@ public record class FSM : imm.Recorded { Context = Context, State = newState, - }, $"Processing: {newState.GetType().Name} for {reason}", + }, $"Processing: {newState.GetType().Name} for {reason}", member, file, line, expression ); return (TSUB)newFSM; diff --git a/imm/Imm.cs b/imm/Imm.cs index 631d9dd..d0dc98a 100644 --- a/imm/Imm.cs +++ b/imm/Imm.cs @@ -49,13 +49,32 @@ static public class Util } } +public interface Meta +{ + public uint Version => 0; + public string Reason => ""; + public string Expression => ""; + public string MemberName => ""; + public string FilePath => ""; + public int LineNumber => -1; + public DateTime CreatedAt => DateTime.MinValue; + public DateTime TouchedAt => DateTime.MinValue; + +} + + +public interface Imm +{ + public Meta Meta { get; } +} + //[lib.Ser( Types = lib.Types.None )] -public record class Versioned +public record class Versioned : Imm where T : Versioned { public delegate void ChangeDelegate( T ZZOld, T next ); - public record class MetaData + public record class MetaData : Meta { public uint Version { get; internal set; } = 0; public string Reason { get; internal set; } = ""; @@ -78,6 +97,8 @@ public record class Versioned [DebuggerBrowsable(DebuggerBrowsableState.Never)] public MetaData Meta => MetaStorage; + Meta Imm.Meta => MetaStorage; + [lib.Dont] [DebuggerBrowsable(DebuggerBrowsableState.Never)] public ChangeDelegate OnChange = (x, y) => {}; @@ -137,7 +158,7 @@ public record class Recorded : Versioned return Process( t => t, reason, dbgName, dbgPath, lineNumber ); } - virtual public T Process( T next, + virtual public T Process( T next, string reason = "", [CallerMemberName] string dbgName = "", [CallerFilePath] string dbgPath = "", @@ -149,7 +170,7 @@ public record class Recorded : Versioned return Process( ( old ) => next, reason, dbgName, dbgPath, lineNumber, dbgExp ); } - virtual public T Process( Func fn, + virtual public T Process( Func fn, string reason = "", [CallerMemberName] string dbgName = "", [CallerFilePath] string dbgPath = "", @@ -207,11 +228,11 @@ public record class Timed : Recorded public TimeSpan Since => Meta.TouchedAt - Meta.Old?.Meta.TouchedAt ?? TimeSpan.MaxValue; - override public T Record( + override public T Record( string reason = "", [CallerMemberName] string dbgName = "", [CallerFilePath] string dbgPath = "", - [CallerLineNumber] int lineNumber = 0 + [CallerLineNumber] int lineNumber = 0 ) { return Process( t => t, reason, dbgName, dbgPath, lineNumber ); @@ -230,7 +251,7 @@ public record class Timed : Recorded } - override public T Process( Func fn, + override public T Process( Func fn, string reason = "", [CallerMemberName] string dbgName = "", [CallerFilePath] string dbgPath = "", diff --git a/imm/iu.cs b/imm/iu.cs index 0d8b0d0..24e8809 100644 --- a/imm/iu.cs +++ b/imm/iu.cs @@ -21,7 +21,7 @@ static public class iu [CallerLineNumber] int lineNumber = 0, [CallerArgumentExpression("fn")] string expression = default ) - where T : Recorded + where T : Recorded, Imm { obj = obj.Process( fn, reason, memberName, filePath, lineNumber, expression ); return obj; @@ -34,7 +34,7 @@ static public class iu [CallerLineNumber] int lineNumber = 0, [CallerArgumentExpression("fn")] string expression = default ) - where T : Recorded + where T : Recorded, Imm { obj = obj.Process( fn, reason, memberName, filePath, lineNumber, expression ); return obj; @@ -48,7 +48,7 @@ static public class iu [CallerLineNumber] int lineNumber = 0, [CallerArgumentExpression("fn")] string expression = default ) - where T : Versioned + where T : Versioned, Imm { obj = obj.Process( fn, reason ); return obj; diff --git a/logging/Log.cs b/logging/Log.cs index d835e2d..fb70475 100644 --- a/logging/Log.cs +++ b/logging/Log.cs @@ -164,7 +164,7 @@ static public class log /* static public Log log { - get + get { return s_log; } @@ -176,11 +176,16 @@ static public class log { var cwd = Directory.GetCurrentDirectory(); - var rel = fullPath.Substring( cwd.Length ); + var rel = fullPath.Substring( cwd.Length + 1 ); return rel; } + static public string thisFilePath( [CallerFilePath] string path = "" ) + { + return relativePath( path ); + } + // Forwards. static public void fatal( string msg, string cat = "", object obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" ) { @@ -202,7 +207,7 @@ static public class log logBase(msg, LogType.High, path, line, member, cat, obj); } - static public void info( string msg, string cat = "", object obj = null, + static public void info( string msg, string cat = "", object obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" ) { logBase( msg, LogType.Info, path, line, member, cat, obj ); @@ -222,7 +227,7 @@ static public class log static public void logBase_old( string msg, LogType type = LogType.Debug, string path = "", int line = -1, string member = "", string cat = "unk", object obj = null ) { - // @@@@@ TODO Get rid of this lock. + // @@@@@ TODO Get rid of this lock. var evt = new LogEvent( type, msg, path, line, member, cat, obj ); lock( s_lock ) @@ -274,7 +279,7 @@ static public class log } } - //This might seem a little odd, but the intent is that usually you wont need to set notExpectedValue. + //This might seem a little odd, but the intent is that usually you wont need to set notExpectedValue. static public void expected( T value, string falseString, string trueString = "", T notExpectedValue = default( T ) ) { @@ -442,7 +447,7 @@ static public class log { try { - // _SHOULDNT_ need this since we lock at the top. + // _SHOULDNT_ need this since we lock at the top. //lock( this ) {