summaryrefslogtreecommitdiff
path: root/Utilities/std
diff options
context:
space:
mode:
authorRegina Pfeifer <regina@mailbox.org>2019-09-24 07:01:03 +0000
committerSebastian Holtermann <sebholt@xwmw.org>2019-09-25 10:30:58 +0200
commit6a05bd3fa6a3f4ecf45c4cc32abc616883998ff9 (patch)
tree0e12015a980394a803144e29b59c457e19233c45 /Utilities/std
parenta6b379181495794d47cae256dfd94f43fbfe5d48 (diff)
downloadcmake-6a05bd3fa6a3f4ecf45c4cc32abc616883998ff9.tar.gz
cm/algorithm: Provide function cm::clamp
Diffstat (limited to 'Utilities/std')
-rw-r--r--Utilities/std/cm/algorithm38
1 files changed, 38 insertions, 0 deletions
diff --git a/Utilities/std/cm/algorithm b/Utilities/std/cm/algorithm
new file mode 100644
index 0000000000..8ade99cb72
--- /dev/null
+++ b/Utilities/std/cm/algorithm
@@ -0,0 +1,38 @@
+// -*-c++-*-
+// vim: set ft=cpp:
+
+/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
+ file Copyright.txt or https://cmake.org/licensing for details. */
+#ifndef cm_algorithm
+#define cm_algorithm
+
+#include <algorithm> // IWYU pragma: export
+#include <cassert>
+
+namespace cm {
+
+#if __cplusplus >= 201703L || defined(_MSVC_LANG) && _MSVC_LANG >= 201703L
+
+using std::clamp;
+
+#else
+
+template <typename T>
+T const& clamp(T const& v, T const& lo, T const& hi)
+{
+ assert(!(hi < lo));
+ return (v < lo) ? lo : (hi < v) ? hi : v;
+}
+
+template <typename T, typename Comp>
+T const& clamp(T const& v, T const& lo, T const& hi, Comp comp)
+{
+ assert(!comp(hi, lo));
+ return comp(v, lo) ? lo : comp(hi, v) ? hi : v;
+}
+
+#endif
+
+} // namespace cm
+
+#endif