Avara3D 0.2.0
C++ API reference
PeriodicTrigger.h
1//
2// PeriodicTrigger.h
3// avara3d
4//
5// Created by Morgan Davis on 8/4/26.
6// Copyright © 2026 Morgan K Davis. All rights reserved.
7//
8
9#ifndef AVARA3D_UTIL_PERIODICTRIGGER_H
10#define AVARA3D_UTIL_PERIODICTRIGGER_H
11
12#include <chrono>
13#include <cstddef>
14#include <functional>
15#include <optional>
16
17namespace a3d::util {
18
27
28 public:
29 // [Public Lifecycle Functions]
30
39 explicit PeriodicTrigger(std::chrono::duration<double> interval, bool deferFirstFire = false);
40
41 // [Public Member Functions]
42
50 template<typename Function>
51 std::size_t update(double time, Function&& function) {
52
53 const auto count = dueCount(time);
54
55 for (std::size_t i = 0; i < count; ++i) {
56 std::invoke(function);
57 }
58
59 return count;
60 }
61
63 void reset() noexcept;
64
65 private:
66 // [Private Member Functions]
67
68 std::size_t dueCount(double time);
69
70 // [Private Member Variables]
71
72 double _interval;
73 bool _deferFirstFire;
74 std::optional<double> _nextFireTime;
75 std::optional<double> _lastTime;
76 };
77
78}
79
80#endif // AVARA3D_UTIL_PERIODICTRIGGER_H
Invokes a callback at fixed intervals along a caller-supplied time line.
void reset() noexcept
Clears timing history so the next update establishes a new schedule.
std::size_t update(double time, Function &&function)
Advances the trigger to time and invokes function once for each due firing.
PeriodicTrigger(std::chrono::duration< double > interval, bool deferFirstFire=false)
Creates a trigger with interval between firings.