summaryrefslogtreecommitdiffhomepage
path: root/compat/util.hpp
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2017-08-19 12:08:00 +0200
committerStanislaw Halik <sthalik@misaki.pl>2017-08-19 12:08:00 +0200
commit29043ce418f86229f7a71cc7cd2d6623163ce256 (patch)
treeafb958595766f0c2449b5f24dfb6961697f6e4d3 /compat/util.hpp
parenta9fa7552b3659c08a68010e6c19a6fb32ec4e71a (diff)
compat/util: implement clamp specialization for floats
Diffstat (limited to 'compat/util.hpp')
-rw-r--r--compat/util.hpp54
1 files changed, 37 insertions, 17 deletions
diff --git a/compat/util.hpp b/compat/util.hpp
index 5ae87632..6db5324b 100644
--- a/compat/util.hpp
+++ b/compat/util.hpp
@@ -68,31 +68,51 @@ inline unsigned uround(const t& val)
namespace util_detail {
template<typename n>
-inline auto clamp_(n val, n min, n max) -> n
+inline auto clamp_float(n val, n min, n max)
{
- if (unlikely(val > max))
- return max;
- if (unlikely(val < min))
- return min;
- return val;
+ return std::fmin(std::fmax(val, min), max);
}
-}
-
-template<typename t, typename u, typename w>
-inline auto clamp(const t& val, const u& min, const w& max) -> decltype(val + min + max)
+template<typename n>
+struct clamp final
{
- return ::util_detail::clamp_<decltype(val + min + max)>(val, min, max);
-}
+ static inline auto clamp_(const n& val, const n& min, const n& max)
+ {
+ if (unlikely(val > max))
+ return max;
+ if (unlikely(val < min))
+ return min;
+ return val;
+ }
+};
+
+template<>
+struct clamp<float>
+{
+ static inline auto clamp_(float val, float min, float max)
+ {
+ return clamp_float(val, min, max);
+ }
+};
+
+template<>
+struct clamp<double>
+{
+ static inline auto clamp_(double val, double min, double max)
+ {
+ return clamp_float(val, min, max);
+ }
+};
+
+} // ns util_detail
-template<typename t, typename... xs>
-auto qptr(xs... args)
+template<typename t, typename u, typename w>
+inline auto clamp(const t& val, const u& min, const w& max)
{
- return QSharedPointer<t>(new t(std::forward<xs>(args)...));
+ using tp = decltype(val + min + max);
+ return ::util_detail::clamp<tp>::clamp_(val, min, max);
}
-template<typename t> using qshared = QSharedPointer<t>;
-
#if defined _MSC_VER
# define never_inline __declspec(noinline)
#elif defined __GNUG__