mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 07:03:55 +00:00 
			
		
		
		
	Convert clamp() helper to backport of std::clamp() (#3010)
This commit is contained in:
		| @@ -215,17 +215,6 @@ void HighFrequencyLoopRequester::stop() { | ||||
| } | ||||
| bool HighFrequencyLoopRequester::is_high_frequency() { return high_freq_num_requests > 0; } | ||||
|  | ||||
| template<typename T> T clamp(const T val, const T min, const T max) { | ||||
|   if (val < min) | ||||
|     return min; | ||||
|   if (val > max) | ||||
|     return max; | ||||
|   return val; | ||||
| } | ||||
| template uint8_t clamp(uint8_t, uint8_t, uint8_t); | ||||
| template float clamp(float, float, float); | ||||
| template int clamp(int, int, int); | ||||
|  | ||||
| float lerp(float completion, float start, float end) { return start + (end - start) * completion; } | ||||
|  | ||||
| bool str_startswith(const std::string &full, const std::string &start) { return full.rfind(start, 0) == 0; } | ||||
|   | ||||
| @@ -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; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user