mpd/src/util/Clamp.hxx

25 lines
451 B
C++
Raw Normal View History

// 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>
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