Last active
August 3, 2022 13:17
-
-
Save shawnfeng0/4674e62b506182ef2bacc2e5dd15ccdc to your computer and use it in GitHub Desktop.
DelayTruth: Delay setting true variable. TimeoutTruth: Set true to false after timeout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <chrono> | |
/* | |
* Delay setting true variable | |
* test code: | |
DelayTruth state(2000); | |
EXPECT_FALSE(state); | |
state.mark(true); | |
EXPECT_FALSE(state); | |
sleep(1); | |
EXPECT_FALSE(state); | |
sleep(2); | |
EXPECT_TRUE(state); | |
state.mark(false); | |
EXPECT_FALSE(state); | |
state.mark(true); | |
state.mark(false); | |
EXPECT_FALSE(state); | |
sleep(3); | |
EXPECT_FALSE(state); | |
*/ | |
class DelayTruth { | |
public: | |
explicit DelayTruth(unsigned delay_ms) : delay_ms_(delay_ms), mark_true_timestamp_(std::chrono::steady_clock::time_point::min()) {} | |
auto mark(bool value = true) -> decltype(*this) { | |
if (value) { | |
mark_true_timestamp_ = std::chrono::steady_clock::now(); | |
} else { | |
// reset the timestamp to mark false | |
mark_true_timestamp_ = std::chrono::steady_clock::time_point::min(); | |
} | |
return *this; | |
} | |
explicit operator bool() const { | |
// if the timestamp is min, then it is false | |
if (mark_true_timestamp_ == std::chrono::steady_clock::time_point::min()) { | |
return false; | |
} | |
auto diff = std::chrono::steady_clock::now() - mark_true_timestamp_; | |
// After the timeout period, it is true | |
return diff > delay_ms_; | |
} | |
private: | |
const std::chrono::milliseconds delay_ms_; | |
std::chrono::steady_clock::time_point mark_true_timestamp_; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <chrono> | |
/* | |
* Set true to false after timeout | |
* test code: | |
TimeoutTruth state(2000); | |
EXPECT_FALSE(state); | |
state.mark(true); | |
EXPECT_TRUE(state); | |
sleep(1); | |
EXPECT_TRUE(state); | |
sleep(2); | |
EXPECT_FALSE(state); | |
state.mark(false); | |
EXPECT_FALSE(state); | |
state.mark(true); | |
state.mark(false); | |
EXPECT_FALSE(state); | |
sleep(3); | |
EXPECT_FALSE(state); | |
*/ | |
class TimeoutTruth { | |
public: | |
explicit TimeoutTruth(unsigned timeout_ms) : timeout_ms_(timeout_ms), mark_true_timestamp_(std::chrono::steady_clock::time_point::min()) {} | |
auto mark(bool value = true) -> decltype(*this) { | |
if (value) { | |
mark_true_timestamp_ = std::chrono::steady_clock::now(); | |
} else { | |
// reset the timestamp to mark false | |
mark_true_timestamp_ = std::chrono::steady_clock::time_point::min(); | |
} | |
return *this; | |
} | |
explicit operator bool() const { | |
// if the timestamp is min, then it is false | |
if (mark_true_timestamp_ == std::chrono::steady_clock::time_point::min()) { | |
return false; | |
} | |
auto diff = std::chrono::steady_clock::now() - mark_true_timestamp_; | |
// if the time difference is greater than the timeout, then the value is false | |
return diff < timeout_ms_; | |
} | |
private: | |
const std::chrono::milliseconds timeout_ms_; | |
std::chrono::steady_clock::time_point mark_true_timestamp_; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment