wavelength 1.0.0
Loading...
Searching...
No Matches
scheduler.h
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4#include <set>
5
6#include "os/binstream.h"
7#include "os/command.h"
8#include "os/commandid.h"
9#include "os/ptr.h"
10#include "os/schedulerclock.h"
11#include "os/string.h"
12
20class Scheduler {
21public:
30 public:
41 Ptr<Command> command, float fTime, int nTick, const CommandId &id, int nRecordable)
42 : mCommand(command), mTime(fTime), mTick(nTick), mId(id), mRecordable(nRecordable) {
43 }
44
53 bool operator<(const CommandInfo &other) const;
54
62 void Save(BinStream &stream) const;
63
71 void Load(BinStream &stream);
72
74 const Ptr<Command> &GetCommand() const {
75 return mCommand;
76 }
77
79 float GetTime() const {
80 return mTime;
81 }
82
84 int GetTick() const {
85 return mTick;
86 }
87
89 CommandId GetId() const {
90 return mId;
91 }
92
94 int GetRecordable() const {
95 return mRecordable;
96 }
97
98 private:
99 Ptr<Command> mCommand;
100 float mTime;
101 int mTick;
102 CommandId mId;
103 int mRecordable;
104 };
105
112 public:
119 virtual bool operator()(const CommandInfo &info) const = 0;
120 };
121
128 class ByCommand : public CancelPred {
129 public:
135 explicit ByCommand(Command *pCommand) : mCommand(pCommand) {
136 }
137
146 bool operator()(const CommandInfo &info) const override;
147
148 private:
149 Command *mCommand; // The command.
150 };
151
158 class Recorder {
159 public:
167
174 void Flush();
175
183 void Record(const CommandInfo &info);
184
185 private:
192 void Save();
193
194 std::unique_ptr<BinStream> mStream; // The runs not yet saved.
195 String mFile; // The file on the memory card.
196 int mDevice; // The memory card, or -1 for none.
197 int mParts; // The number of parts saved.
198 };
199
206 public:
215 Playbacker(const char *pszFile, Scheduler *pScheduler);
216
226 Playbacker(char *pBuffer, int nSize, Scheduler *pScheduler);
227
235
244 void QueueAll();
245
246 private:
247 std::unique_ptr<BinStream> mStream; // The recording.
248 Scheduler *mScheduler; // The scheduler the runs are queued on. Borrowed.
249 };
250
258
266
277 void Reset(const float *pTickDuration, int nTick);
278
292 void ResetForRecording(const float *pTickDuration, const char *pszFile, int nDevice, int nTick);
293
305 void ResetForPlayback(const float *pTickDuration, const char *pszFile, int nTick);
306
319 void ResetForPlayback(const float *pTickDuration, char *pBuffer, int nSize, int nTick);
320
327 void Resume();
328
335 void Pause();
336
344 bool IsRunning();
345
353 void SetSpeed(float fSpeed);
354
364 float GetSpeed();
365
376 void PostAtTime(Command *pCommand, float fTime, const CommandId &id, bool bRecordable);
377
389 void PostAtTime(Command *pCommand, float fTime, bool bRecordable);
390
401 void PostAfter(Command *pCommand, float fDelay, const CommandId &id, bool bRecordable);
402
412 void PostAfter(Command *pCommand, float fDelay, bool bRecordable);
413
424 void PostAt(Command *pCommand, int nTick, const CommandId &id, bool bRecordable);
425
435 void PostAt(Command *pCommand, int nTick, bool bRecordable);
436
449 void PostIn(Command *pCommand, int nDelayTicks, const CommandId &id, bool bRecordable);
450
460 void PostIn(Command *pCommand, int nDelayTicks, bool bRecordable);
461
470
479
488 void Clear();
489
497 void Cancel(Command *pCommand);
498
507 void Poll();
508
510 const std::multiset<CommandInfo> &GetCommands() const {
511 return *mCommands;
512 }
513
515 const float *GetTickDuration() const {
516 return mTickDuration;
517 }
518
520 float GetTime() const {
521 return mTime;
522 }
523
525 int GetTick() const {
526 return mTick;
527 }
528
530 float GetFrameTime() const {
531 return mFrameTime;
532 }
533
535 int GetFrameTick() const {
536 return mFrameTick;
537 }
538
540 float GetPrevFrameTime() const {
541 return mPrevFrameTime;
542 }
543
545 int GetPrevFrameTick() const {
546 return mPrevFrameTick;
547 }
548
549private:
550 std::unique_ptr<std::multiset<CommandInfo>> mCommands;
551 std::unique_ptr<Recorder> mRecorder;
552 std::unique_ptr<Playbacker> mPlaybacker;
553 const float *mTickDuration;
554 float mTime;
555 int mTick;
556 float mFrameTime;
557 int mFrameTick;
558 float mPrevFrameTime;
559 int mPrevFrameTick;
560 SchedulerClock mClock;
561
575 void Insert(Command *pCommand, float fTime, int nTick, const CommandId &id, bool bRecordable);
576
586 void RemoveIf(const CancelPred &pred);
587};
588
600
612BinStream &operator>>(BinStream &stream, Command *&pCommand);
613
620
Stream of raw bytes, the base of the memory, file, and network streams.
Definition binstream.h:22
Tag that a Scheduler stores beside each queued command.
Definition commandid.h:8
Unit of work a Scheduler runs at a set time.
Definition command.h:13
Owner of one reference to an Attachment.
Definition ptr.h:16
Clock that runs at a settable rate on the cycles the system clock accumulates.
Definition schedulerclock.h:9
ByCommand(Command *pCommand)
Construct the test of a command.
Definition scheduler.h:135
bool operator()(const CommandInfo &info) const override
Report whether a run is of the command.
Test that selects the runs Cancel() withdraws.
Definition scheduler.h:111
virtual bool operator()(const CommandInfo &info) const =0
Report whether a run is withdrawn.
One queued run of a command.
Definition scheduler.h:29
const Ptr< Command > & GetCommand() const
Definition scheduler.h:74
float GetTime() const
Definition scheduler.h:79
void Save(BinStream &stream) const
Write the run to a recording.
bool operator<(const CommandInfo &other) const
Report whether this run comes before another.
CommandInfo(Ptr< Command > command, float fTime, int nTick, const CommandId &id, int nRecordable)
Construct a run.
Definition scheduler.h:40
void Load(BinStream &stream)
Read a run Save() wrote.
int GetTick() const
Definition scheduler.h:84
CommandId GetId() const
Definition scheduler.h:89
int GetRecordable() const
Definition scheduler.h:94
Playbacker(const char *pszFile, Scheduler *pScheduler)
Open a recording in a file.
void QueueAll()
Queue every run of the recording.
~Playbacker()
Release the stream.
Playbacker(char *pBuffer, int nSize, Scheduler *pScheduler)
Open a recording in a buffer.
Writer of the recordable runs to a file of a memory card, in parts.
Definition scheduler.h:158
~Recorder()
Save the runs not yet saved and release the stream.
void Flush()
Save the runs not yet saved, when a memory card is set.
void Record(const CommandInfo &info)
Write a run to the stream.
Queue of commands that run when the song clock reaches their tick.
Definition scheduler.h:20
float GetPrevFrameTime() const
Definition scheduler.h:540
float GetFrameTime() const
Definition scheduler.h:530
void Poll()
Advance the clock to the current time and run the commands it passed.
void Resume()
Restart the clock after Pause().
void PostAtTime(Command *pCommand, float fTime, bool bRecordable)
Run a command at a time.
void Reset(const float *pTickDuration, int nTick)
Stop the clock, drop every command, and set the clock to a tick at normal speed.
Scheduler()
Construct a stopped scheduler with no command.
void Cancel(Command *pCommand)
Withdraw every queued run of a command.
void PostIn(Command *pCommand, int nDelayTicks, const CommandId &id, bool bRecordable)
Run a command a number of ticks from now.
float GetClockTime()
Report the clock time.
void PostAfter(Command *pCommand, float fDelay, bool bRecordable)
Run a command a time from now.
void PostAt(Command *pCommand, int nTick, const CommandId &id, bool bRecordable)
Run a command at a tick, with a tag stored beside the entry.
void PostAfter(Command *pCommand, float fDelay, const CommandId &id, bool bRecordable)
Run a command a time from now, with a tag stored beside the entry.
const std::multiset< CommandInfo > & GetCommands() const
Definition scheduler.h:510
bool IsRunning()
Report whether the clock runs.
const float * GetTickDuration() const
Definition scheduler.h:515
void ResetForPlayback(const float *pTickDuration, const char *pszFile, int nTick)
Reset the clock and replay the commands a file recorded.
float GetSpeed()
Report the rate of the clock.
void PostIn(Command *pCommand, int nDelayTicks, bool bRecordable)
Run a command a number of ticks from now.
int GetFrameTick() const
Definition scheduler.h:535
float GetTime() const
Definition scheduler.h:520
int GetClockTick()
Report the clock tick.
void PostAt(Command *pCommand, int nTick, bool bRecordable)
Run a command at a tick.
int GetTick() const
Definition scheduler.h:525
int GetPrevFrameTick() const
Definition scheduler.h:545
void ResetForPlayback(const float *pTickDuration, char *pBuffer, int nSize, int nTick)
Reset the clock and replay the commands a buffer recorded.
void PostAtTime(Command *pCommand, float fTime, const CommandId &id, bool bRecordable)
Run a command at a time, with a tag stored beside the entry.
void Clear()
Drop every queued command.
~Scheduler()
Release the queued commands, the recorder, and the playback.
void SetSpeed(float fSpeed)
Set the rate of the clock.
void Pause()
Stop the clock, and save what the recorder holds.
void ResetForRecording(const float *pTickDuration, const char *pszFile, int nDevice, int nTick)
Reset the clock for a recorded song.
Growable text buffer that is also a PrnStream.
Definition string.h:19
BinStream & operator<<(BinStream &stream, Command *pCommand)
Write a command to a recording: 1, its serial identifier, and what Command::Save() writes,...
BinStream & operator>>(BinStream &stream, Command *&pCommand)
Read a command operator<<() wrote, building it through Factory<Command>.
Scheduler TheMetaScheduler
The scheduler of the front end clock, which times the menu music.
Scheduler TheSongScheduler
The scheduler of the song clock.