From 2b5b89e9483063f3af05fb5485043868d447994b Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 7 Nov 2018 18:54:41 +0800 Subject: Add min max. --- src/base.h | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'src/base.h') diff --git a/src/base.h b/src/base.h index c8866cf6..9b3b50a1 100644 --- a/src/base.h +++ b/src/base.h @@ -10,6 +10,7 @@ #include #include #include +#include namespace cru { @@ -63,4 +64,31 @@ namespace cru if (!condition) throw std::invalid_argument(error_message.data()); } + + template >> + float Coerce(const T n, const std::optional min, const std::optional max) + { + if (min.has_value() && n < min.value()) + return min.value(); + if (max.has_value() && n > max.value()) + return max.value(); + return n; + } + + template >> + float Coerce(const T n, const std::nullopt_t, const std::optional max) + { + if (max.has_value() && n > max.value()) + return max.value(); + return n; + } + + template >> + float Coerce(const T n, const std::optional min, const std::nullopt_t) + { + if (min.has_value() && n < min.value()) + return min.value(); + return n; + } + } -- cgit v1.2.3