Immutability and strip the / off the path

x) Better path substring
x) Add some interfaces for immutability
This commit is contained in:
Marc Hernandez 2024-04-30 12:19:56 -07:00
parent 8879e98229
commit bea7080a5c
4 changed files with 54 additions and 24 deletions

View File

@ -10,15 +10,17 @@ namespace imm;
public record class Context : imm.Recorded<Context>
public record class Context : imm.Recorded<Context>, Imm
{
Meta Imm.Meta => base.Meta;
}
public record class State<TSUB, CTX>( CTX Context ) : imm.Recorded<TSUB>
public record class State<TSUB, CTX>( CTX Context ) : imm.Recorded<TSUB>, Imm
where TSUB : State<TSUB, CTX>
where CTX : Context
{
Meta Imm.Meta => base.Meta;
virtual public (CTX, TSUB) onEnter(CTX ctx, State<TSUB, CTX> oldState)
{
return (ctx, (TSUB)this);
@ -32,11 +34,13 @@ public record class State<TSUB, CTX>( CTX Context ) : imm.Recorded<TSUB>
public record class FSM<TSUB, ST, CTX> : imm.Recorded<TSUB>
public record class FSM<TSUB, ST, CTX> : imm.Recorded<TSUB>, Imm
where TSUB : FSM<TSUB, ST, CTX>
where ST : State<ST, CTX>
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<TSUB, ST, CTX> : imm.Recorded<TSUB>
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<TSUB, ST, CTX> : imm.Recorded<TSUB>
}
/*
public TSUB ( Func<ST, ST> fn, string reason,
public TSUB ( Func<ST, ST> 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<TSUB, ST, CTX> : imm.Recorded<TSUB>
{
Context = Context,
State = newState,
}, $"Processing: {newState.GetType().Name} for {reason}",
}, $"Processing: {newState.GetType().Name} for {reason}",
member, file, line, expression );
return (TSUB)newFSM;

View File

@ -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<T>
public record class Versioned<T> : Imm
where T : Versioned<T>
{
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<T>
[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<T> : Versioned<T>
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<T> : Versioned<T>
return Process( ( old ) => next, reason, dbgName, dbgPath, lineNumber, dbgExp );
}
virtual public T Process( Func<T, T> fn,
virtual public T Process( Func<T, T> fn,
string reason = "",
[CallerMemberName] string dbgName = "",
[CallerFilePath] string dbgPath = "",
@ -207,11 +228,11 @@ public record class Timed<T> : Recorded<T>
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<T> : Recorded<T>
}
override public T Process( Func<T, T> fn,
override public T Process( Func<T, T> fn,
string reason = "",
[CallerMemberName] string dbgName = "",
[CallerFilePath] string dbgPath = "",

View File

@ -21,7 +21,7 @@ static public class iu
[CallerLineNumber] int lineNumber = 0,
[CallerArgumentExpression("fn")]
string expression = default )
where T : Recorded<T>
where T : Recorded<T>, 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<T>
where T : Recorded<T>, 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<T>
where T : Versioned<T>, Imm
{
obj = obj.Process( fn, reason );
return obj;

View File

@ -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>( 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 )
{