| | | 1 | | //======================================================================= |
| | | 2 | | // ChronicleClock.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using System; |
| | | 9 | | |
| | | 10 | | namespace Chronicler.Timing; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// A host-advanced simulation clock with an exact elapsed timeline and a positive step. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <remarks> |
| | | 16 | | /// This clock has no wall-time source or thread synchronization. Its owner controls |
| | | 17 | | /// advancement and invalidates retained work when resetting or restoring a timeline. |
| | | 18 | | /// </remarks> |
| | | 19 | | public sealed class ChronicleClock : IRecordable |
| | | 20 | | { |
| | | 21 | | /// <summary>Creates a clock at frame and time zero.</summary> |
| | | 22 | | /// <param name="stepDuration">The positive duration added to elapsed time on each advance.</param> |
| | | 23 | | /// <exception cref="ArgumentOutOfRangeException">The step is zero or negative.</exception> |
| | 63 | 24 | | public ChronicleClock(ChronicleDuration stepDuration) |
| | | 25 | | { |
| | 63 | 26 | | SetStepDuration(stepDuration); |
| | 60 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <summary>Gets the completed advance count.</summary> |
| | | 30 | | /// <value>A nonnegative count since the current timeline's origin.</value> |
| | | 31 | | public long FrameCount { get; private set; } |
| | | 32 | | |
| | | 33 | | /// <summary>Gets accumulated simulation time.</summary> |
| | | 34 | | /// <value>The sum of all completed step durations relative to the timeline origin.</value> |
| | | 35 | | public ChronicleTimestamp ElapsedTime { get; private set; } |
| | | 36 | | |
| | | 37 | | /// <summary>Gets the next step's duration.</summary> |
| | | 38 | | /// <value>A positive interval. Changing it leaves previously accumulated time intact.</value> |
| | | 39 | | public ChronicleDuration StepDuration { get; private set; } |
| | | 40 | | |
| | | 41 | | /// <summary>Advances one frame and one configured step, or leaves all state unchanged.</summary> |
| | | 42 | | /// <exception cref="InvalidOperationException">The frame counter is exhausted.</exception> |
| | | 43 | | /// <exception cref="OverflowException">The next elapsed timestamp cannot be represented.</exception> |
| | | 44 | | public void Advance() |
| | | 45 | | { |
| | 2243 | 46 | | if (FrameCount == long.MaxValue) |
| | 2 | 47 | | throw new InvalidOperationException("The simulation clock's frame counter is exhausted."); |
| | 2241 | 48 | | long nextFrame = FrameCount + 1; |
| | 2241 | 49 | | ChronicleTimestamp nextTime = ElapsedTime + StepDuration; |
| | 2238 | 50 | | FrameCount = nextFrame; |
| | 2238 | 51 | | ElapsedTime = nextTime; |
| | 2238 | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <summary>Sets the duration of future advances.</summary> |
| | | 55 | | /// <param name="stepDuration">The positive duration to use starting with the next advance.</param> |
| | | 56 | | /// <exception cref="ArgumentOutOfRangeException">The step is zero or negative. The previous step remains in effect. |
| | | 57 | | public void SetStepDuration(ChronicleDuration stepDuration) |
| | | 58 | | { |
| | 2175 | 59 | | ValidateStepDuration(stepDuration); |
| | 2169 | 60 | | StepDuration = stepDuration; |
| | 2169 | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary>Calculates a frame deadline without scheduling work.</summary> |
| | | 64 | | /// <param name="framesFromNow">A nonnegative count of advances from the current frame. Zero selects the current fra |
| | | 65 | | /// <returns>The current frame plus the requested offset.</returns> |
| | | 66 | | /// <exception cref="ArgumentOutOfRangeException">The offset is negative.</exception> |
| | | 67 | | /// <exception cref="OverflowException">The deadline exceeds <see cref="long.MaxValue"/>.</exception> |
| | | 68 | | public long GetDeadlineFrame(long framesFromNow) |
| | | 69 | | { |
| | 2059 | 70 | | if (framesFromNow < 0) |
| | 1 | 71 | | throw new ArgumentOutOfRangeException(nameof(framesFromNow)); |
| | 2058 | 72 | | return checked(FrameCount + framesFromNow); |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <summary>Starts a new zero-based timeline, preserving the configured step.</summary> |
| | | 76 | | /// <remarks>The owner must invalidate work associated with the previous timeline.</remarks> |
| | | 77 | | public void Reset() |
| | | 78 | | { |
| | 5 | 79 | | FrameCount = 0; |
| | 5 | 80 | | ElapsedTime = ChronicleTimestamp.Zero; |
| | 5 | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <summary>Transfers the clock through its version-one schema.</summary> |
| | | 84 | | /// <param name="chronicler">The active pass for reading or writing clock state.</param> |
| | | 85 | | /// <remarks>The load stages all fields and validates them before updating the clock. The host stops advancement and |
| | | 86 | | /// <exception cref="InvalidOperationException">A required schema is absent or unsupported, the frame count is negat |
| | | 87 | | /// <exception cref="ArgumentOutOfRangeException">The elapsed timestamp is negative or the step is not positive.</ex |
| | | 88 | | public void RecordData(IChronicler chronicler) |
| | | 89 | | { |
| | 179 | 90 | | int schemaVersion = 1; |
| | 179 | 91 | | long frameCount = FrameCount; |
| | 179 | 92 | | ChronicleTimestamp elapsedTime = ElapsedTime; |
| | 179 | 93 | | ChronicleDuration stepDuration = StepDuration; |
| | 179 | 94 | | RecordValues.Look(chronicler, ref schemaVersion, "SchemaVersion", 0); |
| | 179 | 95 | | RecordValues.Look(chronicler, ref frameCount, nameof(FrameCount), 0L); |
| | 179 | 96 | | RecordChronicleTime.Look(chronicler, ref elapsedTime, nameof(ElapsedTime)); |
| | 171 | 97 | | RecordChronicleTime.Look(chronicler, ref stepDuration, nameof(StepDuration)); |
| | | 98 | | |
| | 165 | 99 | | if (chronicler.Mode == SerializationMode.Loading) |
| | | 100 | | { |
| | 32 | 101 | | if (schemaVersion != 1) |
| | 4 | 102 | | throw new InvalidOperationException("The clock schema is missing or unsupported."); |
| | 28 | 103 | | if (frameCount < 0 || (frameCount == 0) != (elapsedTime == ChronicleTimestamp.Zero)) |
| | 6 | 104 | | throw new InvalidOperationException("Clock frames and elapsed time must be nonnegative and agree at zero |
| | 22 | 105 | | ValidateStepDuration(stepDuration); |
| | 18 | 106 | | FrameCount = frameCount; |
| | 18 | 107 | | ElapsedTime = elapsedTime; |
| | 18 | 108 | | StepDuration = stepDuration; |
| | | 109 | | } |
| | 151 | 110 | | } |
| | | 111 | | |
| | | 112 | | private static void ValidateStepDuration(ChronicleDuration stepDuration) |
| | | 113 | | { |
| | 2197 | 114 | | if (stepDuration <= ChronicleDuration.Zero) |
| | 10 | 115 | | throw new ArgumentOutOfRangeException(nameof(stepDuration), "The simulation step must be positive."); |
| | 2187 | 116 | | } |
| | | 117 | | } |