-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
05c7536
commit 3c8e965
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#pragma once | ||
|
||
template <typename T> | ||
class ValueMonitor { | ||
private: | ||
T latestValue; | ||
bool firstValue; // Flag to track if it's the first value | ||
|
||
public: | ||
// Constructor | ||
ValueMonitor() : firstValue(true) { | ||
// Initialize latestValue to a default value. Important to avoid | ||
// undefined behavior on the first comparison. Using the minimum | ||
// value for numeric types is usually a good choice. | ||
latestValue = std::numeric_limits<T>::min(); | ||
} | ||
|
||
// Method to update the value and check for difference | ||
bool updateAndCheck(const T& newValue) { | ||
if (firstValue) { | ||
latestValue = newValue; | ||
firstValue = false; | ||
return true; // It's the first value, so it's technically "different" | ||
} | ||
|
||
if (newValue != latestValue) { | ||
latestValue = newValue; | ||
return true; // Values are different | ||
} else { | ||
return false; // Values are the same | ||
} | ||
} | ||
|
||
// Method to get the latest value (const version) | ||
const T& getLatestValue() const { | ||
return latestValue; | ||
} | ||
|
||
// Method to get the latest value (non-const version) | ||
T& getLatestValue() { | ||
return latestValue; | ||
} | ||
}; |