1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-12 08:12:22 +01:00

Convert clamp() helper to backport of std::clamp() (#3010)

This commit is contained in:
Oxan van Leeuwen
2022-01-06 12:56:10 +01:00
committed by GitHub
parent a4931f5d78
commit 5c339d4597
2 changed files with 12 additions and 20 deletions

View File

@@ -58,15 +58,6 @@ class HighFrequencyLoopRequester {
bool started_{false};
};
/** Clamp the value between min and max.
*
* @param val The value.
* @param min The minimum value.
* @param max The maximum value.
* @return val clamped in between min and max.
*/
template<typename T> T clamp(T val, T min, T max);
/** Linearly interpolate between end start and end by completion.
*
* @tparam T The input/output typename.
@@ -277,6 +268,18 @@ using std::is_trivially_copyable;
template<typename T> struct is_trivially_copyable : public std::integral_constant<bool, true> {};
#endif
// std::clamp from C++17
#if __cpp_lib_clamp >= 201603
using std::clamp;
#else
template<typename T, typename Compare> constexpr const T &clamp(const T &v, const T &lo, const T &hi, Compare comp) {
return comp(v, lo) ? lo : comp(hi, v) ? hi : v;
}
template<typename T> constexpr const T &clamp(const T &v, const T &lo, const T &hi) {
return clamp(v, lo, hi, std::less<T>{});
}
#endif
// std::bit_cast from C++20
#if __cpp_lib_bit_cast >= 201806
using std::bit_cast;