2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
// author: Max Kellermann <max.kellermann@gmail.com>
|
2013-12-22 21:08:06 +01:00
|
|
|
|
|
|
|
#ifndef CLAMP_HPP
|
|
|
|
#define CLAMP_HPP
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clamps the specified value in a range. Returns #min or #max if the
|
|
|
|
* value is outside.
|
|
|
|
*/
|
|
|
|
template<typename T>
|
2019-03-12 11:54:51 +01:00
|
|
|
constexpr const T &
|
2023-03-06 15:57:36 +01:00
|
|
|
Clamp(const T &value, const T &min, const T &max) noexcept
|
2013-12-22 21:08:06 +01:00
|
|
|
{
|
2023-03-06 15:57:36 +01:00
|
|
|
if (value < min) [[unlikely]]
|
|
|
|
return min;
|
|
|
|
|
|
|
|
if (value > max) [[unlikely]]
|
|
|
|
return max;
|
|
|
|
|
|
|
|
return value;
|
2013-12-22 21:08:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|