Skip to content

Commit 9dcd636

Browse files
committed
Refactor StatusRef closes #56
1 parent d9f7319 commit 9dcd636

Some content is hidden

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

44 files changed

+364
-270
lines changed

Src/Decorators/Drive.cs

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,19 @@ 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

2927
public Gate? this[bool @in, bool crit]{ get{
30-
hold = @in ? status.cont() : status.fail();
28+
StatusRef.hold = @in ? status.cont() : status.fail();
3129
return @in ? Eval(crit) : Bypass();
3230
}}
3331

@@ -36,7 +34,7 @@ protected Gate Eval(bool crit, ValidString reason=null)
3634

3735
protected Gate? Bypass(ValidString reason=null){
3836
#if !AL_OPTIMIZE
39-
logData = new LogData(this, target, reason);
37+
SetLogData(target, reason);
4038
#endif
4139
return null;
4240
}
@@ -62,51 +60,13 @@ public StatusRef this[status s]{ get{
6260
#if !AL_OPTIMIZE
6361
owner.target = s.targetScope;
6462
#endif
65-
return new StatusRef(crit ? s : hold, logData);
63+
return new StatusRef(crit ? s : StatusRef.hold, logData);
6664
}}
6765

6866
}
6967

7068
// ===============================================================
7169

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-
11070
}
11171

11272
#if !AL_BEST_PERF

Src/Decorators/Once.cs

Lines changed: 3 additions & 46 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,47 +60,7 @@ 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{

Src/Details/ConditionalDetails.cs

Lines changed: 0 additions & 73 deletions
This file was deleted.

Src/Details/IDecorator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
namespace Active.Core{ public interface IDecorator{} }

Src/Details/LogString.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ public class LogString{
66
public string str;
77
public bool valid{ get; private set; }
88

9-
LogString(string str){ this.str = str; }
9+
internal LogString(string str){ this.str = str; }
1010

11-
LogString(string str, bool valid) { this.str = str; this.valid = valid; }
11+
internal LogString(string str, bool valid)
12+
{ this.str = str; this.valid = valid; }
1213

1314
override public string ToString(){
1415
return valid ? str : throw new Ex("Can't extract invalid string");

Src/Details/AbstractDecoratorDetails.cs renamed to Src/Details/Stateful/AbstractDecoratorDetails.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,26 @@
22
#define AL_OPTIMIZE
33
#endif
44

5+
using ArgEx = System.ArgumentException;
56
using Active.Core.Details;
67

78
namespace Active.Core{
89

9-
public interface IDecorator{}
10-
1110
partial class AbstractDecorator{
1211

13-
protected static readonly LogString log = null;
12+
internal static readonly LogString log = null;
1413
static int MaxId = 0;
1514

1615
public abstract action Reset();
1716

1817
override public string ToString()
1918
=> StatusFormat.Decorator(this);
2019

20+
#if !AL_OPTIMIZE
21+
protected void SetLogData(object target, string reason)
22+
=> StatusRef.SetLogData(this, target, reason);
23+
#endif
24+
2125
internal readonly struct LogData{
2226

2327
public readonly AbstractDecorator scope;
@@ -26,6 +30,7 @@ internal readonly struct LogData{
2630

2731
// NOTE: s == null allowed only for testing purposes
2832
public LogData(AbstractDecorator s, object tg, string r){
33+
//if(s == null) throw new ArgEx("Scope cannot be null");
2934
scope = s; target = tg; reason = r;
3035
}
3136

File renamed without changes.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#if !(UNITY_EDITOR || DEBUG)
2+
#define AL_OPTIMIZE
3+
#endif
4+
5+
using Active.Core.Details;
6+
using InvOp = System.InvalidOperationException;
7+
using Self = Active.Core.Conditional;
8+
9+
namespace Active.Core{
10+
partial class Conditional{
11+
12+
#if !AL_OPTIMIZE
13+
protected object target;
14+
#endif
15+
16+
public readonly struct Gate{
17+
18+
readonly Self owner; readonly LogData logData;
19+
20+
internal Gate(Self owner, LogData logData)
21+
{ this.owner = owner; this.logData = logData; }
22+
23+
public StatusRef this[status s]{ get{
24+
#if !AL_OPTIMIZE
25+
owner.target = s.targetScope;
26+
#endif
27+
owner.OnStatus(s);
28+
return new StatusRef(s, logData);
29+
}}
30+
31+
} // Gate
32+
33+
} // Conditional
34+
35+
} // Active.Core
File renamed without changes.

Src/Details/Iterator.cs renamed to Src/Details/Stateful/Iterator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace Active.Core.Details{
22
public abstract class Iterator{
33

4-
protected static readonly LogString log = null;
4+
internal static readonly LogString log = null;
55

66
protected int i;
77
protected readonly Composite κ;

0 commit comments

Comments
 (0)