multi_timer.h
Several clocks in one object.
#include <sonore/multi_timer.h>- 1type
- Guides: Testing your plugin
On this page
Design notes
What it is for
An editor wants a meter at 30 Hz, a blinking record light at 2 Hz, and a "save the settings" nudge every ten seconds. Three periods, one object, and no three separate timer subscriptions to keep in step with each other's lifetimes.
It takes the elapsed time as an ARGUMENT
The same decision the Animator and the TooltipManager already made here, for the same reason. A timer that read a clock could only be tested by sleeping, and a test that sleeps is slow, flaky on a loaded machine, and unable to check the interesting cases at all -- what happens when a single tick is longer than the interval, what happens after the editor was hidden for four seconds.
Driven by its owner: advance() from an editor's editorTick(), a NativeWindow's onTick or an application's timer. Nothing in the SDK calls it by itself.
Catching up, and deciding not to
A host that stalls for two seconds leaves a 30 Hz timer sixty ticks behind. Firing sixty times to catch up is almost never what anybody wants: sixty repaints back to back, and for a blinking light sixty state changes nobody sees. So a late timer fires ONCE and resets. That is a decision rather than an oversight, and it is the one a caller most needs to know about.
Types 1
Several timers in one object, each known by an int id and all reported through one onTimer callback. Mirrors the reference's class of the same name, with the difference that matters: it has no clock of its own. Its owner calls advance() with the milliseconds that passed -- from the editor's tick, or from a test -- and the callbacks run inside that call. See the file comment for why, and for why a late timer fires once rather than catching up.
Intervals are in milliseconds. One thread, the one calling advance(), usually the UI thread: nothing here is locked. A callback may start and stop timers, its own included, or delete the MultiTimer; nothing else fires in that advance() then.