Switch Controller
Loading...
Searching...
No Matches
EventToggle.h
Go to the documentation of this file.
1#pragma once
2
3#include "pch.h"
4
5using namespace std::chrono_literals;
6
7template <typename Callable> class TurboEvent {
8private:
9 Callable callable;
10 bool active{};
11 std::chrono::steady_clock::time_point start{};
12 std::chrono::milliseconds delay;
13
14public:
15 constexpr TurboEvent(Callable callable,
16 const std::chrono::milliseconds &delay)
17 : callable(callable), delay(delay) {}
18
19 constexpr auto operator()() {
20 auto val = callable();
21 if (!val) {
22 active = false;
23 return decltype(val){};
24 }
25 auto now = std::chrono::steady_clock::now();
26 if (!active) {
27 active = true;
28 start = now;
29 }
30 auto diff = now - start;
31 return diff / delay % 2 ? decltype(val){} : val;
32 }
33};
34
35template <typename Callable> class ToggleEvent {
36private:
37 Callable callable;
38 bool value{};
39 std::chrono::steady_clock::time_point start{};
40 std::chrono::milliseconds delay;
41
42public:
43 constexpr ToggleEvent(Callable callable,
44 const std::chrono::milliseconds &delay)
45 : callable(callable), delay(delay) {}
46
47 constexpr bool operator()() {
48 if (callable()) {
49 auto now = std::chrono::steady_clock::now();
50 if (now > start + delay) {
51 start = now;
52 value = !value;
53 }
54 }
55 return value;
56 }
57};
Definition: EventToggle.h:35
constexpr ToggleEvent(Callable callable, const std::chrono::milliseconds &delay)
Definition: EventToggle.h:43
constexpr bool operator()()
Definition: EventToggle.h:47
Definition: EventToggle.h:7
constexpr TurboEvent(Callable callable, const std::chrono::milliseconds &delay)
Definition: EventToggle.h:15
constexpr auto operator()()
Definition: EventToggle.h:19