Immutability and strip the / off the path
x) Better path substring x) Add some interfaces for immutability
This commit is contained in:
parent
8879e98229
commit
bea7080a5c
20
imm/FSM.cs
20
imm/FSM.cs
@ -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 TSUB : State<TSUB, CTX>
|
||||||
where CTX : Context
|
where CTX : Context
|
||||||
{
|
{
|
||||||
|
Meta Imm.Meta => base.Meta;
|
||||||
|
|
||||||
virtual public (CTX, TSUB) onEnter(CTX ctx, State<TSUB, CTX> oldState)
|
virtual public (CTX, TSUB) onEnter(CTX ctx, State<TSUB, CTX> oldState)
|
||||||
{
|
{
|
||||||
return (ctx, (TSUB)this);
|
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 TSUB : FSM<TSUB, ST, CTX>
|
||||||
where ST : State<ST, CTX>
|
where ST : State<ST, CTX>
|
||||||
where CTX : Context
|
where CTX : Context
|
||||||
{
|
{
|
||||||
|
Meta Imm.Meta => base.Meta;
|
||||||
|
|
||||||
public CTX Context { get; init; }
|
public CTX Context { get; init; }
|
||||||
public ST State { 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 origState = State;
|
||||||
|
|
||||||
var (newCtx, oldState) = State.onExit(Context, newState);
|
var (newCtx, oldState) = State.onExit(Context, newState);
|
||||||
|
|
||||||
var (newCTX, storeState) = newState.onEnter(newCtx, oldState);
|
var (newCTX, storeState) = newState.onEnter(newCtx, oldState);
|
||||||
|
|
||||||
var newFSM = this.Process( (v) => (this as TSUB) with
|
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 = "",
|
[CallerMemberName] string member = "",
|
||||||
[CallerFilePath] string file = "",
|
[CallerFilePath] string file = "",
|
||||||
[CallerLineNumber] int line = 0,
|
[CallerLineNumber] int line = 0,
|
||||||
[CallerArgumentExpression("fn")]
|
[CallerArgumentExpression("fn")]
|
||||||
string expression = default
|
string expression = default
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
var newState = fn( State );
|
var newState = fn( State );
|
||||||
@ -88,7 +92,7 @@ public record class FSM<TSUB, ST, CTX> : imm.Recorded<TSUB>
|
|||||||
{
|
{
|
||||||
Context = Context,
|
Context = Context,
|
||||||
State = newState,
|
State = newState,
|
||||||
}, $"Processing: {newState.GetType().Name} for {reason}",
|
}, $"Processing: {newState.GetType().Name} for {reason}",
|
||||||
member, file, line, expression );
|
member, file, line, expression );
|
||||||
|
|
||||||
return (TSUB)newFSM;
|
return (TSUB)newFSM;
|
||||||
|
|||||||
35
imm/Imm.cs
35
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 )]
|
//[lib.Ser( Types = lib.Types.None )]
|
||||||
public record class Versioned<T>
|
public record class Versioned<T> : Imm
|
||||||
where T : Versioned<T>
|
where T : Versioned<T>
|
||||||
{
|
{
|
||||||
public delegate void ChangeDelegate( T ZZOld, T next );
|
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 uint Version { get; internal set; } = 0;
|
||||||
public string Reason { get; internal set; } = "";
|
public string Reason { get; internal set; } = "";
|
||||||
@ -78,6 +97,8 @@ public record class Versioned<T>
|
|||||||
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
|
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
|
||||||
public MetaData Meta => MetaStorage;
|
public MetaData Meta => MetaStorage;
|
||||||
|
|
||||||
|
Meta Imm.Meta => MetaStorage;
|
||||||
|
|
||||||
[lib.Dont]
|
[lib.Dont]
|
||||||
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
|
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
|
||||||
public ChangeDelegate OnChange = (x, y) => {};
|
public ChangeDelegate OnChange = (x, y) => {};
|
||||||
@ -137,7 +158,7 @@ public record class Recorded<T> : Versioned<T>
|
|||||||
return Process( t => t, reason, dbgName, dbgPath, lineNumber );
|
return Process( t => t, reason, dbgName, dbgPath, lineNumber );
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual public T Process( T next,
|
virtual public T Process( T next,
|
||||||
string reason = "",
|
string reason = "",
|
||||||
[CallerMemberName] string dbgName = "",
|
[CallerMemberName] string dbgName = "",
|
||||||
[CallerFilePath] string dbgPath = "",
|
[CallerFilePath] string dbgPath = "",
|
||||||
@ -149,7 +170,7 @@ public record class Recorded<T> : Versioned<T>
|
|||||||
return Process( ( old ) => next, reason, dbgName, dbgPath, lineNumber, dbgExp );
|
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 = "",
|
string reason = "",
|
||||||
[CallerMemberName] string dbgName = "",
|
[CallerMemberName] string dbgName = "",
|
||||||
[CallerFilePath] string dbgPath = "",
|
[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;
|
public TimeSpan Since => Meta.TouchedAt - Meta.Old?.Meta.TouchedAt ?? TimeSpan.MaxValue;
|
||||||
|
|
||||||
|
|
||||||
override public T Record(
|
override public T Record(
|
||||||
string reason = "",
|
string reason = "",
|
||||||
[CallerMemberName] string dbgName = "",
|
[CallerMemberName] string dbgName = "",
|
||||||
[CallerFilePath] string dbgPath = "",
|
[CallerFilePath] string dbgPath = "",
|
||||||
[CallerLineNumber] int lineNumber = 0
|
[CallerLineNumber] int lineNumber = 0
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
return Process( t => t, reason, dbgName, dbgPath, lineNumber );
|
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 = "",
|
string reason = "",
|
||||||
[CallerMemberName] string dbgName = "",
|
[CallerMemberName] string dbgName = "",
|
||||||
[CallerFilePath] string dbgPath = "",
|
[CallerFilePath] string dbgPath = "",
|
||||||
|
|||||||
@ -21,7 +21,7 @@ static public class iu
|
|||||||
[CallerLineNumber] int lineNumber = 0,
|
[CallerLineNumber] int lineNumber = 0,
|
||||||
[CallerArgumentExpression("fn")]
|
[CallerArgumentExpression("fn")]
|
||||||
string expression = default )
|
string expression = default )
|
||||||
where T : Recorded<T>
|
where T : Recorded<T>, Imm
|
||||||
{
|
{
|
||||||
obj = obj.Process( fn, reason, memberName, filePath, lineNumber, expression );
|
obj = obj.Process( fn, reason, memberName, filePath, lineNumber, expression );
|
||||||
return obj;
|
return obj;
|
||||||
@ -34,7 +34,7 @@ static public class iu
|
|||||||
[CallerLineNumber] int lineNumber = 0,
|
[CallerLineNumber] int lineNumber = 0,
|
||||||
[CallerArgumentExpression("fn")]
|
[CallerArgumentExpression("fn")]
|
||||||
string expression = default )
|
string expression = default )
|
||||||
where T : Recorded<T>
|
where T : Recorded<T>, Imm
|
||||||
{
|
{
|
||||||
obj = obj.Process( fn, reason, memberName, filePath, lineNumber, expression );
|
obj = obj.Process( fn, reason, memberName, filePath, lineNumber, expression );
|
||||||
return obj;
|
return obj;
|
||||||
@ -48,7 +48,7 @@ static public class iu
|
|||||||
[CallerLineNumber] int lineNumber = 0,
|
[CallerLineNumber] int lineNumber = 0,
|
||||||
[CallerArgumentExpression("fn")]
|
[CallerArgumentExpression("fn")]
|
||||||
string expression = default )
|
string expression = default )
|
||||||
where T : Versioned<T>
|
where T : Versioned<T>, Imm
|
||||||
{
|
{
|
||||||
obj = obj.Process( fn, reason );
|
obj = obj.Process( fn, reason );
|
||||||
return obj;
|
return obj;
|
||||||
|
|||||||
@ -164,7 +164,7 @@ static public class log
|
|||||||
/*
|
/*
|
||||||
static public Log log
|
static public Log log
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return s_log;
|
return s_log;
|
||||||
}
|
}
|
||||||
@ -176,11 +176,16 @@ static public class log
|
|||||||
{
|
{
|
||||||
var cwd = Directory.GetCurrentDirectory();
|
var cwd = Directory.GetCurrentDirectory();
|
||||||
|
|
||||||
var rel = fullPath.Substring( cwd.Length );
|
var rel = fullPath.Substring( cwd.Length + 1 );
|
||||||
|
|
||||||
return rel;
|
return rel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static public string thisFilePath( [CallerFilePath] string path = "" )
|
||||||
|
{
|
||||||
|
return relativePath( path );
|
||||||
|
}
|
||||||
|
|
||||||
// Forwards.
|
// Forwards.
|
||||||
static public void fatal( string msg, string cat = "", object obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" )
|
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);
|
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 = "" )
|
[CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" )
|
||||||
{
|
{
|
||||||
logBase( msg, LogType.Info, path, line, member, cat, obj );
|
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 )
|
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 );
|
var evt = new LogEvent( type, msg, path, line, member, cat, obj );
|
||||||
|
|
||||||
lock( s_lock )
|
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 ) )
|
static public void expected<T>( T value, string falseString, string trueString = "", T notExpectedValue = default( T ) )
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -442,7 +447,7 @@ static public class log
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// _SHOULDNT_ need this since we lock at the top.
|
// _SHOULDNT_ need this since we lock at the top.
|
||||||
//lock( this )
|
//lock( this )
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user