Reconstruct property sources and operators to generic types.

This commit is contained in:
2023-07-31 15:12:41 +08:00
parent 3db8f61e3b
commit b78e99c59c
4 changed files with 57 additions and 73 deletions

View File

@@ -18,11 +18,15 @@ namespace Cryville.Crtr {
value = buf;
}
protected abstract void InternalGet();
public abstract class FixedBuffer : PropSrc {
public abstract class Fixed<T> : PropSrc {
protected readonly Func<T> _cb;
public Fixed(int type, Func<T> cb) : base(type) { _cb = cb; }
}
public abstract class FixedBuffer<T> : Fixed<T> {
bool m_invalidated = true;
protected override bool Invalidated { get { return m_invalidated; } }
public override void Invalidate() { m_invalidated = true; }
public FixedBuffer(int type, int size) : base(type) { buf = new byte[size]; }
public FixedBuffer(int type, int size, Func<T> cb) : base(type, cb) { buf = new byte[size]; }
protected override void InternalGet() {
m_invalidated = false;
}
@@ -36,18 +40,16 @@ namespace Cryville.Crtr {
buf = _value;
}
}
public class Boolean : FixedBuffer {
readonly Func<bool> _cb;
public Boolean(Func<bool> cb) : base(PdtInternalType.Number, 4) { _cb = cb; }
public class Boolean : FixedBuffer<bool> {
public Boolean(Func<bool> cb) : base(PdtInternalType.Number, 4, cb) { }
protected override void InternalGet() {
base.InternalGet();
buf[0] = _cb() ? (byte)1 : (byte)0;
}
}
public static readonly PropSrc Error = new Arbitrary(PdtInternalType.Error, new byte[0]);
public class Float : FixedBuffer {
readonly Func<float> _cb;
public Float(Func<float> cb) : base(PdtInternalType.Number, 4) { _cb = cb; }
public class Float : FixedBuffer<float> {
public Float(Func<float> cb) : base(PdtInternalType.Number, 4, cb) { }
protected override unsafe void InternalGet() {
base.InternalGet();
fixed (byte* _ptr = buf) {
@@ -56,9 +58,8 @@ namespace Cryville.Crtr {
}
}
public static readonly PropSrc Null = new Arbitrary(PdtInternalType.Null, new byte[0]);
public class String : PropSrc {
readonly Func<string> _cb;
public String(Func<string> cb) : base(PdtInternalType.String) { _cb = cb; }
public class String : Fixed<string> {
public String(Func<string> cb) : base(PdtInternalType.String, cb) { }
protected override unsafe void InternalGet() {
var v = _cb();
int strlen = v.Length;
@@ -71,9 +72,8 @@ namespace Cryville.Crtr {
}
}
}
public class Identifier : FixedBuffer {
readonly Func<int> _cb;
public Identifier(Func<int> cb) : base(PdtInternalType.Undefined, 4) { _cb = cb; }
public class Identifier : FixedBuffer<int> {
public Identifier(Func<int> cb) : base(PdtInternalType.Undefined, 4, cb) { }
protected override unsafe void InternalGet() {
base.InternalGet();
fixed (byte* _ptr = buf) {
@@ -81,9 +81,8 @@ namespace Cryville.Crtr {
}
}
}
public class BeatTime : FixedBuffer {
readonly Func<RBeatTime> _cb;
public BeatTime(Func<RBeatTime> cb) : base(PdtInternalType.Vector, 4 * sizeof(int)) { _cb = cb; }
public class BeatTime : FixedBuffer<RBeatTime> {
public BeatTime(Func<RBeatTime> cb) : base(PdtInternalType.Vector, 4 * sizeof(int), cb) { }
protected override unsafe void InternalGet() {
base.InternalGet();
var bt = _cb();
@@ -96,9 +95,8 @@ namespace Cryville.Crtr {
}
}
}
public class Vector4 : FixedBuffer {
readonly Func<RVector4> _cb;
public Vector4(Func<RVector4> cb) : base(PdtInternalType.Vector, 4 * sizeof(float) + sizeof(int)) { _cb = cb; }
public class Vector4 : FixedBuffer<RVector4> {
public Vector4(Func<RVector4> cb) : base(PdtInternalType.Vector, 4 * sizeof(float) + sizeof(int), cb) { }
protected override unsafe void InternalGet() {
base.InternalGet();
var vec = _cb();