CPP-TOOLBOX
Loading...
Searching...
No Matches
TemporalBinarySwitch Class Reference

A binary switch that tracks temporal transitions between on and off states. More...

#include <temporal_binary_switch.hpp>

Public Member Functions

 TemporalBinarySwitch ()
 Default constructor. Initializes the switch to an off state.
 
void set_true ()
 Sets the switch state to true (on).
 
void set_false ()
 Sets the switch state to false (off).
 
bool just_switched_on () const
 Checks if the switch has just switched on (non-temporal).
 
bool just_switched_off () const
 Checks if the switch has just switched off (non-temporal).
 
bool just_switched_on_temporal ()
 Checks if the switch has just switched on (temporal).
 
bool just_switched_off_temporal ()
 Checks if the switch has just switched off (temporal).
 

Detailed Description

A binary switch that tracks temporal transitions between on and off states.

Note that throughout these docs ^ will refer to an on signal and v is an off signal.

This class manages a boolean state and provides functionality to detect transitions from false → true (just switched on) and true → false (just switched off). It also provides temporal query methods that reset their respective transition flags after being checked.

Example 1: Basic Usage

#include <iostream>
int main() {
toggle.set_true();
if (toggle.just_switched_on()) {
std::cout << "Switch just turned on!\n";
}
if (toggle.just_switched_on_temporal()) {
std::cout << "Temporal on detected!\n";
}
// Subsequent temporal check will return false until it switches again
if (!toggle.just_switched_on_temporal()) {
std::cout << "No new on transition.\n";
}
toggle.set_false();
std::cout << "Switch just turned off!\n";
}
return 0;
}
bool just_switched_off_temporal()
Checks if the switch has just switched off (temporal).
Definition temporal_binary_switch.hpp:165
void set_true()
Sets the switch state to true (on).
Definition temporal_binary_switch.hpp:97
bool just_switched_on() const
Checks if the switch has just switched on (non-temporal).
Definition temporal_binary_switch.hpp:129
void set_false()
Sets the switch state to false (off).
Definition temporal_binary_switch.hpp:113
bool just_switched_on_temporal()
Checks if the switch has just switched on (temporal).
Definition temporal_binary_switch.hpp:149
TemporalBinarySwitch()
Default constructor. Initializes the switch to an off state.
Definition temporal_binary_switch.hpp:89
Definition main.py:1

Example 2: Loop-based Usage

This example demonstrates detecting “just happened” transitions in a simulated update loop.

#include "TemporalBinarySwitch.hpp"
#include <iostream>
int main() {
bool simulated_button_state[] = {false, false, true, true, false, false};
for (int frame = 0; frame < 6; ++frame) {
bool current_state = simulated_button_state[frame];
if (current_state)
input.set_true();
else
input.set_false();
std::cout << "Frame " << frame << ": Button just pressed\n";
std::cout << "Frame " << frame << ": Button just released\n";
}
return 0;
}
Note
The temporal query methods (just_switched_on_temporal and just_switched_off_temporal) consume their respective flags when called. This makes them ideal for polling within update loops or event-driven systems where you only need to react once per transition.

Constructor & Destructor Documentation

◆ TemporalBinarySwitch()

TemporalBinarySwitch::TemporalBinarySwitch ( )
inline

Default constructor. Initializes the switch to an off state.

Member Function Documentation

◆ just_switched_off()

bool TemporalBinarySwitch::just_switched_off ( ) const
inline

Checks if the switch has just switched off (non-temporal).

This function does not modify internal state.

Returns
True if the switch has just transitioned to off; otherwise false.

◆ just_switched_off_temporal()

bool TemporalBinarySwitch::just_switched_off_temporal ( )
inline

Checks if the switch has just switched off (temporal).

This function returns true once per transition from on to off. If called in succession, only the first call will return true

Returns
True if the switch has just transitioned to off since the last check; otherwise false.

◆ just_switched_on()

bool TemporalBinarySwitch::just_switched_on ( ) const
inline

Checks if the switch has just switched on (non-temporal).

This function does not modify internal state.

Returns
True if the switch has just transitioned to on; otherwise false.

◆ just_switched_on_temporal()

bool TemporalBinarySwitch::just_switched_on_temporal ( )
inline

Checks if the switch has just switched on (temporal).

This function returns true once per transition from off to on. If called in succession, only the first call will return true

Returns
True if the switch has just transitioned to on since the last check; otherwise false.

◆ set_false()

void TemporalBinarySwitch::set_false ( )
inline

Sets the switch state to false (off).

If the previous state was true, this marks the switch as having just switched off. If it was already false, no transition flags are modified.

◆ set_true()

void TemporalBinarySwitch::set_true ( )
inline

Sets the switch state to true (on).

If the previous state was false, this marks the switch as having just switched on. If it was already true, no transition flags are modified.


The documentation for this class was generated from the following file: