Skip to content

Commit 090262d

Browse files
authored
Merge pull request #61 from active-logic/StatusRef-Refactor
Status ref refactor and unit coverage
2 parents 09e6b41 + 366d3ab commit 090262d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+1296
-360
lines changed

Src/Certainties/Action.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,26 @@ public readonly partial struct action{
2323

2424
public static loop operator - (action x) => loop._cont;
2525

26-
public static bool operator true (action s)
26+
// NOTE: required since `false` is implemented; there is not
27+
// a known instance where this function would be called.
28+
public static bool operator true(action s)
2729
=> throw new InvOp("truehood cannot be tested (action)");
2830

31+
// NOTE: used in action && action
2932
public static bool operator false (action s) => false;
3033

34+
public override bool Equals(object x) => x is action;
35+
36+
override public int GetHashCode() => 1;
37+
3138
#if AL_OPTIMIZE // --------------------------------------------
3239

3340
public static action done(ValidString reason = null) => _done;
3441

3542
public static failure operator ! (action s) => failure._fail;
3643
#endif
3744

38-
public static implicit operator bool(action self)
39-
=> true;
45+
public static implicit operator bool(action self) => true;
4046

4147
public static implicit operator status(action self)
4248
=> status._done;

Src/Certainties/Failure.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public readonly partial struct failure{
2727
public static bool operator false (failure s)
2828
=> throw new InvOp("falsehood cannot be tested (failure)");
2929

30+
public override bool Equals(object x) => x is failure;
31+
32+
override public int GetHashCode() => -1;
33+
3034
#if AL_OPTIMIZE // --------------------------------------------
3135

3236
public static failure fail(ValidString reason = null) => _fail;

Src/Certainties/Impending.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ public static bool operator true (impending s)
4545
public static bool operator false(impending s)
4646
=> throw new InvOp("Cannot test falsehood (impending)");
4747

48+
public override bool Equals(object x)
49+
=> x is impending && Equals((impending)x);
50+
51+
public bool Equals(in impending x) => true;
52+
53+
override public int GetHashCode() => ω;
54+
4855
override public string ToString()
4956
=> ω == -1 ? "impending.fail" : "impending.cont";
5057

Src/Certainties/Loop.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public static bool operator true (loop s)
3434
public static bool operator false (loop s)
3535
=> throw new InvOp("falsehood cannot be tested (loop)");
3636

37+
override public int GetHashCode() => 0;
38+
3739
public static implicit operator impending(loop self)
3840
=> impending._cont;
3941

Src/Certainties/Pending.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ public static bool operator true (pending s)
3939

4040
public static bool operator false(pending s) => s.ω != 1;
4141

42+
public override bool Equals(object x)
43+
=> x is pending && Equals((pending)x);
44+
45+
public bool Equals(in pending x) => this.ω == x.ω;
46+
47+
override public int GetHashCode() => ω;
48+
4249
public override string ToString()
4350
=> ω == 0 ? "pending.cont" : "pending.done";
4451

Src/Decorators/Drive.cs

Lines changed: 6 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,26 @@ namespace Active.Core{
1313
public class Drive : AbstractDecorator{
1414

1515
static int uid; internal static int id => uid = ID(uid);
16-
//
17-
static status hold;
1816

1917
#if !AL_OPTIMIZE
20-
internal static LogData logData;
18+
2119
protected object target;
2220
#endif
2321

2422
public Gate? this[status @in, bool crit]{ get{
25-
hold = @in;
23+
StatusRef.hold = @in;
2624
return @in.running ? Eval(crit) : Bypass();
2725
}}
2826

29-
public Gate? this[bool @in, bool crit]{ get{
30-
hold = @in ? status.cont() : status.fail();
31-
return @in ? Eval(crit) : Bypass();
32-
}}
27+
public Gate? this[bool @in, bool crit]
28+
=> this[@in ? status.cont() : status.fail(), crit];
3329

3430
protected Gate Eval(bool crit, ValidString reason=null)
3531
=> new Gate(this, crit, new LogData(this, ".", reason));
3632

3733
protected Gate? Bypass(ValidString reason=null){
3834
#if !AL_OPTIMIZE
39-
logData = new LogData(this, target, reason);
35+
SetLogData(target, reason);
4036
#endif
4137
return null;
4238
}
@@ -62,51 +58,13 @@ public StatusRef this[status s]{ get{
6258
#if !AL_OPTIMIZE
6359
owner.target = s.targetScope;
6460
#endif
65-
return new StatusRef(crit ? s : hold, logData);
61+
return new StatusRef(crit ? s : StatusRef.hold, logData);
6662
}}
6763

6864
}
6965

7066
// ===============================================================
7167

72-
public readonly struct StatusRef{
73-
74-
readonly status x;
75-
readonly LogData logData;
76-
77-
internal StatusRef(status value, LogData logData)
78-
{ x = value; this.logData = logData; }
79-
80-
#if AL_OPTIMIZE
81-
public static implicit operator status(StatusRef? self)
82-
=> ToStatus(self);
83-
#else
84-
public static implicit operator status(StatusRef? self)
85-
=> status.log ? ToStatusWithLog(self) : ToStatus(self);
86-
#endif
87-
88-
static status ToStatus(StatusRef? self)
89-
=> self?.x ?? Self.hold;
90-
91-
#if !AL_OPTIMIZE
92-
static status ToStatusWithLog(StatusRef? self){
93-
if(self.HasValue){
94-
var ι = self.Value;
95-
return ι.x.ViaDecorator(
96-
ι.logData.scope,
97-
log && ι.logData.Reason());
98-
}else{
99-
if(Self.logData.scope == null) throw
100-
new InvOp("Log data is null");
101-
return hold.ViaDecorator(
102-
Self.logData.scope,
103-
log && Self.logData.Reason());
104-
}
105-
}
106-
#endif
107-
108-
} // StatusRef
109-
11068
}
11169

11270
#if !AL_BEST_PERF

Src/Decorators/InOut.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Active.Core{
77
public class InOut : Conditional{
88

99
static int uid; internal static int id => uid = ID(uid);
10-
bool passing;
10+
internal bool passing;
1111
int frame;
1212

1313
public Gate? this[bool @in, bool @out]{ get{

Src/Decorators/Once.cs

Lines changed: 4 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,11 @@ namespace Active.Core{
1313
public class Once : AbstractDecorator{
1414

1515
static int uid; internal static int id => uid = ID(uid);
16-
//
17-
static status hold;
1816
//
1917
internal status state = cont();
2018
int frame = 0;
2119

2220
#if !AL_OPTIMIZE
23-
internal static LogData logData;
2421
protected object target;
2522
#endif
2623

@@ -34,9 +31,9 @@ protected Gate Eval(ValidString reason=null)
3431

3532
protected Gate? Bypass(ValidString reason=null){
3633
#if !AL_OPTIMIZE
37-
logData = new LogData(this, target, reason);
34+
SetLogData(target, reason);
3835
#endif
39-
hold = state;
36+
StatusRef.hold = state;
4037
return null;
4138
}
4239

@@ -63,52 +60,12 @@ public StatusRef this[status s]{ get{
6360

6461
}
6562

66-
// ===============================================================
67-
68-
public readonly struct StatusRef{
69-
70-
readonly status x;
71-
readonly LogData logData;
72-
73-
internal StatusRef(status value, LogData logData)
74-
{ x = value; this.logData = logData; }
75-
76-
#if AL_OPTIMIZE
77-
public static implicit operator status(StatusRef? self)
78-
=> ToStatus(self);
79-
#else
80-
public static implicit operator status(StatusRef? self)
81-
=> status.log ? ToStatusWithLog(self) : ToStatus(self);
82-
#endif
83-
84-
internal static status ToStatus(StatusRef? self)
85-
=> self?.x ?? Once.hold;
86-
87-
#if !AL_OPTIMIZE
88-
internal static status ToStatusWithLog(StatusRef? self){
89-
if(self.HasValue){
90-
var ι = self.Value;
91-
return ι.x.ViaDecorator(
92-
ι.logData.scope,
93-
log && ι.logData.Reason());
94-
}else{
95-
if(Self.logData.scope == null) throw
96-
new InvOp("Log data is null");
97-
return hold.ViaDecorator(
98-
Self.logData.scope,
99-
log && Self.logData.Reason());
100-
}
101-
}
102-
#endif
103-
104-
} // StatusRef
105-
106-
}
63+
} // Once
10764

10865
#if !AL_BEST_PERF
10966
partial class Task{
11067
public Self.Gate? Once([Tag] int key = -1)
111-
=> store.Decorator<Self>(key, Self.id)?.pass;
68+
=> store.Decorator<Self>(key, Self.id).pass;
11269
}
11370
#endif
11471

Src/Decorators/Timeout.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ public class Timeout : Conditional, Conditional.OptionalArguments{
99

1010
static int uid; internal static int id => uid = ID(uid);
1111
public float duration = 1;
12-
float stamp;
12+
internal float stamp;
1313
int frame;
1414

15-
bool enabled => stamp != -1;
15+
internal bool enabled => stamp != -1;
1616

1717
public Timeout() => stamp = time;
1818

@@ -36,7 +36,7 @@ public Gate? this[float s]{ get{
3636

3737
override public void OnStatus(status s) => OnStatus(s.running);
3838

39-
void OnStatus(bool running)
39+
internal void OnStatus(bool running)
4040
=> stamp = enabled ^ running ? (enabled ? -1 : time) : stamp;
4141

4242
override public action Reset(){ stamp = -1; return @void(); }

Src/Decorators/Undef.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@ public class Undef : AbstractDecorator{
1414

1515
static int uid; internal static int id => uid = ID(uid);
1616
protected Random _random;
17-
float stamp;
17+
internal float stamp;
1818
status value;
1919

2020
public Undef(){}
2121

22-
protected Random random => _random ?? (_random = new Random());
22+
internal Random random => _random ?? (_random = new Random());
2323

2424
public status this[float s]{ get{
25-
var t = time; if(stamp == 0) stamp = t; var remaining = s + stamp - t;
25+
var t = time;
26+
if(stamp == 0) stamp = t;
27+
var remaining = s + stamp - t;
2628
return (
2729
(remaining > 0) ? value
2830
: (value = status.@unchecked(random.Next(-1, 2)))

0 commit comments

Comments
 (0)