aboutsummaryrefslogtreecommitdiff
path: root/src/base.h
diff options
context:
space:
mode:
author杨宇千 <crupest@outlook.com>2018-11-07 21:17:30 +0800
committerGitHub <noreply@github.com>2018-11-07 21:17:30 +0800
commit634dab6ad2c9e4675beacfb77ac02b2d43cab132 (patch)
treeb44d8cd6f8f4ffbbb6451d42ba9a6b4d98828aec /src/base.h
parentdf0d6e1e282c75d4d8154011715f0b74547b35db (diff)
parent6f76a0ad3df99ea0d99623347d019536cc07e920 (diff)
downloadcru-634dab6ad2c9e4675beacfb77ac02b2d43cab132.tar.gz
cru-634dab6ad2c9e4675beacfb77ac02b2d43cab132.tar.bz2
cru-634dab6ad2c9e4675beacfb77ac02b2d43cab132.zip
Merge pull request #1 from crupest/minmax-layout
Min max layout support.
Diffstat (limited to 'src/base.h')
-rw-r--r--src/base.h28
1 files changed, 28 insertions, 0 deletions
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 <memory>
#include <string_view>
#include <chrono>
+#include <optional>
namespace cru
{
@@ -63,4 +64,31 @@ namespace cru
if (!condition)
throw std::invalid_argument(error_message.data());
}
+
+ template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
+ float Coerce(const T n, const std::optional<T> min, const std::optional<T> max)
+ {
+ if (min.has_value() && n < min.value())
+ return min.value();
+ if (max.has_value() && n > max.value())
+ return max.value();
+ return n;
+ }
+
+ template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
+ float Coerce(const T n, const std::nullopt_t, const std::optional<T> max)
+ {
+ if (max.has_value() && n > max.value())
+ return max.value();
+ return n;
+ }
+
+ template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
+ float Coerce(const T n, const std::optional<T> min, const std::nullopt_t)
+ {
+ if (min.has_value() && n < min.value())
+ return min.value();
+ return n;
+ }
+
}